From 89d8435e80c8955d8299fe60d9a2169067f8ac75 Mon Sep 17 00:00:00 2001 From: SHsuperCM Date: Sat, 28 Aug 2021 18:40:09 +0300 Subject: Added config as well as support for modmenu/cloth --- .../fabric/citresewn/config/CITResewnConfig.java | 51 ++++++++++++++++++++++ .../config/CITResewnConfigScreenFactory.java | 36 +++++++++++++++ .../fabric/citresewn/config/CITResewnModMenu.java | 15 +++++++ 3 files changed, 102 insertions(+) create mode 100644 src/main/java/shcm/shsupercm/fabric/citresewn/config/CITResewnConfig.java create mode 100644 src/main/java/shcm/shsupercm/fabric/citresewn/config/CITResewnConfigScreenFactory.java create mode 100644 src/main/java/shcm/shsupercm/fabric/citresewn/config/CITResewnModMenu.java (limited to 'src/main/java/shcm/shsupercm/fabric/citresewn/config') 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; + } +} -- cgit