From 6dfae693d8682f55d1c5311b3b62b389e7b7a9bf Mon Sep 17 00:00:00 2001 From: Florian Rinke Date: Mon, 11 Oct 2021 20:22:33 +0200 Subject: added hover #10 and chatmessage #12 --- .../java/de/torui/coflsky/WSCommandHandler.java | 97 +++++++++++++--------- 1 file changed, 60 insertions(+), 37 deletions(-) (limited to 'src/main/java/de/torui/coflsky/WSCommandHandler.java') diff --git a/src/main/java/de/torui/coflsky/WSCommandHandler.java b/src/main/java/de/torui/coflsky/WSCommandHandler.java index 6e0f394..dd74d87 100644 --- a/src/main/java/de/torui/coflsky/WSCommandHandler.java +++ b/src/main/java/de/torui/coflsky/WSCommandHandler.java @@ -15,24 +15,20 @@ import net.minecraft.command.ICommandManager; import net.minecraft.entity.Entity; import net.minecraft.event.ClickEvent; import net.minecraft.event.ClickEvent.Action; -import net.minecraft.server.MinecraftServer; +import net.minecraft.event.HoverEvent; import net.minecraft.util.ChatComponentText; import net.minecraft.util.ChatStyle; import net.minecraft.util.IChatComponent; import net.minecraft.util.ResourceLocation; -import net.minecraftforge.client.ClientCommandHandler; -import net.minecraftforge.client.event.sound.SoundEvent; - - public class WSCommandHandler { public static String lastOnClickEvent; - + public static boolean HandleCommand(Command cmd, Entity sender) { - //Entity sender = Minecraft.getMinecraft().thePlayer; + // Entity sender = Minecraft.getMinecraft().thePlayer; System.out.println("Handling Command=" + cmd.toString()); - switch(cmd.getType()) { + switch (cmd.getType()) { case WriteToChat: WriteToChat(cmd); break; @@ -41,56 +37,83 @@ public class WSCommandHandler { break; case PlaySound: PlaySound(cmd, sender); + case ChatMessage: + ChatMessage(cmd); default: break; } - + return true; } - + private static void PlaySound(Command cmd, Entity sender) { - - //Minecraft.getMinecraft().theWorld.playSoundAtEntity(sender, "random.explode",1f, 1f); - + + // Minecraft.getMinecraft().theWorld.playSoundAtEntity(sender, + // "random.explode",1f, 1f); + SoundHandler handler = Minecraft.getMinecraft().getSoundHandler(); - - //random.explode - PositionedSoundRecord psr = PositionedSoundRecord.create(new ResourceLocation(WSClient.gson.fromJson(cmd.getData(), String.class))); - handler.playSound(psr); + + // random.explode + PositionedSoundRecord psr = PositionedSoundRecord + .create(new ResourceLocation(WSClient.gson.fromJson(cmd.getData(), String.class))); + handler.playSound(psr); } private static void Execute(Command cmd, Entity sender) { - - //Minecraft.getMinecraft().thePlayer.sendChatMessage(cmd.getData()); System.out.println("Execute: " + cmd.getData() + " sender:" + sender); - + Minecraft.getMinecraft().thePlayer.sendChatMessage(WSClient.gson.fromJson(cmd.getData(), String.class)); - - //ICommandManager manager = MinecraftServer.getServer().getCommandManager(); - //System.out.println("CommandManager: " + manager); - //manager.executeCommand(sender, cmd.getData()); } - private static void WriteToChat(Command cmd) { - WriteToChatCommand wcmd = WSClient.gson.fromJson(cmd.getData(), WriteToChatCommand.class); - //System.out.println("Executing wcmd Text=" + wcmd.Text + " OnClick=" + wcmd.OnClick); - - if(wcmd.Text != null ) { + + private static IChatComponent CommandToChatComponent(WriteToChatCommand wcmd) { + if (wcmd.Text != null) { IChatComponent comp = new ChatComponentText(wcmd.Text); - + ChatStyle style; - if(wcmd.OnClick != null) { - if(wcmd.OnClick.startsWith("http")) { + if (wcmd.OnClick != null) { + if (wcmd.OnClick.startsWith("http")) { style = new ChatStyle().setChatClickEvent(new ClickEvent(Action.OPEN_URL, wcmd.OnClick)); } else { - style = new ChatStyle().setChatClickEvent(new ClickEvent(Action.RUN_COMMAND, "/cofl callback " +wcmd.OnClick)); - lastOnClickEvent = "/cofl callback " +wcmd.OnClick; + style = new ChatStyle() + .setChatClickEvent(new ClickEvent(Action.RUN_COMMAND, "/cofl callback " + wcmd.OnClick)); + lastOnClickEvent = "/cofl callback " + wcmd.OnClick; } comp.setChatStyle(style); } - - Minecraft.getMinecraft().thePlayer.addChatMessage(comp); + + if (wcmd.Hover != null && !wcmd.Hover.isEmpty()) { + if (comp.getChatStyle() == null) + comp.setChatStyle(new ChatStyle()); + comp.getChatStyle().setChatHoverEvent( + new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ChatComponentText(wcmd.Hover))); + } + return comp; + } + return null; + } + + private static void ChatMessage(Command cmd) { + WriteToChatCommand[] list = WSClient.gson.fromJson(cmd.getData(), WriteToChatCommand[].class); + + IChatComponent master = new ChatComponentText(""); + + for (WriteToChatCommand wcmd : list) { + IChatComponent comp = CommandToChatComponent(wcmd); + if (comp != null) + master.appendSibling(comp); } - + Minecraft.getMinecraft().thePlayer.addChatMessage(master); + } + + + + private static void WriteToChat(Command cmd) { + WriteToChatCommand wcmd = WSClient.gson.fromJson(cmd.getData(), WriteToChatCommand.class); + + IChatComponent comp = CommandToChatComponent(wcmd); + if (comp != null) + Minecraft.getMinecraft().thePlayer.addChatMessage(comp); } + } -- cgit