Replace string->component conversion in 1.8->1.9 (#3794)

This commit is contained in:
EnZaXD 2024-04-21 16:00:30 +02:00 committed by GitHub
parent ce2ad7d5eb
commit 1ed22327f6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
16 changed files with 52 additions and 77 deletions

View File

@ -233,13 +233,6 @@ public interface ViaVersionConfig extends Config {
*/
boolean isChunkBorderFix();
/**
* Force json transform
*
* @return true if enabled
*/
boolean isForceJsonTransform();
/**
* Should we fix nbt array's in json chat messages for 1.12 clients
*

View File

@ -63,7 +63,7 @@ public class DeathListener extends ViaBukkitListener {
wrapper.write(Type.VAR_INT, 2); // Event - Entity dead
wrapper.write(Type.VAR_INT, p.getEntityId()); // Player ID
wrapper.write(Type.INT, p.getEntityId()); // Entity ID
Protocol1_9To1_8.FIX_JSON.write(wrapper, msg); // Message
Protocol1_9To1_8.STRING_TO_JSON.write(wrapper, msg); // Message
wrapper.scheduleSend(Protocol1_9To1_8.class);
} catch (Exception e) {

View File

@ -63,7 +63,6 @@ public abstract class AbstractViaConfig extends Config implements ViaVersionConf
private int pistonReplacementId;
private boolean chunkBorderFix;
private boolean autoTeam;
private boolean forceJsonTransform;
private boolean nbtArrayFix;
private BlockedProtocolVersions blockedProtocolVersions;
private String blockedDisconnectMessage;
@ -129,7 +128,6 @@ public abstract class AbstractViaConfig extends Config implements ViaVersionConf
pistonReplacementId = getInt("replacement-piston-id", 0);
chunkBorderFix = getBoolean("chunk-border-fix", false);
autoTeam = getBoolean("auto-team", true);
forceJsonTransform = getBoolean("force-json-transform", false);
nbtArrayFix = getBoolean("chat-nbt-fix", true);
blockedProtocolVersions = loadBlockedProtocolVersions();
blockedDisconnectMessage = getString("block-disconnect-msg", "You are using an unsupported Minecraft version!");
@ -371,11 +369,6 @@ public abstract class AbstractViaConfig extends Config implements ViaVersionConf
return preventCollision && autoTeam;
}
@Override
public boolean isForceJsonTransform() {
return forceJsonTransform;
}
@Override
public boolean is1_12NBTArrayFix() {
return nbtArrayFix;

View File

@ -29,6 +29,7 @@ import com.viaversion.viaversion.api.protocol.packet.PacketWrapper;
import com.viaversion.viaversion.api.type.Type;
import com.viaversion.viaversion.protocols.protocol1_9to1_8.ClientboundPackets1_9;
import com.viaversion.viaversion.protocols.protocol1_9to1_8.Protocol1_9To1_8;
import com.viaversion.viaversion.util.ComponentUtil;
import java.util.ArrayList;
import java.util.Collections;
import java.util.EnumSet;
@ -245,7 +246,7 @@ public class CommonBoss implements BossBar {
wrapper.write(Type.VAR_INT, action.getId());
switch (action) {
case ADD:
Protocol1_9To1_8.FIX_JSON.write(wrapper, title);
wrapper.write(Type.COMPONENT, ComponentUtil.plainToJson(title));
wrapper.write(Type.FLOAT, health);
wrapper.write(Type.VAR_INT, color.getId());
wrapper.write(Type.VAR_INT, style.getId());
@ -257,7 +258,7 @@ public class CommonBoss implements BossBar {
wrapper.write(Type.FLOAT, health);
break;
case UPDATE_TITLE:
Protocol1_9To1_8.FIX_JSON.write(wrapper, title);
wrapper.write(Type.COMPONENT, ComponentUtil.plainToJson(title));
break;
case UPDATE_STYLE:
wrapper.write(Type.VAR_INT, color.getId());

View File

@ -37,8 +37,8 @@ import com.viaversion.viaversion.protocol.ServerProtocolVersionSingleton;
import com.viaversion.viaversion.protocols.base.packet.BaseClientboundPacket;
import com.viaversion.viaversion.protocols.base.packet.BasePacketTypesProvider;
import com.viaversion.viaversion.protocols.base.packet.BaseServerboundPacket;
import com.viaversion.viaversion.protocols.protocol1_9to1_8.Protocol1_9To1_8;
import com.viaversion.viaversion.util.ChatColorUtil;
import com.viaversion.viaversion.util.ComponentUtil;
import com.viaversion.viaversion.util.GsonUtil;
import io.netty.channel.ChannelFuture;
import java.util.List;
@ -163,8 +163,10 @@ public class BaseProtocol1_7 extends AbstractProtocol<BaseClientboundPacket, Bas
if (!wrapper.user().getChannel().isOpen()) return;
if (!wrapper.user().shouldApplyBlockProtocol()) return;
final String disconnectMessage = ChatColorUtil.translateAlternateColorCodes(Via.getConfig().getBlockedDisconnectMsg());
PacketWrapper disconnectPacket = PacketWrapper.create(ClientboundLoginPackets.LOGIN_DISCONNECT, wrapper.user()); // Disconnect Packet
Protocol1_9To1_8.FIX_JSON.write(disconnectPacket, ChatColorUtil.translateAlternateColorCodes(Via.getConfig().getBlockedDisconnectMsg()));
wrapper.write(Type.COMPONENT, ComponentUtil.plainToJson(disconnectMessage));
wrapper.cancel(); // cancel current
// Send and close

View File

@ -22,7 +22,6 @@ import com.github.steveice10.opennbt.tag.builtin.IntArrayTag;
import com.github.steveice10.opennbt.tag.builtin.ListTag;
import com.github.steveice10.opennbt.tag.builtin.NumberTag;
import com.github.steveice10.opennbt.tag.builtin.StringTag;
import com.github.steveice10.opennbt.tag.builtin.Tag;
import com.viaversion.viaversion.api.minecraft.item.Item;
import com.viaversion.viaversion.api.protocol.packet.PacketWrapper;
import com.viaversion.viaversion.api.protocol.remapper.PacketHandler;

View File

@ -283,7 +283,7 @@ public class ItemRewriter {
for (int i = 0; i < pages.size(); i++) {
final StringTag page = pages.get(i);
page.setValue(ComponentUtil.convertJson(page.getValue(), SerializerVersion.V1_8, SerializerVersion.V1_9).toString());
page.setValue(ComponentUtil.convertJsonOrEmpty(page.getValue(), SerializerVersion.V1_8, SerializerVersion.V1_9).toString());
}
item.setTag(tag);
}

View File

@ -18,7 +18,6 @@
package com.viaversion.viaversion.protocols.protocol1_9to1_8;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.viaversion.viaversion.api.Via;
import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.minecraft.ClientWorld;
@ -49,13 +48,15 @@ import com.viaversion.viaversion.protocols.protocol1_9to1_8.storage.CommandBlock
import com.viaversion.viaversion.protocols.protocol1_9to1_8.storage.EntityTracker1_9;
import com.viaversion.viaversion.protocols.protocol1_9to1_8.storage.InventoryTracker;
import com.viaversion.viaversion.protocols.protocol1_9to1_8.storage.MovementTracker;
import com.viaversion.viaversion.util.GsonUtil;
import com.viaversion.viaversion.util.ComponentUtil;
import com.viaversion.viaversion.util.SerializerVersion;
public class Protocol1_9To1_8 extends AbstractProtocol<ClientboundPackets1_8, ClientboundPackets1_9, ServerboundPackets1_8, ServerboundPackets1_9> {
public static final ValueTransformer<String, JsonElement> FIX_JSON = new ValueTransformer<String, JsonElement>(Type.COMPONENT) {
public static final ValueTransformer<String, JsonElement> STRING_TO_JSON = new ValueTransformer<String, JsonElement>(Type.COMPONENT) {
@Override
public JsonElement transform(PacketWrapper wrapper, String line) {
return fixJson(line);
return ComponentUtil.convertJsonOrEmpty(line, SerializerVersion.V1_8, SerializerVersion.V1_9);
}
};
private final MetadataRewriter1_9To1_8 metadataRewriter = new MetadataRewriter1_9To1_8(this);
@ -64,35 +65,6 @@ public class Protocol1_9To1_8 extends AbstractProtocol<ClientboundPackets1_8, Cl
super(ClientboundPackets1_8.class, ClientboundPackets1_9.class, ServerboundPackets1_8.class, ServerboundPackets1_9.class);
}
public static JsonElement fixJson(String line) {
if (line == null || line.equalsIgnoreCase("null")) {
line = "{\"text\":\"\"}";
} else {
if ((!line.startsWith("\"") || !line.endsWith("\"")) && (!line.startsWith("{") || !line.endsWith("}"))) {
return constructJson(line);
}
if (line.startsWith("\"") && line.endsWith("\"")) {
line = "{\"text\":" + line + "}";
}
}
try {
return GsonUtil.getGson().fromJson(line, JsonObject.class);
} catch (Exception e) {
if (Via.getConfig().isForceJsonTransform()) {
return constructJson(line);
} else {
Via.getPlatform().getLogger().warning("Invalid JSON String: \"" + line + "\" Please report this issue to the ViaVersion Github: " + e.getMessage());
return GsonUtil.getGson().fromJson("{\"text\":\"\"}", JsonObject.class);
}
}
}
private static JsonElement constructJson(String text) {
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("text", text);
return jsonObject;
}
public static Item getHandItem(final UserConnection info) {
return Via.getManager().getProviders().get(HandItemProvider.class).getHandItem(info);
}
@ -121,7 +93,7 @@ public class Protocol1_9To1_8 extends AbstractProtocol<ClientboundPackets1_8, Cl
return;
}
wrapper.write(Type.COMPONENT, fixJson(wrapper.read(Type.STRING)));
STRING_TO_JSON.write(wrapper, wrapper.read(Type.STRING));
});
// Other Handlers

View File

@ -159,7 +159,7 @@ public enum MetaIndex {
ABSTRACT_MINECART_BLOCK_Y(MINECART_ABSTRACT, 21, MetaType1_8.Int, 9, MetaType1_9.VarInt),
ABSTRACT_MINECART_SHOW_BLOCK(MINECART_ABSTRACT, 22, MetaType1_8.Byte, 10, MetaType1_9.Boolean),
// Command minecart (they are still broken)
// Command minecart
MINECART_COMMAND_BLOCK_COMMAND(MINECART_ABSTRACT, 23, MetaType1_8.String, 11, MetaType1_9.String),
MINECART_COMMAND_BLOCK_OUTPUT(MINECART_ABSTRACT, 24, MetaType1_8.String, 12, MetaType1_9.Chat),

View File

@ -30,6 +30,8 @@ import com.viaversion.viaversion.protocols.protocol1_9to1_8.ItemRewriter;
import com.viaversion.viaversion.protocols.protocol1_9to1_8.Protocol1_9To1_8;
import com.viaversion.viaversion.rewriter.EntityRewriter;
import com.viaversion.viaversion.rewriter.meta.MetaHandlerEvent;
import com.viaversion.viaversion.util.ComponentUtil;
import com.viaversion.viaversion.util.SerializerVersion;
import java.util.UUID;
public class MetadataRewriter1_9To1_8 extends EntityRewriter<ClientboundPackets1_8, Protocol1_9To1_8> {
@ -127,8 +129,9 @@ public class MetadataRewriter1_9To1_8 extends EntityRewriter<ClientboundPackets1
metadata.setValue(angle);
break;
case Chat:
value = Protocol1_9To1_8.fixJson(value.toString());
metadata.setValue(value);
// Was previously also a component, so just convert it
String text = (String) value;
metadata.setValue(ComponentUtil.convertJsonOrEmpty(text, SerializerVersion.V1_8, SerializerVersion.V1_9));
break;
case BlockID:
// Convert from int, short, byte

View File

@ -253,7 +253,7 @@ public class EntityPackets {
if (wrapper.get(Type.VAR_INT, 0) == 2) { // entity dead
wrapper.passthrough(Type.VAR_INT); //Player id
wrapper.passthrough(Type.INT); //Entity id
Protocol1_9To1_8.FIX_JSON.write(wrapper, wrapper.read(Type.STRING));
Protocol1_9To1_8.STRING_TO_JSON.write(wrapper, wrapper.read(Type.STRING));
}
});
}

View File

@ -69,7 +69,7 @@ public class InventoryPackets {
public void register() {
map(Type.UNSIGNED_BYTE); // 0 - Window ID
map(Type.STRING); // 1 - Window Type
map(Type.STRING, Protocol1_9To1_8.FIX_JSON); // 2 - Window Title
map(Type.STRING, Protocol1_9To1_8.STRING_TO_JSON); // 2 - Window Title
map(Type.UNSIGNED_BYTE); // 3 - Slot Count
// There is a horse parameter after this, we don't handle it and let it passthrough
// Inventory tracking

View File

@ -37,6 +37,7 @@ import com.viaversion.viaversion.protocols.protocol1_9to1_8.providers.MainHandPr
import com.viaversion.viaversion.protocols.protocol1_9to1_8.storage.ClientChunks;
import com.viaversion.viaversion.protocols.protocol1_9to1_8.storage.EntityTracker1_9;
import com.viaversion.viaversion.protocols.protocol1_9to1_8.storage.MovementTracker;
import com.viaversion.viaversion.util.ComponentUtil;
import java.util.logging.Level;
public class PlayerPackets {
@ -44,7 +45,7 @@ public class PlayerPackets {
protocol.registerClientbound(ClientboundPackets1_8.CHAT_MESSAGE, new PacketHandlers() {
@Override
public void register() {
map(Type.STRING, Protocol1_9To1_8.FIX_JSON); // 0 - Chat Message (json)
map(Type.STRING, Protocol1_9To1_8.STRING_TO_JSON); // 0 - Chat Message (json)
map(Type.BYTE); // 1 - Chat Position
handler(wrapper -> {
@ -61,15 +62,15 @@ public class PlayerPackets {
protocol.registerClientbound(ClientboundPackets1_8.TAB_LIST, new PacketHandlers() {
@Override
public void register() {
map(Type.STRING, Protocol1_9To1_8.FIX_JSON); // 0 - Header
map(Type.STRING, Protocol1_9To1_8.FIX_JSON); // 1 - Footer
map(Type.STRING, Protocol1_9To1_8.STRING_TO_JSON); // 0 - Header
map(Type.STRING, Protocol1_9To1_8.STRING_TO_JSON); // 1 - Footer
}
});
protocol.registerClientbound(ClientboundPackets1_8.DISCONNECT, new PacketHandlers() {
@Override
public void register() {
map(Type.STRING, Protocol1_9To1_8.FIX_JSON); // 0 - Reason
map(Type.STRING, Protocol1_9To1_8.STRING_TO_JSON); // 0 - Reason
}
});
@ -81,7 +82,7 @@ public class PlayerPackets {
handler(wrapper -> {
int action = wrapper.get(Type.VAR_INT, 0);
if (action == 0 || action == 1) {
Protocol1_9To1_8.FIX_JSON.write(wrapper, wrapper.read(Type.STRING));
Protocol1_9To1_8.STRING_TO_JSON.write(wrapper, wrapper.read(Type.STRING));
}
});
// Everything else is handled.
@ -248,13 +249,13 @@ public class PlayerPackets {
wrapper.passthrough(Type.VAR_INT); // ping
String displayName = wrapper.read(Type.OPTIONAL_STRING);
wrapper.write(Type.OPTIONAL_COMPONENT, displayName != null ?
Protocol1_9To1_8.FIX_JSON.transform(wrapper, displayName) : null);
Protocol1_9To1_8.STRING_TO_JSON.transform(wrapper, displayName) : null);
} else if ((action == 1) || (action == 2)) { // update gamemode || update latency
wrapper.passthrough(Type.VAR_INT);
} else if (action == 3) { // update display name
String displayName = wrapper.read(Type.OPTIONAL_STRING);
wrapper.write(Type.OPTIONAL_COMPONENT, displayName != null ?
Protocol1_9To1_8.FIX_JSON.transform(wrapper, displayName) : null);
Protocol1_9To1_8.STRING_TO_JSON.transform(wrapper, displayName) : null);
}
}
});

View File

@ -45,6 +45,7 @@ import com.viaversion.viaversion.protocols.protocol1_9to1_8.sounds.Effect;
import com.viaversion.viaversion.protocols.protocol1_9to1_8.sounds.SoundEffect;
import com.viaversion.viaversion.protocols.protocol1_9to1_8.storage.ClientChunks;
import com.viaversion.viaversion.protocols.protocol1_9to1_8.storage.EntityTracker1_9;
import com.viaversion.viaversion.util.ComponentUtil;
import java.util.ArrayList;
import java.util.Optional;
@ -54,10 +55,12 @@ public class WorldPackets {
@Override
public void register() {
map(Type.POSITION1_8); // 0 - Sign Position
map(Type.STRING, Protocol1_9To1_8.FIX_JSON); // 1 - Sign Line (json)
map(Type.STRING, Protocol1_9To1_8.FIX_JSON); // 2 - Sign Line (json)
map(Type.STRING, Protocol1_9To1_8.FIX_JSON); // 3 - Sign Line (json)
map(Type.STRING, Protocol1_9To1_8.FIX_JSON); // 4 - Sign Line (json)
handler(wrapper -> {
for (int i = 0; i < 4; i++) {
final String line = wrapper.read(Type.STRING); // Should be Type.COMPONENT but would break in some cases
Protocol1_9To1_8.STRING_TO_JSON.write(wrapper, line);
}
});
}
});
@ -245,10 +248,12 @@ public class WorldPackets {
@Override
public void register() {
map(Type.POSITION1_8); // 0 - Sign Position
map(Type.STRING, Protocol1_9To1_8.FIX_JSON); // 1 - Sign Line (json)
map(Type.STRING, Protocol1_9To1_8.FIX_JSON); // 2 - Sign Line (json)
map(Type.STRING, Protocol1_9To1_8.FIX_JSON); // 3 - Sign Line (json)
map(Type.STRING, Protocol1_9To1_8.FIX_JSON); // 4 - Sign Line (json)
handler(wrapper -> {
for (int i = 0; i < 4; i++) {
final String line = wrapper.read(Type.STRING);
wrapper.write(Type.COMPONENT, ComponentUtil.plainToJson(line));
}
});
}
});

View File

@ -100,6 +100,14 @@ public final class ComponentUtil {
return json != null ? convert(from, to, from.toComponent(json)) : null;
}
public static @Nullable JsonElement convertJsonOrEmpty(@Nullable final String json, final SerializerVersion from, final SerializerVersion to) {
final ATextComponent component = from.toComponent(json);
if (component == null) {
return emptyJsonComponent();
}
return to.toJson(component);
}
private static JsonElement convert(final SerializerVersion from, final SerializerVersion to, final ATextComponent component) {
if (from.ordinal() >= SerializerVersion.V1_16.ordinal() && to.ordinal() < SerializerVersion.V1_16.ordinal()) {
// Convert hover event to legacy format

View File

@ -219,8 +219,6 @@ replace-pistons: false
replacement-piston-id: 0
# Fix 1.9+ clients not rendering the far away chunks and improve chunk rendering when moving fast (Increases network usage and decreases client fps slightly)
chunk-border-fix: false
# Force the string -> json transform
force-json-transform: false
# Minimize the cooldown animation in 1.8 servers
minimize-cooldown: true
# Allows 1.9+ left-handedness (main hand) on 1.8 servers