diff options
author | Florian Rinke <develop@torui.de> | 2021-09-23 20:05:34 +0200 |
---|---|---|
committer | Florian Rinke <develop@torui.de> | 2021-09-23 20:05:34 +0200 |
commit | 2c585a31b719e9a6fee3800a1cc57bd4eae6bd2d (patch) | |
tree | 1c7ff966a55009ab1faea9524e825997522f5f1d | |
parent | e5e0c9a3a4fd9b2c10464e0f129bcb60b2db0f19 (diff) | |
download | COFL-2c585a31b719e9a6fee3800a1cc57bd4eae6bd2d.tar.gz COFL-2c585a31b719e9a6fee3800a1cc57bd4eae6bd2d.tar.bz2 COFL-2c585a31b719e9a6fee3800a1cc57bd4eae6bd2d.zip |
switched websocket library
-rw-r--r-- | Dockerfile | 1 | ||||
-rw-r--r-- | MetaFiles/Java-Websocket-1.5.1.jar.meta | 1 | ||||
-rw-r--r-- | MetaFiles/slf4j-api-1.7.25.jar.meta | 1 | ||||
-rw-r--r-- | build.gradle | 68 | ||||
-rw-r--r-- | src/main/java/de/torui/coflsky/CoflSky.java | 4 | ||||
-rw-r--r-- | src/main/java/de/torui/coflsky/CoflSkyCommand.java | 6 | ||||
-rw-r--r-- | src/main/java/de/torui/coflsky/EventRegistry.java | 9 | ||||
-rw-r--r-- | src/main/java/de/torui/coflsky/WSCommandHandler.java | 1 | ||||
-rw-r--r-- | src/main/java/de/torui/coflsky/websocket/WSClient.java | 97 | ||||
-rw-r--r-- | src/main/java/de/torui/coflsky/websocket/WSClientWrapper.java | 26 |
10 files changed, 147 insertions, 67 deletions
diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..e8e9cef --- /dev/null +++ b/Dockerfile @@ -0,0 +1 @@ +FROM openjdk:8-jdk-slim diff --git a/MetaFiles/Java-Websocket-1.5.1.jar.meta b/MetaFiles/Java-Websocket-1.5.1.jar.meta new file mode 100644 index 0000000..581b063 --- /dev/null +++ b/MetaFiles/Java-Websocket-1.5.1.jar.meta @@ -0,0 +1 @@ +Maven-Artifact: org.java-websocket:Java-WebSocket:1.5.1 diff --git a/MetaFiles/slf4j-api-1.7.25.jar.meta b/MetaFiles/slf4j-api-1.7.25.jar.meta new file mode 100644 index 0000000..0b67d2a --- /dev/null +++ b/MetaFiles/slf4j-api-1.7.25.jar.meta @@ -0,0 +1 @@ +Maven-Artifact: org.slf4j:slf4j-api:1.7.25 diff --git a/build.gradle b/build.gradle index fe712d1..769b091 100644 --- a/build.gradle +++ b/build.gradle @@ -10,18 +10,15 @@ buildscript { mavenCentral() } dependencies { + classpath "com.github.jengelman.gradle.plugins:shadow:1.2.0" classpath 'net.minecraftforge.gradle:ForgeGradle:2.1-SNAPSHOT' } } +apply plugin: 'java' +apply plugin: 'com.github.johnrengelman.shadow' apply plugin: 'net.minecraftforge.gradle.forge' -/* -// for people who want stable - not yet functional for MC 1.8.8 - we require the forgegradle 2.1 snapshot -plugins { - //id "net.minecraftforge.gradle.forge" version "2.0.2" -}*/ - version = "1.0" group= "de.torui.coflmod" // http://maven.apache.org/guides/mini/guide-naming-conventions.html archivesBaseName = "CoflMod" @@ -38,46 +35,11 @@ minecraft { mappings = "stable_20" //makeObfSourceJar = false // an Srg named sources jar is made by default. uncomment this to disable. } + configurations { shade - compile.extendsFrom shade -} - -jar { - /*from(configurations.inJar) { - into 'META-INF/libraries' - }*/ - - configurations.shade.each { dep -> - from(project.zipTree(dep)){ - exclude 'META-INF', 'META-INF/**' - } - } -} - -minecraft { - srgExtra "PK: org/slf4j de/torui/org/slf4j" - srgExtra "PK: org/java_websocket de/torui/org/java_websocket" -} - -jar { - /*manifest { - attributes("Implementation-Title": "Gradle", - "Implementation-Version": version, - "ContainedDeps": "Java-WebSocket-1.5.1.jar slf4j-api-1.7.25.jar",) - }*/ - - /* from('./MetaFiles') { - include 'Java-WebSocket-1.5.1.jar.meta' - into('/META-INF/libraries') - } - from('./MetaFiles') { - include 'slf4j-api-1.7.25.jar.meta' - into('/META-INF/libraries') - }*/ - // file("libraries/Java-WebSocket-1.5.1.jar.meta").text= "Maven-Artifact: org.java-websocket:Java-WebSocket:1.5.1" + compile.extendsFrom(shade) } - dependencies { // you may put jars on which you depend on in ./libs // or you may define them like so.. @@ -100,8 +62,12 @@ dependencies { // http://www.gradle.org/docs/current/userguide/artifact_dependencies_tutorial.html // http://www.gradle.org/docs/current/userguide/dependency_management.html //inJar 'javax.websocket:javax.websocket-api:1.1' - shade 'org.java-websocket:Java-WebSocket:1.5.1' - // 'org.slf4j:slf4j-nop:1.7.25' + + shade 'com.neovisionaries:nv-websocket-client:2.14' + //compile 'com.neovisionaries:nv-websocket-client:2.14' + //shade 'org.java-websocket:Java-WebSocket:1.5.2' + //shade fileTree(dir: '.', include: ['Java-WebSocket-1.5.3-SNAPSHOT.jar']) + //shade fileTree(dir: '.', include: ['nv-websocket-client-2.14.jar']) } sourceCompatibility = targetCompatibility = '1.8' // Need this here so eclipse task generates correctly. @@ -121,10 +87,20 @@ processResources // replace version and mcversion expand 'version':project.version, 'mcversion':project.minecraft.version - } + } // copy everything else, thats not the mcmod.info from(sourceSets.main.resources.srcDirs) { exclude 'mcmod.info' } } + + + +shadowJar { + version = 1.0 + configurations = [project.configurations.shade] +} + +reobf { shadowJar { mappingType = "SEARGE" } } +tasks.reobfJar.dependsOn(shadowJar) diff --git a/src/main/java/de/torui/coflsky/CoflSky.java b/src/main/java/de/torui/coflsky/CoflSky.java index bd5d2c5..a07db4b 100644 --- a/src/main/java/de/torui/coflsky/CoflSky.java +++ b/src/main/java/de/torui/coflsky/CoflSky.java @@ -34,6 +34,10 @@ public class CoflSky //Minecraft.getSessionInfo().forEach((a,b) -> System.out.println("Key=" + a + " value=" + b)); + //System.out.println("Loggerfactory: " + LoggerFactory.getILoggerFactory()); + // Logger log = LoggerFactory.getLogger(CoflSky.class); + // log.debug("Testing"); + // some example code System.out.println("Initializing"); diff --git a/src/main/java/de/torui/coflsky/CoflSkyCommand.java b/src/main/java/de/torui/coflsky/CoflSkyCommand.java index 1a68cfc..6bb50de 100644 --- a/src/main/java/de/torui/coflsky/CoflSkyCommand.java +++ b/src/main/java/de/torui/coflsky/CoflSkyCommand.java @@ -48,8 +48,8 @@ public class CoflSkyCommand extends CommandBase { //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\": \"me Hello World\"}"), sender.getCommandSenderEntity()); + 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; case "callback": CallbackCommand(args); @@ -73,7 +73,7 @@ public class CoflSkyCommand extends CommandBase { System.out.println("CallbackData: " + command); //new Thread(()->{ System.out.println("Callback: " + command); - WSCommandHandler.HandleCommand(new Command(CommandType.Execute, command), Minecraft.getMinecraft().thePlayer); + WSCommandHandler.HandleCommand(new Command(CommandType.Execute, command), Minecraft.getMinecraft().thePlayer); CoflSky.Wrapper.SendMessage(new Command(CommandType.Clicked, command)); System.out.println("Sent!"); diff --git a/src/main/java/de/torui/coflsky/EventRegistry.java b/src/main/java/de/torui/coflsky/EventRegistry.java index 4ef15a9..afe39bb 100644 --- a/src/main/java/de/torui/coflsky/EventRegistry.java +++ b/src/main/java/de/torui/coflsky/EventRegistry.java @@ -19,13 +19,14 @@ public class EventRegistry{ @SubscribeEvent public void onConnectedToServerEvent(ClientConnectedToServerEvent event) { - + //String serverIP = Minecraft.getMinecraft().getCurrentServerData().serverIP; + //System.out.println("ServerIP: " + serverIP); if(!event.isLocal) { - String serverIP = Minecraft.getMinecraft().getCurrentServerData().serverIP; + //String serverIP = Minecraft.getMinecraft().getCurrentServerData().serverIP; - if(false && serverIP.equals("hypixel.net")) { + /* if(false && serverIP.equals("hypixel.net")) { - } + }*/ //UUID.randomUUID().toString(); //String username = Minecraft.getSessionInfo().get("X-Minecraft-Username"); diff --git a/src/main/java/de/torui/coflsky/WSCommandHandler.java b/src/main/java/de/torui/coflsky/WSCommandHandler.java index a6fa87b..cdfc954 100644 --- a/src/main/java/de/torui/coflsky/WSCommandHandler.java +++ b/src/main/java/de/torui/coflsky/WSCommandHandler.java @@ -16,6 +16,7 @@ import net.minecraftforge.client.ClientCommandHandler; public class WSCommandHandler { public static boolean HandleCommand(Command cmd, Entity sender) { + //Entity sender = Minecraft.getMinecraft().thePlayer; System.out.println("Handling Command=" + cmd.toString()); switch(cmd.getType()) { case WriteToChat: diff --git a/src/main/java/de/torui/coflsky/websocket/WSClient.java b/src/main/java/de/torui/coflsky/websocket/WSClient.java index 9641277..59e9abb 100644 --- a/src/main/java/de/torui/coflsky/websocket/WSClient.java +++ b/src/main/java/de/torui/coflsky/websocket/WSClient.java @@ -1,20 +1,100 @@ package de.torui.coflsky.websocket; -import java.net.URI; -import java.util.LinkedList; -import java.util.Queue; -import org.java_websocket.client.WebSocketClient; -import org.java_websocket.handshake.ServerHandshake; +import java.io.IOException; +import java.net.URI; import com.google.gson.Gson; import com.google.gson.GsonBuilder; +import com.neovisionaries.ws.client.WebSocket; +import com.neovisionaries.ws.client.WebSocketAdapter; +import com.neovisionaries.ws.client.WebSocketException; +import com.neovisionaries.ws.client.WebSocketFactory; +import com.neovisionaries.ws.client.WebSocketState; +import net.minecraft.client.Minecraft; 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 WebSocketAdapter { + + public static Gson gson; + + + static { + gson = new GsonBuilder()/*.setFieldNamingStrategy(new FieldNamingStrategy() { + @Override + public String translateName(Field f) { + + String name = f.getName(); + char firstChar = name.charAt(0); + return Character.toLowerCase(firstChar) + name.substring(1); + } + })*/.create(); + } + public URI uri; + public WebSocket socket; + + public WSClient(URI uri) { + this.uri = uri; + } + + public void start() throws IOException, WebSocketException { + WebSocketFactory factory = new WebSocketFactory(); + this.socket = factory.createSocket(uri); + this.socket.addListener(this); + this.socket.connect(); + } + + public void stop() { + System.out.println("Closing Socket"); + // socket.sendClose(); + socket.clearListeners(); + + socket.disconnect(); + /*try { + socket.getConnectedSocket().close(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (WebSocketException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + }*/ + System.out.println("Socket closed"); + + } + + @Override + public void onStateChanged(WebSocket websocket, WebSocketState newState) throws Exception { + System.out.println("WebSocket Changed state to: " + newState); + super.onStateChanged(websocket, newState); + } + + + + @Override + public void onTextMessage(WebSocket websocket, String text) throws Exception{ + + //super.onTextMessage(websocket, text); + System.out.println("Received: "+ text); + Command cmd = gson.fromJson(text, Command.class); + //System.out.println(cmd); + WSCommandHandler.HandleCommand(cmd, Minecraft.getMinecraft().thePlayer); + + } + + public void SendCommand(Command cmd) { + String json = gson.toJson(cmd); + this.socket.sendText(json); + } + + + + +} + +/* public class WSClient extends WebSocketClient{ public static Gson gson; @@ -31,7 +111,7 @@ public class WSClient extends WebSocketClient{ char firstChar = name.charAt(0); return Character.toLowerCase(firstChar) + name.substring(1); } - })*/.create(); + })*.create(); } public WSClient(URI serverUri) { @@ -69,3 +149,4 @@ public class WSClient extends WebSocketClient{ } } +*/ diff --git a/src/main/java/de/torui/coflsky/websocket/WSClientWrapper.java b/src/main/java/de/torui/coflsky/websocket/WSClientWrapper.java index ef2ee8b..b3c8832 100644 --- a/src/main/java/de/torui/coflsky/websocket/WSClientWrapper.java +++ b/src/main/java/de/torui/coflsky/websocket/WSClientWrapper.java @@ -1,9 +1,12 @@ package de.torui.coflsky.websocket; +import java.io.IOException; import java.net.URI; import java.net.URISyntaxException; import java.util.UUID; +import com.neovisionaries.ws.client.WebSocketException; + import de.torui.coflsky.CoflSky; import de.torui.coflsky.core.Command; @@ -22,28 +25,39 @@ public class WSClientWrapper { 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 = new Thread(socket); thread.start(); - isRunning=true; + isRunning=true;*/ + isRunning = true; + try { + socket.start(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } catch (WebSocketException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } } } public synchronized void stop() { if(isRunning) { - try { - socket.closeBlocking(); + /* try { + //socket.closeBlocking(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); - } + }*/ + socket.stop(); isRunning = false; socket = null; - socket = null; } } |