Mirror von
https://github.com/ViaVersion/ViaVersion.git
synchronisiert 2024-12-28 00:50:13 +01:00
Ursprung
9220c5ae40
Commit
0b1c210f77
@ -77,6 +77,7 @@ public abstract class Type<T> implements ByteBufReader<T>, ByteBufWriter<T> {
|
||||
@Deprecated
|
||||
public static final Type<Integer[]> UNSIGNED_SHORT_ARRAY = new ArrayType<>(Type.UNSIGNED_SHORT);
|
||||
/* Other Types */
|
||||
public static final Type<String> COMPONENT_STRING = new ComponentStringType();
|
||||
public static final Type<String> STRING = new StringType();
|
||||
public static final Type<String[]> STRING_ARRAY = new ArrayType<>(Type.STRING);
|
||||
|
||||
|
@ -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<? extends Type> getBaseClass() {
|
||||
return StringType.class;
|
||||
}
|
||||
}
|
@ -10,22 +10,28 @@ public class StringType extends Type<String> {
|
||||
// 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<String> {
|
||||
|
||||
@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);
|
||||
|
@ -101,7 +101,7 @@ public class InventoryPackets {
|
||||
}
|
||||
|
||||
wrapper.write(Type.VAR_INT, typeId);
|
||||
wrapper.write(Type.STRING, title);
|
||||
wrapper.write(Type.COMPONENT_STRING, title);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
@ -56,7 +56,7 @@ public class Protocol1_16To1_15_2 extends Protocol<ClientboundPackets1_15, Clien
|
||||
registerOutgoing(ClientboundPackets1_15.CHAT_MESSAGE, new PacketRemapper() {
|
||||
@Override
|
||||
public void registerMap() {
|
||||
map(Type.STRING);
|
||||
map(Type.COMPONENT_STRING);
|
||||
map(Type.BYTE);
|
||||
handler(wrapper -> wrapper.write(Type.UUID, ZERO_UUID)); // sender uuid
|
||||
}
|
||||
|
@ -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);
|
||||
|
Laden…
In neuem Issue referenzieren
Einen Benutzer sperren