From 43c014a8db9ad4d44c19190718835a7596dc0a2d Mon Sep 17 00:00:00 2001 From: Albi <12825442+Flanisch@users.noreply.github.com> Date: Mon, 15 Aug 2022 21:27:24 +0200 Subject: Overriding GUI colors with .mcmeta (#1261) * Implemented gui textcolor override with .mcmeta files * cleanup * Added shared class to reduce code duplication * Moved #drawLine back to GT_NEI_DefaultHandler --- .../gregtech/api/util/ColorsMetadataSection.java | 61 +++++++++++++++++++ .../api/util/ColorsMetadataSectionSerializer.java | 71 ++++++++++++++++++++++ src/main/java/gregtech/api/util/GT_Util.java | 4 ++ 3 files changed, 136 insertions(+) create mode 100644 src/main/java/gregtech/api/util/ColorsMetadataSection.java create mode 100644 src/main/java/gregtech/api/util/ColorsMetadataSectionSerializer.java (limited to 'src/main/java/gregtech/api/util') diff --git a/src/main/java/gregtech/api/util/ColorsMetadataSection.java b/src/main/java/gregtech/api/util/ColorsMetadataSection.java new file mode 100644 index 0000000000..2851435061 --- /dev/null +++ b/src/main/java/gregtech/api/util/ColorsMetadataSection.java @@ -0,0 +1,61 @@ +package gregtech.api.util; + +import cpw.mods.fml.relauncher.Side; +import cpw.mods.fml.relauncher.SideOnly; +import java.util.Map; +import java.util.HashMap; +import gregtech.api.util.GT_Log; +import gregtech.api.GregTech_API; +import net.minecraft.client.resources.data.IMetadataSection; + +@SideOnly(Side.CLIENT) +public class ColorsMetadataSection implements IMetadataSection { + private final Map textColors; + private final Map hexTextColors; + private final Map guiTints; + private final Map hexGuiTints; + private final boolean guiTintEnabled; + + public ColorsMetadataSection(Map hexTextColorMap, Map hexGuiTintMap, boolean guiTintEnabled) { + this.hexTextColors = hexTextColorMap; + this.textColors = convertHexMapToIntMap(hexTextColorMap); + + this.hexGuiTints = hexGuiTintMap; + this.guiTints = convertHexMapToIntMap(hexGuiTintMap); + + this.guiTintEnabled = guiTintEnabled; + } + + private Map convertHexMapToIntMap(Map hexMap) { + Map intMap = new HashMap<>(); + + for (String key : hexMap.keySet()) { + int colorValue = -1; + String hex = hexMap.get(key); + try { + if (!hex.isEmpty()) colorValue = Integer.parseUnsignedInt(hex,16); + } + catch (final NumberFormatException e) { + GT_Log.err.println("Couldn't format color correctly of " + key + " -> " + hex); + } + intMap.put(key, colorValue); + } + return intMap; + } + + public int getTextColorOrDefault(String key, int defaultColor) { + return sColorInMap(key, this.hexTextColors) ? defaultColor : this.textColors.get(key); + } + + public int getGuiTintOrDefault(String key, int defaultColor) { + return sColorInMap(key, this.hexGuiTints) ? defaultColor : this.guiTints.get(key); + } + + private boolean sColorInMap(String key, Map hexMap) { + return hexMap.containsKey(key) && hexMap.get(key).isEmpty(); + } + + public boolean sGuiTintingEnabled() { + return this.guiTintEnabled; + } +} diff --git a/src/main/java/gregtech/api/util/ColorsMetadataSectionSerializer.java b/src/main/java/gregtech/api/util/ColorsMetadataSectionSerializer.java new file mode 100644 index 0000000000..4de8463685 --- /dev/null +++ b/src/main/java/gregtech/api/util/ColorsMetadataSectionSerializer.java @@ -0,0 +1,71 @@ +package gregtech.api.util; + +import com.google.gson.JsonDeserializationContext; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonSerializationContext; +import com.google.gson.JsonSerializer; +import cpw.mods.fml.relauncher.Side; +import cpw.mods.fml.relauncher.SideOnly; +import java.lang.reflect.Type; +import java.util.Map; +import java.util.HashMap; +import gregtech.api.GregTech_API; +import gregtech.api.enums.Dyes; +import net.minecraft.util.JsonUtils; +import net.minecraft.client.resources.data.BaseMetadataSectionSerializer; + +@SideOnly(Side.CLIENT) +public class ColorsMetadataSectionSerializer extends BaseMetadataSectionSerializer implements JsonSerializer { + public ColorsMetadataSection deserialize(JsonElement metadataColors, Type type, JsonDeserializationContext context) { + // Default values + boolean enableGuiTint = GregTech_API.sColoredGUI; + Map hexGuiTintMap = new HashMap(); + Map hexTextColorMap = new HashMap() {{ + put("title", ""); + put("text", ""); + put("value", ""); + put("nei", ""); + }}; + + JsonObject jsonObject = JsonUtils.getJsonElementAsJsonObject(metadataColors, "metadata section"); + if (jsonObject.has("textColor")) { + JsonObject textColors = JsonUtils.func_152754_s(jsonObject, "textColor"); + for (String key : hexTextColorMap.keySet()) { + hexTextColorMap.replace(key, JsonUtils.getJsonObjectStringFieldValueOrDefault(textColors, key, "")); + } + } + + if (jsonObject.has("guiTint")) { + JsonObject guiTints = JsonUtils.func_152754_s(jsonObject, "guiTint"); + enableGuiTint = JsonUtils.getJsonObjectBooleanFieldValueOrDefault(guiTints, "enableGuiTintWhenPainted", true); + + for (Dyes dye : Dyes.values()) { + hexGuiTintMap.put(dye.mName, GT_Util.toHexString(dye.getRGBA())); + } + + for (String key : hexGuiTintMap.keySet()) { + if (enableGuiTint) { + hexGuiTintMap.replace(key, JsonUtils.getJsonObjectStringFieldValueOrDefault(guiTints, key, hexGuiTintMap.get(key))); + } else { + hexGuiTintMap.replace(key, GT_Util.toHexString(Dyes.dyeWhite.getRGBA())); + } + } + } + + return new ColorsMetadataSection(hexTextColorMap, hexGuiTintMap, enableGuiTint); + } + + public JsonElement serialize(ColorsMetadataSection colorsMetaSection, Type type, JsonSerializationContext context) { + JsonObject jsonObject = new JsonObject(); + return jsonObject; + } + + public String getSectionName() { + return "colors"; + } + + public JsonElement serialize(Object object, Type type, JsonSerializationContext context) { + return this.serialize((ColorsMetadataSection) object, type, context); + } +} diff --git a/src/main/java/gregtech/api/util/GT_Util.java b/src/main/java/gregtech/api/util/GT_Util.java index 1079da6c26..45c005bdff 100644 --- a/src/main/java/gregtech/api/util/GT_Util.java +++ b/src/main/java/gregtech/api/util/GT_Util.java @@ -132,6 +132,10 @@ public class GT_Util { return aColors == null ? 16777215 : (aColors[0]) << 16 | (aColors[1] << 8) | aColors[2] | (aColors[3] << 24); } + public static String toHexString(short[] aColors) { + return aColors == null ? "FFFFFF" : Integer.toString((aColors[0] << 16) | (aColors[1] << 8) | aColors[2], 16); + } + public static int getRGBInt(short aR, short aG, short aB) { return (aR << 16) | (aG << 8) | aB; } -- cgit