This commit is contained in:
themode 2024-08-29 22:14:59 +02:00 committed by Matt Worzala
parent 3f079d252e
commit 879942e6ab
5 changed files with 32 additions and 33 deletions

View File

@ -221,7 +221,8 @@ record ComponentNetworkBufferTypeImpl() implements NetworkBufferTypeImpl<Compone
buffer.write(BYTE, TAG_END);
}
@SuppressWarnings("unchecked") private void writeHoverEvent(@NotNull NetworkBuffer buffer, @NotNull HoverEvent<?> hoverEvent) {
@SuppressWarnings("unchecked")
private void writeHoverEvent(@NotNull NetworkBuffer buffer, @NotNull HoverEvent<?> hoverEvent) {
buffer.write(BYTE, TAG_COMPOUND);
writeUtf(buffer, "hoverEvent");
@ -279,9 +280,9 @@ record ComponentNetworkBufferTypeImpl() implements NetworkBufferTypeImpl<Compone
/**
* This is a very gross version of {@link java.io.DataOutputStream#writeUTF(String)}. We need the data in the java
* modified utf-8 format, and I couldnt find a method without creating a new buffer for it.
*
*
* @param buffer the buffer to write to
* @param str the string to write
* @param str the string to write
*/
private static void writeUtf(@NotNull NetworkBuffer buffer, @NotNull String str) {
final int strlen = str.length();
@ -297,27 +298,31 @@ record ComponentNetworkBufferTypeImpl() implements NetworkBufferTypeImpl<Compone
throw new RuntimeException("UTF-8 string too long");
buffer.write(SHORT, (short) utflen);
buffer.ensureSize(utflen);
buffer.ensureWritable(utflen);
var impl = (NetworkBufferImpl) buffer;
int i;
for (i = 0; i < strlen; i++) { // optimized for initial run of ASCII
int c = str.charAt(i);
if (c >= 0x80 || c == 0) break;
buffer.nioBuffer.put(buffer.writeIndex++, (byte) c);
impl._putByte(buffer.writeIndex(), (byte) c);
impl.advanceWrite(1);
}
for (; i < strlen; i++) {
int c = str.charAt(i);
if (c < 0x80 && c != 0) {
buffer.nioBuffer.put(buffer.writeIndex++, (byte) c);
impl._putByte(buffer.writeIndex(), (byte) c);
impl.advanceWrite(1);
} else if (c >= 0x800) {
buffer.nioBuffer.put(buffer.writeIndex++, (byte) (0xE0 | ((c >> 12) & 0x0F)));
buffer.nioBuffer.put(buffer.writeIndex++, (byte) (0x80 | ((c >> 6) & 0x3F)));
buffer.nioBuffer.put(buffer.writeIndex++, (byte) (0x80 | ((c >> 0) & 0x3F)));
impl._putByte(buffer.writeIndex(), (byte) (0xE0 | ((c >> 12) & 0x0F)));
impl._putByte(buffer.writeIndex() + 1, (byte) (0x80 | ((c >> 6) & 0x3F)));
impl._putByte(buffer.writeIndex() + 2, (byte) (0x80 | ((c >> 0) & 0x3F)));
impl.advanceWrite(3);
} else {
buffer.nioBuffer.put(buffer.writeIndex++, (byte) (0xC0 | ((c >> 6) & 0x1F)));
buffer.nioBuffer.put(buffer.writeIndex++, (byte) (0x80 | ((c >> 0) & 0x3F)));
impl._putByte(buffer.writeIndex(), (byte) (0xC0 | ((c >> 6) & 0x1F)));
impl._putByte(buffer.writeIndex() + 1, (byte) (0x80 | ((c >> 0) & 0x3F)));
impl.advanceWrite(2);
}
}
}
}

View File

@ -48,7 +48,7 @@ public sealed interface NetworkBuffer permits NetworkBufferImpl {
@SuppressWarnings({"unchecked", "rawtypes"})
Type<CompoundBinaryTag> NBT_COMPOUND = (Type) new NetworkBufferTypeImpl.NbtType();
Type<Point> BLOCK_POSITION = new NetworkBufferTypeImpl.BlockPositionType();
Type<Component> COMPONENT = new NetworkBufferTypeImpl.ComponentType();
Type<Component> COMPONENT = new ComponentNetworkBufferTypeImpl();
Type<Component> JSON_COMPONENT = new NetworkBufferTypeImpl.JsonComponentType();
Type<UUID> UUID = new NetworkBufferTypeImpl.UUIDType();
Type<Pos> POS = new NetworkBufferTypeImpl.PosType();

View File

@ -5,7 +5,6 @@ import net.minestom.server.entity.attribute.AttributeModifier;
import net.minestom.server.network.NetworkBuffer;
import net.minestom.server.network.NetworkBufferTemplate;
import net.minestom.server.network.packet.server.ServerPacket;
import org.jetbrains.annotations.NotNull;
import java.util.Collection;
import java.util.List;
@ -18,30 +17,26 @@ public record EntityAttributesPacket(int entityId, List<Property> properties) im
public static final NetworkBuffer.Type<EntityAttributesPacket> SERIALIZER = NetworkBufferTemplate.template(
VAR_INT, EntityAttributesPacket::entityId,
Property.NETWORK_TYPE.list(MAX_ENTRIES), EntityAttributesPacket::properties,
Property.SERIALIZER.list(MAX_ENTRIES), EntityAttributesPacket::properties,
EntityAttributesPacket::new);
public EntityAttributesPacket {
properties = List.copyOf(properties);
}
public record Property(Attribute attribute, double value, Collection<AttributeModifier> modifiers) {
public static final NetworkBuffer.Type<Property> NETWORK_TYPE = new NetworkBuffer.Type<>() {
@Override
public void write(@NotNull NetworkBuffer buffer, Property value) {
buffer.write(Attribute.NETWORK_TYPE, value.attribute);
buffer.write(DOUBLE, value.value);
buffer.writeCollection(AttributeModifier.NETWORK_TYPE, value.modifiers);
}
@Override
public Property read(@NotNull NetworkBuffer buffer) {
return new Property(buffer.read(Attribute.NETWORK_TYPE), buffer.read(DOUBLE), buffer.readCollection(AttributeModifier.NETWORK_TYPE, Short.MAX_VALUE));
}
};
public record Property(Attribute attribute, double value, List<AttributeModifier> modifiers) {
public static final NetworkBuffer.Type<Property> SERIALIZER = NetworkBufferTemplate.template(
Attribute.NETWORK_TYPE, Property::attribute,
DOUBLE, Property::value,
AttributeModifier.NETWORK_TYPE.list(), Property::modifiers,
Property::new);
public Property {
modifiers = List.copyOf(modifiers);
}
public Property(Attribute attribute, double value, Collection<AttributeModifier> modifiers) {
this(attribute, value, List.copyOf(modifiers));
}
}
}

View File

@ -2,7 +2,6 @@ package net.minestom.server.network.player;
import net.minestom.server.network.NetworkBuffer;
import net.minestom.server.network.NetworkBufferTemplate;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@ -12,7 +11,7 @@ import java.util.UUID;
import static net.minestom.server.network.NetworkBuffer.STRING;
public record GameProfile(@NotNull UUID uuid, @NotNull String name,
@NotNull List<@NotNull Property> properties) implements NetworkBuffer.Writer {
@NotNull List<@NotNull Property> properties) {
public static final int MAX_PROPERTIES = 1024;
public GameProfile {

View File

@ -5,7 +5,6 @@ import net.minestom.server.adventure.serializer.nbt.NbtComponentSerializer;
import org.jetbrains.annotations.NotNull;
import org.junit.jupiter.api.Test;
import java.nio.ByteBuffer;
import java.util.List;
import static net.minestom.server.network.NetworkBuffer.COMPONENT;
@ -80,7 +79,8 @@ public class ComponentNetworkBufferTypeTest {
private static void assertWriteReadEquality(@NotNull Component comp) {
var array = NetworkBuffer.makeArray(buffer -> buffer.write(COMPONENT, comp));
var actual = NBT_READER.deserialize(new NetworkBuffer(ByteBuffer.wrap(array)).read(NBT));
var buffer = NetworkBuffer.wrap(array, 0, array.length);
var actual = NBT_READER.deserialize(buffer.read(NBT));
assertEquals(comp, actual);
}
}