feat: added config file
Some checks are pending
Build and test / build-and-test (push) Waiting to run

This commit is contained in:
Yui 2024-12-01 16:40:14 -03:00
parent 354f647e54
commit b8da1ca68e
Signed by: yui
GPG Key ID: F368D23A0ABA04B4
5 changed files with 25 additions and 9 deletions

1
.gitignore vendored
View File

@ -36,3 +36,4 @@ addon.local.gradle.kts
addon.late.local.gradle
addon.late.local.gradle.kts
layout.json
java~

View File

@ -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)

View File

@ -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();

View File

@ -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

View File

@ -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");
}
}
}
}