From bbd40c34e104dcead78290af1394e6f5867df565 Mon Sep 17 00:00:00 2001 From: Albi <12825442+Flanisch@users.noreply.github.com> Date: Fri, 12 Aug 2022 20:34:37 +0200 Subject: Added means of customizing GUI colors for resource packs (#1203) * Added means of customizing GUI colors through lang file * Color values are now stored in json file instead of the lang file * Made json only load upon resource reload and ensured server compatibility Co-authored-by: Martin Robertz --- .../java/gregtech/loaders/misc/GT_JsonLoader.java | 64 ++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 src/main/java/gregtech/loaders/misc/GT_JsonLoader.java (limited to 'src/main/java/gregtech/loaders') diff --git a/src/main/java/gregtech/loaders/misc/GT_JsonLoader.java b/src/main/java/gregtech/loaders/misc/GT_JsonLoader.java new file mode 100644 index 0000000000..d46c7eae0b --- /dev/null +++ b/src/main/java/gregtech/loaders/misc/GT_JsonLoader.java @@ -0,0 +1,64 @@ +package gregtech.loaders.misc; + +import cpw.mods.fml.relauncher.Side; +import cpw.mods.fml.relauncher.SideOnly; + +import java.io.BufferedInputStream; +import java.io.ByteArrayOutputStream; + +import net.minecraft.client.Minecraft; +import net.minecraft.client.resources.IReloadableResourceManager; +import net.minecraft.client.resources.IResourceManager; +import net.minecraft.client.resources.IResourceManagerReloadListener; +import net.minecraft.util.ResourceLocation; + +import org.json.JSONObject; + +import gregtech.api.util.GT_Log; + +@SideOnly(Side.CLIENT) +public class GT_JsonLoader implements IResourceManagerReloadListener { + private JSONObject json; + private ResourceLocation jsonLocation; + private IResourceManager resourceManager; + + public String getString(String key) { + String s = ""; + try { + s = this.json.getString(key); + } + catch (Exception e) { + GT_Log.err.println("GT_JsonLoader" + e); + } + return s; + } + + public void loadJson() { + this.json = new JSONObject("{}"); + try { + BufferedInputStream bis = new BufferedInputStream(this.resourceManager.getResource(this.jsonLocation).getInputStream()); + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + + for (int result = bis.read(); result != -1; result = bis.read()) { + bos.write((byte)result); + } + + this.json = new JSONObject(bos.toString("UTF-8")); + } + catch (Exception e) { + GT_Log.err.println("GT_JsonLoader: " + e); + } + } + + public void onResourceManagerReload(IResourceManager rm) { + loadJson(); + } + + public GT_JsonLoader (String resourcePath) { + GT_Log.err.println("GT_JsonLoader: Init"); + this.jsonLocation = new ResourceLocation("gregtech", resourcePath); + this.resourceManager = Minecraft.getMinecraft().getResourceManager(); + ((IReloadableResourceManager) Minecraft.getMinecraft().getResourceManager()).registerReloadListener(this); + loadJson(); + } +} \ No newline at end of file -- cgit