From a24c0547dedb5b594a2d921e1f23711e1080b956 Mon Sep 17 00:00:00 2001 From: RuscalWorld Date: Mon, 24 May 2021 19:53:32 +0300 Subject: [PATCH] Make server port and update interval configurable --- .../fabricexporter/FabricExporter.java | 29 +++++++++++- .../fabricexporter/config/Config.java | 47 +++++++++++++++++++ .../fabricexporter/config/MainConfig.java | 39 +++++++++++++++ .../fabricexporter/util/ConvertUtil.java | 11 +++++ src/main/resources/config/exporter.properties | 2 + 5 files changed, 126 insertions(+), 2 deletions(-) create mode 100644 src/main/java/ru/ruscalworld/fabricexporter/config/Config.java create mode 100644 src/main/java/ru/ruscalworld/fabricexporter/config/MainConfig.java create mode 100644 src/main/java/ru/ruscalworld/fabricexporter/util/ConvertUtil.java create mode 100644 src/main/resources/config/exporter.properties diff --git a/src/main/java/ru/ruscalworld/fabricexporter/FabricExporter.java b/src/main/java/ru/ruscalworld/fabricexporter/FabricExporter.java index 378ad89..2050e20 100644 --- a/src/main/java/ru/ruscalworld/fabricexporter/FabricExporter.java +++ b/src/main/java/ru/ruscalworld/fabricexporter/FabricExporter.java @@ -4,6 +4,9 @@ import io.prometheus.client.exporter.HTTPServer; import net.fabricmc.api.ModInitializer; import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents; import net.minecraft.server.MinecraftServer; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import ru.ruscalworld.fabricexporter.config.MainConfig; import ru.ruscalworld.fabricexporter.metrics.OnlinePlayers; import java.io.IOException; @@ -11,13 +14,23 @@ import java.util.Timer; public class FabricExporter implements ModInitializer { private MinecraftServer server; + private MainConfig config; @Override public void onInitialize() { + try { + MainConfig config = new MainConfig("exporter.properties"); + config.load(); + this.setConfig(config); + } catch (IOException e) { + FabricExporter.getLogger().fatal("Unable to load config"); + e.printStackTrace(); + } + ServerLifecycleEvents.SERVER_STARTED.register(this::setServer); try { - HTTPServer httpServer = new HTTPServer(1337); + new HTTPServer(this.getConfig().getPort()); } catch (IOException e) { e.printStackTrace(); } @@ -26,7 +39,11 @@ public class FabricExporter implements ModInitializer { metricUpdater.registerMetric(new OnlinePlayers()); Timer timer = new Timer(); - timer.schedule(metricUpdater, 1000, 1000); + timer.schedule(metricUpdater, 1000, this.getConfig().getUpdateInterval()); + } + + public static Logger getLogger() { + return LogManager.getLogger(); } public static String getMetricName(String name) { @@ -40,4 +57,12 @@ public class FabricExporter implements ModInitializer { private void setServer(MinecraftServer server) { this.server = server; } + + public MainConfig getConfig() { + return config; + } + + public void setConfig(MainConfig config) { + this.config = config; + } } diff --git a/src/main/java/ru/ruscalworld/fabricexporter/config/Config.java b/src/main/java/ru/ruscalworld/fabricexporter/config/Config.java new file mode 100644 index 0000000..d3fb371 --- /dev/null +++ b/src/main/java/ru/ruscalworld/fabricexporter/config/Config.java @@ -0,0 +1,47 @@ +package ru.ruscalworld.fabricexporter.config; + +import net.fabricmc.loader.api.FabricLoader; +import org.apache.logging.log4j.LogManager; +import ru.ruscalworld.fabricexporter.FabricExporter; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Properties; + +public abstract class Config { + private final String fileName; + + public Config(String fileName) { + this.fileName = fileName; + } + + public void load() throws IOException { + Path path = FabricLoader.getInstance().getConfigDir().resolve(this.getFileName()); + if (!path.toFile().exists()) try { + URL url = this.getClass().getClassLoader().getResource("config/" + this.getFileName()); + assert url != null; + File file = new File(url.toURI()); + byte[] bytes = Files.readAllBytes(file.toPath()); + + Files.createFile(path); + Files.write(path, bytes); + } catch (Exception exception) { + exception.printStackTrace(); + FabricExporter.getLogger().fatal("Unable to save default config"); + } + + Properties properties = new Properties(); + properties.load(Files.newInputStream(path)); + this.parse(properties); + } + + public abstract void parse(Properties properties); + + public String getFileName() { + return fileName; + } +} diff --git a/src/main/java/ru/ruscalworld/fabricexporter/config/MainConfig.java b/src/main/java/ru/ruscalworld/fabricexporter/config/MainConfig.java new file mode 100644 index 0000000..f49a967 --- /dev/null +++ b/src/main/java/ru/ruscalworld/fabricexporter/config/MainConfig.java @@ -0,0 +1,39 @@ +package ru.ruscalworld.fabricexporter.config; + +import ru.ruscalworld.fabricexporter.util.ConvertUtil; + +import java.util.Properties; + +public class MainConfig extends Config { + private int port; + private int updateInterval; + + public MainConfig(String name) { + super(name); + } + + @Override + public void parse(Properties properties) { + String portString = properties.getProperty("server-port", "25585"); + this.setPort(ConvertUtil.intToStringOrDefault(portString, 25585)); + + String updateIntervalString = properties.getProperty("update-interval", "1000"); + this.setUpdateInterval(ConvertUtil.intToStringOrDefault(updateIntervalString, 1000)); + } + + public int getPort() { + return port; + } + + public void setPort(int port) { + this.port = port; + } + + public int getUpdateInterval() { + return updateInterval; + } + + public void setUpdateInterval(int updateInterval) { + this.updateInterval = updateInterval; + } +} diff --git a/src/main/java/ru/ruscalworld/fabricexporter/util/ConvertUtil.java b/src/main/java/ru/ruscalworld/fabricexporter/util/ConvertUtil.java new file mode 100644 index 0000000..e0093e4 --- /dev/null +++ b/src/main/java/ru/ruscalworld/fabricexporter/util/ConvertUtil.java @@ -0,0 +1,11 @@ +package ru.ruscalworld.fabricexporter.util; + +public class ConvertUtil { + public static int intToStringOrDefault(String input, int def) { + int result = def; + try { + result = Integer.parseInt(input); + } catch (Exception ignored) { } + return result; + } +} diff --git a/src/main/resources/config/exporter.properties b/src/main/resources/config/exporter.properties new file mode 100644 index 0000000..ca6ab71 --- /dev/null +++ b/src/main/resources/config/exporter.properties @@ -0,0 +1,2 @@ +server-port=25585 +update-interval=1000 \ No newline at end of file