Skip to content

Commit

Permalink
Finish up things
Browse files Browse the repository at this point in the history
  • Loading branch information
kennytv committed Dec 27, 2023
1 parent 96293e6 commit 5334f77
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,13 @@
*/
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.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.JsonObject;
import com.viaversion.viaversion.libs.gson.JsonParseException;
import com.viaversion.viaversion.libs.gson.JsonPrimitive;
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> {

Expand Down Expand Up @@ -76,33 +74,8 @@ public void processText(JsonElement value) {
}

// show_text as chat component json, show_entity and show_item serialized as snbt
// Let adventure handle all of that
try {
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();
}
}
JsonObject convertedObject = (JsonObject) ComponentUtil.convertJson(object, ComponentUtil.SerializerVersion.V1_16, ComponentUtil.SerializerVersion.V1_15);
object.add("hoverEvent", convertedObject.getAsJsonObject("hoverEvent"));
}

private String getClosestChatColor(int rgb) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ public void register() {
}

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);
final int filterMaskType = wrapper.read(Type.VAR_INT);
if (filterMaskType == 2) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public void register() {
}
if (message == null) {
// 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@
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.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.StatisticsRewriter;
import com.viaversion.viaversion.rewriter.TagRewriter;
import com.viaversion.viaversion.util.ComponentUtil;
import java.util.BitSet;
import java.util.UUID;

Expand Down Expand Up @@ -368,13 +368,13 @@ private PacketHandler resourcePackHandler() {
private void convertComponent(final PacketWrapper wrapper) throws Exception {
final Tag tag = wrapper.read(Type.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 {
final Tag tag = wrapper.read(Type.OPTIONAL_TAG);
translatableRewriter.processTag(tag);
wrapper.write(Type.OPTIONAL_COMPONENT, ComponentConverter.tagComponentToJson(tag));
wrapper.write(Type.OPTIONAL_COMPONENT, ComponentUtil.tagToJson(tag));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
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_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> {

Expand Down Expand Up @@ -99,10 +99,10 @@ protected void registerRewrites() {
filter().handler((event, meta) -> {
final MetaType type = meta.metaType();
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;
} 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;
} else if (type == Types1_20_3.META_TYPES.particleType) {
final Particle particle = (Particle) meta.getValue();
Expand Down

0 comments on commit 5334f77

Please sign in to comment.