3
0
Mirror von https://github.com/ViaVersion/ViaVersion.git synchronisiert 2024-12-27 16:40:10 +01:00

Rewrite 1.12.2->1.13 ComponentRewriter (#3765)

Dieser Commit ist enthalten in:
EnZaXD 2024-04-01 13:38:43 +02:00 committet von GitHub
Ursprung f44782d21d
Commit 51745cb2ef
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: B5690EEEBB952194

Datei anzeigen

@ -17,14 +17,12 @@
*/ */
package com.viaversion.viaversion.protocols.protocol1_13to1_12_2.data; package com.viaversion.viaversion.protocols.protocol1_13to1_12_2.data;
import com.github.steveice10.opennbt.stringified.SNBT;
import com.github.steveice10.opennbt.tag.builtin.CompoundTag; import com.github.steveice10.opennbt.tag.builtin.CompoundTag;
import com.github.steveice10.opennbt.tag.builtin.NumberTag; import com.github.steveice10.opennbt.tag.builtin.NumberTag;
import com.github.steveice10.opennbt.tag.builtin.ShortTag; import com.github.steveice10.opennbt.tag.builtin.ShortTag;
import com.google.gson.JsonArray; import com.google.gson.JsonArray;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import com.viaversion.viaversion.api.Via; import com.viaversion.viaversion.api.Via;
import com.viaversion.viaversion.api.minecraft.item.DataItem; import com.viaversion.viaversion.api.minecraft.item.DataItem;
import com.viaversion.viaversion.api.minecraft.item.Item; import com.viaversion.viaversion.api.minecraft.item.Item;
@ -32,6 +30,9 @@ import com.viaversion.viaversion.api.protocol.Protocol;
import com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType; import com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType;
import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.Protocol1_13To1_12_2; import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.Protocol1_13To1_12_2;
import com.viaversion.viaversion.rewriter.ComponentRewriter; import com.viaversion.viaversion.rewriter.ComponentRewriter;
import net.lenni0451.mcstructs.snbt.SNbtSerializer;
import net.lenni0451.mcstructs.text.ATextComponent;
import net.lenni0451.mcstructs.text.serializer.TextComponentSerializer;
import java.util.logging.Level; import java.util.logging.Level;
public class ComponentRewriter1_13<C extends ClientboundPacketType> extends ComponentRewriter<C> { public class ComponentRewriter1_13<C extends ClientboundPacketType> extends ComponentRewriter<C> {
@ -43,21 +44,19 @@ public class ComponentRewriter1_13<C extends ClientboundPacketType> extends Comp
@Override @Override
protected void handleHoverEvent(JsonObject hoverEvent) { protected void handleHoverEvent(JsonObject hoverEvent) {
super.handleHoverEvent(hoverEvent); super.handleHoverEvent(hoverEvent);
String action = hoverEvent.getAsJsonPrimitive("action").getAsString(); final String action = hoverEvent.getAsJsonPrimitive("action").getAsString();
if (!action.equals("show_item")) return; if (!action.equals("show_item")) return;
JsonElement value = hoverEvent.get("value"); final JsonElement value = hoverEvent.get("value");
if (value == null) return; if (value == null) return;
String text = findItemNBT(value); final ATextComponent nbt = TextComponentSerializer.V1_12.deserialize(value);
if (text == null) return;
CompoundTag tag; CompoundTag tag;
try { try {
tag = SNBT.deserializeCompoundTag(text); tag = SNbtSerializer.V1_12.deserialize(nbt.asUnformattedString());
} catch (Exception e) { } catch (Exception e) {
if (!Via.getConfig().isSuppressConversionWarnings() || Via.getManager().isDebug()) { if (!Via.getConfig().isSuppressConversionWarnings() || Via.getManager().isDebug()) {
Via.getPlatform().getLogger().log(Level.WARNING, "Error reading NBT in show_item:" + text, e); Via.getPlatform().getLogger().log(Level.WARNING, "Error reading 1.12.2 NBT in show_item: " + nbt, e);
} }
return; return;
} }
@ -85,33 +84,14 @@ public class ComponentRewriter1_13<C extends ClientboundPacketType> extends Comp
array.add(object); array.add(object);
String serializedNBT; String serializedNBT;
try { try {
serializedNBT = SNBT.serialize(tag); serializedNBT = SNbtSerializer.V1_13.serialize(tag);
object.addProperty("text", serializedNBT); object.addProperty("text", serializedNBT);
hoverEvent.add("value", array); hoverEvent.add("value", array);
} catch (Exception e) { } catch (Exception e) {
Via.getPlatform().getLogger().log(Level.WARNING, "Error writing NBT in show_item:" + text, e); Via.getPlatform().getLogger().log(Level.WARNING, "Error writing 1.13 NBT in show_item: " + nbt, e);
} }
} }
protected String findItemNBT(JsonElement element) {
if (element.isJsonArray()) {
for (JsonElement jsonElement : element.getAsJsonArray()) {
String value = findItemNBT(jsonElement);
if (value != null) {
return value;
}
}
} else if (element.isJsonObject()) {
JsonPrimitive text = element.getAsJsonObject().getAsJsonPrimitive("text");
if (text != null) {
return text.getAsString();
}
} else if (element.isJsonPrimitive()) {
return element.getAsJsonPrimitive().getAsString();
}
return null;
}
@Override @Override
protected void handleTranslate(JsonObject object, String translate) { protected void handleTranslate(JsonObject object, String translate) {
super.handleTranslate(object, translate); super.handleTranslate(object, translate);