Rewrite 1.11->1.12 ChatItemRewriter (#3740)

This commit is contained in:
EnZaXD 2024-03-09 10:58:42 +01:00 committed by GitHub
parent f868dfa125
commit a9bfda4568
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 67 additions and 75 deletions

View File

@ -1,70 +0,0 @@
/*
* This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion
* Copyright (C) 2016-2024 ViaVersion and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.viaversion.viaversion.protocols.protocol1_12to1_11_1;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import com.viaversion.viaversion.api.connection.UserConnection;
import java.util.regex.Pattern;
public class ChatItemRewriter {
private static final Pattern indexRemoval = Pattern.compile("(?<![\\w-.+])\\d+:(?=([^\"\\\\]*(\\\\.|\"([^\"\\\\]*\\\\.)*[^\"\\\\]*\"))*[^\"]*$)");
// Taken from https://stackoverflow.com/questions/6462578/alternative-to-regex-match-all-instances-not-inside-quotes
public static void toClient(JsonElement element, UserConnection user) {
if (element instanceof JsonObject) {
JsonObject obj = (JsonObject) element;
if (obj.has("hoverEvent")) {
if (obj.get("hoverEvent") instanceof JsonObject) {
JsonObject hoverEvent = (JsonObject) obj.get("hoverEvent");
if (hoverEvent.has("action") && hoverEvent.has("value")) {
String type = hoverEvent.get("action").getAsString();
if (type.equals("show_item") || type.equals("show_entity")) {
JsonElement value = hoverEvent.get("value");
if (value.isJsonPrimitive() && value.getAsJsonPrimitive().isString()) {
String newValue = indexRemoval.matcher(value.getAsString()).replaceAll("");
hoverEvent.addProperty("value", newValue);
} else if (value.isJsonArray()) {
JsonArray newArray = new JsonArray();
for (JsonElement valueElement : value.getAsJsonArray()) {
if (valueElement.isJsonPrimitive() && valueElement.getAsJsonPrimitive().isString()) {
String newValue = indexRemoval.matcher(valueElement.getAsString()).replaceAll("");
newArray.add(new JsonPrimitive(newValue));
}
}
hoverEvent.add("value", newArray);
}
}
}
}
} else if (obj.has("extra")) {
toClient(obj.get("extra"), user);
}
} else if (element instanceof JsonArray) {
JsonArray array = (JsonArray) element;
for (JsonElement value : array) {
toClient(value, user);
}
}
}
}

View File

@ -40,6 +40,8 @@ import com.viaversion.viaversion.data.entity.EntityTrackerBase;
import com.viaversion.viaversion.protocols.protocol1_12to1_11_1.metadata.MetadataRewriter1_12To1_11_1;
import com.viaversion.viaversion.protocols.protocol1_12to1_11_1.packets.InventoryPackets;
import com.viaversion.viaversion.protocols.protocol1_12to1_11_1.providers.InventoryQuickMoveProvider;
import com.viaversion.viaversion.protocols.protocol1_12to1_11_1.rewriter.ChatItemRewriter;
import com.viaversion.viaversion.protocols.protocol1_12to1_11_1.rewriter.TranslateRewriter;
import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.ClientboundPackets1_13;
import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.Protocol1_13To1_12_2;
import com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.ClientboundPackets1_9_3;
@ -98,8 +100,9 @@ public class Protocol1_12To1_11_1 extends AbstractProtocol<ClientboundPackets1_9
if (!Via.getConfig().is1_12NBTArrayFix()) return;
try {
JsonElement obj = Protocol1_9To1_8.FIX_JSON.transform(null, wrapper.passthrough(Type.COMPONENT).toString());
TranslateRewriter.toClient(obj, wrapper.user());
ChatItemRewriter.toClient(obj, wrapper.user());
TranslateRewriter.toClient(obj);
ChatItemRewriter.toClient(obj);
wrapper.set(Type.COMPONENT, 0, obj);
} catch (Exception e) {
e.printStackTrace();

View File

@ -0,0 +1,60 @@
/*
* This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion
* Copyright (C) 2016-2024 ViaVersion and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.viaversion.viaversion.protocols.protocol1_12to1_11_1.rewriter;
import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import net.lenni0451.mcstructs.snbt.SNbtSerializer;
import net.lenni0451.mcstructs.snbt.exceptions.SNbtDeserializeException;
import net.lenni0451.mcstructs.snbt.exceptions.SNbtSerializeException;
import net.lenni0451.mcstructs.text.ATextComponent;
import net.lenni0451.mcstructs.text.serializer.TextComponentSerializer;
public class ChatItemRewriter {
public static void toClient(JsonElement element) throws SNbtDeserializeException, SNbtSerializeException {
if (element instanceof JsonObject) {
JsonObject obj = (JsonObject) element;
if (obj.has("hoverEvent")) {
if (obj.get("hoverEvent") instanceof JsonObject) {
final JsonObject hoverEvent = (JsonObject) obj.get("hoverEvent");
if (hoverEvent.has("action") && hoverEvent.has("value")) {
final String type = hoverEvent.get("action").getAsString();
final JsonElement value = hoverEvent.get("value");
if (type.equals("show_item")) {
final ATextComponent component = TextComponentSerializer.V1_8.deserialize(value);
final CompoundTag compound = SNbtSerializer.V1_8.deserialize(component.asUnformattedString());
hoverEvent.addProperty("value", SNbtSerializer.V1_12.serialize(compound));
}
}
}
} else if (obj.has("extra")) {
toClient(obj.get("extra"));
}
} else if (element instanceof JsonArray) {
JsonArray array = (JsonArray) element;
for (JsonElement value : array) {
toClient(value);
}
}
}
}

View File

@ -15,13 +15,12 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.viaversion.viaversion.protocols.protocol1_12to1_11_1;
package com.viaversion.viaversion.protocols.protocol1_12to1_11_1.rewriter;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.viaversion.viaversion.api.Via;
import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.protocols.protocol1_12to1_11_1.data.AchievementTranslationMapping;
import com.viaversion.viaversion.protocols.protocol1_9_3to1_9_1_2.ClientboundPackets1_9_3;
import com.viaversion.viaversion.rewriter.ComponentRewriter;
@ -103,7 +102,7 @@ public class TranslateRewriter {
}
};
public static void toClient(JsonElement element, UserConnection user) {
public static void toClient(JsonElement element) {
if (element instanceof JsonObject) {
JsonObject obj = (JsonObject) element;
JsonElement translate = obj.get("translate");