Refactored chat messaging api
This commit is contained in:
parent
3489800f28
commit
cbe16a669c
Binary file not shown.
@ -6,6 +6,9 @@ import eu.mhsl.minenet.minigames.message.type.ChatMessage;
|
||||
import eu.mhsl.minenet.minigames.message.type.TitleMessage;
|
||||
import net.minestom.server.command.builder.Command;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class DebugCommand extends Command {
|
||||
public DebugCommand() {
|
||||
super("debug");
|
||||
@ -16,6 +19,22 @@ public class DebugCommand extends Command {
|
||||
new ChatMessage(Icon.CHAT).appendTranslated("sample").send(sender);
|
||||
new ActionBarMessage().appendTranslated("sample").send(sender);
|
||||
new TitleMessage().subtitle(subtitleMessage -> subtitleMessage.appendTranslated("sample")).appendTranslated("sample").send(sender);
|
||||
List<String> testplayers = new ArrayList<>() {
|
||||
{
|
||||
add("MineTec");
|
||||
add("Goldi187");
|
||||
add("Test");
|
||||
}
|
||||
};
|
||||
new ChatMessage(Icon.STAR)
|
||||
.appendTranslated("score#result")
|
||||
.newLine()
|
||||
.appendStatic("Test")
|
||||
.appendStatic("Test")
|
||||
.indent()
|
||||
.list(testplayers)
|
||||
.appendTranslated("score#thanks")
|
||||
.send(sender);
|
||||
});
|
||||
|
||||
}
|
||||
|
@ -25,9 +25,9 @@ public class GcCommand extends Command {
|
||||
System.gc();
|
||||
long after = Monitoring.getRamUsage();
|
||||
|
||||
new ChatMessage(Icon.SUCCESS).appendStatic("Garbage collector ran successfully!").indent(1).newLine()
|
||||
new ChatMessage(Icon.SUCCESS).appendStatic("Garbage collector ran successfully!").newLine()
|
||||
.appendStatic("before: ").appendStatic(String.valueOf(before)).appendStatic("MB").newLine()
|
||||
.appendStatic("now: ").appendStatic(String.valueOf(after)).appendStatic("MB").indent(1).newLine()
|
||||
.appendStatic("now: ").appendStatic(String.valueOf(after)).appendStatic("MB").newLine()
|
||||
.appendStatic("difference: ").appendStatic(String.valueOf(before-after)).appendStatic("MB")
|
||||
.send(sender);
|
||||
});
|
||||
|
@ -16,13 +16,13 @@ public class RoomCommand extends Command {
|
||||
setCondition((sender, commandString) -> sender.hasPermission("admin"));
|
||||
|
||||
setDefaultExecutor((sender, context) -> {
|
||||
TranslatableMessage out = new ChatMessage(Icon.SCIENCE).appendStatic("Rooms:").indent(1).newLine();
|
||||
TranslatableMessage out = new ChatMessage(Icon.SCIENCE).appendStatic("Rooms:").newLine();
|
||||
|
||||
Room.getAllRooms().forEach((roomInstance) -> out
|
||||
.newLine()
|
||||
.appendStatic("Owner: ").appendStatic(roomInstance.getOwner().getUsername()).newLine()
|
||||
.appendStatic("Players: ").appendStatic(String.valueOf(roomInstance.getAllMembers().size())).indent(1).newLine()
|
||||
.list(roomInstance.getAllMembers().stream().map(Player::getUsername).collect(Collectors.toList())).indent(-1).newLine());
|
||||
.appendStatic("Players: ").appendStatic(String.valueOf(roomInstance.getAllMembers().size())).newLine()
|
||||
.list(roomInstance.getAllMembers().stream().map(Player::getUsername).collect(Collectors.toList())).newLine());
|
||||
|
||||
out.send(sender);
|
||||
});
|
||||
|
@ -1,6 +1,5 @@
|
||||
package eu.mhsl.minenet.minigames.handler.global;
|
||||
|
||||
import eu.mhsl.minenet.minigames.message.Icon;
|
||||
import eu.mhsl.minenet.minigames.message.type.ChatMessage;
|
||||
import net.minestom.server.event.EventListener;
|
||||
import net.minestom.server.event.player.PlayerChatEvent;
|
||||
@ -15,7 +14,7 @@ public class PlayerChatHandler implements EventListener<PlayerChatEvent> {
|
||||
@Override
|
||||
public @NotNull Result run(@NotNull PlayerChatEvent event) {
|
||||
event.setChatFormat(
|
||||
(messages) -> new ChatMessage(Icon.CHAT)
|
||||
(messages) -> new ChatMessage()
|
||||
.appendStatic(event.getPlayer().getUsername())
|
||||
.pipe()
|
||||
.appendStatic(messages.getMessage())
|
||||
|
@ -37,7 +37,7 @@ public class GameSelector extends InteractableEntity {
|
||||
if(playerEntityInteractEvent.getPlayer() != room.getOwner()) {
|
||||
abstractVillagerMeta.setHeadShakeTimer(20);
|
||||
|
||||
new ChatMessage(Icon.ERROR).appendStatic("Only the room leader can start games!").indent(1).newLine()
|
||||
new ChatMessage(Icon.ERROR).appendStatic("Only the room leader can start games!").newLine()
|
||||
.appendStatic("current leader: ").appendStatic(room.getOwner().getUsername())
|
||||
.send(playerEntityInteractEvent.getPlayer());
|
||||
|
||||
|
@ -13,7 +13,7 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public abstract class TranslatableMessage implements Sendable {
|
||||
int indention = 0;
|
||||
protected int indention = 0;
|
||||
final List<ComponentLike> chain = new ArrayList<>();
|
||||
|
||||
public TranslatableMessage() {
|
||||
@ -36,6 +36,7 @@ public abstract class TranslatableMessage implements Sendable {
|
||||
}
|
||||
|
||||
public TranslatableMessage list(List<String> list) {
|
||||
newLine();
|
||||
list.forEach(s -> {
|
||||
chain.add(Component.text(s));
|
||||
newLine();
|
||||
@ -43,6 +44,16 @@ public abstract class TranslatableMessage implements Sendable {
|
||||
return this;
|
||||
}
|
||||
|
||||
public TranslatableMessage numberedList(List<String> list) {
|
||||
newLine();
|
||||
for (int i = 0; i < list.size(); i++) {
|
||||
appendStatic(i+1 + ". ");
|
||||
appendStatic(list.get(i));
|
||||
newLine();
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public TranslatableMessage pipe() {
|
||||
chain.add(Component.text(" | ", NamedTextColor.DARK_GRAY));
|
||||
return this;
|
||||
@ -53,14 +64,29 @@ public abstract class TranslatableMessage implements Sendable {
|
||||
return this;
|
||||
}
|
||||
|
||||
public TranslatableMessage indent(int amount) {
|
||||
this.indention += amount;
|
||||
this.newLine();
|
||||
public TranslatableMessage newLine() {
|
||||
chain.add(Component.text("\n"));
|
||||
this.appendStatic(" ".repeat(indention));
|
||||
return this;
|
||||
}
|
||||
|
||||
public TranslatableMessage newLine() {
|
||||
chain.add(Component.text(" ".repeat(indention) + "\n"));
|
||||
public TranslatableMessage indent(int indention) {
|
||||
this.indention += indention;
|
||||
return this;
|
||||
}
|
||||
|
||||
public TranslatableMessage indent() {
|
||||
this.indent(1);
|
||||
return this;
|
||||
}
|
||||
|
||||
public TranslatableMessage undent() {
|
||||
this.indent(-1);
|
||||
return this;
|
||||
}
|
||||
|
||||
public TranslatableMessage resetIndention() {
|
||||
indention = 0;
|
||||
return this;
|
||||
}
|
||||
|
||||
|
@ -6,10 +6,25 @@ import net.minestom.server.entity.Player;
|
||||
|
||||
public class ChatMessage extends TranslatableMessage {
|
||||
public ChatMessage(Icon icon) {
|
||||
super.appendStatic("\n");
|
||||
appendStatic(icon.getComponent());
|
||||
pipe();
|
||||
}
|
||||
|
||||
public ChatMessage() {
|
||||
appendStatic(Icon.CHAT.getComponent());
|
||||
pipe();
|
||||
}
|
||||
|
||||
@Override
|
||||
public TranslatableMessage newLine() {
|
||||
super.appendStatic("\n");
|
||||
super.appendStatic(" ");
|
||||
super.pipe();
|
||||
super.appendStatic(" ".repeat(super.indention));
|
||||
return this;
|
||||
}
|
||||
|
||||
public void send(Player p) {
|
||||
p.sendMessage(build(p));
|
||||
}
|
||||
|
@ -33,10 +33,11 @@ public abstract class Score {
|
||||
isDone = true;
|
||||
new ChatMessage(Icon.STAR)
|
||||
.appendTranslated("score#result")
|
||||
.indent(1)
|
||||
.pipe()
|
||||
.list(getResults())
|
||||
.indent(-1).newLine()
|
||||
.newLine()
|
||||
.indent()
|
||||
.numberedList(getResults())
|
||||
.undent()
|
||||
.newLine()
|
||||
.appendTranslated("score#thanks")
|
||||
.send(instance.getPlayers());
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user