diff options
author | Florian Rinke <develop@torui.de> | 2021-10-11 20:22:33 +0200 |
---|---|---|
committer | Florian Rinke <develop@torui.de> | 2021-10-11 20:22:33 +0200 |
commit | 6dfae693d8682f55d1c5311b3b62b389e7b7a9bf (patch) | |
tree | 6fe1ee1b6c234184983ef099f6be14f08fca5ffa /src/main/java/de | |
parent | 035ea5d24739afd1d137b4bbf3a2575a12f53758 (diff) | |
download | COFL-6dfae693d8682f55d1c5311b3b62b389e7b7a9bf.tar.gz COFL-6dfae693d8682f55d1c5311b3b62b389e7b7a9bf.tar.bz2 COFL-6dfae693d8682f55d1c5311b3b62b389e7b7a9bf.zip |
added hover #10 and chatmessage #12
Diffstat (limited to 'src/main/java/de')
3 files changed, 75 insertions, 43 deletions
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); } + } diff --git a/src/main/java/de/torui/coflsky/core/CommandType.java b/src/main/java/de/torui/coflsky/core/CommandType.java index 2771470..5f5ceeb 100644 --- a/src/main/java/de/torui/coflsky/core/CommandType.java +++ b/src/main/java/de/torui/coflsky/core/CommandType.java @@ -15,5 +15,7 @@ public enum CommandType { @SerializedName("tokenLogin") Clicked, @SerializedName("playSound") - PlaySound; + PlaySound, + @SerializedName("chatMessage") + ChatMessage; } diff --git a/src/main/java/de/torui/coflsky/core/WriteToChatCommand.java b/src/main/java/de/torui/coflsky/core/WriteToChatCommand.java index 939c386..b8c64bb 100644 --- a/src/main/java/de/torui/coflsky/core/WriteToChatCommand.java +++ b/src/main/java/de/torui/coflsky/core/WriteToChatCommand.java @@ -7,12 +7,19 @@ public class WriteToChatCommand { public String Text; @SerializedName("onClick") public String OnClick; - public WriteToChatCommand(String text, String onClickEvent) { - super(); - Text = text; - OnClick = onClickEvent; - } + @SerializedName("hover") + public String Hover; + public WriteToChatCommand() { + + } + + public WriteToChatCommand(String text, String onClick, String hover) { + super(); + Text = text; + OnClick = onClick; + Hover = hover; + } } |