From b8da1ca68e373cc9ef13028df6ffc8c1b2f93ae3 Mon Sep 17 00:00:00 2001 From: Yui Date: Sun, 1 Dec 2024 16:40:14 -0300 Subject: [PATCH] feat: added config file --- .gitignore | 1 + .../java/moe/yuyui/mcstreamerbot/CommonProxy.java | 3 +-- src/main/java/moe/yuyui/mcstreamerbot/Config.java | 14 +++++++++++--- .../moe/yuyui/mcstreamerbot/common/WSServer.java | 6 ++++-- .../events/minecraft/OnWorldJoin.java | 10 ++++++++-- 5 files changed, 25 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index 5e80e0a..4fd7204 100644 --- a/.gitignore +++ b/.gitignore @@ -36,3 +36,4 @@ addon.local.gradle.kts addon.late.local.gradle addon.late.local.gradle.kts layout.json +java~ diff --git a/src/main/java/moe/yuyui/mcstreamerbot/CommonProxy.java b/src/main/java/moe/yuyui/mcstreamerbot/CommonProxy.java index a11fac6..0767451 100644 --- a/src/main/java/moe/yuyui/mcstreamerbot/CommonProxy.java +++ b/src/main/java/moe/yuyui/mcstreamerbot/CommonProxy.java @@ -16,8 +16,7 @@ public class CommonProxy { public void preInit(FMLPreInitializationEvent event) { Config.synchronizeConfiguration(event.getSuggestedConfigurationFile()); - MinecraftStreamerBotIntegration.LOG.info(Config.greeting); - MinecraftStreamerBotIntegration.LOG.info("I am MyMod at version " + Tags.VERSION); + MinecraftStreamerBotIntegration.LOG.info("MCStreamerBot is meowing in version " + Tags.VERSION); } // load "Do your mod setup. Build whatever data structures you care about. Register recipes." (Remove if not needed) diff --git a/src/main/java/moe/yuyui/mcstreamerbot/Config.java b/src/main/java/moe/yuyui/mcstreamerbot/Config.java index 1a4a366..feb47d9 100644 --- a/src/main/java/moe/yuyui/mcstreamerbot/Config.java +++ b/src/main/java/moe/yuyui/mcstreamerbot/Config.java @@ -6,12 +6,20 @@ import net.minecraftforge.common.config.Configuration; public class Config { - public static String greeting = "Hello World"; + private static final String CATEGORY_WEBSOCKET = "websocket"; + public static int portNumber = 8069; + public static String ipAddress = "127.0.0.1"; + public static String authToken = ""; public static void synchronizeConfiguration(File configFile) { Configuration configuration = new Configuration(configFile); - - greeting = configuration.getString("greeting", Configuration.CATEGORY_GENERAL, greeting, "How shall I greet?"); + configuration.addCustomCategoryComment("General", "General configuration of the bot"); + portNumber = configuration + .getInt("port", CATEGORY_WEBSOCKET, portNumber, 1024, 65535, "The port that the websocket will listen on"); + ipAddress = configuration + .getString("ip", CATEGORY_WEBSOCKET, ipAddress, "The ip that the websocket will listen on"); + authToken = configuration + .getString("authToken", CATEGORY_WEBSOCKET, authToken, "The authentication token for the websocket"); if (configuration.hasChanged()) { configuration.save(); diff --git a/src/main/java/moe/yuyui/mcstreamerbot/common/WSServer.java b/src/main/java/moe/yuyui/mcstreamerbot/common/WSServer.java index c3f2686..c80b12c 100644 --- a/src/main/java/moe/yuyui/mcstreamerbot/common/WSServer.java +++ b/src/main/java/moe/yuyui/mcstreamerbot/common/WSServer.java @@ -36,8 +36,10 @@ public class WSServer extends WebSocketServer { public WSServer(InetSocketAddress address, String authToken) throws NoSuchAlgorithmException { super(address); - this._authToken = MessageDigest.getInstance("SHA-256") - .digest(authToken.getBytes(StandardCharsets.UTF_8)); + if (!authToken.isEmpty()) { + this._authToken = MessageDigest.getInstance("SHA-256") + .digest(authToken.getBytes(StandardCharsets.UTF_8)); + } } @Override diff --git a/src/main/java/moe/yuyui/mcstreamerbot/events/minecraft/OnWorldJoin.java b/src/main/java/moe/yuyui/mcstreamerbot/events/minecraft/OnWorldJoin.java index 0aaa0c9..2c9af56 100644 --- a/src/main/java/moe/yuyui/mcstreamerbot/events/minecraft/OnWorldJoin.java +++ b/src/main/java/moe/yuyui/mcstreamerbot/events/minecraft/OnWorldJoin.java @@ -1,11 +1,13 @@ package moe.yuyui.mcstreamerbot.events.minecraft; import java.net.InetSocketAddress; +import java.security.NoSuchAlgorithmException; import net.minecraft.client.Minecraft; import net.minecraftforge.event.entity.EntityJoinWorldEvent; import cpw.mods.fml.common.eventhandler.SubscribeEvent; +import moe.yuyui.mcstreamerbot.Config; import moe.yuyui.mcstreamerbot.MinecraftStreamerBotIntegration; import moe.yuyui.mcstreamerbot.common.EventListener; import moe.yuyui.mcstreamerbot.common.WSServer; @@ -21,8 +23,12 @@ public class OnWorldJoin { return; } } - MinecraftStreamerBotIntegration.eventListener = new EventListener( - new WSServer(new InetSocketAddress("127.0.0.1", 8080))); + try { + MinecraftStreamerBotIntegration.eventListener = new EventListener( + new WSServer(new InetSocketAddress(Config.ipAddress, Config.portNumber), Config.authToken)); + } catch (NoSuchAlgorithmException e) { + MinecraftStreamerBotIntegration.LOG.error("Your computer does not support SHA-256 hashing"); + } } } }