Refactored chat messaging api

This commit is contained in:
Elias Müller 2023-09-30 23:31:36 +02:00
parent 3489800f28
commit cbe16a669c
9 changed files with 78 additions and 18 deletions

View File

@ -6,6 +6,9 @@ import eu.mhsl.minenet.minigames.message.type.ChatMessage;
import eu.mhsl.minenet.minigames.message.type.TitleMessage; import eu.mhsl.minenet.minigames.message.type.TitleMessage;
import net.minestom.server.command.builder.Command; import net.minestom.server.command.builder.Command;
import java.util.ArrayList;
import java.util.List;
public class DebugCommand extends Command { public class DebugCommand extends Command {
public DebugCommand() { public DebugCommand() {
super("debug"); super("debug");
@ -16,6 +19,22 @@ public class DebugCommand extends Command {
new ChatMessage(Icon.CHAT).appendTranslated("sample").send(sender); new ChatMessage(Icon.CHAT).appendTranslated("sample").send(sender);
new ActionBarMessage().appendTranslated("sample").send(sender); new ActionBarMessage().appendTranslated("sample").send(sender);
new TitleMessage().subtitle(subtitleMessage -> subtitleMessage.appendTranslated("sample")).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);
}); });
} }

View File

@ -25,9 +25,9 @@ public class GcCommand extends Command {
System.gc(); System.gc();
long after = Monitoring.getRamUsage(); 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("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") .appendStatic("difference: ").appendStatic(String.valueOf(before-after)).appendStatic("MB")
.send(sender); .send(sender);
}); });

View File

@ -16,13 +16,13 @@ public class RoomCommand extends Command {
setCondition((sender, commandString) -> sender.hasPermission("admin")); setCondition((sender, commandString) -> sender.hasPermission("admin"));
setDefaultExecutor((sender, context) -> { 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 Room.getAllRooms().forEach((roomInstance) -> out
.newLine() .newLine()
.appendStatic("Owner: ").appendStatic(roomInstance.getOwner().getUsername()).newLine() .appendStatic("Owner: ").appendStatic(roomInstance.getOwner().getUsername()).newLine()
.appendStatic("Players: ").appendStatic(String.valueOf(roomInstance.getAllMembers().size())).indent(1).newLine() .appendStatic("Players: ").appendStatic(String.valueOf(roomInstance.getAllMembers().size())).newLine()
.list(roomInstance.getAllMembers().stream().map(Player::getUsername).collect(Collectors.toList())).indent(-1).newLine()); .list(roomInstance.getAllMembers().stream().map(Player::getUsername).collect(Collectors.toList())).newLine());
out.send(sender); out.send(sender);
}); });

View File

@ -1,6 +1,5 @@
package eu.mhsl.minenet.minigames.handler.global; package eu.mhsl.minenet.minigames.handler.global;
import eu.mhsl.minenet.minigames.message.Icon;
import eu.mhsl.minenet.minigames.message.type.ChatMessage; import eu.mhsl.minenet.minigames.message.type.ChatMessage;
import net.minestom.server.event.EventListener; import net.minestom.server.event.EventListener;
import net.minestom.server.event.player.PlayerChatEvent; import net.minestom.server.event.player.PlayerChatEvent;
@ -15,7 +14,7 @@ public class PlayerChatHandler implements EventListener<PlayerChatEvent> {
@Override @Override
public @NotNull Result run(@NotNull PlayerChatEvent event) { public @NotNull Result run(@NotNull PlayerChatEvent event) {
event.setChatFormat( event.setChatFormat(
(messages) -> new ChatMessage(Icon.CHAT) (messages) -> new ChatMessage()
.appendStatic(event.getPlayer().getUsername()) .appendStatic(event.getPlayer().getUsername())
.pipe() .pipe()
.appendStatic(messages.getMessage()) .appendStatic(messages.getMessage())

View File

@ -37,7 +37,7 @@ public class GameSelector extends InteractableEntity {
if(playerEntityInteractEvent.getPlayer() != room.getOwner()) { if(playerEntityInteractEvent.getPlayer() != room.getOwner()) {
abstractVillagerMeta.setHeadShakeTimer(20); 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()) .appendStatic("current leader: ").appendStatic(room.getOwner().getUsername())
.send(playerEntityInteractEvent.getPlayer()); .send(playerEntityInteractEvent.getPlayer());

View File

@ -13,7 +13,7 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
public abstract class TranslatableMessage implements Sendable { public abstract class TranslatableMessage implements Sendable {
int indention = 0; protected int indention = 0;
final List<ComponentLike> chain = new ArrayList<>(); final List<ComponentLike> chain = new ArrayList<>();
public TranslatableMessage() { public TranslatableMessage() {
@ -36,6 +36,7 @@ public abstract class TranslatableMessage implements Sendable {
} }
public TranslatableMessage list(List<String> list) { public TranslatableMessage list(List<String> list) {
newLine();
list.forEach(s -> { list.forEach(s -> {
chain.add(Component.text(s)); chain.add(Component.text(s));
newLine(); newLine();
@ -43,6 +44,16 @@ public abstract class TranslatableMessage implements Sendable {
return this; 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() { public TranslatableMessage pipe() {
chain.add(Component.text(" | ", NamedTextColor.DARK_GRAY)); chain.add(Component.text(" | ", NamedTextColor.DARK_GRAY));
return this; return this;
@ -53,14 +64,29 @@ public abstract class TranslatableMessage implements Sendable {
return this; return this;
} }
public TranslatableMessage indent(int amount) { public TranslatableMessage newLine() {
this.indention += amount; chain.add(Component.text("\n"));
this.newLine(); this.appendStatic(" ".repeat(indention));
return this; return this;
} }
public TranslatableMessage newLine() { public TranslatableMessage indent(int indention) {
chain.add(Component.text(" ".repeat(indention) + "\n")); 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; return this;
} }

View File

@ -6,10 +6,25 @@ import net.minestom.server.entity.Player;
public class ChatMessage extends TranslatableMessage { public class ChatMessage extends TranslatableMessage {
public ChatMessage(Icon icon) { public ChatMessage(Icon icon) {
super.appendStatic("\n");
appendStatic(icon.getComponent()); appendStatic(icon.getComponent());
pipe(); 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) { public void send(Player p) {
p.sendMessage(build(p)); p.sendMessage(build(p));
} }

View File

@ -33,10 +33,11 @@ public abstract class Score {
isDone = true; isDone = true;
new ChatMessage(Icon.STAR) new ChatMessage(Icon.STAR)
.appendTranslated("score#result") .appendTranslated("score#result")
.indent(1) .newLine()
.pipe() .indent()
.list(getResults()) .numberedList(getResults())
.indent(-1).newLine() .undent()
.newLine()
.appendTranslated("score#thanks") .appendTranslated("score#thanks")
.send(instance.getPlayers()); .send(instance.getPlayers());