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

This commit is contained in:
EnZaXD 2024-04-01 15:52:57 +02:00 committed by GitHub
parent 307414eb51
commit cd65925d6b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 84 additions and 48 deletions

View File

@ -23,7 +23,6 @@ import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.viaversion.viaversion.util.ComponentUtil;
import com.viaversion.viaversion.util.SerializerVersion;
import com.viaversion.viaversion.util.TagUtil;
public final class ChatItemRewriter {
@ -45,7 +44,7 @@ public final class ChatItemRewriter {
if (type.equals("show_item")) {
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")) {
toClient(obj.get("extra"));

View File

@ -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.util.ComponentUtil;
import com.viaversion.viaversion.util.SerializerVersion;
import com.viaversion.viaversion.util.TagUtil;
import java.util.logging.Level;
public class ComponentRewriter1_13<C extends ClientboundPacketType> extends ComponentRewriter<C> {
@ -60,12 +59,13 @@ public class ComponentRewriter1_13<C extends ClientboundPacketType> extends Comp
return;
}
CompoundTag itemTag = tag.getCompoundTag("tag");
NumberTag damageTag = tag.getNumberTag("Damage");
final CompoundTag itemTag = tag.getCompoundTag("tag");
final NumberTag damageTag = tag.getNumberTag("Damage");
// Call item converter
short damage = damageTag != null ? damageTag.asShort() : 0;
Item item = new DataItem();
final short damage = damageTag != null ? damageTag.asShort() : 0;
final Item item = new DataItem();
item.setData(damage);
item.setTag(itemTag);
protocol.getItemRewriter().handleItemToClient(item);
@ -78,16 +78,16 @@ public class ComponentRewriter1_13<C extends ClientboundPacketType> extends Comp
tag.put("tag", itemTag);
}
JsonArray array = new JsonArray();
JsonObject object = new JsonObject();
array.add(object);
String serializedNBT;
final JsonArray newValue = new JsonArray();
final JsonObject showItem = new JsonObject();
newValue.add(showItem);
try {
serializedNBT = TagUtil.toSNBT(tag, SerializerVersion.V1_13);
object.addProperty("text", serializedNBT);
hoverEvent.add("value", array);
showItem.addProperty("text", SerializerVersion.V1_13.toSNBT(tag));
hoverEvent.add("value", newValue);
} 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);
}
}
}

View File

@ -49,11 +49,11 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import com.viaversion.viaversion.util.SerializerVersion;
import net.lenni0451.mcstructs.core.TextFormatting;
import net.lenni0451.mcstructs.text.ATextComponent;
import net.lenni0451.mcstructs.text.Style;
import net.lenni0451.mcstructs.text.components.TranslationComponent;
import net.lenni0451.mcstructs.text.serializer.TextComponentSerializer;
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> {
@ -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());
}
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));
}
}

View File

@ -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.impl.TextHoverEvent;
import net.lenni0451.mcstructs.text.serializer.LegacyStringDeserializer;
import net.lenni0451.mcstructs.text.serializer.TextComponentCodec;
import net.lenni0451.mcstructs.text.serializer.TextComponentSerializer;
import org.checkerframework.checker.nullness.qual.Nullable;
@ -54,7 +53,7 @@ public final class ComponentUtil {
public static @Nullable JsonElement tagToJson(@Nullable final Tag tag) {
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;
} catch (final Exception e) {
Via.getPlatform().getLogger().log(Level.SEVERE, "Error converting tag: " + tag, e);
@ -68,8 +67,8 @@ public final class ComponentUtil {
}
try {
final ATextComponent component = TextComponentSerializer.V1_19_4.deserialize(element);
return trimStrings(TextComponentCodec.V1_20_3.serializeNbt(component));
final ATextComponent component = SerializerVersion.V1_19_4.toComponent(element);
return trimStrings(SerializerVersion.V1_20_3.toTag(component));
} catch (final Exception e) {
Via.getPlatform().getLogger().log(Level.SEVERE, "Error converting component: " + element, e);
return new StringTag("<error>");
@ -126,7 +125,7 @@ public final class ComponentUtil {
if (itemData) {
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) {
@ -134,11 +133,10 @@ public final class ComponentUtil {
}
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) {
final ATextComponent component = version.jsonSerializer.deserialize(element);
return TagUtil.fromSNBT(component.asUnformattedString(), version);
return (CompoundTag) version.toTag(version.toComponent(element).asUnformattedString());
}
}

View File

@ -17,14 +17,18 @@
*/
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 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.TextComponentCodec;
import net.lenni0451.mcstructs.text.serializer.TextComponentSerializer;
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_9(TextComponentSerializer.V1_9, SNbtSerializer.V1_8),
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);
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.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) {
this.jsonSerializer = codec.asSerializer();
this.snbtSerializer = snbtSerializer;
public String toString(final ATextComponent component) {
return jsonSerializer.serialize(component);
}
public JsonElement toJson(final ATextComponent 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);
}
}
}

View File

@ -48,22 +48,6 @@ public final class TagUtil {
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
public interface TagUpdater {