From 2f6dac6ae5b1bcbfcbd6735cdf39e1a0abe55f5c Mon Sep 17 00:00:00 2001 From: Chip <65827213+ChipmunkMC@users.noreply.github.com> Date: Sun, 8 Jan 2023 17:27:52 -0500 Subject: [PATCH] Default config & Java 17 because yes --- pom.xml | 4 +-- .../java/land/chipmunk/chipmunkbot/Main.java | 26 ++++++++++++++++++- src/main/resources/default_config.json | 9 +++++++ 3 files changed, 36 insertions(+), 3 deletions(-) create mode 100644 src/main/resources/default_config.json diff --git a/pom.xml b/pom.xml index 84348df..9d36622 100644 --- a/pom.xml +++ b/pom.xml @@ -8,8 +8,8 @@ chipmunkbot - 1.8 - 1.8 + 17 + 17 diff --git a/src/main/java/land/chipmunk/chipmunkbot/Main.java b/src/main/java/land/chipmunk/chipmunkbot/Main.java index 260f233..df79145 100644 --- a/src/main/java/land/chipmunk/chipmunkbot/Main.java +++ b/src/main/java/land/chipmunk/chipmunkbot/Main.java @@ -10,7 +10,9 @@ import java.io.InputStream; import java.io.FileInputStream; import java.io.InputStreamReader; import java.io.BufferedReader; +import java.io.BufferedWriter; import java.io.File; +import java.io.FileWriter; import com.google.gson.JsonObject; import com.google.gson.JsonElement; @@ -19,9 +21,31 @@ import com.google.gson.JsonParser; import com.google.gson.Gson; public class Main { - private static File CONFIG_FILE = new File("config.json"); + private static final File CONFIG_FILE = new File("config.json"); private static JsonObject getConfig () throws Exception { + if (!CONFIG_FILE.exists()) { + // Read the default config + InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("default_config.json"); + BufferedReader reader = new BufferedReader(new InputStreamReader(is)); + StringBuilder stringBuilder = new StringBuilder(); + while (reader.ready()) { + char character = (char) reader.read(); + stringBuilder.append(character); + } + String defaultConfig = stringBuilder.toString(); + + // Write the default config + BufferedWriter configWriter = new BufferedWriter(new FileWriter(CONFIG_FILE)); + configWriter.write(defaultConfig); + configWriter.close(); + + System.out.println("The config.json file was not found, so a default one was created."); + + // Return the default config (instead of reading again, for efficiency) + return JsonParser.parseString(defaultConfig).getAsJsonObject(); + } + InputStream opt = new FileInputStream(CONFIG_FILE); BufferedReader reader = new BufferedReader(new InputStreamReader(opt)); diff --git a/src/main/resources/default_config.json b/src/main/resources/default_config.json new file mode 100644 index 0000000..a736854 --- /dev/null +++ b/src/main/resources/default_config.json @@ -0,0 +1,9 @@ +{ + "bots": [ + { + "host": "localhost", + "port": 25565, + "username": "ChipmunkBot" + } + ] +} \ No newline at end of file