Skip to content

Commit

Permalink
config getter改为util
Browse files Browse the repository at this point in the history
  • Loading branch information
Cjsah committed Sep 17, 2024
1 parent 1c6675b commit a013f91
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 21 deletions.
23 changes: 2 additions & 21 deletions src/main/java/dev/anvilcraft/rg/RollingGate.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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);
}
}
}
30 changes: 30 additions & 0 deletions src/main/java/dev/anvilcraft/rg/util/ConfigUtil.java
Original file line number Diff line number Diff line change
@@ -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);
}
}

}

0 comments on commit a013f91

Please sign in to comment.