From a013f91a16c961aacc312639cf69f2293533acc8 Mon Sep 17 00:00:00 2001 From: Cjsah Date: Tue, 17 Sep 2024 21:32:38 +0800 Subject: [PATCH] =?UTF-8?q?config=20getter=E6=94=B9=E4=B8=BAutil?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/dev/anvilcraft/rg/RollingGate.java | 23 ++------------ .../dev/anvilcraft/rg/util/ConfigUtil.java | 30 +++++++++++++++++++ 2 files changed, 32 insertions(+), 21 deletions(-) create mode 100644 src/main/java/dev/anvilcraft/rg/util/ConfigUtil.java diff --git a/src/main/java/dev/anvilcraft/rg/RollingGate.java b/src/main/java/dev/anvilcraft/rg/RollingGate.java index a096bf9..bfe882e 100644 --- a/src/main/java/dev/anvilcraft/rg/RollingGate.java +++ b/src/main/java/dev/anvilcraft/rg/RollingGate.java @@ -3,10 +3,9 @@ import com.google.gson.JsonObject; import com.mojang.logging.LogUtils; import dev.anvilcraft.rg.api.RGAdditional; -import dev.anvilcraft.rg.api.RGRuleException; import dev.anvilcraft.rg.api.RGRuleManager; +import dev.anvilcraft.rg.util.ConfigUtil; import net.minecraft.server.MinecraftServer; -import net.minecraft.util.GsonHelper; import net.minecraft.world.level.storage.LevelResource; import net.neoforged.bus.api.IEventBus; import net.neoforged.bus.api.SubscribeEvent; @@ -16,13 +15,9 @@ import net.neoforged.fml.event.lifecycle.FMLLoadCompleteEvent; import net.neoforged.neoforge.common.NeoForge; import net.neoforged.neoforge.event.server.ServerStartingEvent; -import org.apache.commons.io.FileUtils; import org.jetbrains.annotations.NotNull; import org.slf4j.Logger; -import org.spongepowered.include.com.google.common.base.Charsets; -import java.io.File; -import java.io.IOException; import java.nio.file.Path; import java.util.Optional; @@ -56,21 +51,7 @@ public void loadRGRules(FMLLoadCompleteEvent event) { public void reInitRules(@NotNull ServerStartingEvent event) { MinecraftServer server = event.getServer(); Path path = server.getWorldPath(RollingGate.RULE_PATH); - JsonObject config = getOrCreateContent(path); + JsonObject config = ConfigUtil.getOrCreateContent(path); RollingGate.RULE_MANAGER.reInit(config); } - - private static @NotNull JsonObject getOrCreateContent(@NotNull Path path) { - File file = path.toFile(); - try { - if (!file.exists() || file.isDirectory()) { - FileUtils.writeStringToFile(file, "{}", Charsets.UTF_8); - return new JsonObject(); - } - String value = FileUtils.readFileToString(path.toFile(), Charsets.UTF_8); - return GsonHelper.parse(value); - } catch (IOException e) { - throw new RGRuleException("Failed to read rolling gate config file", e); - } - } } diff --git a/src/main/java/dev/anvilcraft/rg/util/ConfigUtil.java b/src/main/java/dev/anvilcraft/rg/util/ConfigUtil.java new file mode 100644 index 0000000..8c43333 --- /dev/null +++ b/src/main/java/dev/anvilcraft/rg/util/ConfigUtil.java @@ -0,0 +1,30 @@ +package dev.anvilcraft.rg.util; + +import com.google.gson.JsonObject; +import dev.anvilcraft.rg.api.RGRuleException; +import net.minecraft.util.GsonHelper; +import org.apache.commons.io.FileUtils; +import org.jetbrains.annotations.NotNull; + +import java.io.File; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Path; + +public class ConfigUtil { + + public static @NotNull JsonObject getOrCreateContent(@NotNull Path path) { + File file = path.toFile(); + try { + if (!file.exists() || file.isDirectory()) { + FileUtils.writeStringToFile(file, "{}", StandardCharsets.UTF_8); + return new JsonObject(); + } + String value = FileUtils.readFileToString(path.toFile(), StandardCharsets.UTF_8); + return GsonHelper.parse(value); + } catch (IOException e) { + throw new RGRuleException("Failed to read rolling gate config file", e); + } + } + +}