From a987bad22130c1bc76ba4703b24bb32d6e9769db Mon Sep 17 00:00:00 2001 From: Florian Rinke Date: Sat, 2 Oct 2021 08:47:02 +0200 Subject: Implements #6 --- src/main/java/de/torui/coflsky/CoflSkyCommand.java | 42 ++++++++++++++++------ .../java/de/torui/coflsky/WSCommandHandler.java | 21 ++++++----- .../java/de/torui/coflsky/core/StringCommand.java | 38 ++++++++++++++++++++ .../java/de/torui/coflsky/websocket/WSClient.java | 8 +++++ .../torui/coflsky/websocket/WSClientWrapper.java | 10 ++++++ 5 files changed, 100 insertions(+), 19 deletions(-) create mode 100644 src/main/java/de/torui/coflsky/core/StringCommand.java (limited to 'src') diff --git a/src/main/java/de/torui/coflsky/CoflSkyCommand.java b/src/main/java/de/torui/coflsky/CoflSkyCommand.java index b269d19..1a63ce8 100644 --- a/src/main/java/de/torui/coflsky/CoflSkyCommand.java +++ b/src/main/java/de/torui/coflsky/CoflSkyCommand.java @@ -5,6 +5,7 @@ import java.util.List; import de.torui.coflsky.core.Command; import de.torui.coflsky.core.CommandType; +import de.torui.coflsky.core.StringCommand; import net.minecraft.client.Minecraft; import net.minecraft.command.CommandBase; import net.minecraft.command.CommandException; @@ -37,9 +38,10 @@ public class CoflSkyCommand extends CommandBase { return HelpText; } - public static final String HelpText = "Available sub-commands:\n" + public static final String HelpText = "Available local sub-commands:\n" + "start: starts a new connection\n" - + "stop: stops the connection"; + + "stop: stops the connection\n" + + "status: Emits status information\nServer-Only Commands:"; @Override public List addTabCompletionOptions(ICommandSender sender, String[] args, BlockPos pos) { @@ -69,21 +71,19 @@ public class CoflSkyCommand extends CommandBase { .appendSibling(new ChatComponentText(" or click this message")) .setChatStyle(new ChatStyle().setChatClickEvent(new ClickEvent(Action.RUN_COMMAND, "/cofl start"))) ); - - //)§1C§6oflnet§8§f.\n To restart enter §8\"§f/cofl start§8\""))); - //todo: stop break; - case "debug": - WSCommandHandler.HandleCommand(new Command(CommandType.Execute, "/me hewwo"), sender.getCommandSenderEntity()); + //case "debug": + // WSCommandHandler.HandleCommand(new Command(CommandType.Execute, "/me hewwo"), sender.getCommandSenderEntity()); // WSCommandHandler.HandleCommand(new Command(CommandType.WriteToChat, "{ \"text\": \"Clickable Texts are fun\", \"onClick\": \"me Hello World\"}"), sender.getCommandSenderEntity()); - break; + // break; case "callback": CallbackCommand(args); break; + case "status": + sender.addChatMessage(new ChatComponentText(StatusMessage())); + break; default: - QueryServerCommands.QueryCommands(); - sender.addChatMessage(new ChatComponentText("" + args[0] +"is not a valid subcommand!")); - System.out.println(args[0] +"is not a valid subcommand!"); + CommandNotRecognized(args, sender); return; } } @@ -94,7 +94,27 @@ public class CoflSkyCommand extends CommandBase { } + public String StatusMessage() { + + String vendor = System.getProperty("java.vm.vendor"); + String name = System.getProperty("java.vm.name"); + String version = System.getProperty("java.version"); + String detailedVersion = System.getProperty("java.vm.version"); + + return vendor + " " + name + " " + version + " " + detailedVersion + "|Connection = " + (CoflSky.Wrapper!=null?CoflSky.Wrapper.GetStatus():"UNINITIALIZED_WRAPPER"); + } + public void CommandNotRecognized(String[] args, ICommandSender sender) { + String command = String.join(" ", Arrays.copyOfRange(args, 1, args.length)); + + StringCommand sc = new StringCommand(args[0], command); + + if(CoflSky.Wrapper.isRunning) { + CoflSky.Wrapper.SendMessage(sc); + } else { + sender.addChatMessage(new ChatComponentText("CoflSky not active. Server Commands are currently not available.").setChatStyle(new ChatStyle().setColor(EnumChatFormatting.RED))); + } + } diff --git a/src/main/java/de/torui/coflsky/WSCommandHandler.java b/src/main/java/de/torui/coflsky/WSCommandHandler.java index cdfc954..8eb1a78 100644 --- a/src/main/java/de/torui/coflsky/WSCommandHandler.java +++ b/src/main/java/de/torui/coflsky/WSCommandHandler.java @@ -13,6 +13,7 @@ import net.minecraft.util.ChatComponentText; import net.minecraft.util.ChatStyle; import net.minecraft.util.IChatComponent; import net.minecraftforge.client.ClientCommandHandler; +import net.minecraftforge.client.event.sound.SoundEvent; public class WSCommandHandler { public static boolean HandleCommand(Command cmd, Entity sender) { @@ -31,6 +32,10 @@ public class WSCommandHandler { return true; } + + private static void PlaySound(Command cmd, Entity sender) { + + } private static void Execute(Command cmd, Entity sender) { @@ -48,19 +53,19 @@ public class WSCommandHandler { WriteToChatCommand wcmd = WSClient.gson.fromJson(cmd.getData(), WriteToChatCommand.class); //System.out.println("Executing wcmd Text=" + wcmd.Text + " OnClick=" + wcmd.OnClick); - if(wcmd.Text != null && wcmd.OnClick != null) { + if(wcmd.Text != null ) { IChatComponent comp = new ChatComponentText(wcmd.Text); ChatStyle style; - 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)); + 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)); + } + comp.setChatStyle(style); } - - comp.setChatStyle(style); - Minecraft.getMinecraft().thePlayer.addChatMessage(comp); } diff --git a/src/main/java/de/torui/coflsky/core/StringCommand.java b/src/main/java/de/torui/coflsky/core/StringCommand.java new file mode 100644 index 0000000..745f1ba --- /dev/null +++ b/src/main/java/de/torui/coflsky/core/StringCommand.java @@ -0,0 +1,38 @@ +package de.torui.coflsky.core; + +import com.google.gson.annotations.SerializedName; + +public class StringCommand { + @SerializedName("type") + private String Type; + @SerializedName("data") + private String data; + + public StringCommand() {} + + public StringCommand(String type, String data) { + super(); + this.Type = type; + this.data = data; + } + + public String getType() { + return Type; + } + + public void setType(String type) { + Type = type; + } + + public String getData() { + return data; + } + public void setData(String data) { + this.data = data; + } + + @Override + public String toString() { + return "Command [Type=" + Type + ", data=" + data + "]"; + } +} diff --git a/src/main/java/de/torui/coflsky/websocket/WSClient.java b/src/main/java/de/torui/coflsky/websocket/WSClient.java index 7fb8e34..c4ad799 100644 --- a/src/main/java/de/torui/coflsky/websocket/WSClient.java +++ b/src/main/java/de/torui/coflsky/websocket/WSClient.java @@ -17,6 +17,7 @@ import net.minecraft.client.Minecraft; import de.torui.coflsky.WSCommandHandler; import de.torui.coflsky.core.Command; +import de.torui.coflsky.core.StringCommand; public class WSClient extends WebSocketAdapter { @@ -37,6 +38,7 @@ public class WSClient extends WebSocketAdapter { } public URI uri; public WebSocket socket; + public WebSocketState currentState = WebSocketState.CLOSED; public WSClient(URI uri) { this.uri = uri; @@ -92,6 +94,7 @@ public class WSClient extends WebSocketAdapter { @Override public void onStateChanged(WebSocket websocket, WebSocketState newState) throws Exception { System.out.println("WebSocket Changed state to: " + newState); + currentState = newState; super.onStateChanged(websocket, newState); } @@ -113,6 +116,11 @@ public class WSClient extends WebSocketAdapter { this.socket.sendText(json); } + public void SendCommand(StringCommand sc) { + String json = gson.toJson(sc); + this.socket.sendText(json); + } + diff --git a/src/main/java/de/torui/coflsky/websocket/WSClientWrapper.java b/src/main/java/de/torui/coflsky/websocket/WSClientWrapper.java index 96a2729..2cdfda1 100644 --- a/src/main/java/de/torui/coflsky/websocket/WSClientWrapper.java +++ b/src/main/java/de/torui/coflsky/websocket/WSClientWrapper.java @@ -10,6 +10,7 @@ import com.neovisionaries.ws.client.WebSocketException; import de.torui.coflsky.CoflSky; import de.torui.coflsky.core.Command; +import de.torui.coflsky.core.StringCommand; public class WSClientWrapper { @@ -67,4 +68,13 @@ public class WSClientWrapper { public synchronized void SendMessage(Command cmd){ this.socket.SendCommand(cmd); } + + public void SendMessage(StringCommand sc) { + this.socket.SendCommand(sc); + } + + public String GetStatus() { + return "" + isRunning + " " + + (this.socket!=null ? this.socket.currentState.toString() : "NOT_INITIALIZED"); + } } -- cgit