From b1ca8cc2a0bd2a7fe129895a35fbfe7297f445c5 Mon Sep 17 00:00:00 2001 From: KennyTV Date: Thu, 23 Apr 2020 12:25:09 +0200 Subject: [PATCH] Approximate hex color to chat color, fix show_text hover TODO: show_entity hover --- .../api/rewriters/TranslatableRewriter.java | 4 +- .../Protocol1_15_2To1_16.java | 32 +---- .../chat/TranslatableRewriter1_16.java | 135 ++++++++++++++++++ .../viabackwards/data/mapping-1.15to1.16.json | 2 +- 4 files changed, 141 insertions(+), 32 deletions(-) create mode 100644 core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_15_2to1_16/chat/TranslatableRewriter1_16.java diff --git a/core/src/main/java/nl/matsv/viabackwards/api/rewriters/TranslatableRewriter.java b/core/src/main/java/nl/matsv/viabackwards/api/rewriters/TranslatableRewriter.java index a8b68ffb..246140e7 100644 --- a/core/src/main/java/nl/matsv/viabackwards/api/rewriters/TranslatableRewriter.java +++ b/core/src/main/java/nl/matsv/viabackwards/api/rewriters/TranslatableRewriter.java @@ -11,6 +11,7 @@ import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.packets.State; import us.myles.viaversion.libs.gson.JsonElement; import us.myles.viaversion.libs.gson.JsonObject; +import us.myles.viaversion.libs.gson.JsonParser; import java.util.HashMap; import java.util.Map; @@ -18,7 +19,8 @@ import java.util.Map; public class TranslatableRewriter { private static final Map> TRANSLATABLES = new HashMap<>(); - private final BackwardsProtocol protocol; + protected static final JsonParser JSON_PARSER = new JsonParser(); + protected final BackwardsProtocol protocol; protected final Map newTranslatables; public static void loadTranslatables() { diff --git a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_15_2to1_16/Protocol1_15_2To1_16.java b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_15_2to1_16/Protocol1_15_2To1_16.java index 68a4924a..42e5e918 100644 --- a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_15_2to1_16/Protocol1_15_2To1_16.java +++ b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_15_2to1_16/Protocol1_15_2To1_16.java @@ -5,6 +5,7 @@ import nl.matsv.viabackwards.api.BackwardsProtocol; import nl.matsv.viabackwards.api.entities.storage.EntityTracker; import nl.matsv.viabackwards.api.rewriters.SoundRewriter; import nl.matsv.viabackwards.api.rewriters.TranslatableRewriter; +import nl.matsv.viabackwards.protocol.protocol1_15_2to1_16.chat.TranslatableRewriter1_16; import nl.matsv.viabackwards.protocol.protocol1_15_2to1_16.data.BackwardsMappings; import nl.matsv.viabackwards.protocol.protocol1_15_2to1_16.packets.BlockItemPackets1_16; import nl.matsv.viabackwards.protocol.protocol1_15_2to1_16.packets.EntityPackets1_16; @@ -16,10 +17,6 @@ import us.myles.ViaVersion.packets.State; import us.myles.ViaVersion.protocols.protocol1_16to1_15_2.Protocol1_16To1_15_2; import us.myles.ViaVersion.protocols.protocol1_16to1_15_2.data.MappingData; import us.myles.ViaVersion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld; -import us.myles.ViaVersion.util.GsonUtil; -import us.myles.viaversion.libs.gson.JsonElement; -import us.myles.viaversion.libs.gson.JsonObject; -import us.myles.viaversion.libs.gson.JsonPrimitive; import java.util.UUID; @@ -35,32 +32,7 @@ public class Protocol1_15_2To1_16 extends BackwardsProtocol { EntityPackets1_16 entityPackets = new EntityPackets1_16(this); entityPackets.register(); - TranslatableRewriter translatableRewriter = new TranslatableRewriter(this) { - @Override - public String processTranslate(String value) { - JsonObject object = GsonUtil.getGson().fromJson(value, JsonObject.class); - JsonElement with = object.get("with"); - if (with == null) { - return super.processTranslate(value); - } - - for (JsonElement element : with.getAsJsonArray()) { - if (!element.isJsonObject()) continue; - - JsonElement hoverEventElement = element.getAsJsonObject().get("hoverEvent"); - if (hoverEventElement == null) continue; - - JsonObject hoverEvent = hoverEventElement.getAsJsonObject(); - JsonElement contentsElement = hoverEvent.remove("contents"); - if (contentsElement != null) { - JsonObject values = new JsonObject(); - values.add("text", new JsonPrimitive(contentsElement.toString())); - hoverEvent.add("value", values); - } - } - return super.processTranslate(object.toString()); - } - }; + TranslatableRewriter translatableRewriter = new TranslatableRewriter1_16(this); translatableRewriter.registerBossBar(0x0D, 0x0D); translatableRewriter.registerChatMessage(0x0F, 0x0F); translatableRewriter.registerCombatEvent(0x33, 0x33); diff --git a/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_15_2to1_16/chat/TranslatableRewriter1_16.java b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_15_2to1_16/chat/TranslatableRewriter1_16.java new file mode 100644 index 00000000..7d478ac2 --- /dev/null +++ b/core/src/main/java/nl/matsv/viabackwards/protocol/protocol1_15_2to1_16/chat/TranslatableRewriter1_16.java @@ -0,0 +1,135 @@ +package nl.matsv.viabackwards.protocol.protocol1_15_2to1_16.chat; + +import nl.matsv.viabackwards.api.BackwardsProtocol; +import nl.matsv.viabackwards.api.rewriters.TranslatableRewriter; +import us.myles.viaversion.libs.gson.JsonArray; +import us.myles.viaversion.libs.gson.JsonElement; +import us.myles.viaversion.libs.gson.JsonObject; +import us.myles.viaversion.libs.gson.JsonPrimitive; + +public class TranslatableRewriter1_16 extends TranslatableRewriter { + + private static final ChatColor[] COLORS = { + new ChatColor("black", 0x000000), + new ChatColor("dark_blue", 0x0000aa), + new ChatColor("dark_green", 0x00aa00), + new ChatColor("dark_aqua", 0x00aaaa), + new ChatColor("dark_red", 0xaa0000), + new ChatColor("dark_purple", 0xaa00aa), + new ChatColor("gold", 0xffaa00), + new ChatColor("gray", 0xaaaaaa), + new ChatColor("dark_gray", 0x555555), + new ChatColor("blue", 0x5555ff), + new ChatColor("green", 0x55ff55), + new ChatColor("aqua", 0x55ffff), + new ChatColor("red", 0xff5555), + new ChatColor("light_purple", 0xff55ff), + new ChatColor("yellow", 0xffff55), + new ChatColor("white", 0xffffff) + }; + + public TranslatableRewriter1_16(BackwardsProtocol protocol) { + super(protocol); + } + + @Override + public String processTranslate(String value) { + JsonElement root = JSON_PARSER.parse(value); + if (!root.isJsonObject()) { + return super.processTranslate(value); + } + + processTranslate(root); + return super.processTranslate(root.toString()); + } + + private void processTranslate(JsonElement value) { + if (!value.isJsonObject()) return; + + // Iterate all sub components + JsonObject object = value.getAsJsonObject(); + JsonArray with = object.getAsJsonArray("with"); + if (with != null) { + for (JsonElement element : with) { + processTranslate(element); + } + } + JsonArray extra = object.getAsJsonArray("extra"); + if (extra != null) { + for (JsonElement element : extra) { + processTranslate(element); + } + } + + // Hoverevent structure changed + JsonObject hoverEvent = object.getAsJsonObject("hoverEvent"); + if (hoverEvent != null) { + JsonElement contentsElement = hoverEvent.remove("contents"); + String action = hoverEvent.getAsJsonPrimitive("action").getAsString(); + if (contentsElement != null) { + if (action.equals("show_text")) { + // text as chat component + hoverEvent.add("value", contentsElement); + } else { + // show_entity and show_item as plain strings in a text field + final JsonObject jsonObject = new JsonObject(); + jsonObject.addProperty("text", contentsElement.toString()); + hoverEvent.add("value", jsonObject); + //hoverEvent.addProperty("value", contentsElement.toString()); + } + } + } + + // c o l o r s + JsonPrimitive color = object.getAsJsonPrimitive("color"); + if (color != null) { + String colorName = color.getAsString(); + if (!colorName.isEmpty() && colorName.charAt(0) == '#') { + int rgb = Integer.parseInt(colorName.substring(1), 16); + String closestChatColor = getClosestChatColor(rgb); + object.addProperty("color", closestChatColor); + } + } + } + + private String getClosestChatColor(int rgb) { + int r = (rgb >> 16) & 0xFF; + int g = (rgb >> 8) & 0xFF; + int b = rgb & 0xFF; + + ChatColor closest = null; + int smallestDiff = 0; + + for (ChatColor color : COLORS) { + if (color.rgb == rgb) { + return color.colorName; + } + + // Check by the greatest diff of the 3 values + int rDiff = Math.abs(color.r - r); + int gDiff = Math.abs(color.g - g); + int bDiff = Math.abs(color.b - b); + int maxDiff = Math.max(Math.max(rDiff, gDiff), bDiff); + if (closest == null || maxDiff < smallestDiff) { + closest = color; + smallestDiff = maxDiff; + } + } + return closest.colorName; + } + + private static final class ChatColor { + + private final String colorName; + private final int rgb; + private final int r, g, b; + + ChatColor(String colorName, int rgb) { + this.colorName = colorName; + this.rgb = rgb; + r = (rgb >> 16) & 0xFF; + g = (rgb >> 8) & 0xFF; + b = rgb & 0xFF; + } + } +} diff --git a/core/src/main/resources/assets/viabackwards/data/mapping-1.15to1.16.json b/core/src/main/resources/assets/viabackwards/data/mapping-1.15to1.16.json index ec1cd334..9b3875c5 100644 --- a/core/src/main/resources/assets/viabackwards/data/mapping-1.15to1.16.json +++ b/core/src/main/resources/assets/viabackwards/data/mapping-1.15to1.16.json @@ -5675,7 +5675,7 @@ "minecraft:chiseled_nether_bricks": "minecraft:nether_bricks", "minecraft:cracked_nether_bricks": "minecraft:nether_bricks", "minecraft:quartz_bricks": "minecraft:quartz_block", - "minecraft:chain": "minecraft:air" + "minecraft:chain": "minecraft:iron_bars[east=false,north=false,south=false,waterlogged=false,west=false]" }, "items": { "minecraft:crimson_nylium": {