diff options
Diffstat (limited to 'src/main/java/gregtech/api/util')
3 files changed, 136 insertions, 0 deletions
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<String, Integer> textColors; + private final Map<String, String> hexTextColors; + private final Map<String, Integer> guiTints; + private final Map<String, String> hexGuiTints; + private final boolean guiTintEnabled; + + public ColorsMetadataSection(Map<String, String> hexTextColorMap, Map<String, String> hexGuiTintMap, boolean guiTintEnabled) { + this.hexTextColors = hexTextColorMap; + this.textColors = convertHexMapToIntMap(hexTextColorMap); + + this.hexGuiTints = hexGuiTintMap; + this.guiTints = convertHexMapToIntMap(hexGuiTintMap); + + this.guiTintEnabled = guiTintEnabled; + } + + private Map<String, Integer> convertHexMapToIntMap(Map <String, String> hexMap) { + Map<String, Integer> 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<String,String> 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<String, String> hexGuiTintMap = new HashMap<String, String>(); + Map<String, String> hexTextColorMap = new HashMap<String, String>() {{ + 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; } |