diff options
Diffstat (limited to 'src/main/java')
6 files changed, 174 insertions, 80 deletions
diff --git a/src/main/java/de/torui/coflsky/CoflSky.java b/src/main/java/de/torui/coflsky/CoflSky.java index 045b0fc..bd5d2c5 100644 --- a/src/main/java/de/torui/coflsky/CoflSky.java +++ b/src/main/java/de/torui/coflsky/CoflSky.java @@ -2,8 +2,11 @@ package de.torui.coflsky; import java.net.URI; import java.net.URISyntaxException; +import java.util.UUID; import de.torui.coflsky.websocket.WSClient; +import de.torui.coflsky.websocket.WSClientWrapper; +import net.minecraft.client.Minecraft; import net.minecraft.init.Blocks; import net.minecraftforge.client.ClientCommandHandler; import net.minecraftforge.common.MinecraftForge; @@ -21,28 +24,38 @@ public class CoflSky { public static final String MODID = "CoflSky"; public static final String VERSION = "1.0"; - public static WSClient WS; + public static WSClientWrapper Wrapper; + + public static String PlayerUUID = ""; + @EventHandler public void init(FMLInitializationEvent event) throws URISyntaxException { + + //Minecraft.getSessionInfo().forEach((a,b) -> System.out.println("Key=" + a + " value=" + b)); + // some example code System.out.println("Initializing"); //new Thread(new WSClient(new URI("ws://localhost:8080"))).start(); System.out.println(">>>Started"); - ClientCommandHandler.instance.registerCommand(new CoflSkyCommand()); - MinecraftForge.EVENT_BUS.register(new EventRegistry()); + + CoflSky.Wrapper = new WSClientWrapper("wss://sky-commands.coflnet.com/modsocket?uuid="); + + if(event.getSide() == Side.CLIENT) + ClientCommandHandler.instance.registerCommand(new CoflSkyCommand()); + MinecraftForge.EVENT_BUS.register(new EventRegistry()); } - @EventHandler + /* @EventHandler public void init(FMLServerStartingEvent event) { if(event.getSide() == Side.CLIENT) return; //event.registerServerCommand(new CoflSkyCommand()); - } + }*/ }
\ No newline at end of file diff --git a/src/main/java/de/torui/coflsky/CoflSkyCommand.java b/src/main/java/de/torui/coflsky/CoflSkyCommand.java index 77ac4d7..1a68cfc 100644 --- a/src/main/java/de/torui/coflsky/CoflSkyCommand.java +++ b/src/main/java/de/torui/coflsky/CoflSkyCommand.java @@ -1,5 +1,6 @@ package de.torui.coflsky; +import java.util.Arrays; import java.util.List; import de.torui.coflsky.core.Command; @@ -8,10 +9,10 @@ import net.minecraft.client.Minecraft; import net.minecraft.command.CommandBase; import net.minecraft.command.CommandException; import net.minecraft.command.ICommandSender; +import net.minecraft.server.MinecraftServer; import net.minecraft.util.BlockPos; import net.minecraft.util.ChatComponentText; import net.minecraft.util.IChatComponent; -import scala.actors.threadpool.Arrays; public class CoflSkyCommand extends CommandBase { @@ -24,41 +25,59 @@ public class CoflSkyCommand extends CommandBase { @Override public String getCommandName() { - return "coflsky"; + return "cofl"; } @Override public String getCommandUsage(ICommandSender sender) { - return "/coflsky token <token> to register\n/coflsky start to connect\n/coflsky stop to stop"; + return "/cofl token <token> to register\n/coflsky start to connect\n/coflsky stop to stop"; } @Override public void processCommand(ICommandSender sender, String[] args) throws CommandException { System.out.println(Arrays.toString(args)); - if(args.length == 1) { + if(args.length >= 1) { switch(args[0]) { case "start": //todo: start + CoflSky.Wrapper.start(); break; case "stop": + CoflSky.Wrapper.stop(); //todo: stop break; case "debug": - WSCommandHandler.HandleCommand(new Command(CommandType.Execute, "/say hewwo"), sender.getCommandSenderEntity()); - WSCommandHandler.HandleCommand(new Command(CommandType.WriteToChat, "{ \"text\": \"Clickable Texts are fun\", \"onClick\": \"/give @p minecraft:apple 1\"}"), sender.getCommandSenderEntity()); + //WSCommandHandler.HandleCommand(new Command(CommandType.Execute, "/say hewwo"), sender.getCommandSenderEntity()); + //WSCommandHandler.HandleCommand(new Command(CommandType.WriteToChat, "{ \"text\": \"Clickable Texts are fun\", \"onClick\": \"me Hello World\"}"), sender.getCommandSenderEntity()); break; case "callback": + CallbackCommand(args); break; default: sender.addChatMessage(new ChatComponentText("" + args[0] +"is not a valid subcommand!")); + System.out.println(args[0] +"is not a valid subcommand!"); return; } } - if(args.length == 2 && args[0].equals("token")) { + /*if(args.length == 2 && args[0].equals("token")) { //todo: send authorisation message - } + }*/ + + } + + public void CallbackCommand(String[] args) { + + String command = String.join(" ", Arrays.copyOfRange(args, 1, args.length)); + System.out.println("CallbackData: " + command); + //new Thread(()->{ + System.out.println("Callback: " + command); + WSCommandHandler.HandleCommand(new Command(CommandType.Execute, command), Minecraft.getMinecraft().thePlayer); + CoflSky.Wrapper.SendMessage(new Command(CommandType.Clicked, 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 aa1592d..4ef15a9 100644 --- a/src/main/java/de/torui/coflsky/EventRegistry.java +++ b/src/main/java/de/torui/coflsky/EventRegistry.java @@ -1,70 +1,62 @@ package de.torui.coflsky; -import java.net.URI; -import java.net.URISyntaxException; +import java.util.UUID; -import de.torui.coflsky.websocket.WSClient; import net.minecraft.client.Minecraft; -import net.minecraft.client.multiplayer.ServerData; -import net.minecraftforge.fml.common.SidedProxy; +import net.minecraft.server.MinecraftServer; +import net.minecraft.server.gui.MinecraftServerGui; +import net.minecraftforge.event.world.WorldEvent; +import net.minecraftforge.fml.client.FMLClientHandler; import net.minecraftforge.fml.common.eventhandler.SubscribeEvent; import net.minecraftforge.fml.common.gameevent.PlayerEvent.PlayerLoggedInEvent; import net.minecraftforge.fml.common.gameevent.PlayerEvent.PlayerLoggedOutEvent; -import net.minecraftforge.fml.relauncher.Side; -import net.minecraftforge.fml.relauncher.SideOnly; +import net.minecraftforge.fml.common.network.FMLNetworkEvent; +import net.minecraftforge.fml.common.network.FMLNetworkEvent.ClientConnectedToServerEvent; +import net.minecraftforge.fml.common.network.FMLNetworkEvent.ClientDisconnectionFromServerEvent; +import net.minecraftforge.fml.server.FMLServerHandler; public class EventRegistry{ - @SideOnly(Side.CLIENT) @SubscribeEvent - public void PlayerLoggedIn(PlayerLoggedInEvent plie) { - - System.out.println("COFLSKY initialized"); + public void onConnectedToServerEvent(ClientConnectedToServerEvent event) { - if(plie.player.getEntityWorld().isRemote) { - //is a server - /*ServerData sd = Minecraft.getMinecraft().getCurrentServerData(); - if(sd != null) { - System.out.println("ServerIP:= " + sd.serverIP); - } else { - System.out.println("Could not get serverdata"); - } - */ - if(CoflSky.WS == null) { - try { - String uuid = Minecraft.getMinecraft().thePlayer.getPersistentID().toString(); - //String uuid = Minecraft.getMinecraft().thePlayer.getUUID(null) - CoflSky.WS = new WSClient(new URI("wss://sky-commands.coflnet.com/modsocket?uuid=" + uuid)); - } catch (URISyntaxException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - - Thread t = new Thread(CoflSky.WS); + if(!event.isLocal) { + String serverIP = Minecraft.getMinecraft().getCurrentServerData().serverIP; + + if(false && serverIP.equals("hypixel.net")) { } + //UUID.randomUUID().toString(); - } else { - System.out.println("World is not remote"); + //String username = Minecraft.getSessionInfo().get("X-Minecraft-Username"); + + //String id = FMLClientHandler.instance().getClient().thePlayer.getUniqueID().toString(); + + String id = UUID.randomUUID().toString();//Minecraft.getMinecraft().thePlayer.getUniqueID().toString(); + System.out.println("PlayerUUID:" + id); + CoflSky.PlayerUUID = id; + + System.out.println("Connected to server"); + CoflSky.Wrapper.start(); + System.out.println("CoflSky started"); } - - } + } + + @SubscribeEvent + public void onDisconnectedFromServerEvent(ClientDisconnectionFromServerEvent event) { + System.out.println("Disconnected from server"); + CoflSky.Wrapper.stop(); + System.out.println("CoflSky stopped"); + } + /*@SubscribeEvent +public void OnSomething(FMLNetworkEvent.ClientConnectedToServerEvent event) { + System.out.println("Client connect to server from network"); +} - @SideOnly(Side.CLIENT) @SubscribeEvent public void PlayerLoggedOut(PlayerLoggedOutEvent ploe) { - System.out.println("COFLSKY disabled"); - - if(CoflSky.WS != null) { - try { - CoflSky.WS.closeBlocking(); - CoflSky.WS = null; - } catch (InterruptedException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - - } - } + //CoflSky.Wrapper.stop(); + System.out.println("COFLSKY disabled"); + }*/ } diff --git a/src/main/java/de/torui/coflsky/WSCommandHandler.java b/src/main/java/de/torui/coflsky/WSCommandHandler.java index 9822f23..a6fa87b 100644 --- a/src/main/java/de/torui/coflsky/WSCommandHandler.java +++ b/src/main/java/de/torui/coflsky/WSCommandHandler.java @@ -4,6 +4,7 @@ import de.torui.coflsky.core.Command; import de.torui.coflsky.core.WriteToChatCommand; import de.torui.coflsky.websocket.WSClient; import net.minecraft.client.Minecraft; +import net.minecraft.command.ICommandManager; import net.minecraft.entity.Entity; import net.minecraft.event.ClickEvent; import net.minecraft.event.ClickEvent.Action; @@ -15,7 +16,7 @@ import net.minecraftforge.client.ClientCommandHandler; public class WSCommandHandler { public static boolean HandleCommand(Command cmd, Entity sender) { - + System.out.println("Handling Command=" + cmd.toString()); switch(cmd.getType()) { case WriteToChat: WriteToChat(cmd); @@ -23,31 +24,44 @@ public class WSCommandHandler { case Execute: Execute(cmd, sender); break; + default: + break; } return true; } private static void Execute(Command cmd, Entity sender) { + //Minecraft.getMinecraft().thePlayer.sendChatMessage(cmd.getData()); - MinecraftServer.getServer().getCommandManager().executeCommand(sender, cmd.getData()); + System.out.println("Execute: " + cmd.getData() + " sender:" + sender); + + Minecraft.getMinecraft().thePlayer.sendChatMessage(cmd.getData()); + + //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); - wcmd.Text = "Hello World"; - wcmd.OnClick = "/give @p apple 1" -; /*String command = "tellraw @p [\"\", {\"text\":\"Hewwo World\", \"clickEvent\": {\"action\":\"run_command\", \"value\":\"/say hi\"}}]"; - //"/tellraw @p [\"\",{\"text\":\"" + wcmd.Text + "\",\"clickEvent\":{\"action\":\"run_command\",\"value\":\"" + wcmd.OnClick + "\"}}]" - int result = ClientCommandHandler.instance.executeCommand(Minecraft.getMinecraft().thePlayer, command); - System.out.println("Sent to commandhandler with result" + result);*/ + //System.out.println("Executing wcmd Text=" + wcmd.Text + " OnClick=" + wcmd.OnClick); + if(wcmd.Text != null && wcmd.OnClick != 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)); + } - IChatComponent comp = new ChatComponentText(wcmd.Text); - ChatStyle style = new ChatStyle().setChatClickEvent(new ClickEvent(Action.RUN_COMMAND, wcmd.OnClick)); - comp.setChatStyle(style); + + comp.setChatStyle(style); + + Minecraft.getMinecraft().thePlayer.addChatMessage(comp); + } - Minecraft.getMinecraft().thePlayer.addChatMessage(comp); } } diff --git a/src/main/java/de/torui/coflsky/websocket/WSClient.java b/src/main/java/de/torui/coflsky/websocket/WSClient.java index c26edf0..9641277 100644 --- a/src/main/java/de/torui/coflsky/websocket/WSClient.java +++ b/src/main/java/de/torui/coflsky/websocket/WSClient.java @@ -9,7 +9,10 @@ import org.java_websocket.handshake.ServerHandshake; import com.google.gson.Gson; import com.google.gson.GsonBuilder; +import de.torui.coflsky.WSCommandHandler; import de.torui.coflsky.core.Command; +import net.minecraft.client.Minecraft; +import net.minecraft.server.MinecraftServer; public class WSClient extends WebSocketClient{ @@ -43,12 +46,11 @@ public class WSClient extends WebSocketClient{ @Override public void onMessage(String message) { - System.out.println(message); + //System.out.println(message); Command cmd = gson.fromJson(message, Command.class); - - - System.out.println(cmd); + //System.out.println(cmd); + WSCommandHandler.HandleCommand(cmd, Minecraft.getMinecraft().thePlayer); } @Override @@ -62,7 +64,8 @@ public class WSClient extends WebSocketClient{ } public void SendCommand(Command command) { - + String json = gson.toJson(command); + this.send(json); } } diff --git a/src/main/java/de/torui/coflsky/websocket/WSClientWrapper.java b/src/main/java/de/torui/coflsky/websocket/WSClientWrapper.java new file mode 100644 index 0000000..ef2ee8b --- /dev/null +++ b/src/main/java/de/torui/coflsky/websocket/WSClientWrapper.java @@ -0,0 +1,53 @@ +package de.torui.coflsky.websocket; + +import java.net.URI; +import java.net.URISyntaxException; +import java.util.UUID; + +import de.torui.coflsky.CoflSky; +import de.torui.coflsky.core.Command; + + +public class WSClientWrapper { + public WSClient socket; + public Thread thread; + public boolean isRunning; + public String uri = ""; + + public WSClientWrapper(String uri) { + this.uri = uri; + } + + public synchronized void start() { + if(!isRunning) { + String uuid = CoflSky.PlayerUUID; + try { + socket = new WSClient(new URI(uri + uuid)); + } catch (URISyntaxException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + thread = new Thread(socket); + thread.start(); + isRunning=true; + } + } + + public synchronized void stop() { + if(isRunning) { + try { + socket.closeBlocking(); + } catch (InterruptedException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + isRunning = false; + socket = null; + socket = null; + } + } + + public synchronized void SendMessage(Command cmd){ + this.socket.SendCommand(cmd); + } +} |