Use 1.12 serializer for json to legacy conversion

This commit is contained in:
Nassim Jahnke 2023-12-27 11:40:45 +01:00
parent 984c625776
commit dc2986600c
No known key found for this signature in database
GPG Key ID: EF6771C01F6EF02F
7 changed files with 79 additions and 67 deletions

View File

@ -330,7 +330,13 @@ public final class Protocol1_19_1To1_19 extends AbstractProtocol<ClientboundPack
connection.put(new ChatTypeStorage());
}
public static @Nullable ChatDecorationResult decorateChatMessage(final CompoundTag chatType, final int chatTypeId, final JsonElement senderName, @Nullable final JsonElement teamName, final JsonElement message) {
public static @Nullable ChatDecorationResult decorateChatMessage(
final CompoundTag chatType,
final int chatTypeId,
final JsonElement senderName,
@Nullable final JsonElement teamName,
final JsonElement message
) {
if (chatType == null) {
Via.getPlatform().getLogger().warning("Chat message has unknown chat type id " + chatTypeId + ". Message: " + message);
return null;
@ -356,7 +362,12 @@ public final class Protocol1_19_1To1_19 extends AbstractProtocol<ClientboundPack
return new ChatDecorationResult(translatabaleComponentFromTag(decoration, senderName, teamName, message), overlay);
}
public static JsonElement translatabaleComponentFromTag(final CompoundTag tag, final JsonElement senderName, @Nullable final JsonElement targetName, final JsonElement message) {
public static JsonElement translatabaleComponentFromTag(
final CompoundTag tag,
final JsonElement senderName,
@Nullable final JsonElement targetName,
final JsonElement message
) {
final String translationKey = (String) tag.get("translation_key").getValue();
final Style style = new Style();
@ -415,11 +426,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.LATEST.deserialize(argument));
arguments.add(TextComponentSerializer.V1_18.deserialize(argument));
}
}
}
return TextComponentSerializer.LATEST.serializeJson(new TranslationComponent(translationKey, arguments));
return TextComponentSerializer.V1_18.serializeJson(new TranslationComponent(translationKey, arguments));
}
}

View File

@ -198,7 +198,7 @@ public final class Protocol1_19_3To1_19_1 extends AbstractProtocol<ClientboundPa
decoratedMessage = unsignedMessage;
}
if (decoratedMessage == null) {
decoratedMessage = ComponentUtil.plainTextToJson(plainMessage);
decoratedMessage = ComponentUtil.plainToJson(plainMessage);
}
final int filterMaskType = wrapper.read(Type.VAR_INT);

View File

@ -42,10 +42,10 @@ import com.viaversion.viaversion.protocols.protocol1_20_3to1_20_2.packet.Clientb
import com.viaversion.viaversion.protocols.protocol1_20_3to1_20_2.packet.ServerboundPackets1_20_3;
import com.viaversion.viaversion.protocols.protocol1_20_3to1_20_2.rewriter.BlockItemPacketRewriter1_20_3;
import com.viaversion.viaversion.protocols.protocol1_20_3to1_20_2.rewriter.EntityPacketRewriter1_20_3;
import com.viaversion.viaversion.protocols.protocol1_20_3to1_20_2.util.ComponentConverter;
import com.viaversion.viaversion.rewriter.SoundRewriter;
import com.viaversion.viaversion.rewriter.StatisticsRewriter;
import com.viaversion.viaversion.rewriter.TagRewriter;
import com.viaversion.viaversion.util.ComponentUtil;
import java.nio.charset.StandardCharsets;
import java.util.BitSet;
import java.util.UUID;
@ -336,11 +336,11 @@ public final class Protocol1_20_3To1_20_2 extends AbstractProtocol<ClientboundPa
}
private void convertComponent(final PacketWrapper wrapper) throws Exception {
wrapper.write(Type.TAG, ComponentConverter.jsonComponentToTag(wrapper.read(Type.COMPONENT)));
wrapper.write(Type.TAG, ComponentUtil.jsonToTag(wrapper.read(Type.COMPONENT)));
}
private void convertOptionalComponent(final PacketWrapper wrapper) throws Exception {
wrapper.write(Type.OPTIONAL_TAG, ComponentConverter.jsonComponentToTag(wrapper.read(Type.OPTIONAL_COMPONENT)));
wrapper.write(Type.OPTIONAL_TAG, ComponentUtil.jsonToTag(wrapper.read(Type.OPTIONAL_COMPONENT)));
}
@Override

