From 2297be65c50d36916e6efc690ad297a149590e1f Mon Sep 17 00:00:00 2001 From: Abhiram555 <63419731+Sasuke449@users.noreply.github.com> Date: Thu, 19 Jan 2023 03:57:35 +0530 Subject: Fixed issue #81 (#82) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Remove the useless relocation in gradle * Implement the getMods command * Update src/main/java/de/torui/coflsky/commands/models/ModListData.java Co-authored-by: Äkwav <16632490+Ekwav@users.noreply.github.com> --- .vscode/launch.json | 31 ++++++++++++++++ build.gradle.kts | 3 -- src/main/java/de/torui/coflsky/CoflSky.java | 2 ++ .../java/de/torui/coflsky/WSCommandHandler.java | 42 +++++++++++++++++++--- .../de/torui/coflsky/commands/CommandType.java | 5 ++- .../torui/coflsky/commands/models/ModListData.java | 33 +++++++++++++++++ .../java/de/torui/coflsky/network/WSClient.java | 1 - .../java/de/torui/coflsky/utils/FileUtils.java | 40 +++++++++++++++++++++ 8 files changed, 148 insertions(+), 9 deletions(-) create mode 100644 .vscode/launch.json create mode 100644 src/main/java/de/torui/coflsky/commands/models/ModListData.java create mode 100644 src/main/java/de/torui/coflsky/utils/FileUtils.java diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..19c0552 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,31 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "type": "java", + "name": "Minecraft Client", + "request": "launch", + "cwd": "${workspaceFolder}/run", + "console": "internalConsole", + "stopOnEntry": false, + "mainClass": "net.fabricmc.devlaunchinjector.Main", + "vmArgs": "\"-Dfabric.dli.config\u003dE:\\CoflWork\\SkyblockMod\\.gradle\\loom-cache\\launch.cfg\" \"-Dfabric.dli.env\u003dclient\" \"-Dfabric.dli.main\u003dnet.minecraft.launchwrapper.Launch\"", + "args": "", + "env": {}, + "projectName": "" + }, + { + "type": "java", + "name": "Minecraft Server", + "request": "launch", + "cwd": "${workspaceFolder}/run", + "console": "internalConsole", + "stopOnEntry": false, + "mainClass": "net.fabricmc.devlaunchinjector.Main", + "vmArgs": "\"-Dfabric.dli.config\u003dE:\\CoflWork\\SkyblockMod\\.gradle\\loom-cache\\launch.cfg\" \"-Dfabric.dli.env\u003dserver\" \"-Dfabric.dli.main\u003dnet.minecraft.launchwrapper.Launch\"", + "args": "\"nogui\"", + "env": {}, + "projectName": "" + } + ] +} \ No newline at end of file diff --git a/build.gradle.kts b/build.gradle.kts index da4183b..734083b 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -85,9 +85,6 @@ tasks.shadowJar { println("Config: ${it.files}") } } - - // If you want to include other dependencies and shadow them, you can relocate them in here - fun relocate(name: String) = relocate(name, "com.examplemod.deps.$name") } tasks.assemble.get().dependsOn(tasks.remapJar) diff --git a/src/main/java/de/torui/coflsky/CoflSky.java b/src/main/java/de/torui/coflsky/CoflSky.java index 53c3fd3..7ab2448 100644 --- a/src/main/java/de/torui/coflsky/CoflSky.java +++ b/src/main/java/de/torui/coflsky/CoflSky.java @@ -64,6 +64,8 @@ public class CoflSky if (config == null) { config = LocalConfig.createDefaultConfig(); } + // Cache all the mods on load + WSCommandHandler.cacheMods(); } @EventHandler public void init(FMLInitializationEvent event) throws URISyntaxException diff --git a/src/main/java/de/torui/coflsky/WSCommandHandler.java b/src/main/java/de/torui/coflsky/WSCommandHandler.java index ef15381..5427364 100644 --- a/src/main/java/de/torui/coflsky/WSCommandHandler.java +++ b/src/main/java/de/torui/coflsky/WSCommandHandler.java @@ -1,16 +1,16 @@ package de.torui.coflsky; +import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; import de.torui.coflsky.commands.Command; import de.torui.coflsky.commands.CommandType; import de.torui.coflsky.commands.JsonStringCommand; -import de.torui.coflsky.commands.models.ChatMessageData; -import de.torui.coflsky.commands.models.FlipData; -import de.torui.coflsky.commands.models.SoundData; +import de.torui.coflsky.commands.RawCommand; +import de.torui.coflsky.commands.models.*; import de.torui.coflsky.configuration.ConfigurationManager; -import de.torui.coflsky.commands.models.TimerData; import de.torui.coflsky.handlers.EventRegistry; +import de.torui.coflsky.utils.FileUtils; import net.minecraft.client.Minecraft; import net.minecraft.client.audio.PositionedSoundRecord; import net.minecraft.client.audio.SoundHandler; @@ -23,11 +23,18 @@ import net.minecraft.util.ChatStyle; import net.minecraft.util.IChatComponent; import net.minecraft.util.ResourceLocation; import net.minecraftforge.client.ClientCommandHandler; +import net.minecraftforge.common.ForgeModContainer; +import net.minecraftforge.fml.common.Loader; +import net.minecraftforge.fml.common.ModContainer; + +import java.io.File; public class WSCommandHandler { public static transient String lastOnClickEvent; public static FlipHandler flipHandler = new FlipHandler(); + private static final ModListData modListData = new ModListData(); + private static final Gson gson = new Gson(); public static boolean HandleCommand(JsonStringCommand cmd, Entity sender) { // Entity sender = Minecraft.getMinecraft().thePlayer; @@ -54,6 +61,9 @@ public class WSCommandHandler { case Countdown: StartTimer(cmd.GetAs(new TypeToken() {})); break; + case GetMods: + getMods(); + break; default: break; } @@ -61,6 +71,30 @@ public class WSCommandHandler { return true; } + public static void cacheMods(){ + File modFolder = new File(Minecraft.getMinecraft().mcDataDir, "mods"); + for(File mods : modFolder.listFiles()){ + modListData.addFilename(mods.getName()); + try { + modListData.addFileHashes(FileUtils.getMD5Checksum(mods)); + } catch (Exception exception){ + // Highly less likely to happen unless something goes wrong + exception.printStackTrace(); + } + } + + for(ModContainer mod : Loader.instance().getModList()){ + modListData.addModname(mod.getName()); + modListData.addModname(mod.getModId()); + } + } + + private static void getMods(){ + // the Cofl server has asked for an mod list now let's respond with all the info + CoflSky.Wrapper.SendMessage(new RawCommand("foundMods",gson.toJson(modListData))); + } + + private static void Flip(Command cmd) { //handle chat message ChatMessageData[] messages = cmd.getData().Messages; diff --git a/src/main/java/de/torui/coflsky/commands/CommandType.java b/src/main/java/de/torui/coflsky/commands/CommandType.java index 7f196d4..b7dbc37 100644 --- a/src/main/java/de/torui/coflsky/commands/CommandType.java +++ b/src/main/java/de/torui/coflsky/commands/CommandType.java @@ -53,7 +53,10 @@ public enum CommandType { chatBatch, @SerializedName("uploadTab") uploadTab, - ; + @SerializedName("getMods") + GetMods; + + public static Map data; static { data = new HashMap<>(); diff --git a/src/main/java/de/torui/coflsky/commands/models/ModListData.java b/src/main/java/de/torui/coflsky/commands/models/ModListData.java new file mode 100644 index 0000000..f654ecd --- /dev/null +++ b/src/main/java/de/torui/coflsky/commands/models/ModListData.java @@ -0,0 +1,33 @@ +package de.torui.coflsky.commands.models; + +import com.google.gson.annotations.SerializedName; + +import java.util.LinkedList; +import java.util.List; + +public class ModListData { + + @SerializedName("fileNames") + private final List fileNames = new LinkedList<>(); + + @SerializedName("modNames") + private final List modNames = new LinkedList<>(); + + @SerializedName("fileHashes") + private final List fileHashes = new LinkedList<>(); + + + public void addFilename(String name){ + this.fileNames.add(name); + } + + public void addModname(String modname){ + this.modNames.add(modname); + } + + public void addFileHashes(String hash){ + this.fileHashes.add(hash); + } + + +} diff --git a/src/main/java/de/torui/coflsky/network/WSClient.java b/src/main/java/de/torui/coflsky/network/WSClient.java index f306751..12543e0 100644 --- a/src/main/java/de/torui/coflsky/network/WSClient.java +++ b/src/main/java/de/torui/coflsky/network/WSClient.java @@ -22,7 +22,6 @@ import de.torui.coflsky.commands.RawCommand; public class WSClient extends WebSocketAdapter { - public static Gson gson; diff --git a/src/main/java/de/torui/coflsky/utils/FileUtils.java b/src/main/java/de/torui/coflsky/utils/FileUtils.java new file mode 100644 index 0000000..1541a75 --- /dev/null +++ b/src/main/java/de/torui/coflsky/utils/FileUtils.java @@ -0,0 +1,40 @@ +package de.torui.coflsky.utils; + +import java.io.File; +import java.io.FileInputStream; +import java.io.InputStream; +import java.security.MessageDigest; + +public class FileUtils { + + public static byte[] createChecksum(File file) throws Exception { + InputStream fis = new FileInputStream(file); + + byte[] buffer = new byte[1024]; + MessageDigest complete = MessageDigest.getInstance("MD5"); + int numRead; + + do { + numRead = fis.read(buffer); + if (numRead > 0) { + complete.update(buffer, 0, numRead); + } + } while (numRead != -1); + + fis.close(); + return complete.digest(); + } + + + public static String getMD5Checksum(File file) throws Exception { + byte[] b = createChecksum(file); + String result = ""; + + for (int i=0; i < b.length; i++) { + result += Integer.toString( ( b[i] & 0xff ) + 0x100, 16).substring( 1 ); + } + return result; + } + + +} -- cgit