3
0
Mirror von https://github.com/ViaVersion/ViaVersion.git synchronisiert 2024-11-19 14:30:16 +01:00

Add conversion methods to SerializerVersion and replace missing MCStructs usages (#3769)

Dieser Commit ist enthalten in:
EnZaXD 2024-04-01 15:52:57 +02:00 committet von GitHub
Ursprung 307414eb51
Commit cd65925d6b
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: B5690EEEBB952194
6 geänderte Dateien mit 84 neuen und 48 gelöschten Zeilen

Datei anzeigen

@ -23,7 +23,6 @@ import com.google.gson.JsonElement;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.viaversion.viaversion.util.ComponentUtil; import com.viaversion.viaversion.util.ComponentUtil;
import com.viaversion.viaversion.util.SerializerVersion; import com.viaversion.viaversion.util.SerializerVersion;
import com.viaversion.viaversion.util.TagUtil;
public final class ChatItemRewriter { public final class ChatItemRewriter {
@ -45,7 +44,7 @@ public final class ChatItemRewriter {
if (type.equals("show_item")) { if (type.equals("show_item")) {
final CompoundTag compound = ComponentUtil.deserializeLegacyShowItem(value, SerializerVersion.V1_8); final CompoundTag compound = ComponentUtil.deserializeLegacyShowItem(value, SerializerVersion.V1_8);
hoverEvent.addProperty("value", TagUtil.toSNBT(compound, SerializerVersion.V1_12)); hoverEvent.addProperty("value", SerializerVersion.V1_12.toSNBT(compound));
} }
} else if (obj.has("extra")) { } else if (obj.has("extra")) {
toClient(obj.get("extra")); toClient(obj.get("extra"));

Datei anzeigen

@ -32,7 +32,6 @@ import com.viaversion.viaversion.protocols.protocol1_13to1_12_2.Protocol1_13To1_
import com.viaversion.viaversion.rewriter.ComponentRewriter; import com.viaversion.viaversion.rewriter.ComponentRewriter;
import com.viaversion.viaversion.util.ComponentUtil; import com.viaversion.viaversion.util.ComponentUtil;
import com.viaversion.viaversion.util.SerializerVersion; import com.viaversion.viaversion.util.SerializerVersion;
import com.viaversion.viaversion.util.TagUtil;
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> {
@ -60,12 +59,13 @@ public class ComponentRewriter1_13<C extends ClientboundPacketType> extends Comp
return; return;
} }
CompoundTag itemTag = tag.getCompoundTag("tag"); final CompoundTag itemTag = tag.getCompoundTag("tag");
NumberTag damageTag = tag.getNumberTag("Damage"); final NumberTag damageTag = tag.getNumberTag("Damage");
// Call item converter // Call item converter
short damage = damageTag != null ? damageTag.asShort() : 0; final short damage = damageTag != null ? damageTag.asShort() : 0;
Item item = new DataItem();
final Item item = new DataItem();
item.setData(damage); item.setData(damage);
item.setTag(itemTag); item.setTag(itemTag);
protocol.getItemRewriter().handleItemToClient(item); protocol.getItemRewriter().handleItemToClient(item);
@ -78,16 +78,16 @@ public class ComponentRewriter1_13<C extends ClientboundPacketType> extends Comp
tag.put("tag", itemTag); tag.put("tag", itemTag);
} }
JsonArray array = new JsonArray(); final JsonArray newValue = new JsonArray();
JsonObject object = new JsonObject(); final JsonObject showItem = new JsonObject();
array.add(object); newValue.add(showItem);
String serializedNBT;
try { try {
serializedNBT = TagUtil.toSNBT(tag, SerializerVersion.V1_13); showItem.addProperty("text", SerializerVersion.V1_13.toSNBT(tag));
object.addProperty("text", serializedNBT); hoverEvent.add("value", newValue);
hoverEvent.add("value", array);
} catch (Exception e) { } catch (Exception e) {
Via.getPlatform().getLogger().log(Level.WARNING, "Error writing 1.13 NBT in show_item: " + value, e); if (!Via.getConfig().isSuppressConversionWarnings() || Via.getManager().isDebug()) {
Via.getPlatform().getLogger().log(Level.WARNING, "Error writing 1.13 NBT in show_item: " + value, e);
}
} }
} }

Datei anzeigen

