diff options
author | Jonas Gunz <himself@jonasgunz.de> | 2020-10-26 01:46:32 +0100 |
---|---|---|
committer | Jonas Gunz <himself@jonasgunz.de> | 2020-10-26 01:46:32 +0100 |
commit | 125cc1a0f9ab4cd440fea5e1ae7c9a6f5345246d (patch) | |
tree | 33c8b190d2d92ede27aa1389d979440aeea95755 /src/main/java/re | |
parent | fe5332a54f36f046ab35e9def71d3bd9df76e4e8 (diff) | |
download | mcqtt-125cc1a0f9ab4cd440fea5e1ae7c9a6f5345246d.tar.gz |
Diffstat (limited to 'src/main/java/re')
-rw-r--r-- | src/main/java/re/jag/mcqtt/MQTTHandler.java | 38 | ||||
-rw-r--r-- | src/main/java/re/jag/mcqtt/Mcqtt.java | 17 | ||||
-rw-r--r-- | src/main/java/re/jag/mcqtt/McqttSettings.java | 78 | ||||
-rw-r--r-- | src/main/java/re/jag/mcqtt/mixin/MinecraftServerMixin.java | 27 |
4 files changed, 160 insertions, 0 deletions
diff --git a/src/main/java/re/jag/mcqtt/MQTTHandler.java b/src/main/java/re/jag/mcqtt/MQTTHandler.java new file mode 100644 index 0000000..78033c1 --- /dev/null +++ b/src/main/java/re/jag/mcqtt/MQTTHandler.java @@ -0,0 +1,38 @@ +package re.jag.mcqtt; + +import org.eclipse.paho.client.mqttv3.MqttClient; +import org.eclipse.paho.client.mqttv3.MqttException; + +public class MQTTHandler { + private String server; + private String user; + private String password; + + private MqttClient client; + private Thread client_thread; + private boolean should_stop = false; + + MQTTHandler(String _server, String _user, String _password){ + try { + client = new MqttClient(_server, _user); + } catch (MqttException e) { + Mcqtt.LOG.error(e.getMessage()); + return; + } + + client_thread = new Thread(this::run); + client_thread.run(); + } + + private void run() { + while ( ! should_stop ) { + // TODO wait for publish interval + if ( ! client.isConnected() ) { + try { client.connect(); } catch (MqttException e) { + Mcqtt.LOG.error( e.getMessage() ); + continue; // TODO wait for reconnect + } + } + } + } +} diff --git a/src/main/java/re/jag/mcqtt/Mcqtt.java b/src/main/java/re/jag/mcqtt/Mcqtt.java index 10678d8..b44fc2a 100644 --- a/src/main/java/re/jag/mcqtt/Mcqtt.java +++ b/src/main/java/re/jag/mcqtt/Mcqtt.java @@ -2,10 +2,18 @@ package re.jag.mcqtt; import net.fabricmc.api.ModInitializer; +import net.minecraft.server.MinecraftServer; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + import org.eclipse.paho.client.mqttv3.MqttClient; import org.eclipse.paho.client.mqttv3.MqttException; public class Mcqtt implements ModInitializer { + public static final Logger LOG = LogManager.getLogger(); + private static MinecraftServer server = null; + @Override public void onInitialize() { System.out.println("Hello Fabric world!"); @@ -14,4 +22,13 @@ public class Mcqtt implements ModInitializer { MqttClient mqtt = new MqttClient("tcp://localhost", "mcserver", null); } catch (MqttException e) {} } + + public static void onStart(MinecraftServer _server) { + if (server == null) + server = _server; + } + + public static void onTick() { + //LOG.info("TICK: " + String.valueOf(server.getTickTime()) + "ms"); + } } diff --git a/src/main/java/re/jag/mcqtt/McqttSettings.java b/src/main/java/re/jag/mcqtt/McqttSettings.java new file mode 100644 index 0000000..086b550 --- /dev/null +++ b/src/main/java/re/jag/mcqtt/McqttSettings.java @@ -0,0 +1,78 @@ +package re.jag.mcqtt; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.nio.file.FileSystems; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Properties; + +public class McqttSettings { + private Properties properties = new Properties(); + private boolean changed_config = false; + + public final String mqtt_server; + public final String mqtt_user; + public final String mqtt_password; + public final String mqtt_certificate; + public final String mqtt_base_topic; + + public final int publish_interval; + + public McqttSettings(String _properties_file) { + Path path = FileSystems.getDefault().getPath(_properties_file); + load(path); + + this.mqtt_server = get_string("mqtt-server", "tcp://localhost"); + this.mqtt_user = get_string("mqtt-user", "username"); + this.mqtt_password = get_string("mqtt-password", "p4assword"); + this.mqtt_certificate = get_string("mqtt-certificate", ""); + this.mqtt_base_topic = get_string("mqtt-base-topic", "minecraft"); + + this.publish_interval = get_int("publish-interval-mins", 5); + + if(changed_config) + save(path); + } + + private void load(Path _path) { + try { + InputStream inp = Files.newInputStream(_path); + properties.load(inp); + } catch (IOException e) { + Mcqtt.LOG.warn("Failed to read from " + _path.getFileName()); + } + } + + private void save(Path _path) { + try { + OutputStream out = Files.newOutputStream(_path); + properties.store(out, "McQTT mod settings"); + } catch (IOException e) { + Mcqtt.LOG.error("Failed to write to " + _path.getFileName()); + } + } + + private String get(String _name, String _default) { + String val = properties.getProperty(_name); + if (val == null) { + properties.setProperty(_name, _default); + changed_config = true; + return _default; + } + return val; + } + + private boolean get_boolean(String _name, boolean _default) { + return Boolean.parseBoolean(get(_name, String.valueOf(_default))); + } + + private String get_string(String _name, String _default) { + return get(_name, _default); + } + + private int get_int(String _name, int _default) { + return Integer.parseInt(get(_name, String.valueOf(_default))); + } +} diff --git a/src/main/java/re/jag/mcqtt/mixin/MinecraftServerMixin.java b/src/main/java/re/jag/mcqtt/mixin/MinecraftServerMixin.java new file mode 100644 index 0000000..2889592 --- /dev/null +++ b/src/main/java/re/jag/mcqtt/mixin/MinecraftServerMixin.java @@ -0,0 +1,27 @@ +package re.jag.mcqtt.mixin; + +import net.minecraft.server.MinecraftServer; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; +import re.jag.mcqtt.Mcqtt; + +import java.util.function.BooleanSupplier; +import java.util.function.Function; + +@Mixin (MinecraftServer.class) +public class MinecraftServerMixin { + + @Inject(method = "tick", at = @At("RETURN")) + protected void onTick(BooleanSupplier shouldKeepTicking, CallbackInfo ci) { + Mcqtt.onTick(); + } + + @Inject(method = "startServer", at = @At("RETURN")) + private static void onStartServer(Function serverFactory, CallbackInfoReturnable<MinecraftServer> cir){ + Mcqtt.onStart(cir.getReturnValue()); + } +} |