From 16b9821f55eec8f4a77ee1743cbc3f9b4ba3e303 Mon Sep 17 00:00:00 2001 From: Florian Rinke Date: Mon, 11 Oct 2021 17:52:17 +0200 Subject: implements #11, #8 --- src/main/java/de/torui/coflsky/CoflSky.java | 59 +++------ src/main/java/de/torui/coflsky/CoflSkyCommand.java | 24 ++-- .../minecraft_integration/CoflSessionManager.java | 141 +++++++++++++++++++++ .../java/de/torui/coflsky/websocket/WSClient.java | 68 ++-------- .../torui/coflsky/websocket/WSClientWrapper.java | 106 +++++++++++++--- 5 files changed, 266 insertions(+), 132 deletions(-) create mode 100644 src/main/java/de/torui/coflsky/minecraft_integration/CoflSessionManager.java (limited to 'src') diff --git a/src/main/java/de/torui/coflsky/CoflSky.java b/src/main/java/de/torui/coflsky/CoflSky.java index cc762ca..85d05b6 100644 --- a/src/main/java/de/torui/coflsky/CoflSky.java +++ b/src/main/java/de/torui/coflsky/CoflSky.java @@ -1,13 +1,12 @@ package de.torui.coflsky; -import java.io.IOException; + import java.net.URISyntaxException; -import java.util.UUID; + import org.lwjgl.input.Keyboard; -import de.torui.coflsky.minecraft_integration.PlayerDataProvider; -import de.torui.coflsky.minecraft_integration.TemporarySession; + import de.torui.coflsky.websocket.WSClientWrapper; import net.minecraft.client.settings.KeyBinding; import net.minecraftforge.client.ClientCommandHandler; @@ -27,46 +26,26 @@ public class CoflSky public static WSClientWrapper Wrapper; public static KeyBinding[] keyBindings; - + + public static final String[] webSocketURIPrefix = new String [] { + "wss://sky-commands.coflnet.com/modsocket", + "wss://sky-mod.coflnet.com/modsocket", + "ws://sky-commands.coflnet.com/modsocket", + "ws://sky-mod.coflnet.com/modsocket", + }; + + public static String CommandUri = "https://sky-commands.coflnet.com/api/mod/commands"; @EventHandler public void init(FMLInitializationEvent event) throws URISyntaxException { - System.out.println(">>>> "+ TemporarySession.GetTempFileFolder().toString()); - //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"); - - //new Thread(new WSClient(new URI("ws://localhost:8080"))).start(); - System.out.println(">>>Started"); - - String username = PlayerDataProvider.getUsername(); - String uuid = PlayerDataProvider.getActivePlayerUUID(); - System.out.println(">>> Username= " + username + " with UUID=" + uuid ); - - String tempUUID = UUID.randomUUID().toString(); - try { - TemporarySession.UpdateSessions(); - tempUUID = TemporarySession.GetSession(username).SessionUUID; - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } + System.out.println(">>>Started"); - - //CoflSky.Wrapper = new WSClientWrapper("wss://sky-commands.coflnet.com/modsocket?version=" + CoflSky.VERSION + "&uuid="); - CoflSky.Wrapper = new WSClientWrapper("ws://sky-mod.coflnet.com/modsocket?version=" + CoflSky.VERSION + "&SId=" + tempUUID + "&uuid=" + uuid); + CoflSky.Wrapper = new WSClientWrapper(webSocketURIPrefix); keyBindings = new KeyBinding[] { - new KeyBinding("key.replay_last.onclick", Keyboard.KEY_R, "SkyCofl") + new KeyBinding("key.replay_last.onclick", Keyboard.KEY_R, "SkyCofl"), }; if(event.getSide() == Side.CLIENT) { @@ -82,14 +61,6 @@ public class CoflSky MinecraftForge.EVENT_BUS.register(new EventRegistry()); } - - /* @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 0cee4cb..7eacc16 100644 --- a/src/main/java/de/torui/coflsky/CoflSkyCommand.java +++ b/src/main/java/de/torui/coflsky/CoflSkyCommand.java @@ -6,6 +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.minecraft_integration.CoflSessionManager; import de.torui.coflsky.websocket.WSClient; import net.minecraft.client.Minecraft; import net.minecraft.command.CommandBase; @@ -42,14 +43,8 @@ public class CoflSkyCommand extends CommandBase { public static final String HelpText = "Available local sub-commands:\n" + "start: starts a new connection\n" + "stop: stops the connection\n" + + "reset: resets all local session information and stops the connection\n" + "status: Emits status information\nServer-Only Commands:"; - - @Override - public List addTabCompletionOptions(ICommandSender sender, String[] args, BlockPos pos) { - // TODO Auto-generated method stub - return super.addTabCompletionOptions(sender, args, pos); - } - @Override public void processCommand(ICommandSender sender, String[] args) throws CommandException { System.out.println(Arrays.toString(args)); @@ -58,7 +53,8 @@ public class CoflSkyCommand extends CommandBase { switch(args[0]) { case "start": //todo: start - CoflSky.Wrapper.start(); + sender.addChatMessage(new ChatComponentText("starting connection...")); + CoflSky.Wrapper.startConnection(); break; case "stop": CoflSky.Wrapper.stop(); @@ -84,6 +80,9 @@ public class CoflSkyCommand extends CommandBase { case "status": sender.addChatMessage(new ChatComponentText(StatusMessage())); break; + case "reset": + HandleReset(); + break; default: CommandNotRecognized(args, sender); return; @@ -96,6 +95,15 @@ public class CoflSkyCommand extends CommandBase { } + private void HandleReset() { + CoflSky.Wrapper.stop(); + Minecraft.getMinecraft().thePlayer.addChatMessage(new ChatComponentText("Stopping Connection to CoflNet")); + CoflSessionManager.DeleteAllCoflSessions(); + Minecraft.getMinecraft().thePlayer.addChatMessage(new ChatComponentText("Deleting CoflNet sessions...")); + CoflSky.Wrapper.startConnection(); + Minecraft.getMinecraft().thePlayer.addChatMessage(new ChatComponentText("Started the Connection to CoflNet")); + } + public String StatusMessage() { String vendor = System.getProperty("java.vm.vendor"); diff --git a/src/main/java/de/torui/coflsky/minecraft_integration/CoflSessionManager.java b/src/main/java/de/torui/coflsky/minecraft_integration/CoflSessionManager.java new file mode 100644 index 0000000..ebff38a --- /dev/null +++ b/src/main/java/de/torui/coflsky/minecraft_integration/CoflSessionManager.java @@ -0,0 +1,141 @@ +package de.torui.coflsky.minecraft_integration; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.time.Duration; +import java.time.LocalDateTime; +import java.time.ZonedDateTime; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.UUID; +import java.util.stream.Collectors; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.TypeAdapter; +import com.google.gson.stream.JsonReader; +import com.google.gson.stream.JsonWriter; + +import net.minecraftforge.fml.common.Loader; + +public class CoflSessionManager { + private static Gson gson = new GsonBuilder() .registerTypeAdapter(ZonedDateTime.class, new TypeAdapter() { + @Override + public void write(JsonWriter out, ZonedDateTime value) throws IOException { + out.value(value.toString()); + } + + @Override + public ZonedDateTime read(JsonReader in) throws IOException { + return ZonedDateTime.parse(in.nextString()); + } + }) + .enableComplexMapKeySerialization().create(); + public static class CoflSession { + + public String SessionUUID; + public ZonedDateTime timestampCreated; + public CoflSession() {} + public CoflSession(String sessionUUID, ZonedDateTime timestampCreated) { + super(); + SessionUUID = sessionUUID; + this.timestampCreated = timestampCreated; + } + + } + + public static void UpdateCoflSessions() throws IOException { + Map sessions = GetCoflSessions(); + + for (String username : sessions.keySet()) { + if(!isValidSession(sessions.get(username))) { + DeleteCoflSession(username); + } + } + } + + public static Path GetTempFileFolder() { + + Path dataPath = Paths.get(Loader.instance().getConfigDir().getPath(), "CoflSky", "sessions"); + dataPath.toFile().mkdirs(); + + return dataPath; + } + + public static Map GetCoflSessions() throws IOException{ + + File[] sessions = GetTempFileFolder().toFile().listFiles(); + + Map map = new HashMap<>(); + + for (int i= 0; i