@ -49,11 +49,11 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.UUID; import java.util.UUID;
import com.viaversion.viaversion.util.SerializerVersion;
import net.lenni0451.mcstructs.core.TextFormatting; import net.lenni0451.mcstructs.core.TextFormatting;
import net.lenni0451.mcstructs.text.ATextComponent; import net.lenni0451.mcstructs.text.ATextComponent;
import net.lenni0451.mcstructs.text.Style; import net.lenni0451.mcstructs.text.Style;
import net.lenni0451.mcstructs.text.components.TranslationComponent; import net.lenni0451.mcstructs.text.components.TranslationComponent;
import net.lenni0451.mcstructs.text.serializer.TextComponentSerializer;
import org.checkerframework.checker.nullness.qual.Nullable; import org.checkerframework.checker.nullness.qual.Nullable;
public final class Protocol1_19_1To1_19 extends AbstractProtocol<ClientboundPackets1_19, ClientboundPackets1_19_1, ServerboundPackets1_19, ServerboundPackets1_19_1> { public final class Protocol1_19_1To1_19 extends AbstractProtocol<ClientboundPackets1_19, ClientboundPackets1_19_1, ServerboundPackets1_19, ServerboundPackets1_19_1> {
@ -392,11 +392,11 @@ public final class Protocol1_19_1To1_19 extends AbstractProtocol<ClientboundPack
Via.getPlatform().getLogger().warning("Unknown parameter for chat decoration: " + element.getValue()); Via.getPlatform().getLogger().warning("Unknown parameter for chat decoration: " + element.getValue());
} }
if (argument != null) { if (argument != null) {
arguments.add(TextComponentSerializer.V1_18.deserialize(argument)); arguments.add(SerializerVersion.V1_18.toComponent(argument));
} }
} }
} }
return TextComponentSerializer.V1_18.serializeJson(new TranslationComponent(translationKey, arguments)); return SerializerVersion.V1_18.toJson(new TranslationComponent(translationKey, arguments));
} }
} }

Datei anzeigen

