aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorÄkwav <16632490+Ekwav@users.noreply.github.com>2021-11-05 21:34:33 +0100
committerGitHub <noreply@github.com>2021-11-05 21:34:33 +0100
commit39a061b57e38667987a4e38df71b5d8030222c1b (patch)
treed87a007269aa6696b38fe37ad3e3a2e536f4f5f5
parent5cc73b705cf4c6d6887e650daf8222b63dedc011 (diff)
parente533c7f6f9860479f5f472280d6fce8b87d8d0b8 (diff)
downloadCOFL-39a061b57e38667987a4e38df71b5d8030222c1b.tar.gz
COFL-39a061b57e38667987a4e38df71b5d8030222c1b.tar.bz2
COFL-39a061b57e38667987a4e38df71b5d8030222c1b.zip
Merge pull request #30 from Coflnet/feature/command-rewrite-plus-ci
Rewrite Commands to be Generic
-rw-r--r--src/main/java/de/torui/coflsky/CoflSkyCommand.java12
-rw-r--r--src/main/java/de/torui/coflsky/EventRegistry.java11
-rw-r--r--src/main/java/de/torui/coflsky/WSCommandHandler.java55
-rw-r--r--src/main/java/de/torui/coflsky/commands/Command.java (renamed from src/main/java/de/torui/coflsky/core/Command.java)30
-rw-r--r--src/main/java/de/torui/coflsky/commands/CommandType.java (renamed from src/main/java/de/torui/coflsky/core/CommandType.java)23
-rw-r--r--src/main/java/de/torui/coflsky/commands/JsonStringCommand.java28
-rw-r--r--src/main/java/de/torui/coflsky/commands/models/AuctionData.java30
-rw-r--r--src/main/java/de/torui/coflsky/commands/models/ChatMessageData.java (renamed from src/main/java/de/torui/coflsky/core/WriteToChatCommand.java)8
-rw-r--r--src/main/java/de/torui/coflsky/commands/models/SoundData.java (renamed from src/main/java/de/torui/coflsky/core/SoundCommand.java)14
-rw-r--r--src/main/java/de/torui/coflsky/core/StringCommand.java38
-rw-r--r--src/main/java/de/torui/coflsky/network/WSClient.java12
-rw-r--r--src/main/java/de/torui/coflsky/network/WSClientWrapper.java7
12 files changed, 142 insertions, 126 deletions
diff --git a/src/main/java/de/torui/coflsky/CoflSkyCommand.java b/src/main/java/de/torui/coflsky/CoflSkyCommand.java
index e48d131..b48a3cd 100644
--- a/src/main/java/de/torui/coflsky/CoflSkyCommand.java
+++ b/src/main/java/de/torui/coflsky/CoflSkyCommand.java
@@ -4,9 +4,9 @@ import java.io.IOException;
import java.util.Arrays;
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.commands.Command;
+import de.torui.coflsky.commands.CommandType;
+import de.torui.coflsky.commands.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 c68836f..f7d708c 100644
--- a/src/main/java/de/torui/coflsky/EventRegistry.java
+++ b/src/main/java/de/torui/coflsky/EventRegistry.java
@@ -2,6 +2,10 @@ package de.torui.coflsky;
import java.util.UUID;
+import de.torui.coflsky.commands.Command;
+import de.torui.coflsky.commands.CommandType;
+import de.torui.coflsky.commands.JsonStringCommand;
+import de.torui.coflsky.network.WSClient;
import net.minecraft.client.Minecraft;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.gui.MinecraftServerGui;
@@ -58,14 +62,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..a06640c 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 de.torui.coflsky.core.Command;
-import de.torui.coflsky.core.SoundCommand;
-import de.torui.coflsky.core.WriteToChatCommand;
+import com.google.gson.reflect.TypeToken;
+
+import de.torui.coflsky.commands.Command;
+import de.torui.coflsky.commands.JsonStringCommand;
+import de.torui.coflsky.commands.models.ChatMessageData;
+import de.torui.coflsky.commands.models.SoundData;
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/commands/Command.java
index fc6cd13..20ab8f1 100644
--- a/src/main/java/de/torui/coflsky/core/Command.java
+++ b/src/main/java/de/torui/coflsky/commands/Command.java
@@ -1,21 +1,22 @@
-package de.torui.coflsky.core;
+package de.torui.coflsky.commands;
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/commands/CommandType.java
index 67b9b93..3fac287 100644
--- a/src/main/java/de/torui/coflsky/core/CommandType.java
+++ b/src/main/java/de/torui/coflsky/commands/CommandType.java
@@ -1,21 +1,30 @@
-package de.torui.coflsky.core;
+package de.torui.coflsky.commands;
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/commands/JsonStringCommand.java b/src/main/java/de/torui/coflsky/commands/JsonStringCommand.java
new file mode 100644
index 0000000..6bc22d8
--- /dev/null
+++ b/src/main/java/de/torui/coflsky/commands/JsonStringCommand.java
@@ -0,0 +1,28 @@
+package de.torui.coflsky.commands;
+
+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/commands/models/AuctionData.java b/src/main/java/de/torui/coflsky/commands/models/AuctionData.java
new file mode 100644
index 0000000..ff1f705
--- /dev/null
+++ b/src/main/java/de/torui/coflsky/commands/models/AuctionData.java
@@ -0,0 +1,30 @@
+package de.torui.coflsky.commands.models;
+
+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/commands/models/ChatMessageData.java
index b8c64bb..65459c5 100644
--- a/src/main/java/de/torui/coflsky/core/WriteToChatCommand.java
+++ b/src/main/java/de/torui/coflsky/commands/models/ChatMessageData.java
@@ -1,8 +1,8 @@
-package de.torui.coflsky.core;
+package de.torui.coflsky.commands.models;
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/commands/models/SoundData.java
index bdba82d..5df18d3 100644
--- a/src/main/java/de/torui/coflsky/core/SoundCommand.java
+++ b/src/main/java/de/torui/coflsky/commands/models/SoundData.java
@@ -1,23 +1,19 @@
-package de.torui.coflsky.core;
+package de.torui.coflsky.commands.models;
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/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/network/WSClient.java b/src/main/java/de/torui/coflsky/network/WSClient.java
index 529056b..cd23b30 100644
--- a/src/main/java/de/torui/coflsky/network/WSClient.java
+++ b/src/main/java/de/torui/coflsky/network/WSClient.java
@@ -16,8 +16,8 @@ import com.neovisionaries.ws.client.WebSocketState;
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.commands.Command;
+import de.torui.coflsky.commands.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..6542542 100644
--- a/src/main/java/de/torui/coflsky/network/WSClientWrapper.java
+++ b/src/main/java/de/torui/coflsky/network/WSClientWrapper.java
@@ -9,8 +9,8 @@ import java.util.UUID;
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.commands.Command;
+import de.torui.coflsky.commands.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 + " " +