diff options
author | Florian Rinke <develop@torui.de> | 2021-11-05 17:07:39 +0100 |
---|---|---|
committer | Florian Rinke <develop@torui.de> | 2021-11-05 20:05:05 +0100 |
commit | bf5bdf3ca643c6a28def732e9085a6c9766a8830 (patch) | |
tree | c5a7e828279c497924b3fe1fa39a8ef54dc5a94d | |
parent | 161c01a2be916670c5bb4730760966f78e35afd0 (diff) | |
download | COFL-bf5bdf3ca643c6a28def732e9085a6c9766a8830.tar.gz COFL-bf5bdf3ca643c6a28def732e9085a6c9766a8830.tar.bz2 COFL-bf5bdf3ca643c6a28def732e9085a6c9766a8830.zip |
Modularized Command Infrastructure
-rw-r--r-- | src/main/java/de/torui/coflsky/CoflSkyCommand.java | 8 | ||||
-rw-r--r-- | src/main/java/de/torui/coflsky/EventRegistry.java | 10 | ||||
-rw-r--r-- | src/main/java/de/torui/coflsky/WSCommandHandler.java | 53 | ||||
-rw-r--r-- | src/main/java/de/torui/coflsky/core/Command.java | 28 | ||||
-rw-r--r-- | src/main/java/de/torui/coflsky/core/CommandType.java | 21 | ||||
-rw-r--r-- | src/main/java/de/torui/coflsky/core/JsonStringCommand.java | 28 | ||||
-rw-r--r-- | src/main/java/de/torui/coflsky/core/StringCommand.java | 38 | ||||
-rw-r--r-- | src/main/java/de/torui/coflsky/core/commandEntities/AuctionData.java | 30 | ||||
-rw-r--r-- | src/main/java/de/torui/coflsky/core/commandEntities/ChatMessageData.java (renamed from src/main/java/de/torui/coflsky/core/WriteToChatCommand.java) | 8 | ||||
-rw-r--r-- | src/main/java/de/torui/coflsky/core/commandEntities/SoundData.java (renamed from src/main/java/de/torui/coflsky/core/SoundCommand.java) | 14 | ||||
-rw-r--r-- | src/main/java/de/torui/coflsky/network/WSClient.java | 10 | ||||
-rw-r--r-- | src/main/java/de/torui/coflsky/network/WSClientWrapper.java | 5 |
12 files changed, 134 insertions, 119 deletions
diff --git a/src/main/java/de/torui/coflsky/CoflSkyCommand.java b/src/main/java/de/torui/coflsky/CoflSkyCommand.java index e48d131..8f4e9e7 100644 --- a/src/main/java/de/torui/coflsky/CoflSkyCommand.java +++ b/src/main/java/de/torui/coflsky/CoflSkyCommand.java @@ -6,7 +6,7 @@ import java.util.List; import de.torui.coflsky.core.Command; import de.torui.coflsky.core.CommandType; -import de.torui.coflsky.core.StringCommand; +import de.torui.coflsky.core.JsonStringCommand; import de.torui.coflsky.minecraft_integration.CoflSessionManager; import de.torui.coflsky.minecraft_integration.CoflSessionManager.CoflSession; import de.torui.coflsky.network.QueryServerCommands; @@ -136,7 +136,7 @@ public class CoflSkyCommand extends CommandBase { public void CommandNotRecognized(String[] args, ICommandSender sender) { String command = String.join(" ", Arrays.copyOfRange(args, 1, args.length)); - StringCommand sc = new StringCommand(args[0], WSClient.gson.toJson(command)); + JsonStringCommand sc = new JsonStringCommand(args[0], WSClient.gson.toJson(command)); if(CoflSky.Wrapper.isRunning) { CoflSky.Wrapper.SendMessage(sc); @@ -158,8 +158,8 @@ public class CoflSkyCommand extends CommandBase { System.out.println("CallbackData: " + command); //new Thread(()->{ System.out.println("Callback: " + command); - WSCommandHandler.HandleCommand(new Command(CommandType.Execute, WSClient.gson.toJson(command)), Minecraft.getMinecraft().thePlayer); - CoflSky.Wrapper.SendMessage(new Command(CommandType.Clicked, WSClient.gson.toJson(command))); + WSCommandHandler.HandleCommand(new JsonStringCommand(CommandType.Execute, WSClient.gson.toJson(command)), Minecraft.getMinecraft().thePlayer); + CoflSky.Wrapper.SendMessage(new JsonStringCommand(CommandType.Clicked, WSClient.gson.toJson(command))); System.out.println("Sent!"); //}).start(); diff --git a/src/main/java/de/torui/coflsky/EventRegistry.java b/src/main/java/de/torui/coflsky/EventRegistry.java index f47d0a1..c0b4546 100644 --- a/src/main/java/de/torui/coflsky/EventRegistry.java +++ b/src/main/java/de/torui/coflsky/EventRegistry.java @@ -1,5 +1,8 @@ package de.torui.coflsky; +import de.torui.coflsky.core.Command; +import de.torui.coflsky.core.CommandType; +import de.torui.coflsky.core.JsonStringCommand; import java.util.UUID; import net.minecraft.client.Minecraft; @@ -58,14 +61,13 @@ public class EventRegistry{ public void onEvent(KeyInputEvent event) { if(CoflSky.keyBindings[0].isPressed()) { - //System.out.println(">>>>> Key Pressed"); - + if(WSCommandHandler.lastOnClickEvent != null) { String command = WSCommandHandler.lastOnClickEvent; WSCommandHandler.lastOnClickEvent = null; - //System.out.println(">>>>> HasLastONClickEvent = " + command); - WSCommandHandler.Execute(command,Minecraft.getMinecraft().thePlayer); + WSCommandHandler.HandleCommand(new JsonStringCommand(CommandType.Execute, WSClient.gson.toJson(command)), + Minecraft.getMinecraft().thePlayer); } diff --git a/src/main/java/de/torui/coflsky/WSCommandHandler.java b/src/main/java/de/torui/coflsky/WSCommandHandler.java index 7e857db..287ed09 100644 --- a/src/main/java/de/torui/coflsky/WSCommandHandler.java +++ b/src/main/java/de/torui/coflsky/WSCommandHandler.java @@ -1,25 +1,19 @@ package de.torui.coflsky; +import com.google.gson.reflect.TypeToken; + import de.torui.coflsky.core.Command; -import de.torui.coflsky.core.SoundCommand; -import de.torui.coflsky.core.WriteToChatCommand; +import de.torui.coflsky.core.JsonStringCommand; +import de.torui.coflsky.core.commandEntities.SoundData; +import de.torui.coflsky.core.commandEntities.ChatMessageData; import de.torui.coflsky.network.WSClient; import net.minecraft.client.Minecraft; -import net.minecraft.client.audio.ISound; -import net.minecraft.client.audio.PositionedSound; import net.minecraft.client.audio.PositionedSoundRecord; -import net.minecraft.client.audio.SoundCategory; -import net.minecraft.client.audio.SoundEventAccessorComposite; import net.minecraft.client.audio.SoundHandler; -import net.minecraft.client.audio.SoundManager; -import net.minecraft.command.ICommandManager; -import net.minecraft.command.ICommandSender; import net.minecraft.entity.Entity; import net.minecraft.event.ClickEvent; import net.minecraft.event.ClickEvent.Action; import net.minecraft.event.HoverEvent; -import net.minecraft.server.MinecraftServer; -import net.minecraft.server.integrated.IntegratedServerCommandManager; import net.minecraft.util.ChatComponentText; import net.minecraft.util.ChatStyle; import net.minecraft.util.IChatComponent; @@ -28,23 +22,24 @@ import net.minecraftforge.client.ClientCommandHandler; public class WSCommandHandler { - public static String lastOnClickEvent; + public static transient String lastOnClickEvent; - public static boolean HandleCommand(Command cmd, Entity sender) { + public static boolean HandleCommand(JsonStringCommand cmd, Entity sender) { // Entity sender = Minecraft.getMinecraft().thePlayer; System.out.println("Handling Command=" + cmd.toString()); + switch (cmd.getType()) { case WriteToChat: - WriteToChat(cmd); + WriteToChat(cmd.GetAs(new TypeToken<ChatMessageData>() {})); break; case Execute: - Execute(cmd, sender); + Execute(cmd.GetAs(new TypeToken<String>() {}), sender); break; case PlaySound: - PlaySound(cmd, sender); + PlaySound(cmd.GetAs(new TypeToken<SoundData>() {}), sender); break; case ChatMessage: - ChatMessage(cmd); + ChatMessage(cmd.GetAs(new TypeToken<ChatMessageData[]>() {})); break; default: break; @@ -53,9 +48,9 @@ public class WSCommandHandler { return true; } - private static void PlaySound(Command cmd, Entity sender) { - - SoundCommand sc = WSClient.gson.fromJson(cmd.getData(), SoundCommand.class); + private static void PlaySound(Command<SoundData> cmd, Entity sender) { + + SoundData sc = cmd.getData(); SoundHandler handler = Minecraft.getMinecraft().getSoundHandler(); @@ -66,10 +61,10 @@ public class WSCommandHandler { handler.playSound(psr); } - private static void Execute(Command cmd, Entity sender) { + private static void Execute(Command<String> cmd, Entity sender) { System.out.println("Execute: " + cmd.getData() + " sender:" + sender); - String dummy = WSClient.gson.fromJson(cmd.getData(), String.class); - Execute(dummy,sender); + //String dummy = WSClient.gson.fromJson(cmd.getData(), String.class); + Execute(cmd.getData(),sender); } public static void Execute(String cmd, Entity sender) @@ -82,7 +77,7 @@ public class WSCommandHandler { } - private static IChatComponent CommandToChatComponent(WriteToChatCommand wcmd) { + private static IChatComponent CommandToChatComponent(ChatMessageData wcmd) { if(wcmd.OnClick != null) lastOnClickEvent = "/cofl callback " + wcmd.OnClick; if (wcmd.Text != null) { @@ -110,12 +105,12 @@ public class WSCommandHandler { return null; } - private static void ChatMessage(Command cmd) { - WriteToChatCommand[] list = WSClient.gson.fromJson(cmd.getData(), WriteToChatCommand[].class); + private static void ChatMessage(Command<ChatMessageData[]> cmd) { + ChatMessageData[] list = cmd.getData() ;//WSClient.gson.fromJson(cmd.getData(), WriteToChatCommand[].class); IChatComponent master = new ChatComponentText(""); - for (WriteToChatCommand wcmd : list) { + for (ChatMessageData wcmd : list) { IChatComponent comp = CommandToChatComponent(wcmd); if (comp != null) master.appendSibling(comp); @@ -125,8 +120,8 @@ public class WSCommandHandler { - private static void WriteToChat(Command cmd) { - WriteToChatCommand wcmd = WSClient.gson.fromJson(cmd.getData(), WriteToChatCommand.class); + private static void WriteToChat(Command<ChatMessageData> cmd) { + ChatMessageData wcmd = cmd.getData(); IChatComponent comp = CommandToChatComponent(wcmd); if (comp != null) diff --git a/src/main/java/de/torui/coflsky/core/Command.java b/src/main/java/de/torui/coflsky/core/Command.java index fc6cd13..e18c59a 100644 --- a/src/main/java/de/torui/coflsky/core/Command.java +++ b/src/main/java/de/torui/coflsky/core/Command.java @@ -2,20 +2,21 @@ package de.torui.coflsky.core; import com.google.gson.annotations.SerializedName; -public class Command { +public class Command<T> { @SerializedName("type") private CommandType Type; @SerializedName("data") - private String data; - - public Command() {} - - public Command(CommandType type, String data) { + private T data; + + public Command() { + } + + public Command(CommandType type, T data) { super(); - this.Type = type; + Type = type; this.data = data; } - + public CommandType getType() { return Type; } @@ -24,17 +25,18 @@ public class Command { Type = type; } - public String getData() { + public T getData() { return data; } - public void setData(String data) { + + public void setData(T data) { this.data = data; } @Override public String toString() { return "Command [Type=" + Type + ", data=" + data + "]"; - } - - + } + } + diff --git a/src/main/java/de/torui/coflsky/core/CommandType.java b/src/main/java/de/torui/coflsky/core/CommandType.java index 67b9b93..74e8a41 100644 --- a/src/main/java/de/torui/coflsky/core/CommandType.java +++ b/src/main/java/de/torui/coflsky/core/CommandType.java @@ -5,17 +5,26 @@ import com.google.gson.annotations.SerializedName; public enum CommandType { @SerializedName("writeToChat") WriteToChat, - + @SerializedName("execute") Execute, - + @SerializedName("tokenLogin") TokenLogin, - + @SerializedName("clicked") - Clicked, + Clicked, + @SerializedName("playSound") - PlaySound, + PlaySound, + @SerializedName("chatMessage") - ChatMessage; + ChatMessage, + + @SerializedName("purchaseStart") + PurchaseStart, + + @SerializedName("purchaseConfirm") + PurchaseConfirm, + } diff --git a/src/main/java/de/torui/coflsky/core/JsonStringCommand.java b/src/main/java/de/torui/coflsky/core/JsonStringCommand.java new file mode 100644 index 0000000..9ce0cb1 --- /dev/null +++ b/src/main/java/de/torui/coflsky/core/JsonStringCommand.java @@ -0,0 +1,28 @@ +package de.torui.coflsky.core; + +import com.google.gson.GsonBuilder; +import com.google.gson.reflect.TypeToken; + +public class JsonStringCommand extends Command<String> { + + public JsonStringCommand(String type, String data) { + this.setType(CommandType.valueOf(type)); + this.setData(data); + } + + public JsonStringCommand() { + super(); + + } + + public JsonStringCommand(CommandType type, String data) { + super(type, data); + } + + public <T> Command<T> GetAs(TypeToken<T> type){ + T t = new GsonBuilder().create().fromJson(this.getData(),type.getType()); + Command<?> cmd = new Command<Object>(this.getType(), t); + + return (Command<T>) cmd; + } +} diff --git a/src/main/java/de/torui/coflsky/core/StringCommand.java b/src/main/java/de/torui/coflsky/core/StringCommand.java deleted file mode 100644 index 745f1ba..0000000 --- a/src/main/java/de/torui/coflsky/core/StringCommand.java +++ /dev/null @@ -1,38 +0,0 @@ -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/core/commandEntities/AuctionData.java b/src/main/java/de/torui/coflsky/core/commandEntities/AuctionData.java new file mode 100644 index 0000000..0e677b2 --- /dev/null +++ b/src/main/java/de/torui/coflsky/core/commandEntities/AuctionData.java @@ -0,0 +1,30 @@ +package de.torui.coflsky.core.commandEntities; + +import com.google.gson.annotations.SerializedName; + +public class AuctionData { + @SerializedName("auctionId") + private String auctionId; + @SerializedName("itemId") + private String itemId; + public String getAuctionId() { + return auctionId; + } + public void setAuctionId(String auctionId) { + this.auctionId = auctionId; + } + public String getItemId() { + return itemId; + } + public void setItemId(String itemId) { + this.itemId = itemId; + } + public AuctionData(String auctionId, String itemId) { + super(); + this.auctionId = auctionId; + this.itemId = itemId; + } + + public AuctionData() {} + +} diff --git a/src/main/java/de/torui/coflsky/core/WriteToChatCommand.java b/src/main/java/de/torui/coflsky/core/commandEntities/ChatMessageData.java index b8c64bb..cfc1381 100644 --- a/src/main/java/de/torui/coflsky/core/WriteToChatCommand.java +++ b/src/main/java/de/torui/coflsky/core/commandEntities/ChatMessageData.java @@ -1,8 +1,8 @@ -package de.torui.coflsky.core; +package de.torui.coflsky.core.commandEntities; import com.google.gson.annotations.SerializedName; -public class WriteToChatCommand { +public class ChatMessageData { @SerializedName("text") public String Text; @SerializedName("onClick") @@ -11,11 +11,11 @@ public class WriteToChatCommand { @SerializedName("hover") public String Hover; - public WriteToChatCommand() { + public ChatMessageData() { } - public WriteToChatCommand(String text, String onClick, String hover) { + public ChatMessageData(String text, String onClick, String hover) { super(); Text = text; OnClick = onClick; diff --git a/src/main/java/de/torui/coflsky/core/SoundCommand.java b/src/main/java/de/torui/coflsky/core/commandEntities/SoundData.java index bdba82d..64687ab 100644 --- a/src/main/java/de/torui/coflsky/core/SoundCommand.java +++ b/src/main/java/de/torui/coflsky/core/commandEntities/SoundData.java @@ -1,23 +1,19 @@ -package de.torui.coflsky.core; +package de.torui.coflsky.core.commandEntities; import com.google.gson.annotations.SerializedName; -public class SoundCommand { +public class SoundData { @SerializedName("name") public String Name; @SerializedName("pitch") - public float Pitch; - - + public float Pitch; - public SoundCommand() { + public SoundData() { super(); } - - - public SoundCommand(String name, float pitch) { + public SoundData(String name, float pitch) { super(); Name = name; Pitch = pitch; diff --git a/src/main/java/de/torui/coflsky/network/WSClient.java b/src/main/java/de/torui/coflsky/network/WSClient.java index 529056b..b3f28ca 100644 --- a/src/main/java/de/torui/coflsky/network/WSClient.java +++ b/src/main/java/de/torui/coflsky/network/WSClient.java @@ -17,7 +17,7 @@ import net.minecraft.client.Minecraft; import de.torui.coflsky.CoflSky; import de.torui.coflsky.WSCommandHandler; import de.torui.coflsky.core.Command; -import de.torui.coflsky.core.StringCommand; +import de.torui.coflsky.core.JsonStringCommand; public class WSClient extends WebSocketAdapter { @@ -112,7 +112,7 @@ public class WSClient extends WebSocketAdapter { //super.onTextMessage(websocket, text); System.out.println("Received: "+ text); - Command cmd = gson.fromJson(text, Command.class); + JsonStringCommand cmd = gson.fromJson(text, JsonStringCommand.class); //System.out.println(cmd); WSCommandHandler.HandleCommand(cmd, Minecraft.getMinecraft().thePlayer); @@ -122,12 +122,6 @@ public class WSClient extends WebSocketAdapter { String json = gson.toJson(cmd); 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/network/WSClientWrapper.java b/src/main/java/de/torui/coflsky/network/WSClientWrapper.java index f30d0f9..dd9d13b 100644 --- a/src/main/java/de/torui/coflsky/network/WSClientWrapper.java +++ b/src/main/java/de/torui/coflsky/network/WSClientWrapper.java @@ -10,7 +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; +import de.torui.coflsky.core.JsonStringCommand; import de.torui.coflsky.minecraft_integration.PlayerDataProvider; import net.minecraft.client.Minecraft; import net.minecraft.util.ChatComponentText; @@ -141,9 +141,6 @@ public class WSClientWrapper { } - public void SendMessage(StringCommand sc) { - this.socket.SendCommand(sc); - } public String GetStatus() { return "" + isRunning + " " + |