@ -29,7 +29,6 @@ import net.lenni0451.mcstructs.text.Style;
import net.lenni0451.mcstructs.text.events.hover.AHoverEvent; import net.lenni0451.mcstructs.text.events.hover.AHoverEvent;
import net.lenni0451.mcstructs.text.events.hover.impl.TextHoverEvent; import net.lenni0451.mcstructs.text.events.hover.impl.TextHoverEvent;
import net.lenni0451.mcstructs.text.serializer.LegacyStringDeserializer; import net.lenni0451.mcstructs.text.serializer.LegacyStringDeserializer;
import net.lenni0451.mcstructs.text.serializer.TextComponentCodec;
import net.lenni0451.mcstructs.text.serializer.TextComponentSerializer; import net.lenni0451.mcstructs.text.serializer.TextComponentSerializer;
import org.checkerframework.checker.nullness.qual.Nullable; import org.checkerframework.checker.nullness.qual.Nullable;
@ -54,7 +53,7 @@ public final class ComponentUtil {
public static @Nullable JsonElement tagToJson(@Nullable final Tag tag) { public static @Nullable JsonElement tagToJson(@Nullable final Tag tag) {
try { try {
final ATextComponent component = TextComponentCodec.V1_20_3.deserializeNbtTree(tag); final ATextComponent component = SerializerVersion.V1_20_3.toComponent(tag);
return component != null ? SerializerVersion.V1_19_4.toJson(component) : null; return component != null ? SerializerVersion.V1_19_4.toJson(component) : null;
} catch (final Exception e) { } catch (final Exception e) {
Via.getPlatform().getLogger().log(Level.SEVERE, "Error converting tag: " + tag, e); Via.getPlatform().getLogger().log(Level.SEVERE, "Error converting tag: " + tag, e);
@ -68,8 +67,8 @@ public final class ComponentUtil {
} }
try { try {
final ATextComponent component = TextComponentSerializer.V1_19_4.deserialize(element); final ATextComponent component = SerializerVersion.V1_19_4.toComponent(element);
return trimStrings(TextComponentCodec.V1_20_3.serializeNbt(component)); return trimStrings(SerializerVersion.V1_20_3.toTag(component));
} catch (final Exception e) { } catch (final Exception e) {
Via.getPlatform().getLogger().log(Level.SEVERE, "Error converting component: " + element, e); Via.getPlatform().getLogger().log(Level.SEVERE, "Error converting component: " + element, e);
return new StringTag("<error>"); return new StringTag("<error>");
@ -126,7 +125,7 @@ public final class ComponentUtil {
if (itemData) { if (itemData) {
component.setParentStyle(new Style().setItalic(false)); component.setParentStyle(new Style().setItalic(false));
} }
return TextComponentSerializer.V1_12.serialize(component); return SerializerVersion.V1_12.toString(component);
} }
public static String jsonToLegacy(final String value) { public static String jsonToLegacy(final String value) {
@ -134,11 +133,10 @@ public final class ComponentUtil {
} }
public static String jsonToLegacy(final JsonElement value) { public static String jsonToLegacy(final JsonElement value) {
return TextComponentSerializer.V1_12.deserialize(value).asLegacyFormatString(); return SerializerVersion.V1_12.toComponent(value).asLegacyFormatString();
} }
public static CompoundTag deserializeLegacyShowItem(final JsonElement element, final SerializerVersion version) { public static CompoundTag deserializeLegacyShowItem(final JsonElement element, final SerializerVersion version) {
final ATextComponent component = version.jsonSerializer.deserialize(element); return (CompoundTag) version.toTag(version.toComponent(element).asUnformattedString());
return TagUtil.fromSNBT(component.asUnformattedString(), version);
} }
} }

Datei anzeigen

@ -17,14 +17,18 @@
*/ */
package com.viaversion.viaversion.util; package com.viaversion.viaversion.util;
import com.github.steveice10.opennbt.tag.builtin.CompoundTag; import com.github.steveice10.opennbt.tag.builtin.Tag;
import com.google.gson.JsonElement; import com.google.gson.JsonElement;
import net.lenni0451.mcstructs.snbt.SNbtSerializer; 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.ATextComponent;
import net.lenni0451.mcstructs.text.serializer.TextComponentCodec; import net.lenni0451.mcstructs.text.serializer.TextComponentCodec;
import net.lenni0451.mcstructs.text.serializer.TextComponentSerializer; import net.lenni0451.mcstructs.text.serializer.TextComponentSerializer;
public enum SerializerVersion { public enum SerializerVersion {
V1_6(TextComponentSerializer.V1_6, null),
V1_7(TextComponentSerializer.V1_7, SNbtSerializer.V1_7),
V1_8(TextComponentSerializer.V1_8, SNbtSerializer.V1_8), V1_8(TextComponentSerializer.V1_8, SNbtSerializer.V1_8),
V1_9(TextComponentSerializer.V1_9, SNbtSerializer.V1_8), V1_9(TextComponentSerializer.V1_9, SNbtSerializer.V1_8),
V1_12(TextComponentSerializer.V1_12, SNbtSerializer.V1_12), V1_12(TextComponentSerializer.V1_12, SNbtSerializer.V1_12),
@ -38,19 +42,70 @@ public enum SerializerVersion {
V1_20_3(TextComponentCodec.V1_20_3, SNbtSerializer.V1_14); V1_20_3(TextComponentCodec.V1_20_3, SNbtSerializer.V1_14);
final TextComponentSerializer jsonSerializer; final TextComponentSerializer jsonSerializer;
final SNbtSerializer<CompoundTag> snbtSerializer; final SNbtSerializer<? extends Tag> snbtSerializer;
final TextComponentCodec codec;
SerializerVersion(final TextComponentSerializer jsonSerializer, final SNbtSerializer<CompoundTag> snbtSerializer) { SerializerVersion(final TextComponentSerializer jsonSerializer, final SNbtSerializer<? extends Tag> snbtSerializer) {
this.jsonSerializer = jsonSerializer; this.jsonSerializer = jsonSerializer;
this.snbtSerializer = snbtSerializer; this.snbtSerializer = snbtSerializer;
this.codec = null;
}
SerializerVersion(final TextComponentCodec codec, final SNbtSerializer<? extends Tag> snbtSerializer) {
this.codec = codec;
this.jsonSerializer = codec.asSerializer();
this.snbtSerializer = snbtSerializer;
} }
SerializerVersion(final TextComponentCodec codec, final SNbtSerializer<CompoundTag> snbtSerializer) { public String toString(final ATextComponent component) {
this.jsonSerializer = codec.asSerializer(); return jsonSerializer.serialize(component);
this.snbtSerializer = snbtSerializer;
} }
public JsonElement toJson(final ATextComponent component) { public JsonElement toJson(final ATextComponent component) {
return jsonSerializer.serializeJson(component); return jsonSerializer.serializeJson(component);
} }
public Tag toTag(final ATextComponent component) {
if (codec == null) {
throw new IllegalStateException("Cannot convert component to NBT with this version");
}
return codec.serializeNbt(component);
}
public ATextComponent toComponent(final JsonElement json) {
return jsonSerializer.deserialize(json);
}
public ATextComponent toComponent(final String json) {
return jsonSerializer.deserializeReader(json);
}
public ATextComponent toComponent(final Tag tag) {
if (codec == null) {
throw new IllegalStateException("Cannot convert NBT to component with this version");
}
return codec.deserializeNbtTree(tag);
}
public Tag toTag(final String snbt) {
if (snbtSerializer == null) {
throw new IllegalStateException("Cannot convert SNBT to NBT with this version");
}
try {
return snbtSerializer.deserialize(snbt);
} catch (SNbtDeserializeException e) {
throw new RuntimeException(e);
}
}
public String toSNBT(final Tag tag) {
if (snbtSerializer == null) {
throw new IllegalStateException("Cannot convert SNBT to NBT with this version");
}
try {
return snbtSerializer.serialize(tag);
} catch (SNbtSerializeException e) {
throw new RuntimeException(e);
}
}
} }

Datei anzeigen

@ -48,22 +48,6 @@ public final class TagUtil {
listTag.getValue().replaceAll(t -> (T) handleDeep(null, t, consumer)); listTag.getValue().replaceAll(t -> (T) handleDeep(null, t, consumer));
} }
public static String toSNBT(final Tag tag, final SerializerVersion version) {
try {
return version.snbtSerializer.serialize(tag);
} catch (final SNbtSerializeException e) {
throw new RuntimeException(e);
}
}
public static CompoundTag fromSNBT(final String snbt, final SerializerVersion version) {
try {
return version.snbtSerializer.deserialize(snbt);
} catch (final SNbtDeserializeException e) {
throw new RuntimeException(e);
}
}
@FunctionalInterface @FunctionalInterface
public interface TagUpdater { public interface TagUpdater {