diff options
Diffstat (limited to 'src/main/java/shcm/shsupercm/fabric/citresewn/config')
3 files changed, 102 insertions, 0 deletions
diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/config/CITResewnConfig.java b/src/main/java/shcm/shsupercm/fabric/citresewn/config/CITResewnConfig.java new file mode 100644 index 0000000..38995ca --- /dev/null +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/config/CITResewnConfig.java @@ -0,0 +1,51 @@ +package shcm.shsupercm.fabric.citresewn.config; + +import com.google.gson.Gson; +import com.google.gson.stream.JsonWriter; +import org.apache.commons.io.IOUtils; +import shcm.shsupercm.fabric.citresewn.CITResewn; + +import java.io.*; + +public class CITResewnConfig { + public boolean enabled = true; + + private static final File FILE = new File("config/citresewn.json"); + public static CITResewnConfig INSTANCE() { + return CITResewn.INSTANCE.config; + } + + public static CITResewnConfig read() { + if (!FILE.exists()) + return new CITResewnConfig().write(); + + Reader reader = null; + try { + return new Gson().fromJson(reader = new FileReader(FILE), CITResewnConfig.class); + } catch (Exception e) { + CITResewn.LOG.error("Couldn't load config"); + e.printStackTrace(); + throw new RuntimeException(e); + } finally { + IOUtils.closeQuietly(reader); + } + } + + public CITResewnConfig write() { + Gson gson = new Gson(); + JsonWriter writer = null; + try { + writer = gson.newJsonWriter(new FileWriter(FILE)); + writer.setIndent(" "); + + gson.toJson(gson.toJsonTree(this, CITResewnConfig.class), writer); + } catch (Exception e) { + CITResewn.LOG.error("Couldn't save config"); + e.printStackTrace(); + throw new RuntimeException(e); + } finally { + IOUtils.closeQuietly(writer); + } + return this; + } +} diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/config/CITResewnConfigScreenFactory.java b/src/main/java/shcm/shsupercm/fabric/citresewn/config/CITResewnConfigScreenFactory.java new file mode 100644 index 0000000..bc00d2b --- /dev/null +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/config/CITResewnConfigScreenFactory.java @@ -0,0 +1,36 @@ +package shcm.shsupercm.fabric.citresewn.config; + +import me.shedaniel.clothconfig2.api.ConfigBuilder; +import me.shedaniel.clothconfig2.api.ConfigCategory; +import me.shedaniel.clothconfig2.api.ConfigEntryBuilder; +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.gui.screen.Screen; +import net.minecraft.text.LiteralText; +import net.minecraft.text.TranslatableText; + +public class CITResewnConfigScreenFactory { + public static Screen create(Screen parent) { + CITResewnConfig currentConfig = CITResewnConfig.INSTANCE(), defaultConfig = new CITResewnConfig(); + + ConfigBuilder builder = ConfigBuilder.create() + .setParentScreen(parent) + .setTitle(new TranslatableText("config.citresewn.title")) + .setSavingRunnable(currentConfig::write); + + ConfigCategory category = builder.getOrCreateCategory(new LiteralText("")); + ConfigEntryBuilder entryBuilder = builder.entryBuilder(); + + category.addEntry(entryBuilder.startBooleanToggle(new TranslatableText("config.citresewn.citsEnabled.title"), currentConfig.enabled) + .setTooltip(new TranslatableText("config.citresewn.citsEnabled.tooltip")) + .setSaveConsumer(newConfig -> { + if (currentConfig.enabled != newConfig) { + currentConfig.enabled = newConfig; + MinecraftClient.getInstance().reloadResources(); + } + }) + .setDefaultValue(defaultConfig.enabled) + .build()); + + return builder.build(); + } +} diff --git a/src/main/java/shcm/shsupercm/fabric/citresewn/config/CITResewnModMenu.java b/src/main/java/shcm/shsupercm/fabric/citresewn/config/CITResewnModMenu.java new file mode 100644 index 0000000..4c3f20a --- /dev/null +++ b/src/main/java/shcm/shsupercm/fabric/citresewn/config/CITResewnModMenu.java @@ -0,0 +1,15 @@ +package shcm.shsupercm.fabric.citresewn.config; + +import com.terraformersmc.modmenu.api.ConfigScreenFactory; +import com.terraformersmc.modmenu.api.ModMenuApi; +import net.fabricmc.loader.api.FabricLoader; + +public class CITResewnModMenu implements ModMenuApi { + @Override + public ConfigScreenFactory<?> getModConfigScreenFactory() { + if (FabricLoader.getInstance().isModLoaded("cloth-config2")) + return CITResewnConfigScreenFactory::create; + + return parent -> null; + } +} |