Move 2a03.json to the config directory
This commit is contained in:
parent
0ee693cce4
commit
7fb945136d
@ -2,6 +2,10 @@ package party._2a03.mc;
|
|||||||
|
|
||||||
import net.fabricmc.api.ModInitializer;
|
import net.fabricmc.api.ModInitializer;
|
||||||
import net.fabricmc.fabric.api.registry.CommandRegistry;
|
import net.fabricmc.fabric.api.registry.CommandRegistry;
|
||||||
|
import net.fabricmc.loader.api.FabricLoader;
|
||||||
|
import java.io.File;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
import org.apache.logging.log4j.LogManager;
|
import org.apache.logging.log4j.LogManager;
|
||||||
import org.apache.logging.log4j.Logger;
|
import org.apache.logging.log4j.Logger;
|
||||||
import party._2a03.mc.command.ConfigCommand;
|
import party._2a03.mc.command.ConfigCommand;
|
||||||
@ -13,14 +17,28 @@ import party._2a03.mc.server.Config;
|
|||||||
|
|
||||||
public class MinecraftTweaks2a03 implements ModInitializer {
|
public class MinecraftTweaks2a03 implements ModInitializer {
|
||||||
private static final Logger LOGGER = LogManager.getLogger();
|
private static final Logger LOGGER = LogManager.getLogger();
|
||||||
|
private Path configDir;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onInitialize() {
|
public void onInitialize() {
|
||||||
|
configDir = new File(FabricLoader.getInstance().getConfigDirectory(), "minecraft-tweaks-2a03").toPath();
|
||||||
|
if (!Files.exists(configDir)) {
|
||||||
|
LOGGER.info("Creating 2a03.party config directory");
|
||||||
|
try {
|
||||||
|
Files.createDirectory(configDir);
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
LOGGER.error(e);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Config.initConfig(configDir.toFile());
|
||||||
try {
|
try {
|
||||||
Config.loadConfig();
|
Config.loadConfig();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
LOGGER.error(e);
|
LOGGER.error(e);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
LOGGER.info("Registering 2a03.party commands");
|
LOGGER.info("Registering 2a03.party commands");
|
||||||
CommandRegistry.INSTANCE.register(false, dispatcher -> {
|
CommandRegistry.INSTANCE.register(false, dispatcher -> {
|
||||||
|
@ -5,24 +5,30 @@ import java.io.File;
|
|||||||
import java.io.FileWriter;
|
import java.io.FileWriter;
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
import java.nio.file.Path;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
import org.json.JSONArray;
|
import org.json.JSONArray;
|
||||||
import org.apache.commons.io.IOUtils;
|
import org.apache.commons.io.IOUtils;
|
||||||
import org.apache.logging.log4j.LogManager;
|
import org.apache.logging.log4j.LogManager;
|
||||||
import org.apache.logging.log4j.Logger;
|
import org.apache.logging.log4j.Logger;
|
||||||
|
import party._2a03.mc.MinecraftTweaks2a03;
|
||||||
import party._2a03.mc.server.PlayerData;
|
import party._2a03.mc.server.PlayerData;
|
||||||
import party._2a03.mc.server.PlayerPosition;
|
import party._2a03.mc.server.PlayerPosition;
|
||||||
|
|
||||||
public class Config {
|
public class Config {
|
||||||
private static final Logger LOGGER = LogManager.getLogger();
|
private static final Logger LOGGER = LogManager.getLogger();
|
||||||
private static JSONObject json;
|
private static JSONObject json;
|
||||||
|
private static File config;
|
||||||
|
|
||||||
|
public static void initConfig(File configDir) {
|
||||||
|
config = new File(configDir, "2a03.json");
|
||||||
|
}
|
||||||
|
|
||||||
public static void loadConfig() throws Exception {
|
public static void loadConfig() throws Exception {
|
||||||
LOGGER.info("Loading 2a03.party configuration");
|
LOGGER.info("Loading 2a03.party configuration");
|
||||||
File f = new File("2a03.json");
|
if (config.exists()) {
|
||||||
if (f.exists()) {
|
InputStream is = new FileInputStream(config);
|
||||||
InputStream is = new FileInputStream("2a03.json");
|
|
||||||
String jsonRaw = IOUtils.toString(is, "UTF-8");
|
String jsonRaw = IOUtils.toString(is, "UTF-8");
|
||||||
json = new JSONObject(jsonRaw);
|
json = new JSONObject(jsonRaw);
|
||||||
} else {
|
} else {
|
||||||
@ -77,7 +83,7 @@ public class Config {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static void saveConfig() {
|
private static void saveConfig() {
|
||||||
try (FileWriter file = new FileWriter("2a03.json")) {
|
try (FileWriter file = new FileWriter(config)) {
|
||||||
file.write(JSONObject.valueToString(json));
|
file.write(JSONObject.valueToString(json));
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
LOGGER.error("Failed to save config file");
|
LOGGER.error("Failed to save config file");
|
||||||
|
Loading…
Reference in New Issue
Block a user