Finish up things

This commit is contained in:
Nassim Jahnke 2023-12-27 17:30:47 +01:00
parent 96293e6e66
commit 5334f77d49
No known key found for this signature in database
GPG Key ID: EF6771C01F6EF02F
5 changed files with 11 additions and 38 deletions

View File

@ -17,15 +17,13 @@
*/ */
package com.viaversion.viabackwards.protocol.protocol1_15_2to1_16.chat; package com.viaversion.viabackwards.protocol.protocol1_15_2to1_16.chat;
import com.viaversion.viabackwards.ViaBackwards;
import com.viaversion.viabackwards.api.rewriters.TranslatableRewriter; import com.viaversion.viabackwards.api.rewriters.TranslatableRewriter;
import com.viaversion.viabackwards.protocol.protocol1_15_2to1_16.Protocol1_15_2To1_16; import com.viaversion.viabackwards.protocol.protocol1_15_2to1_16.Protocol1_15_2To1_16;
import com.viaversion.viaversion.api.Via;
import com.viaversion.viaversion.libs.gson.JsonElement; import com.viaversion.viaversion.libs.gson.JsonElement;
import com.viaversion.viaversion.libs.gson.JsonObject; import com.viaversion.viaversion.libs.gson.JsonObject;
import com.viaversion.viaversion.libs.gson.JsonParseException;
import com.viaversion.viaversion.libs.gson.JsonPrimitive; import com.viaversion.viaversion.libs.gson.JsonPrimitive;
import com.viaversion.viaversion.protocols.protocol1_16to1_15_2.ClientboundPackets1_16; import com.viaversion.viaversion.protocols.protocol1_16to1_15_2.ClientboundPackets1_16;
import com.viaversion.viaversion.util.ComponentUtil;
public class TranslatableRewriter1_16 extends TranslatableRewriter<ClientboundPackets1_16> { public class TranslatableRewriter1_16 extends TranslatableRewriter<ClientboundPackets1_16> {
@ -76,33 +74,8 @@ public class TranslatableRewriter1_16 extends TranslatableRewriter<ClientboundPa
} }
// show_text as chat component json, show_entity and show_item serialized as snbt // show_text as chat component json, show_entity and show_item serialized as snbt
// Let adventure handle all of that JsonObject convertedObject = (JsonObject) ComponentUtil.convertJson(object, ComponentUtil.SerializerVersion.V1_16, ComponentUtil.SerializerVersion.V1_15);
try { object.add("hoverEvent", convertedObject.getAsJsonObject("hoverEvent"));
Component component = ChatRewriter.HOVER_GSON_SERIALIZER.deserializeFromTree(object);
JsonObject convertedObject;
try {
convertedObject = (JsonObject) ChatRewriter.HOVER_GSON_SERIALIZER.serializeToTree(component);
} catch (JsonParseException e) {
JsonObject contents = hoverEvent.getAsJsonObject("contents");
if (contents.remove("tag") == null) {
throw e; // Just rethrow if this is not an item with a tag provided
}
// Most likely an invalid nbt tag - try again after its removal
component = ChatRewriter.HOVER_GSON_SERIALIZER.deserializeFromTree(object);
convertedObject = (JsonObject) ChatRewriter.HOVER_GSON_SERIALIZER.serializeToTree(component);
}
// Remove new format
JsonObject processedHoverEvent = convertedObject.getAsJsonObject("hoverEvent");
processedHoverEvent.remove("contents");
object.add("hoverEvent", processedHoverEvent);
} catch (Exception e) {
if (!Via.getConfig().isSuppressConversionWarnings()) {
ViaBackwards.getPlatform().getLogger().severe("Error converting hover event component: " + object);
e.printStackTrace();
}
}
} }
private String getClosestChatColor(int rgb) { private String getClosestChatColor(int rgb) {

View File

@ -345,7 +345,7 @@ public final class Protocol1_19_1To1_19_3 extends BackwardsProtocol<ClientboundP
} }
final JsonElement unsignedContent = wrapper.read(Type.OPTIONAL_COMPONENT); final JsonElement unsignedContent = wrapper.read(Type.OPTIONAL_COMPONENT);
final JsonElement content = unsignedContent != null ? unsignedContent : ComponentUtil.plainTextToJson(plainContent); final JsonElement content = unsignedContent != null ? unsignedContent : ComponentUtil.plainToJson(plainContent);
translatableRewriter.processText(content); translatableRewriter.processText(content);
final int filterMaskType = wrapper.read(Type.VAR_INT); final int filterMaskType = wrapper.read(Type.VAR_INT);
if (filterMaskType == 2) { if (filterMaskType == 2) {

View File

@ -156,7 +156,7 @@ public final class Protocol1_19To1_19_1 extends BackwardsProtocol<ClientboundPac
} }
if (message == null) { if (message == null) {
// If no decorated or unsigned message is given, use the plain one // If no decorated or unsigned message is given, use the plain one
message = ComponentUtil.plainTextToJson(plainMessage); message = ComponentUtil.plainToJson(plainMessage);
} }
final int filterMaskType = wrapper.read(Type.VAR_INT); final int filterMaskType = wrapper.read(Type.VAR_INT);

View File