View File

@ -32,8 +32,8 @@ import com.viaversion.viaversion.protocols.protocol1_20_2to1_20.packet.Clientbou
import com.viaversion.viaversion.protocols.protocol1_20_2to1_20.packet.ClientboundPackets1_20_2;
import com.viaversion.viaversion.protocols.protocol1_20_3to1_20_2.Protocol1_20_3To1_20_2;
import com.viaversion.viaversion.protocols.protocol1_20_3to1_20_2.packet.ClientboundPackets1_20_3;
import com.viaversion.viaversion.protocols.protocol1_20_3to1_20_2.util.ComponentConverter;
import com.viaversion.viaversion.rewriter.EntityRewriter;
import com.viaversion.viaversion.util.ComponentUtil;
import com.viaversion.viaversion.util.Key;
public final class EntityPacketRewriter1_20_3 extends EntityRewriter<ClientboundPackets1_20_2, Protocol1_20_3To1_20_2> {
@ -103,9 +103,9 @@ public final class EntityPacketRewriter1_20_3 extends EntityRewriter<Clientbound
filter().handler((event, meta) -> {
final MetaType type = meta.metaType();
if (type == Types1_20_2.META_TYPES.componentType) {
meta.setTypeAndValue(Types1_20_3.META_TYPES.componentType, ComponentConverter.jsonComponentToTag(meta.value()));
meta.setTypeAndValue(Types1_20_3.META_TYPES.componentType, ComponentUtil.jsonToTag(meta.value()));
} else if (type == Types1_20_2.META_TYPES.optionalComponentType) {
meta.setTypeAndValue(Types1_20_3.META_TYPES.optionalComponentType, ComponentConverter.jsonComponentToTag(meta.value()));
meta.setTypeAndValue(Types1_20_3.META_TYPES.optionalComponentType, ComponentUtil.jsonToTag(meta.value()));
} else if (type == Types1_20_2.META_TYPES.particleType) {
final Particle particle = (Particle) meta.getValue();
final ParticleMappings particleMappings = protocol.getMappingData().getParticleMappings();

View File

@ -1,49 +0,0 @@
/*
* This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion
* Copyright (C) 2023 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_20_3to1_20_2.util;
import com.github.steveice10.opennbt.tag.builtin.Tag;
import com.google.gson.JsonElement;
import com.viaversion.viaversion.api.Via;
import net.lenni0451.mcstructs.text.ATextComponent;
import net.lenni0451.mcstructs.text.serializer.TextComponentCodec;
import net.lenni0451.mcstructs.text.serializer.TextComponentSerializer;
import org.checkerframework.checker.nullness.qual.Nullable;
public final class ComponentConverter {
private static final boolean DEBUG = Boolean.getBoolean("viaversion.debug.components");
public static @Nullable JsonElement tagComponentToJson(@Nullable final Tag tag) {
if (DEBUG) {
Via.getPlatform().getLogger().info("Converting tag to json: " + tag);
}
final ATextComponent component = TextComponentCodec.V1_20_3.deserializeNbtTree(NBTConverter.viaToMcStructs(tag));
return component != null ? TextComponentSerializer.V1_19_4.serializeJson(component) : null;
}
public static @Nullable Tag jsonComponentToTag(@Nullable final JsonElement element) {
if (DEBUG) {
Via.getPlatform().getLogger().info("Converting json to tag: " + element);
}
final ATextComponent component = TextComponentSerializer.V1_19_4.deserialize(element);
return component != null ? NBTConverter.mcStructsToVia(TextComponentCodec.V1_20_3.serializeNbt(component)) : null;
}
}

View File

@ -17,29 +17,45 @@
*/
package com.viaversion.viaversion.util;
import com.github.steveice10.opennbt.tag.builtin.Tag;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import net.lenni0451.mcstructs.text.ATextComponent;
import net.lenni0451.mcstructs.text.Style;
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;
/**
* Component conversion utility, trying to divert most calls to the component library to this class instead for easy replacement.
*/
public final class ComponentUtil {
public static JsonObject emptyJsonComponent() {
return plainTextToJson("");
return plainToJson("");
}
public static String emptyJsonComponentString() {
return "{\"text\":\"\"}";
}
public static JsonObject plainTextToJson(final String message) {
public static JsonObject plainToJson(final String message) {
final JsonObject object = new JsonObject();
object.addProperty("text", message);
return object;
}
public static @Nullable JsonElement tagToJson(@Nullable final Tag tag) {
final ATextComponent component = TextComponentCodec.V1_20_3.deserializeNbtTree(NBTConverter.viaToMcStructs(tag));
return component != null ? TextComponentSerializer.V1_19_4.serializeJson(component) : null;
}
public static @Nullable Tag jsonToTag(@Nullable final JsonElement element) {
final ATextComponent component = TextComponentSerializer.V1_19_4.deserialize(element);
return component != null ? NBTConverter.mcStructsToVia(TextComponentCodec.V1_20_3.serializeNbt(component)) : null;
}
public static JsonElement legacyToJson(final String message) {
return TextComponentSerializer.V1_12.serializeJson(LegacyStringDeserializer.parse(message, true));
}
@ -57,10 +73,44 @@ public final class ComponentUtil {
}
public static String jsonToLegacy(final String value) {
return TextComponentSerializer.LATEST.deserialize(value).asLegacyFormatString();
return TextComponentSerializer.V1_12.deserialize(value).asLegacyFormatString();
}
public static String jsonToLegacy(final JsonElement value) {
return TextComponentSerializer.LATEST.deserialize(value).asLegacyFormatString();
return TextComponentSerializer.V1_12.deserialize(value).asLegacyFormatString();
}
public enum SerializerVersion {
V1_8(TextComponentSerializer.V1_8),
V1_9(TextComponentSerializer.V1_9),
V1_12(TextComponentSerializer.V1_12),
V1_14(TextComponentSerializer.V1_14),
V1_15(TextComponentSerializer.V1_15),
V1_16(TextComponentSerializer.V1_16),
V1_17(TextComponentSerializer.V1_17),
V1_18(TextComponentSerializer.V1_18),
V1_19_4(TextComponentSerializer.V1_19_4),
V1_20_3(TextComponentCodec.V1_20_3);
private final TextComponentSerializer serializer;
private final TextComponentCodec codec;
SerializerVersion(final TextComponentSerializer serializer) {
this.serializer = serializer;
this.codec = null;
}
SerializerVersion(final TextComponentCodec codec) {
this.serializer = codec.asSerializer();
this.codec = codec;
}
public TextComponentSerializer serializer() {
return serializer;
}
public @Nullable TextComponentCodec codec() {
return codec;
}
}
}

View File

@ -15,7 +15,7 @@
* 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_20_3to1_20_2.util;
package com.viaversion.viaversion.util;
import com.github.steveice10.opennbt.tag.builtin.ByteArrayTag;
import com.github.steveice10.opennbt.tag.builtin.ByteTag;
@ -37,7 +37,7 @@ import org.checkerframework.checker.nullness.qual.Nullable;
final class NBTConverter {
public static @Nullable Tag mcStructsToVia(@Nullable final INbtTag nbtTag) {
static @Nullable Tag mcStructsToVia(@Nullable final INbtTag nbtTag) {
if (nbtTag == null) {
return null;
} else if (nbtTag.isByteTag()) {
@ -78,7 +78,7 @@ final class NBTConverter {
}
}
public static @Nullable INbtTag viaToMcStructs(@Nullable final Tag tag) {
static @Nullable INbtTag viaToMcStructs(@Nullable final Tag tag) {
if (tag == null) {
return null;
} else if (tag instanceof ByteTag) {