diff --git a/common/src/main/java/us/myles/ViaVersion/api/type/Type.java b/common/src/main/java/us/myles/ViaVersion/api/type/Type.java index c99c34384..215bcb9f0 100644 --- a/common/src/main/java/us/myles/ViaVersion/api/type/Type.java +++ b/common/src/main/java/us/myles/ViaVersion/api/type/Type.java @@ -77,6 +77,7 @@ public abstract class Type implements ByteBufReader, ByteBufWriter { @Deprecated public static final Type UNSIGNED_SHORT_ARRAY = new ArrayType<>(Type.UNSIGNED_SHORT); /* Other Types */ + public static final Type COMPONENT_STRING = new ComponentStringType(); public static final Type STRING = new StringType(); public static final Type STRING_ARRAY = new ArrayType<>(Type.STRING); diff --git a/common/src/main/java/us/myles/ViaVersion/api/type/types/ComponentStringType.java b/common/src/main/java/us/myles/ViaVersion/api/type/types/ComponentStringType.java new file mode 100644 index 000000000..bc99f5047 --- /dev/null +++ b/common/src/main/java/us/myles/ViaVersion/api/type/types/ComponentStringType.java @@ -0,0 +1,15 @@ +package us.myles.ViaVersion.api.type.types; + +import us.myles.ViaVersion.api.type.Type; + +public class ComponentStringType extends StringType { + + public ComponentStringType() { + super(262144); + } + + @Override + public Class getBaseClass() { + return StringType.class; + } +} diff --git a/common/src/main/java/us/myles/ViaVersion/api/type/types/StringType.java b/common/src/main/java/us/myles/ViaVersion/api/type/types/StringType.java index 651676997..9a1d2619c 100644 --- a/common/src/main/java/us/myles/ViaVersion/api/type/types/StringType.java +++ b/common/src/main/java/us/myles/ViaVersion/api/type/types/StringType.java @@ -10,22 +10,28 @@ public class StringType extends Type { // String#length() (used to limit the string in Minecraft source code) uses char[]#length private static final int maxJavaCharUtf8Length = Character.toString(Character.MAX_VALUE) .getBytes(StandardCharsets.UTF_8).length; + private final int maxLength; public StringType() { + this(Short.MAX_VALUE); + } + + public StringType(int maxLength) { super(String.class); + this.maxLength = maxLength; } @Override public String read(ByteBuf buffer) throws Exception { int len = Type.VAR_INT.readPrimitive(buffer); - Preconditions.checkArgument(len <= Short.MAX_VALUE * maxJavaCharUtf8Length, + Preconditions.checkArgument(len <= maxLength * maxJavaCharUtf8Length, "Cannot receive string longer than Short.MAX_VALUE * " + maxJavaCharUtf8Length + " bytes (got %s bytes)", len); String string = buffer.toString(buffer.readerIndex(), len, StandardCharsets.UTF_8); buffer.skipBytes(len); - Preconditions.checkArgument(string.length() <= Short.MAX_VALUE, + Preconditions.checkArgument(string.length() <= maxLength, "Cannot receive string longer than Short.MAX_VALUE characters (got %s bytes)", string.length()); return string; @@ -33,7 +39,7 @@ public class StringType extends Type { @Override public void write(ByteBuf buffer, String object) throws Exception { - Preconditions.checkArgument(object.length() <= Short.MAX_VALUE, "Cannot send string longer than Short.MAX_VALUE (got %s characters)", object.length()); + Preconditions.checkArgument(object.length() <= maxLength, "Cannot send string longer than Short.MAX_VALUE (got %s characters)", object.length()); byte[] b = object.getBytes(StandardCharsets.UTF_8); Type.VAR_INT.writePrimitive(buffer, b.length); diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/packets/InventoryPackets.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/packets/InventoryPackets.java index b2d4a43c6..9e6b7b570 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/packets/InventoryPackets.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_14to1_13_2/packets/InventoryPackets.java @@ -101,7 +101,7 @@ public class InventoryPackets { } wrapper.write(Type.VAR_INT, typeId); - wrapper.write(Type.STRING, title); + wrapper.write(Type.COMPONENT_STRING, title); } } }); diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_16to1_15_2/Protocol1_16To1_15_2.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_16to1_15_2/Protocol1_16To1_15_2.java index e44bc5cf2..2e95f570e 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_16to1_15_2/Protocol1_16To1_15_2.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_16to1_15_2/Protocol1_16To1_15_2.java @@ -56,7 +56,7 @@ public class Protocol1_16To1_15_2 extends Protocol wrapper.write(Type.UUID, ZERO_UUID)); // sender uuid } diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_16to1_15_2/packets/InventoryPackets.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_16to1_15_2/packets/InventoryPackets.java index e27451ec3..4fb84e267 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_16to1_15_2/packets/InventoryPackets.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_16to1_15_2/packets/InventoryPackets.java @@ -27,7 +27,7 @@ public class InventoryPackets { public void registerMap() { map(Type.VAR_INT); map(Type.VAR_INT); - map(Type.STRING); + map(Type.COMPONENT_STRING); handler(wrapper -> { int windowType = wrapper.get(Type.VAR_INT, 1);