@ -47,10 +47,10 @@ import com.viaversion.viaversion.protocols.protocol1_20_3to1_20_2.Protocol1_20_3
import com.viaversion.viaversion.protocols.protocol1_20_3to1_20_2.packet.ClientboundConfigurationPackets1_20_3; import com.viaversion.viaversion.protocols.protocol1_20_3to1_20_2.packet.ClientboundConfigurationPackets1_20_3;
import com.viaversion.viaversion.protocols.protocol1_20_3to1_20_2.packet.ClientboundPackets1_20_3; import com.viaversion.viaversion.protocols.protocol1_20_3to1_20_2.packet.ClientboundPackets1_20_3;
import com.viaversion.viaversion.protocols.protocol1_20_3to1_20_2.packet.ServerboundPackets1_20_3; import com.viaversion.viaversion.protocols.protocol1_20_3to1_20_2.packet.ServerboundPackets1_20_3;
import com.viaversion.viaversion.protocols.protocol1_20_3to1_20_2.util.ComponentConverter;
import com.viaversion.viaversion.rewriter.ComponentRewriter.ReadType; import com.viaversion.viaversion.rewriter.ComponentRewriter.ReadType;
import com.viaversion.viaversion.rewriter.StatisticsRewriter; import com.viaversion.viaversion.rewriter.StatisticsRewriter;
import com.viaversion.viaversion.rewriter.TagRewriter; import com.viaversion.viaversion.rewriter.TagRewriter;
import com.viaversion.viaversion.util.ComponentUtil;
import java.util.BitSet; import java.util.BitSet;
import java.util.UUID; import java.util.UUID;
@ -368,13 +368,13 @@ public final class Protocol1_20_2To1_20_3 extends BackwardsProtocol<ClientboundP
private void convertComponent(final PacketWrapper wrapper) throws Exception { private void convertComponent(final PacketWrapper wrapper) throws Exception {
final Tag tag = wrapper.read(Type.TAG); final Tag tag = wrapper.read(Type.TAG);
translatableRewriter.processTag(tag); translatableRewriter.processTag(tag);
wrapper.write(Type.COMPONENT, ComponentConverter.tagComponentToJson(tag)); wrapper.write(Type.COMPONENT, ComponentUtil.tagToJson(tag));
} }
private void convertOptionalComponent(final PacketWrapper wrapper) throws Exception { private void convertOptionalComponent(final PacketWrapper wrapper) throws Exception {
final Tag tag = wrapper.read(Type.OPTIONAL_TAG); final Tag tag = wrapper.read(Type.OPTIONAL_TAG);
translatableRewriter.processTag(tag); translatableRewriter.processTag(tag);
wrapper.write(Type.OPTIONAL_COMPONENT, ComponentConverter.tagComponentToJson(tag)); wrapper.write(Type.OPTIONAL_COMPONENT, ComponentUtil.tagToJson(tag));
} }
@Override @Override

View File

@ -33,7 +33,7 @@ import com.viaversion.viaversion.api.type.types.version.Types1_20_2;
import com.viaversion.viaversion.api.type.types.version.Types1_20_3; import com.viaversion.viaversion.api.type.types.version.Types1_20_3;
import com.viaversion.viaversion.protocols.protocol1_20_2to1_20.packet.ClientboundConfigurationPackets1_20_2; import com.viaversion.viaversion.protocols.protocol1_20_2to1_20.packet.ClientboundConfigurationPackets1_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.packet.ClientboundPackets1_20_3;
import com.viaversion.viaversion.protocols.protocol1_20_3to1_20_2.util.ComponentConverter; import com.viaversion.viaversion.util.ComponentUtil;
public final class EntityPacketRewriter1_20_3 extends EntityRewriter<ClientboundPackets1_20_3, Protocol1_20_2To1_20_3> { public final class EntityPacketRewriter1_20_3 extends EntityRewriter<ClientboundPackets1_20_3, Protocol1_20_2To1_20_3> {
@ -99,10 +99,10 @@ public final class EntityPacketRewriter1_20_3 extends EntityRewriter<Clientbound
filter().handler((event, meta) -> { filter().handler((event, meta) -> {
final MetaType type = meta.metaType(); final MetaType type = meta.metaType();
if (type == Types1_20_3.META_TYPES.componentType) { if (type == Types1_20_3.META_TYPES.componentType) {
meta.setTypeAndValue(Types1_20_2.META_TYPES.componentType, ComponentConverter.tagComponentToJson(meta.value())); meta.setTypeAndValue(Types1_20_2.META_TYPES.componentType, ComponentUtil.tagToJson(meta.value()));
return; return;
} else if (type == Types1_20_3.META_TYPES.optionalComponentType) { } else if (type == Types1_20_3.META_TYPES.optionalComponentType) {
meta.setTypeAndValue(Types1_20_2.META_TYPES.optionalComponentType, ComponentConverter.tagComponentToJson(meta.value())); meta.setTypeAndValue(Types1_20_2.META_TYPES.optionalComponentType, ComponentUtil.tagToJson(meta.value()));
return; return;
} else if (type == Types1_20_3.META_TYPES.particleType) { } else if (type == Types1_20_3.META_TYPES.particleType) {
final Particle particle = (Particle) meta.getValue(); final Particle particle = (Particle) meta.getValue();