Skip to content

Commit

Permalink
Add logging and fix config file reading/writing
Browse files Browse the repository at this point in the history
  • Loading branch information
Nincodedo committed Jun 26, 2023
1 parent 5b4770d commit 3fd4495
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 30 deletions.
51 changes: 26 additions & 25 deletions src/main/java/dev/nincodedo/mcserverdescption/Config.java
Original file line number Diff line number Diff line change
@@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,27 @@

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) {
long dayCount = overworld.getTimeOfDay() / 24000;
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);
Expand Down Expand Up @@ -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");
}
}

0 comments on commit 3fd4495

Please sign in to comment.