From 3fd449510ca0db09a7d2db61c2a5a723c4308b5b Mon Sep 17 00:00:00 2001 From: Nincodedo Date: Sun, 25 Jun 2023 23:35:36 -0500 Subject: [PATCH] Add logging and fix config file reading/writing --- .../nincodedo/mcserverdescption/Config.java | 51 ++++++++++--------- .../McServerDescription.java | 15 ++++-- 2 files changed, 36 insertions(+), 30 deletions(-) diff --git a/src/main/java/dev/nincodedo/mcserverdescption/Config.java b/src/main/java/dev/nincodedo/mcserverdescption/Config.java index d1f6a18..ba4fa9a 100644 --- a/src/main/java/dev/nincodedo/mcserverdescption/Config.java +++ b/src/main/java/dev/nincodedo/mcserverdescption/Config.java @@ -1,58 +1,59 @@ package dev.nincodedo.mcserverdescption; import net.fabricmc.loader.api.FabricLoader; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import java.io.File; +import java.io.FileInputStream; import java.io.FileWriter; import java.io.IOException; -import java.nio.file.Files; +import java.io.InputStreamReader; +import java.nio.charset.StandardCharsets; import java.util.Properties; public class Config { + + private static final Logger LOGGER = LoggerFactory.getLogger(Config.class); + private static final String PROPERTY_FILE_NAME = "mcserverdescription.properties"; + public static void createConfig() { try { - File config = new File(String.valueOf(FabricLoader.getInstance().getConfigDir().resolve("mcserverdescription.properties"))); + File config = new File(String.valueOf(FabricLoader.getInstance() + .getConfigDir() + .resolve(PROPERTY_FILE_NAME))); if (config.createNewFile()) { writeDefault(); } } catch (IOException e) { - e.printStackTrace(); + LOGGER.error("Failed to create the config file.", e); } } private static void writeDefault() { - try { - FileWriter configWriter = new FileWriter(String.valueOf(FabricLoader.getInstance().getConfigDir().resolve("mcserverdescription.properties"))); + try (FileWriter configWriter = new FileWriter(String.valueOf(FabricLoader.getInstance() + .getConfigDir() + .resolve(PROPERTY_FILE_NAME)), StandardCharsets.UTF_8)) { configWriter.write("# MC Server Description Config\n"); - configWriter.write("description=§fCurrent Day: {dayCount} - Time: {timeString} - Weather: {weatherStatus}\n"); + configWriter.write("description=§fCurrent Day: {dayCount} - Time: {timeString} - Weather: " + + "{weatherStatus}\n"); configWriter.write("thundering=Thundering\n"); configWriter.write("raining=Raining\n"); configWriter.write("clear=Clear\n"); - configWriter.close(); } catch (IOException e) { - e.printStackTrace(); + LOGGER.error("Failed to write default config file.", e); } } - public static String getServerDescription() { - Properties properties = new Properties(); - String description; - try { - properties.load(Files.newInputStream(FabricLoader.getInstance().getConfigDir().resolve("mcserverdescription.properties"))); - } catch (IOException e) { - - e.printStackTrace(); - } - description = properties.getProperty("description"); - - return description; - } - public static Properties getConfigProperties() { Properties properties = new Properties(); - try { - properties.load(Files.newInputStream(FabricLoader.getInstance().getConfigDir().resolve("mcserverdescription.properties"))); + try (FileInputStream fileInputStream = new FileInputStream(FabricLoader.getInstance() + .getConfigDir() + .resolve(PROPERTY_FILE_NAME) + .toFile())) { + properties.load(new InputStreamReader(fileInputStream, StandardCharsets.UTF_8)); } catch (IOException e) { - e.printStackTrace(); + LOGGER.error("Failed to get the config properties.", e); } return properties; } diff --git a/src/main/java/dev/nincodedo/mcserverdescption/McServerDescription.java b/src/main/java/dev/nincodedo/mcserverdescption/McServerDescription.java index ede93fe..48459fe 100644 --- a/src/main/java/dev/nincodedo/mcserverdescption/McServerDescription.java +++ b/src/main/java/dev/nincodedo/mcserverdescption/McServerDescription.java @@ -9,14 +9,19 @@ public class McServerDescription implements ModInitializer { + private Properties properties; + @Override public void onInitialize() { Config.createConfig(); + properties = Config.getConfigProperties(); + String descriptionTemplate = properties.getProperty("description"); + String descriptionTemplateStart = descriptionTemplate.substring(0, descriptionTemplate.indexOf("{")); ServerTickEvents.END_SERVER_TICK.register(server -> { if (server.isRemote()) { String motd = server.getServerMotd(); - if (motd != null && motd.contains("\n§fCurrent Day: ")) { - motd = motd.substring(0, motd.indexOf("\n§fCurrent Day: ")); + if (motd != null && motd.contains("\n" + descriptionTemplateStart)) { + motd = motd.substring(0, motd.indexOf("\n" + descriptionTemplateStart)); } World overworld = server.getOverworld(); if (overworld != null) { @@ -24,7 +29,7 @@ public void onInitialize() { String timeString = getTimeString(overworld.getTimeOfDay()); String weatherStatus = getWeatherStatus(overworld.isRaining(), overworld.isThundering()); - String stringBuilder = Config.getServerDescription(); + String stringBuilder = descriptionTemplate; stringBuilder = stringBuilder.replace("{dayCount}", String.valueOf(dayCount)); stringBuilder = stringBuilder.replace("{timeString}", timeString); stringBuilder = stringBuilder.replace("{weatherStatus}", weatherStatus); @@ -52,7 +57,7 @@ private String getTimeString(long serverTimeOfDay) { } private String getWeatherStatus(boolean isRaining, boolean isThundering) { - Properties weather = Config.getConfigProperties(); - return isThundering ? weather.getProperty("thundering") : isRaining ? weather.getProperty("raining") : weather.getProperty("clear"); + return isThundering ? properties.getProperty("thundering") : isRaining ? properties.getProperty("raining") : + properties.getProperty("clear"); } }