diff --git a/src/main/java/net/minestom/server/attribute/AttributeModifier.java b/src/main/java/net/minestom/server/attribute/AttributeModifier.java index 2bcb99e08..143b5ef58 100644 --- a/src/main/java/net/minestom/server/attribute/AttributeModifier.java +++ b/src/main/java/net/minestom/server/attribute/AttributeModifier.java @@ -9,7 +9,7 @@ import java.util.UUID; */ public class AttributeModifier { - private final float amount; + private final double amount; private final String name; private final AttributeOperation operation; private final UUID id; @@ -21,7 +21,7 @@ public class AttributeModifier { * @param amount the value of this modifier * @param operation the operation to apply this modifier with */ - public AttributeModifier(@NotNull String name, float amount, @NotNull AttributeOperation operation) { + public AttributeModifier(@NotNull String name, double amount, @NotNull AttributeOperation operation) { this(UUID.randomUUID(), name, amount, operation); } @@ -33,7 +33,7 @@ public class AttributeModifier { * @param amount the value of this modifier * @param operation the operation to apply this modifier with */ - public AttributeModifier(@NotNull UUID id, @NotNull String name, float amount, @NotNull AttributeOperation operation) { + public AttributeModifier(@NotNull UUID id, @NotNull String name, double amount, @NotNull AttributeOperation operation) { this.id = id; this.name = name; this.amount = amount; @@ -65,7 +65,7 @@ public class AttributeModifier { * * @return the value of this modifier */ - public float getAmount() { + public double getAmount() { return amount; } diff --git a/src/main/java/net/minestom/server/crypto/ArgumentSignatures.java b/src/main/java/net/minestom/server/crypto/ArgumentSignatures.java index 45e8627f9..44b30173d 100644 --- a/src/main/java/net/minestom/server/crypto/ArgumentSignatures.java +++ b/src/main/java/net/minestom/server/crypto/ArgumentSignatures.java @@ -1,34 +1,34 @@ package net.minestom.server.crypto; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; -import net.minestom.server.utils.binary.Writeable; +import net.minestom.server.network.NetworkBuffer; import org.jetbrains.annotations.NotNull; import java.util.List; -public record ArgumentSignatures(@NotNull List<@NotNull Entry> entries) implements Writeable { +import static net.minestom.server.network.NetworkBuffer.STRING; + +public record ArgumentSignatures(@NotNull List<@NotNull Entry> entries) implements NetworkBuffer.Writer { public ArgumentSignatures { entries = List.copyOf(entries); } - public ArgumentSignatures(BinaryReader reader) { - this(reader.readVarIntList(Entry::new)); + public ArgumentSignatures(@NotNull NetworkBuffer reader) { + this(reader.readCollection(Entry::new)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeVarIntList(entries, BinaryWriter::write); + public void write(@NotNull NetworkBuffer writer) { + writer.writeCollection(entries); } - public record Entry(@NotNull String name, @NotNull MessageSignature signature) implements Writeable { - public Entry(BinaryReader reader) { - this(reader.readSizedString(), new MessageSignature(reader)); + public record Entry(@NotNull String name, @NotNull MessageSignature signature) implements NetworkBuffer.Writer { + public Entry(@NotNull NetworkBuffer reader) { + this(reader.read(STRING), new MessageSignature(reader)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeSizedString(name); + public void write(@NotNull NetworkBuffer writer) { + writer.write(STRING, name); writer.write(signature); } } diff --git a/src/main/java/net/minestom/server/crypto/LastSeenMessages.java b/src/main/java/net/minestom/server/crypto/LastSeenMessages.java index f18f04ccc..e6508888d 100644 --- a/src/main/java/net/minestom/server/crypto/LastSeenMessages.java +++ b/src/main/java/net/minestom/server/crypto/LastSeenMessages.java @@ -1,50 +1,46 @@ package net.minestom.server.crypto; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; -import net.minestom.server.utils.binary.Writeable; +import net.minestom.server.network.NetworkBuffer; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.util.List; import java.util.UUID; -public record LastSeenMessages(@NotNull List<@NotNull Entry> entries) implements Writeable { +public record LastSeenMessages(@NotNull List<@NotNull Entry> entries) implements NetworkBuffer.Writer { public LastSeenMessages { entries = List.copyOf(entries); } - public LastSeenMessages(BinaryReader reader) { - this(reader.readVarIntList(Entry::new)); + public LastSeenMessages(@NotNull NetworkBuffer reader) { + this(reader.readCollection(Entry::new)); } @Override - public void write(@NotNull BinaryWriter writer) { - + public void write(@NotNull NetworkBuffer writer) { } - public record Entry(UUID from, MessageSignature lastSignature) implements Writeable { - public Entry(BinaryReader reader) { - this(reader.readUuid(), new MessageSignature(reader)); + public record Entry(UUID from, MessageSignature lastSignature) implements NetworkBuffer.Writer { + public Entry(@NotNull NetworkBuffer reader) { + this(reader.read(NetworkBuffer.UUID), new MessageSignature(reader)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeUuid(from); + public void write(@NotNull NetworkBuffer writer) { + writer.write(NetworkBuffer.UUID, from); writer.write(lastSignature); } } - public record Update(LastSeenMessages lastSeen, @Nullable Entry lastReceived) implements Writeable { - public Update(BinaryReader reader) { - this(new LastSeenMessages(reader), reader.readBoolean() ? new Entry(reader) : null); + public record Update(LastSeenMessages lastSeen, @Nullable Entry lastReceived) implements NetworkBuffer.Writer { + public Update(@NotNull NetworkBuffer reader) { + this(new LastSeenMessages(reader), reader.readOptional(Entry::new)); } @Override - public void write(@NotNull BinaryWriter writer) { + public void write(@NotNull NetworkBuffer writer) { writer.write(lastSeen); - writer.writeBoolean(lastReceived != null); - if (lastReceived != null) writer.write(lastReceived); + writer.writeOptional(lastReceived); } } } diff --git a/src/main/java/net/minestom/server/crypto/MessageSignature.java b/src/main/java/net/minestom/server/crypto/MessageSignature.java index 70b8496c0..d76e3b8e6 100644 --- a/src/main/java/net/minestom/server/crypto/MessageSignature.java +++ b/src/main/java/net/minestom/server/crypto/MessageSignature.java @@ -1,17 +1,17 @@ package net.minestom.server.crypto; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; -import net.minestom.server.utils.binary.Writeable; +import net.minestom.server.network.NetworkBuffer; import org.jetbrains.annotations.NotNull; -public record MessageSignature(byte @NotNull [] signature) implements Writeable { - public MessageSignature(BinaryReader reader) { - this(reader.readByteArray()); +import static net.minestom.server.network.NetworkBuffer.BYTE_ARRAY; + +public record MessageSignature(byte @NotNull [] signature) implements NetworkBuffer.Writer { + public MessageSignature(@NotNull NetworkBuffer reader) { + this(reader.read(BYTE_ARRAY)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeByteArray(signature); + public void write(@NotNull NetworkBuffer writer) { + writer.write(BYTE_ARRAY, signature); } } diff --git a/src/main/java/net/minestom/server/crypto/PlayerPublicKey.java b/src/main/java/net/minestom/server/crypto/PlayerPublicKey.java index 62cc0f0cf..d052badfb 100644 --- a/src/main/java/net/minestom/server/crypto/PlayerPublicKey.java +++ b/src/main/java/net/minestom/server/crypto/PlayerPublicKey.java @@ -1,28 +1,31 @@ package net.minestom.server.crypto; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; -import net.minestom.server.utils.binary.Writeable; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.utils.crypto.KeyUtils; +import org.jetbrains.annotations.NotNull; import java.security.PublicKey; import java.time.Instant; import java.util.Arrays; +import static net.minestom.server.network.NetworkBuffer.BYTE_ARRAY; +import static net.minestom.server.network.NetworkBuffer.LONG; + /** * Player's public key used to sign chat messages */ -public record PlayerPublicKey(Instant expiresAt, PublicKey publicKey, byte[] signature) implements Writeable { - public PlayerPublicKey(BinaryReader reader) { - this(Instant.ofEpochMilli(reader.readLong()), - KeyUtils.publicRSAKeyFrom(reader.readByteArray()), reader.readByteArray()); +public record PlayerPublicKey(Instant expiresAt, PublicKey publicKey, + byte[] signature) implements NetworkBuffer.Writer { + public PlayerPublicKey(@NotNull NetworkBuffer reader) { + this(Instant.ofEpochMilli(reader.read(LONG)), + KeyUtils.publicRSAKeyFrom(reader.read(BYTE_ARRAY)), reader.read(BYTE_ARRAY)); } @Override - public void write(BinaryWriter writer) { - writer.writeLong(expiresAt().toEpochMilli()); - writer.writeByteArray(publicKey.getEncoded()); - writer.writeByteArray(signature()); + public void write(@NotNull NetworkBuffer writer) { + writer.write(LONG, expiresAt().toEpochMilli()); + writer.write(BYTE_ARRAY, publicKey.getEncoded()); + writer.write(BYTE_ARRAY, signature()); } @Override diff --git a/src/main/java/net/minestom/server/crypto/SaltSignaturePair.java b/src/main/java/net/minestom/server/crypto/SaltSignaturePair.java index feb751235..132df950a 100644 --- a/src/main/java/net/minestom/server/crypto/SaltSignaturePair.java +++ b/src/main/java/net/minestom/server/crypto/SaltSignaturePair.java @@ -1,18 +1,19 @@ package net.minestom.server.crypto; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; -import net.minestom.server.utils.binary.Writeable; +import net.minestom.server.network.NetworkBuffer; import org.jetbrains.annotations.NotNull; -public record SaltSignaturePair(long salt, byte[] signature) implements Writeable { - public SaltSignaturePair(BinaryReader reader) { - this(reader.readLong(), reader.readByteArray()); +import static net.minestom.server.network.NetworkBuffer.BYTE_ARRAY; +import static net.minestom.server.network.NetworkBuffer.LONG; + +public record SaltSignaturePair(long salt, byte[] signature) implements NetworkBuffer.Writer { + public SaltSignaturePair(@NotNull NetworkBuffer reader) { + this(reader.read(LONG), reader.read(BYTE_ARRAY)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeLong(salt); - writer.writeByteArray(signature); + public void write(@NotNull NetworkBuffer writer) { + writer.write(LONG, salt); + writer.write(BYTE_ARRAY, signature); } } diff --git a/src/main/java/net/minestom/server/crypto/SignatureValidator.java b/src/main/java/net/minestom/server/crypto/SignatureValidator.java index 52eae5651..2a8b7f8c8 100644 --- a/src/main/java/net/minestom/server/crypto/SignatureValidator.java +++ b/src/main/java/net/minestom/server/crypto/SignatureValidator.java @@ -2,7 +2,7 @@ package net.minestom.server.crypto; import net.minestom.server.MinecraftServer; import net.minestom.server.entity.Player; -import net.minestom.server.utils.binary.BinaryWriter; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.utils.crypto.KeyUtils; import org.jetbrains.annotations.Nullable; @@ -36,8 +36,8 @@ public interface SignatureValidator { */ boolean validate(byte[] payload, byte[] signature); - default boolean validate(Consumer payload, byte[] signature) { - return validate(BinaryWriter.makeArray(payload), signature); + default boolean validate(Consumer payload, byte[] signature) { + return validate(NetworkBuffer.makeArray(payload), signature); } static SignatureValidator from(PublicKey publicKey, KeyUtils.SignatureAlgorithm algorithm) { diff --git a/src/main/java/net/minestom/server/crypto/SignedMessageHeader.java b/src/main/java/net/minestom/server/crypto/SignedMessageHeader.java index 036e2cd6d..5aa6734cc 100644 --- a/src/main/java/net/minestom/server/crypto/SignedMessageHeader.java +++ b/src/main/java/net/minestom/server/crypto/SignedMessageHeader.java @@ -1,22 +1,20 @@ package net.minestom.server.crypto; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; -import net.minestom.server.utils.binary.Writeable; +import net.minestom.server.network.NetworkBuffer; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.util.UUID; -public record SignedMessageHeader(@Nullable MessageSignature previousSignature, UUID sender) implements Writeable { - public SignedMessageHeader(BinaryReader reader) { - this(reader.readBoolean() ? new MessageSignature(reader) : null, reader.readUuid()); +public record SignedMessageHeader(@Nullable MessageSignature previousSignature, + @NotNull UUID sender) implements NetworkBuffer.Writer { + public SignedMessageHeader(@NotNull NetworkBuffer reader) { + this(reader.readOptional(MessageSignature::new), reader.read(NetworkBuffer.UUID)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeBoolean(previousSignature != null); - if (previousSignature != null) writer.write(previousSignature); - writer.writeUuid(sender); + public void write(@NotNull NetworkBuffer writer) { + writer.writeOptional(previousSignature); + writer.write(NetworkBuffer.UUID, sender); } } diff --git a/src/main/java/net/minestom/server/entity/Metadata.java b/src/main/java/net/minestom/server/entity/Metadata.java index 8f5887224..b70d0d253 100644 --- a/src/main/java/net/minestom/server/entity/Metadata.java +++ b/src/main/java/net/minestom/server/entity/Metadata.java @@ -2,13 +2,10 @@ package net.minestom.server.entity; import net.kyori.adventure.text.Component; import net.minestom.server.coordinate.Point; -import net.minestom.server.coordinate.Vec; import net.minestom.server.item.ItemStack; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.play.EntityMetaDataPacket; import net.minestom.server.utils.Direction; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; -import net.minestom.server.utils.binary.Writeable; import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -24,104 +21,78 @@ import java.util.UUID; public final class Metadata { public static Entry Byte(byte value) { - return new MetadataImpl.EntryImpl<>(TYPE_BYTE, value, BinaryWriter::writeByte, BinaryReader::readByte); + return new MetadataImpl.EntryImpl<>(TYPE_BYTE, value, NetworkBuffer.BYTE); } public static Entry VarInt(int value) { - return new MetadataImpl.EntryImpl<>(TYPE_VARINT, value, BinaryWriter::writeVarInt, BinaryReader::readVarInt); + return new MetadataImpl.EntryImpl<>(TYPE_VARINT, value, NetworkBuffer.VAR_INT); } public static Entry Float(float value) { - return new MetadataImpl.EntryImpl<>(TYPE_FLOAT, value, BinaryWriter::writeFloat, BinaryReader::readFloat); + return new MetadataImpl.EntryImpl<>(TYPE_FLOAT, value, NetworkBuffer.FLOAT); } public static Entry String(@NotNull String value) { - return new MetadataImpl.EntryImpl<>(TYPE_STRING, value, BinaryWriter::writeSizedString, BinaryReader::readSizedString); + return new MetadataImpl.EntryImpl<>(TYPE_STRING, value, NetworkBuffer.STRING); } public static Entry Chat(@NotNull Component value) { - return new MetadataImpl.EntryImpl<>(TYPE_CHAT, value, BinaryWriter::writeComponent, BinaryReader::readComponent); + return new MetadataImpl.EntryImpl<>(TYPE_CHAT, value, NetworkBuffer.COMPONENT); } public static Entry OptChat(@Nullable Component value) { - return new MetadataImpl.EntryImpl<>(TYPE_OPTCHAT, value, (writer, v) -> { - writer.writeBoolean(v != null); - if (v != null) writer.writeComponent(v); - }, reader -> reader.readBoolean() ? reader.readComponent() : null); + return new MetadataImpl.EntryImpl<>(TYPE_OPTCHAT, value, NetworkBuffer.OPT_CHAT); } public static Entry Slot(@NotNull ItemStack value) { - return new MetadataImpl.EntryImpl<>(TYPE_SLOT, value, BinaryWriter::writeItemStack, BinaryReader::readItemStack); + return new MetadataImpl.EntryImpl<>(TYPE_SLOT, value, NetworkBuffer.ITEM); } public static Entry Boolean(boolean value) { - return new MetadataImpl.EntryImpl<>(TYPE_BOOLEAN, value, BinaryWriter::writeBoolean, BinaryReader::readBoolean); + return new MetadataImpl.EntryImpl<>(TYPE_BOOLEAN, value, NetworkBuffer.BOOLEAN); } public static Entry Rotation(@NotNull Point value) { - return new MetadataImpl.EntryImpl<>(TYPE_ROTATION, value, (writer, v) -> { - writer.writeFloat((float) v.x()); - writer.writeFloat((float) v.y()); - writer.writeFloat((float) v.z()); - }, reader -> new Vec(reader.readFloat(), reader.readFloat(), reader.readFloat())); + return new MetadataImpl.EntryImpl<>(TYPE_ROTATION, value, NetworkBuffer.ROTATION); } public static Entry Position(@NotNull Point value) { - return new MetadataImpl.EntryImpl<>(TYPE_POSITION, value, BinaryWriter::writeBlockPosition, BinaryReader::readBlockPosition); + return new MetadataImpl.EntryImpl<>(TYPE_POSITION, value, NetworkBuffer.BLOCK_POSITION); } public static Entry OptPosition(@Nullable Point value) { - return new MetadataImpl.EntryImpl<>(TYPE_OPTPOSITION, value, (writer, v) -> { - writer.writeBoolean(v != null); - if (v != null) writer.writeBlockPosition(v); - }, reader -> reader.readBoolean() ? reader.readBlockPosition() : null); + return new MetadataImpl.EntryImpl<>(TYPE_OPTPOSITION, value, NetworkBuffer.OPT_BLOCK_POSITION); } public static Entry Direction(@NotNull Direction value) { - return new MetadataImpl.EntryImpl<>(TYPE_DIRECTION, value, - (writer, v) -> writer.writeVarInt(v.ordinal()), - reader -> Direction.values()[reader.readVarInt()]); + return new MetadataImpl.EntryImpl<>(TYPE_DIRECTION, value, NetworkBuffer.DIRECTION); } public static Entry OptUUID(@Nullable UUID value) { - return new MetadataImpl.EntryImpl<>(TYPE_OPTUUID, value, (writer, v) -> { - writer.writeBoolean(v != null); - if (v != null) writer.writeUuid(v); - }, reader -> reader.readBoolean() ? reader.readUuid() : null); + return new MetadataImpl.EntryImpl<>(TYPE_OPTUUID, value, NetworkBuffer.OPT_UUID); } public static Entry OptBlockID(@Nullable Integer value) { - return new MetadataImpl.EntryImpl<>(TYPE_OPTBLOCKID, value, - (writer, v) -> writer.writeVarInt(v != null ? v : 0), - reader -> reader.readBoolean() ? reader.readVarInt() : null); + return new MetadataImpl.EntryImpl<>(TYPE_OPTBLOCKID, value, NetworkBuffer.OPT_BLOCK_ID); } public static Entry NBT(@NotNull NBT nbt) { - return new MetadataImpl.EntryImpl<>(TYPE_NBT, nbt, (writer, v) -> writer.writeNBT("", v), BinaryReader::readTag); + return new MetadataImpl.EntryImpl<>(TYPE_NBT, nbt, NetworkBuffer.NBT); } public static Entry VillagerData(int villagerType, int villagerProfession, int level) { return new MetadataImpl.EntryImpl<>(TYPE_VILLAGERDATA, new int[]{villagerType, villagerProfession, level}, - (writer, v) -> { - writer.writeVarInt(v[0]); - writer.writeVarInt(v[1]); - writer.writeVarInt(v[2]); - }, - reader -> new int[]{reader.readVarInt(), reader.readVarInt(), reader.readVarInt()}); + NetworkBuffer.VILLAGER_DATA); } public static Entry OptVarInt(@Nullable Integer value) { - return new MetadataImpl.EntryImpl<>(TYPE_OPTVARINT, - value, (writer, v) -> writer.writeVarInt(v != null ? v + 1 : 0), - reader -> reader.readBoolean() ? reader.readVarInt() : null); + return new MetadataImpl.EntryImpl<>(TYPE_OPTVARINT, value, NetworkBuffer.OPT_VAR_INT); } public static Entry Pose(@NotNull Entity.Pose value) { - return new MetadataImpl.EntryImpl<>(TYPE_POSE, value, - (writer, v) -> writer.writeVarInt(v.ordinal()), - reader -> Entity.Pose.values()[reader.readVarInt()]); + return new MetadataImpl.EntryImpl<>(TYPE_POSE, value, NetworkBuffer.POSE); } public static final byte TYPE_BYTE = 0; @@ -229,14 +200,14 @@ public final class Metadata { return map; } - public sealed interface Entry extends Writeable + public sealed interface Entry extends NetworkBuffer.Writer permits MetadataImpl.EntryImpl { int type(); @UnknownNullability T value(); @ApiStatus.Internal - static @NotNull Entry read(int type, @NotNull BinaryReader reader) { + static @NotNull Entry read(int type, @NotNull NetworkBuffer reader) { return MetadataImpl.EntryImpl.read(type, reader); } } diff --git a/src/main/java/net/minestom/server/entity/MetadataImpl.java b/src/main/java/net/minestom/server/entity/MetadataImpl.java index 0ac9fa68f..9ba6129d1 100644 --- a/src/main/java/net/minestom/server/entity/MetadataImpl.java +++ b/src/main/java/net/minestom/server/entity/MetadataImpl.java @@ -3,22 +3,19 @@ package net.minestom.server.entity; import net.kyori.adventure.text.Component; import net.minestom.server.coordinate.Vec; import net.minestom.server.item.ItemStack; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.utils.Direction; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import net.minestom.server.utils.collection.ObjectArray; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.UnknownNullability; import org.jglrxavpok.hephaistos.nbt.NBTEnd; -import java.util.function.BiConsumer; -import java.util.function.Function; - import static net.minestom.server.entity.Metadata.Boolean; import static net.minestom.server.entity.Metadata.Byte; import static net.minestom.server.entity.Metadata.Float; import static net.minestom.server.entity.Metadata.String; import static net.minestom.server.entity.Metadata.*; +import static net.minestom.server.network.NetworkBuffer.VAR_INT; final class MetadataImpl { static final ObjectArray> EMPTY_VALUES = ObjectArray.singleThread(20); @@ -47,22 +44,21 @@ final class MetadataImpl { } record EntryImpl(int type, @UnknownNullability T value, - @NotNull BiConsumer writer, - @NotNull Function reader) implements Metadata.Entry { - static Entry read(int type, @NotNull BinaryReader reader) { + @NotNull NetworkBuffer.Type serializer) implements Metadata.Entry { + static Entry read(int type, @NotNull NetworkBuffer reader) { final EntryImpl value = (EntryImpl) EMPTY_VALUES.get(type); if (value == null) throw new UnsupportedOperationException("Unknown value type: " + type); return value.withValue(reader); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeVarInt(type); - this.writer.accept(writer, value); + public void write(@NotNull NetworkBuffer writer) { + writer.write(VAR_INT, type); + writer.write(serializer, value); } - private EntryImpl withValue(@NotNull BinaryReader reader) { - return new EntryImpl<>(type, this.reader.apply(reader), writer, this.reader); + private EntryImpl withValue(@NotNull NetworkBuffer reader) { + return new EntryImpl<>(type, reader.read(serializer), serializer); } } } diff --git a/src/main/java/net/minestom/server/extras/velocity/VelocityProxy.java b/src/main/java/net/minestom/server/extras/velocity/VelocityProxy.java index 53e61842c..c817ed425 100644 --- a/src/main/java/net/minestom/server/extras/velocity/VelocityProxy.java +++ b/src/main/java/net/minestom/server/extras/velocity/VelocityProxy.java @@ -45,7 +45,7 @@ public final class VelocityProxy { return enabled; } - public static boolean checkIntegrity(NetworkBuffer buffer) { + public static boolean checkIntegrity(@NotNull NetworkBuffer buffer) { final byte[] signature = new byte[32]; for (int i = 0; i < signature.length; i++) { signature[i] = buffer.read(BYTE); diff --git a/src/main/java/net/minestom/server/instance/DynamicChunk.java b/src/main/java/net/minestom/server/instance/DynamicChunk.java index 95a62ec9d..027b182c8 100644 --- a/src/main/java/net/minestom/server/instance/DynamicChunk.java +++ b/src/main/java/net/minestom/server/instance/DynamicChunk.java @@ -9,6 +9,7 @@ import net.minestom.server.entity.Player; import net.minestom.server.entity.pathfinding.PFBlock; import net.minestom.server.instance.block.Block; import net.minestom.server.instance.block.BlockHandler; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.CachedPacket; import net.minestom.server.network.packet.server.play.ChunkDataPacket; import net.minestom.server.network.packet.server.play.UpdateLightPacket; @@ -20,7 +21,6 @@ import net.minestom.server.snapshot.SnapshotUpdater; import net.minestom.server.utils.ArrayUtils; import net.minestom.server.utils.MathUtils; import net.minestom.server.utils.ObjectPool; -import net.minestom.server.utils.binary.BinaryWriter; import net.minestom.server.utils.chunk.ChunkUtils; import net.minestom.server.world.biomes.Biome; import org.jetbrains.annotations.NotNull; @@ -203,11 +203,10 @@ public class DynamicChunk extends Chunk { "WORLD_SURFACE", NBT.LongArray(encodeBlocks(worldSurface, bitsForHeight)))); } // Data - final byte[] data = ObjectPool.PACKET_POOL.use(buffer -> { - final BinaryWriter writer = new BinaryWriter(buffer); - for (Section section : sections) writer.write(section); - return writer.toByteArray(); - }); + final byte[] data = ObjectPool.PACKET_POOL.use(buffer -> + NetworkBuffer.makeArray(networkBuffer -> { + for (Section section : sections) networkBuffer.write(section); + })); return new ChunkDataPacket(chunkX, chunkZ, new ChunkData(heightmapsNBT, data, entries), createLightData()); diff --git a/src/main/java/net/minestom/server/instance/Section.java b/src/main/java/net/minestom/server/instance/Section.java index edda1189d..f9c4e49bf 100644 --- a/src/main/java/net/minestom/server/instance/Section.java +++ b/src/main/java/net/minestom/server/instance/Section.java @@ -1,11 +1,12 @@ package net.minestom.server.instance; import net.minestom.server.instance.palette.Palette; -import net.minestom.server.utils.binary.BinaryWriter; -import net.minestom.server.utils.binary.Writeable; +import net.minestom.server.network.NetworkBuffer; import org.jetbrains.annotations.NotNull; -public final class Section implements Writeable { +import static net.minestom.server.network.NetworkBuffer.SHORT; + +public final class Section implements NetworkBuffer.Writer { private Palette blockPalette; private Palette biomePalette; private byte[] skyLight; @@ -62,8 +63,8 @@ public final class Section implements Writeable { } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeShort((short) blockPalette.count()); + public void write(@NotNull NetworkBuffer writer) { + writer.write(SHORT, (short) blockPalette.count()); writer.write(blockPalette); writer.write(biomePalette); } diff --git a/src/main/java/net/minestom/server/instance/palette/AdaptivePalette.java b/src/main/java/net/minestom/server/instance/palette/AdaptivePalette.java index 6b36f3748..d64930f9a 100644 --- a/src/main/java/net/minestom/server/instance/palette/AdaptivePalette.java +++ b/src/main/java/net/minestom/server/instance/palette/AdaptivePalette.java @@ -2,8 +2,8 @@ package net.minestom.server.instance.palette; import it.unimi.dsi.fastutil.ints.IntOpenHashSet; import it.unimi.dsi.fastutil.ints.IntSet; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.utils.MathUtils; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; import java.util.function.IntUnaryOperator; @@ -106,7 +106,7 @@ final class AdaptivePalette implements Palette, Cloneable { } @Override - public void write(@NotNull BinaryWriter writer) { + public void write(@NotNull NetworkBuffer writer) { final SpecializedPalette optimized = optimizedPalette(); this.palette = optimized; optimized.write(writer); diff --git a/src/main/java/net/minestom/server/instance/palette/FilledPalette.java b/src/main/java/net/minestom/server/instance/palette/FilledPalette.java index 4dfd139f3..669d098f6 100644 --- a/src/main/java/net/minestom/server/instance/palette/FilledPalette.java +++ b/src/main/java/net/minestom/server/instance/palette/FilledPalette.java @@ -1,8 +1,11 @@ package net.minestom.server.instance.palette; -import net.minestom.server.utils.binary.BinaryWriter; +import net.minestom.server.network.NetworkBuffer; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.BYTE; +import static net.minestom.server.network.NetworkBuffer.VAR_INT; + /** * Palette containing a single value. Useful for both empty and full palettes. */ @@ -43,9 +46,9 @@ record FilledPalette(byte dim, int value) implements SpecializedPalette.Immutabl } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeByte((byte) 0); - writer.writeVarInt(value); - writer.writeVarInt(0); + public void write(@NotNull NetworkBuffer writer) { + writer.write(BYTE, (byte) 0); + writer.write(VAR_INT, value); + writer.write(VAR_INT, 0); } } diff --git a/src/main/java/net/minestom/server/instance/palette/FlexiblePalette.java b/src/main/java/net/minestom/server/instance/palette/FlexiblePalette.java index d5429247b..f5b9b907b 100644 --- a/src/main/java/net/minestom/server/instance/palette/FlexiblePalette.java +++ b/src/main/java/net/minestom/server/instance/palette/FlexiblePalette.java @@ -3,14 +3,16 @@ package net.minestom.server.instance.palette; import it.unimi.dsi.fastutil.ints.Int2IntOpenHashMap; import it.unimi.dsi.fastutil.ints.IntArrayList; import net.minestom.server.MinecraftServer; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.utils.MathUtils; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; import java.util.Arrays; import java.util.concurrent.atomic.AtomicInteger; import java.util.function.IntUnaryOperator; +import static net.minestom.server.network.NetworkBuffer.*; + /** * Palette able to take any value anywhere. May consume more memory than required. */ @@ -207,12 +209,12 @@ final class FlexiblePalette implements SpecializedPalette, Cloneable { } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeByte(bitsPerEntry); + public void write(@NotNull NetworkBuffer writer) { + writer.write(BYTE, bitsPerEntry); if (bitsPerEntry <= maxBitsPerEntry()) { // Palette index - writer.writeVarIntList(paletteToValueList, BinaryWriter::writeVarInt); + writer.writeCollection(VAR_INT, paletteToValueList); } - writer.writeLongArray(values); + writer.write(LONG_ARRAY, values); } private void retrieveAll(@NotNull EntryConsumer consumer, boolean consumeEmpty) { diff --git a/src/main/java/net/minestom/server/instance/palette/Palette.java b/src/main/java/net/minestom/server/instance/palette/Palette.java index 5a07685e9..93a14b9b3 100644 --- a/src/main/java/net/minestom/server/instance/palette/Palette.java +++ b/src/main/java/net/minestom/server/instance/palette/Palette.java @@ -1,6 +1,6 @@ package net.minestom.server.instance.palette; -import net.minestom.server.utils.binary.Writeable; +import net.minestom.server.network.NetworkBuffer; import org.jetbrains.annotations.NotNull; import java.util.function.IntUnaryOperator; @@ -10,7 +10,7 @@ import java.util.function.IntUnaryOperator; *

* 0 is the default value. */ -public interface Palette extends Writeable { +public interface Palette extends NetworkBuffer.Writer { static Palette blocks() { return newPalette(16, 8, 4); } diff --git a/src/main/java/net/minestom/server/item/ItemMeta.java b/src/main/java/net/minestom/server/item/ItemMeta.java index d37934f1f..65d37d2d4 100644 --- a/src/main/java/net/minestom/server/item/ItemMeta.java +++ b/src/main/java/net/minestom/server/item/ItemMeta.java @@ -3,11 +3,11 @@ package net.minestom.server.item; import net.kyori.adventure.text.Component; import net.minestom.server.instance.block.Block; import net.minestom.server.item.attribute.ItemAttribute; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.registry.ProtocolObject; import net.minestom.server.tag.Tag; import net.minestom.server.tag.TagReadable; import net.minestom.server.tag.Taggable; -import net.minestom.server.utils.binary.Writeable; import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -17,7 +17,7 @@ import org.jglrxavpok.hephaistos.nbt.NBTCompound; import java.util.*; import java.util.function.Consumer; -public sealed interface ItemMeta extends TagReadable, Writeable +public sealed interface ItemMeta extends TagReadable, NetworkBuffer.Writer permits ItemMetaImpl { @Override diff --git a/src/main/java/net/minestom/server/item/ItemMetaImpl.java b/src/main/java/net/minestom/server/item/ItemMetaImpl.java index 4cde432d8..22483b9e5 100644 --- a/src/main/java/net/minestom/server/item/ItemMetaImpl.java +++ b/src/main/java/net/minestom/server/item/ItemMetaImpl.java @@ -1,8 +1,8 @@ package net.minestom.server.item; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.tag.Tag; import net.minestom.server.tag.TagHandler; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.UnknownNullability; import org.jglrxavpok.hephaistos.nbt.NBTCompound; @@ -10,6 +10,9 @@ import org.jglrxavpok.hephaistos.nbt.NBTCompound; import java.util.Objects; import java.util.function.Consumer; +import static net.minestom.server.network.NetworkBuffer.BYTE; +import static net.minestom.server.network.NetworkBuffer.NBT; + record ItemMetaImpl(TagHandler tagHandler) implements ItemMeta { static final ItemMetaImpl EMPTY = new ItemMetaImpl(TagHandler.newHandler()); @@ -36,13 +39,13 @@ record ItemMetaImpl(TagHandler tagHandler) implements ItemMeta { } @Override - public void write(@NotNull BinaryWriter writer) { + public void write(@NotNull NetworkBuffer writer) { final NBTCompound nbt = toNBT(); if (nbt.isEmpty()) { - writer.writeByte((byte) 0); + writer.write(BYTE, (byte) 0); return; } - writer.writeNBT("", nbt); + writer.write(NBT, nbt); } @Override diff --git a/src/main/java/net/minestom/server/network/NetworkBuffer.java b/src/main/java/net/minestom/server/network/NetworkBuffer.java index c7e1fae75..57d2329ea 100644 --- a/src/main/java/net/minestom/server/network/NetworkBuffer.java +++ b/src/main/java/net/minestom/server/network/NetworkBuffer.java @@ -2,7 +2,10 @@ package net.minestom.server.network; import net.kyori.adventure.text.Component; import net.minestom.server.coordinate.Point; +import net.minestom.server.entity.Entity; import net.minestom.server.item.ItemStack; +import net.minestom.server.utils.Direction; +import net.minestom.server.utils.Either; import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -15,12 +18,16 @@ import java.nio.ByteOrder; import java.util.Collection; import java.util.List; import java.util.UUID; +import java.util.function.BiConsumer; +import java.util.function.Consumer; +import java.util.function.Function; @ApiStatus.Experimental public final class NetworkBuffer { public static final Type BOOLEAN = NetworkBufferTypes.BOOLEAN; public static final Type BYTE = NetworkBufferTypes.BYTE; public static final Type SHORT = NetworkBufferTypes.SHORT; + public static final Type UNSIGNED_SHORT = NetworkBufferTypes.UNSIGNED_SHORT; public static final Type INT = NetworkBufferTypes.INT; public static final Type LONG = NetworkBufferTypes.LONG; public static final Type FLOAT = NetworkBufferTypes.FLOAT; @@ -35,6 +42,22 @@ public final class NetworkBuffer { public static final Type UUID = NetworkBufferTypes.UUID; public static final Type ITEM = NetworkBufferTypes.ITEM; + public static final Type BYTE_ARRAY = NetworkBufferTypes.BYTE_ARRAY; + public static final Type LONG_ARRAY = NetworkBufferTypes.LONG_ARRAY; + public static final Type VAR_INT_ARRAY = NetworkBufferTypes.VAR_INT_ARRAY; + public static final Type VAR_LONG_ARRAY = NetworkBufferTypes.VAR_LONG_ARRAY; + + // METADATA + public static final Type OPT_CHAT = NetworkBufferTypes.OPT_CHAT; + public static final Type ROTATION = NetworkBufferTypes.ROTATION; + public static final Type OPT_BLOCK_POSITION = NetworkBufferTypes.OPT_BLOCK_POSITION; + public static final Type DIRECTION = NetworkBufferTypes.DIRECTION; + public static final Type OPT_UUID = NetworkBufferTypes.OPT_UUID; + public static final Type OPT_BLOCK_ID = NetworkBufferTypes.OPT_BLOCK_ID; + public static final Type VILLAGER_DATA = NetworkBufferTypes.VILLAGER_DATA; + public static final Type OPT_VAR_INT = NetworkBufferTypes.OPT_VAR_INT; + public static final Type POSE = NetworkBufferTypes.POSE; + ByteBuffer nioBuffer; final boolean resizable; int writeIndex; @@ -69,6 +92,10 @@ public final class NetworkBuffer { if (length != -1) this.writeIndex += length; } + public void write(@NotNull Writer writer) { + writer.write(this); + } + public @NotNull T read(@NotNull Type type) { var impl = (NetworkBufferTypes.TypeImpl) type; return impl.reader().read(this); @@ -79,10 +106,19 @@ public final class NetworkBuffer { if (value != null) write(type, value); } + public void writeOptional(@Nullable Writer writer) { + write(BOOLEAN, writer != null); + if (writer != null) write(writer); + } + public @Nullable T readOptional(@NotNull Type type) { return read(BOOLEAN) ? read(type) : null; } + public @Nullable T readOptional(@NotNull Function<@NotNull NetworkBuffer, @NotNull T> function) { + return read(BOOLEAN) ? function.apply(this) : null; + } + public void writeCollection(@NotNull Type type, @Nullable Collection<@NotNull T> values) { if (values == null) { write(BYTE, (byte) 0); @@ -99,19 +135,93 @@ public final class NetworkBuffer { writeCollection(type, values == null ? null : List.of(values)); } - public @NotNull Collection<@NotNull T> readCollection(@NotNull Type type) { + public void writeCollection(@Nullable Collection<@NotNull T> values) { + if (values == null) { + write(BYTE, (byte) 0); + return; + } + write(VAR_INT, values.size()); + for (T value : values) { + write(value); + } + } + + public void writeCollection(@Nullable Collection<@NotNull T> values, + @NotNull BiConsumer<@NotNull NetworkBuffer, @NotNull T> consumer) { + if (values == null) { + write(BYTE, (byte) 0); + return; + } + write(VAR_INT, values.size()); + for (T value : values) { + consumer.accept(this, value); + } + } + + public @NotNull List<@NotNull T> readCollection(@NotNull Type type) { final int size = read(VAR_INT); - final Collection values = new java.util.ArrayList<>(size); + final List values = new java.util.ArrayList<>(size); for (int i = 0; i < size; i++) { values.add(read(type)); } return values; } + public @NotNull List<@NotNull T> readCollection(@NotNull Function<@NotNull NetworkBuffer, @NotNull T> function) { + final int size = read(VAR_INT); + final List values = new java.util.ArrayList<>(size); + for (int i = 0; i < size; i++) { + values.add(function.apply(this)); + } + return values; + } + + public void writeEither(Either either, BiConsumer leftWriter, BiConsumer rightWriter) { + if (either.isLeft()) { + write(BOOLEAN, true); + leftWriter.accept(this, either.left()); + } else { + write(BOOLEAN, false); + rightWriter.accept(this, either.right()); + } + } + + public @NotNull Either readEither(@NotNull Function leftReader, Function rightReader) { + if (read(BOOLEAN)) { + return Either.left(leftReader.apply(this)); + } else { + return Either.right(rightReader.apply(this)); + } + } + + public > void writeEnum(@NotNull Class enumClass, @NotNull E value) { + write(VAR_INT, value.ordinal()); + } + + public > @NotNull E readEnum(@NotNull Class<@NotNull E> enumClass) { + return enumClass.getEnumConstants()[read(VAR_INT)]; + } + + public byte[] readBytes(int length) { + byte[] bytes = new byte[length]; + nioBuffer.get(readIndex, bytes, 0, length); + readIndex += length; + return bytes; + } + public void copyTo(int srcOffset, byte @NotNull [] dest, int destOffset, int length) { this.nioBuffer.get(srcOffset, dest, destOffset, length); } + public byte @NotNull [] extractBytes(@NotNull Consumer<@NotNull NetworkBuffer> extractor) { + final int startingPosition = readIndex(); + extractor.accept(this); + final int endingPosition = readIndex(); + byte[] output = new byte[endingPosition - startingPosition]; + copyTo(startingPosition, output, 0, output.length); + return output; + } + public void clear() { this.writeIndex = 0; this.readIndex = 0; @@ -153,6 +263,20 @@ public final class NetworkBuffer { } } + public sealed interface Type permits NetworkBufferTypes.TypeImpl { } + + @FunctionalInterface + public interface Writer { + void write(@NotNull NetworkBuffer writer); + } + + public static byte[] makeArray(@NotNull Consumer<@NotNull NetworkBuffer> writing) { + NetworkBuffer writer = new NetworkBuffer(); + writing.accept(writer); + byte[] bytes = new byte[writer.writeIndex]; + writer.copyTo(0, bytes, 0, bytes.length); + return bytes; + } } diff --git a/src/main/java/net/minestom/server/network/NetworkBufferTypes.java b/src/main/java/net/minestom/server/network/NetworkBufferTypes.java index 419df3ecc..aedcfd8af 100644 --- a/src/main/java/net/minestom/server/network/NetworkBufferTypes.java +++ b/src/main/java/net/minestom/server/network/NetworkBufferTypes.java @@ -4,9 +4,12 @@ import net.kyori.adventure.text.Component; import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer; import net.minestom.server.coordinate.Point; import net.minestom.server.coordinate.Vec; +import net.minestom.server.entity.Entity; import net.minestom.server.item.ItemStack; import net.minestom.server.item.Material; +import net.minestom.server.utils.Direction; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.UnknownNullability; import org.jglrxavpok.hephaistos.nbt.*; import java.io.IOException; @@ -52,6 +55,17 @@ final class NetworkBufferTypes { buffer.readIndex += 2; return value; }); + static final TypeImpl UNSIGNED_SHORT = new TypeImpl<>(Integer.class, + (buffer, value) -> { + buffer.ensureSize(2); + buffer.nioBuffer.putShort(buffer.writeIndex(), (short) (value & 0xFFFF)); + return 2; + }, + buffer -> { + final short value = buffer.nioBuffer.getShort(buffer.readIndex()); + buffer.readIndex += 2; + return value & 0xFFFF; + }); static final TypeImpl INT = new TypeImpl<>(Integer.class, (buffer, value) -> { buffer.ensureSize(4); @@ -306,6 +320,184 @@ final class NetworkBufferTypes { return ItemStack.fromNBT(material, compound, amount); }); + static final TypeImpl BYTE_ARRAY = new TypeImpl<>(byte[].class, + (buffer, value) -> { + buffer.write(VAR_INT, value.length); + buffer.write(RAW_BYTES, value); + return -1; + }, + buffer -> { + final int length = buffer.read(VAR_INT); + final byte[] bytes = new byte[length]; + buffer.nioBuffer.get(buffer.readIndex(), bytes); + buffer.readIndex += length; + return bytes; + }); + static final TypeImpl LONG_ARRAY = new TypeImpl<>(long[].class, + (buffer, value) -> { + buffer.write(VAR_INT, value.length); + for (long l : value) { + buffer.write(LONG, l); + } + return -1; + }, + buffer -> { + final int length = buffer.read(VAR_INT); + final long[] longs = new long[length]; + for (int i = 0; i < length; i++) { + longs[i] = buffer.read(LONG); + } + return longs; + }); + static final TypeImpl VAR_INT_ARRAY = new TypeImpl<>(int[].class, + (buffer, value) -> { + buffer.write(VAR_INT, value.length); + for (int i : value) { + buffer.write(VAR_INT, i); + } + return -1; + }, + buffer -> { + final int length = buffer.read(VAR_INT); + final int[] ints = new int[length]; + for (int i = 0; i < length; i++) { + ints[i] = buffer.read(VAR_INT); + } + return ints; + }); + static final TypeImpl VAR_LONG_ARRAY = new TypeImpl<>(long[].class, + (buffer, value) -> { + buffer.write(VAR_INT, value.length); + for (long l : value) { + buffer.write(VAR_LONG, l); + } + return -1; + }, + buffer -> { + final int length = buffer.read(VAR_INT); + final long[] longs = new long[length]; + for (int i = 0; i < length; i++) { + longs[i] = buffer.read(VAR_LONG); + } + return longs; + }); + // METADATA + static final TypeImpl OPT_CHAT = new TypeImpl<>(Component.class, + (buffer, value) -> { + if (value == null) { + buffer.write(BOOLEAN, false); + return -1; + } + buffer.write(BOOLEAN, true); + buffer.write(COMPONENT, value); + return -1; + }, + buffer -> { + final boolean present = buffer.read(BOOLEAN); + if (!present) return null; + return buffer.read(COMPONENT); + }); + static final TypeImpl ROTATION = new TypeImpl<>(Point.class, + (buffer, value) -> { + buffer.write(FLOAT, (float) value.x()); + buffer.write(FLOAT, (float) value.y()); + buffer.write(FLOAT, (float) value.z()); + return -1; + }, + buffer -> { + final float x = buffer.read(FLOAT); + final float y = buffer.read(FLOAT); + final float z = buffer.read(FLOAT); + return new Vec(x, y, z); + }); + static final TypeImpl OPT_BLOCK_POSITION = new TypeImpl<>(Point.class, + (buffer, value) -> { + if (value == null) { + buffer.write(BOOLEAN, false); + return -1; + } + buffer.write(BOOLEAN, true); + buffer.write(BLOCK_POSITION, value); + return -1; + }, + buffer -> { + final boolean present = buffer.read(BOOLEAN); + if (!present) return null; + return buffer.read(BLOCK_POSITION); + }); + static final TypeImpl DIRECTION = new TypeImpl<>(Direction.class, + (buffer, value) -> { + buffer.write(VAR_INT, value.ordinal()); + return -1; + }, + buffer -> { + final int ordinal = buffer.read(VAR_INT); + return Direction.values()[ordinal]; + }); + static final TypeImpl OPT_UUID = new TypeImpl<>(UUID.class, + (buffer, value) -> { + if (value == null) { + buffer.write(BOOLEAN, false); + return -1; + } + buffer.write(BOOLEAN, true); + buffer.write(UUID, value); + return -1; + }, + buffer -> { + final boolean present = buffer.read(BOOLEAN); + if (!present) return null; + return buffer.read(UUID); + }); + static final TypeImpl OPT_BLOCK_ID = new TypeImpl<>(Integer.class, + (buffer, value) -> { + if (value == null) { + buffer.write(NetworkBuffer.VAR_INT, 0); + return -1; + } + buffer.write(VAR_INT, value); + return -1; + }, + buffer -> { + final int value = buffer.read(VAR_INT); + return value == 0 ? null : value; + }); + static final TypeImpl VILLAGER_DATA = new TypeImpl<>(int[].class, + (buffer, value) -> { + buffer.write(VAR_INT, value[0]); + buffer.write(VAR_INT, value[1]); + buffer.write(VAR_INT, value[2]); + return -1; + }, + buffer -> { + final int[] value = new int[3]; + value[0] = buffer.read(VAR_INT); + value[1] = buffer.read(VAR_INT); + value[2] = buffer.read(VAR_INT); + return value; + }); + static final TypeImpl OPT_VAR_INT = new TypeImpl<>(int.class, + (buffer, value) -> { + if (value == null) { + buffer.write(VAR_INT, 0); + return -1; + } + buffer.write(VAR_INT, value + 1); + return -1; + }, + buffer -> { + final int value = buffer.read(VAR_INT); + return value == 0 ? null : value - 1; + }); + static final TypeImpl POSE = new TypeImpl<>(Entity.Pose.class, + (buffer, value) -> { + buffer.write(VAR_INT, value.ordinal()); + return -1; + }, + buffer -> { + final int ordinal = buffer.read(VAR_INT); + return Entity.Pose.values()[ordinal]; + }); record TypeImpl(@NotNull Class type, @NotNull TypeWriter writer, @@ -313,10 +505,10 @@ final class NetworkBufferTypes { } interface TypeWriter { - long write(@NotNull NetworkBuffer buffer, @NotNull T value); + long write(@NotNull NetworkBuffer buffer, @UnknownNullability T value); } interface TypeReader { - @NotNull T read(@NotNull NetworkBuffer buffer); + @UnknownNullability T read(@NotNull NetworkBuffer buffer); } } diff --git a/src/main/java/net/minestom/server/network/PacketProcessor.java b/src/main/java/net/minestom/server/network/PacketProcessor.java index cf810bd24..dcfbd8b3c 100644 --- a/src/main/java/net/minestom/server/network/PacketProcessor.java +++ b/src/main/java/net/minestom/server/network/PacketProcessor.java @@ -27,18 +27,17 @@ public record PacketProcessor(@NotNull ClientPacketsHandler statusHandler, } public @NotNull ClientPacket create(@NotNull ConnectionState connectionState, int packetId, ByteBuffer body) { - NetworkBuffer networkBuffer = new NetworkBuffer(body); - BinaryReader reader = new BinaryReader(networkBuffer); + NetworkBuffer buffer = new NetworkBuffer(body); final ClientPacket clientPacket = switch (connectionState) { - case PLAY -> playHandler.create(packetId, reader); - case LOGIN -> loginHandler.create(packetId, reader); - case STATUS -> statusHandler.create(packetId, reader); + case PLAY -> playHandler.create(packetId, buffer); + case LOGIN -> loginHandler.create(packetId, buffer); + case STATUS -> statusHandler.create(packetId, buffer); case UNKNOWN -> { assert packetId == 0; - yield new HandshakePacket(reader); + yield new HandshakePacket(buffer); } }; - body.position(networkBuffer.readIndex()); + body.position(buffer.readIndex()); return clientPacket; } diff --git a/src/main/java/net/minestom/server/network/packet/client/ClientPacket.java b/src/main/java/net/minestom/server/network/packet/client/ClientPacket.java index 6469f7ea3..be55d454e 100644 --- a/src/main/java/net/minestom/server/network/packet/client/ClientPacket.java +++ b/src/main/java/net/minestom/server/network/packet/client/ClientPacket.java @@ -1,11 +1,11 @@ package net.minestom.server.network.packet.client; -import net.minestom.server.utils.binary.Writeable; +import net.minestom.server.network.NetworkBuffer; /** * Represents a packet received from a client. *

- * Packets are value-based, and should therefore be reliant on identity. + * Packets are value-based, and should therefore not be reliant on identity. */ -public interface ClientPacket extends Writeable { +public interface ClientPacket extends NetworkBuffer.Writer { } diff --git a/src/main/java/net/minestom/server/network/packet/client/ClientPacketsHandler.java b/src/main/java/net/minestom/server/network/packet/client/ClientPacketsHandler.java index e25f66280..25fd1a4c1 100644 --- a/src/main/java/net/minestom/server/network/packet/client/ClientPacketsHandler.java +++ b/src/main/java/net/minestom/server/network/packet/client/ClientPacketsHandler.java @@ -1,12 +1,12 @@ package net.minestom.server.network.packet.client; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.client.login.EncryptionResponsePacket; import net.minestom.server.network.packet.client.login.LoginPluginResponsePacket; import net.minestom.server.network.packet.client.login.LoginStartPacket; import net.minestom.server.network.packet.client.play.*; import net.minestom.server.network.packet.client.status.PingPacket; import net.minestom.server.network.packet.client.status.StatusRequestPacket; -import net.minestom.server.utils.binary.BinaryReader; import net.minestom.server.utils.collection.ObjectArray; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.UnknownNullability; @@ -16,20 +16,20 @@ import java.util.function.Function; /** * Contains registered packets and a way to instantiate them. *

- * Packets are registered using {@link #register(int, Function)} and created using {@link #create(int, BinaryReader)}. + * Packets are registered using {@link #register(int, Function)} and created using {@link #create(int, NetworkBuffer)}. */ public sealed class ClientPacketsHandler permits ClientPacketsHandler.Status, ClientPacketsHandler.Login, ClientPacketsHandler.Play { - private final ObjectArray> suppliers = ObjectArray.singleThread(0x10); + private final ObjectArray> suppliers = ObjectArray.singleThread(0x10); private ClientPacketsHandler() { } - public void register(int id, @NotNull Function<@NotNull BinaryReader, @NotNull ClientPacket> packetSupplier) { + public void register(int id, @NotNull Function<@NotNull NetworkBuffer, @NotNull ClientPacket> packetSupplier) { this.suppliers.set(id, packetSupplier); } - public @UnknownNullability ClientPacket create(int packetId, @NotNull BinaryReader reader) { - final Function supplier = suppliers.get(packetId); + public @UnknownNullability ClientPacket create(int packetId, @NotNull NetworkBuffer reader) { + final Function supplier = suppliers.get(packetId); if (supplier == null) throw new IllegalStateException("Packet id 0x" + Integer.toHexString(packetId) + " isn't registered!"); return supplier.apply(reader); diff --git a/src/main/java/net/minestom/server/network/packet/client/handshake/HandshakePacket.java b/src/main/java/net/minestom/server/network/packet/client/handshake/HandshakePacket.java index 232484855..e24d2a8f0 100644 --- a/src/main/java/net/minestom/server/network/packet/client/handshake/HandshakePacket.java +++ b/src/main/java/net/minestom/server/network/packet/client/handshake/HandshakePacket.java @@ -9,13 +9,12 @@ import net.kyori.adventure.text.format.NamedTextColor; import net.minestom.server.MinecraftServer; import net.minestom.server.extras.bungee.BungeeCordProxy; import net.minestom.server.network.ConnectionState; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.client.ClientPreplayPacket; import net.minestom.server.network.packet.server.login.LoginDisconnectPacket; import net.minestom.server.network.player.GameProfile; import net.minestom.server.network.player.PlayerConnection; import net.minestom.server.network.player.PlayerSocketConnection; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; import java.net.SocketAddress; @@ -23,6 +22,8 @@ import java.util.ArrayList; import java.util.List; import java.util.UUID; +import static net.minestom.server.network.NetworkBuffer.*; + public record HandshakePacket(int protocolVersion, @NotNull String serverAddress, int serverPort, int nextState) implements ClientPreplayPacket { @@ -32,21 +33,27 @@ public record HandshakePacket(int protocolVersion, @NotNull String serverAddress private static final Component INVALID_VERSION_TEXT = Component.text("Invalid Version, please use " + MinecraftServer.VERSION_NAME, NamedTextColor.RED); private static final Component INVALID_BUNGEE_FORWARDING = Component.text("If you wish to use IP forwarding, please enable it in your BungeeCord config as well!", NamedTextColor.RED); - public HandshakePacket(BinaryReader reader) { - this(reader.readVarInt(), reader.readSizedString(BungeeCordProxy.isEnabled() ? Short.MAX_VALUE : 255), - reader.readUnsignedShort(), reader.readVarInt()); + public HandshakePacket { + if (serverAddress.length() > (BungeeCordProxy.isEnabled() ? Short.MAX_VALUE : 255)) { + throw new IllegalArgumentException("Server address too long: " + serverAddress.length()); + } + } + + public HandshakePacket(@NotNull NetworkBuffer reader) { + this(reader.read(VAR_INT), reader.read(STRING), + reader.read(UNSIGNED_SHORT), reader.read(VAR_INT)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeVarInt(protocolVersion); + public void write(@NotNull NetworkBuffer writer) { + writer.write(VAR_INT, protocolVersion); int maxLength = BungeeCordProxy.isEnabled() ? Short.MAX_VALUE : 255; if (serverAddress.length() > maxLength) { throw new IllegalArgumentException("serverAddress is " + serverAddress.length() + " characters long, maximum allowed is " + maxLength); } - writer.writeSizedString(serverAddress); - writer.writeUnsignedShort(serverPort); - writer.writeVarInt(nextState); + writer.write(STRING, serverAddress); + writer.write(UNSIGNED_SHORT, serverPort); + writer.write(VAR_INT, nextState); } @Override @@ -63,7 +70,7 @@ public record HandshakePacket(int protocolVersion, @NotNull String serverAddress ((java.net.InetSocketAddress) connection.getRemoteAddress()).getPort()); socketConnection.setRemoteAddress(socketAddress); - UUID playerUuid = UUID.fromString( + UUID playerUuid = java.util.UUID.fromString( split[2] .replaceFirst( "(\\p{XDigit}{8})(\\p{XDigit}{4})(\\p{XDigit}{4})(\\p{XDigit}{4})(\\p{XDigit}+)", "$1-$2-$3-$4-$5" diff --git a/src/main/java/net/minestom/server/network/packet/client/login/EncryptionResponsePacket.java b/src/main/java/net/minestom/server/network/packet/client/login/EncryptionResponsePacket.java index 5c10f3f32..f622a16b7 100644 --- a/src/main/java/net/minestom/server/network/packet/client/login/EncryptionResponsePacket.java +++ b/src/main/java/net/minestom/server/network/packet/client/login/EncryptionResponsePacket.java @@ -7,14 +7,13 @@ import net.minestom.server.crypto.SaltSignaturePair; import net.minestom.server.crypto.SignatureValidator; import net.minestom.server.extras.MojangAuth; import net.minestom.server.extras.mojangAuth.MojangCrypt; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.client.ClientPreplayPacket; import net.minestom.server.network.player.PlayerConnection; import net.minestom.server.network.player.PlayerSocketConnection; import net.minestom.server.utils.Either; import net.minestom.server.utils.InterfaceUtils; import net.minestom.server.utils.async.AsyncUtils; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import net.minestom.server.utils.crypto.KeyUtils; import org.jetbrains.annotations.NotNull; @@ -29,11 +28,14 @@ import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.UUID; -public record EncryptionResponsePacket(byte[] sharedSecret, Either nonceOrSignature) implements ClientPreplayPacket { +import static net.minestom.server.network.NetworkBuffer.*; + +public record EncryptionResponsePacket(byte[] sharedSecret, + Either nonceOrSignature) implements ClientPreplayPacket { private static final Gson GSON = new Gson(); - public EncryptionResponsePacket(BinaryReader reader) { - this(reader.readByteArray(), reader.readEither(BinaryReader::readByteArray, SaltSignaturePair::new)); + public EncryptionResponsePacket(@NotNull NetworkBuffer reader) { + this(reader.read(BYTE_ARRAY), reader.readEither(networkBuffer -> networkBuffer.read(BYTE_ARRAY), SaltSignaturePair::new)); } @Override @@ -54,8 +56,8 @@ public record EncryptionResponsePacket(byte[] sharedSecret, Either !hasPublicKey || !SignatureValidator .from(connection.playerPublicKey().publicKey(), KeyUtils.SignatureAlgorithm.SHA256withRSA) .validate(binaryWriter -> { - binaryWriter.writeBytes(socketConnection.getNonce()); - binaryWriter.writeLong(signature.salt()); + binaryWriter.write(RAW_BYTES, socketConnection.getNonce()); + binaryWriter.write(LONG, signature.salt()); }, signature.signature())); if (verificationFailed) { @@ -93,7 +95,7 @@ public record EncryptionResponsePacket(byte[] sharedSecret, Either networkBuffer.write(BYTE_ARRAY, bytes), + InterfaceUtils.flipBiConsumer(SaltSignaturePair::write)); } private SecretKey getSecretKey() { diff --git a/src/main/java/net/minestom/server/network/packet/client/login/LoginPluginResponsePacket.java b/src/main/java/net/minestom/server/network/packet/client/login/LoginPluginResponsePacket.java index 652af3113..2fc8fdef3 100644 --- a/src/main/java/net/minestom/server/network/packet/client/login/LoginPluginResponsePacket.java +++ b/src/main/java/net/minestom/server/network/packet/client/login/LoginPluginResponsePacket.java @@ -11,8 +11,6 @@ import net.minestom.server.network.packet.server.login.LoginDisconnectPacket; import net.minestom.server.network.player.GameProfile; import net.minestom.server.network.player.PlayerConnection; import net.minestom.server.network.player.PlayerSocketConnection; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -22,14 +20,14 @@ import java.net.SocketAddress; import java.net.UnknownHostException; import java.nio.ByteBuffer; -import static net.minestom.server.network.NetworkBuffer.STRING; +import static net.minestom.server.network.NetworkBuffer.*; public record LoginPluginResponsePacket(int messageId, byte @Nullable [] data) implements ClientPreplayPacket { private final static ConnectionManager CONNECTION_MANAGER = MinecraftServer.getConnectionManager(); public static final Component INVALID_PROXY_RESPONSE = Component.text("Invalid proxy response!", NamedTextColor.RED); - public LoginPluginResponsePacket(BinaryReader reader) { - this(reader.readVarInt(), reader.readBoolean() ? reader.readRemainingBytes() : null); + public LoginPluginResponsePacket(@NotNull NetworkBuffer reader) { + this(reader.read(VAR_INT), reader.readOptional(RAW_BYTES)); } @Override @@ -59,7 +57,7 @@ public record LoginPluginResponsePacket(int messageId, byte @Nullable [] data) i } final int port = ((java.net.InetSocketAddress) connection.getRemoteAddress()).getPort(); socketAddress = new InetSocketAddress(address, port); - gameProfile = new GameProfile(new BinaryReader(buffer)); + gameProfile = new GameProfile(buffer); } } } @@ -77,9 +75,8 @@ public record LoginPluginResponsePacket(int messageId, byte @Nullable [] data) i } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeVarInt(messageId); - writer.writeBoolean(data != null); - if (data != null) writer.writeBytes(data); + public void write(@NotNull NetworkBuffer writer) { + writer.write(VAR_INT, messageId); + writer.writeOptional(RAW_BYTES, data); } } diff --git a/src/main/java/net/minestom/server/network/packet/client/login/LoginStartPacket.java b/src/main/java/net/minestom/server/network/packet/client/login/LoginStartPacket.java index caa46bfb0..07716f76c 100644 --- a/src/main/java/net/minestom/server/network/packet/client/login/LoginStartPacket.java +++ b/src/main/java/net/minestom/server/network/packet/client/login/LoginStartPacket.java @@ -9,14 +9,13 @@ import net.minestom.server.extras.MojangAuth; import net.minestom.server.extras.bungee.BungeeCordProxy; import net.minestom.server.extras.velocity.VelocityProxy; import net.minestom.server.network.ConnectionState; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.client.ClientPreplayPacket; import net.minestom.server.network.packet.server.login.EncryptionRequestPacket; import net.minestom.server.network.packet.server.login.LoginDisconnectPacket; import net.minestom.server.network.packet.server.login.LoginPluginRequestPacket; import net.minestom.server.network.player.PlayerConnection; import net.minestom.server.network.player.PlayerSocketConnection; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -24,15 +23,15 @@ import java.time.Instant; import java.util.UUID; import java.util.concurrent.ThreadLocalRandom; +import static net.minestom.server.network.NetworkBuffer.*; + public record LoginStartPacket(@NotNull String username, @Nullable PlayerPublicKey publicKey, @Nullable UUID profileId) implements ClientPreplayPacket { private static final Component ALREADY_CONNECTED = Component.text("You are already on this server", NamedTextColor.RED); - public LoginStartPacket(BinaryReader reader) { - this(reader.readSizedString(16), - reader.readBoolean() ? new PlayerPublicKey(reader) : null, - reader.readBoolean() ? reader.readUuid() : null); + public LoginStartPacket(@NotNull NetworkBuffer reader) { + this(reader.read(STRING), reader.readOptional(PlayerPublicKey::new), reader.readOptional(UUID)); } @Override @@ -42,13 +41,13 @@ public record LoginStartPacket(@NotNull String username, if (publicKey != null) { if (!SignatureValidator.YGGDRASIL.validate(binaryWriter -> { if (profileId != null) { - binaryWriter.writeLong(profileId.getMostSignificantBits()); - binaryWriter.writeLong(profileId.getLeastSignificantBits()); + binaryWriter.write(LONG, profileId.getMostSignificantBits()); + binaryWriter.write(LONG, profileId.getLeastSignificantBits()); } else { MinecraftServer.LOGGER.warn("Profile ID was null for player {}, signature will not match!", username); } - binaryWriter.writeLong(publicKey.expiresAt().toEpochMilli()); - binaryWriter.writeBytes(publicKey.publicKey().getEncoded()); + binaryWriter.write(LONG, publicKey.expiresAt().toEpochMilli()); + binaryWriter.write(RAW_BYTES, publicKey.publicKey().getEncoded()); }, publicKey.signature())) { connection.sendPacket(new LoginDisconnectPacket(Component.text("Invalid Profile Public Key!"))); connection.disconnect(); @@ -101,9 +100,9 @@ public record LoginStartPacket(@NotNull String username, } @Override - public void write(@NotNull BinaryWriter writer) { + public void write(@NotNull NetworkBuffer writer) { if (username.length() > 16) throw new IllegalArgumentException("Username is not allowed to be longer than 16 characters"); - writer.writeSizedString(username); + writer.write(STRING, username); } } diff --git a/src/main/java/net/minestom/server/network/packet/client/play/ClientAdvancementTabPacket.java b/src/main/java/net/minestom/server/network/packet/client/play/ClientAdvancementTabPacket.java index f7aae2afb..fec3c5c7f 100644 --- a/src/main/java/net/minestom/server/network/packet/client/play/ClientAdvancementTabPacket.java +++ b/src/main/java/net/minestom/server/network/packet/client/play/ClientAdvancementTabPacket.java @@ -1,15 +1,22 @@ package net.minestom.server.network.packet.client.play; import net.minestom.server.advancements.AdvancementAction; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.client.ClientPacket; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import static net.minestom.server.network.NetworkBuffer.STRING; + public record ClientAdvancementTabPacket(@NotNull AdvancementAction action, @Nullable String tabIdentifier) implements ClientPacket { - public ClientAdvancementTabPacket(BinaryReader reader) { + public ClientAdvancementTabPacket { + if (tabIdentifier != null && tabIdentifier.length() > 256) { + throw new IllegalArgumentException("Tab identifier too long: " + tabIdentifier.length()); + } + } + + public ClientAdvancementTabPacket(@NotNull NetworkBuffer reader) { this(read(reader)); } @@ -17,21 +24,21 @@ public record ClientAdvancementTabPacket(@NotNull AdvancementAction action, this(packet.action, packet.tabIdentifier); } - private static ClientAdvancementTabPacket read(BinaryReader reader) { - var action = AdvancementAction.values()[reader.readVarInt()]; - var tabIdentifier = action == AdvancementAction.OPENED_TAB ? reader.readSizedString(256) : null; + private static ClientAdvancementTabPacket read(@NotNull NetworkBuffer reader) { + var action = reader.readEnum(AdvancementAction.class); + var tabIdentifier = action == AdvancementAction.OPENED_TAB ? reader.read(STRING) : null; return new ClientAdvancementTabPacket(action, tabIdentifier); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeVarInt(action.ordinal()); + public void write(@NotNull NetworkBuffer writer) { + writer.writeEnum(AdvancementAction.class, action); if (action == AdvancementAction.OPENED_TAB) { assert tabIdentifier != null; if (tabIdentifier.length() > 256) { throw new IllegalArgumentException("Tab identifier cannot be longer than 256 characters."); } - writer.writeSizedString(tabIdentifier); + writer.write(STRING, tabIdentifier); } } } diff --git a/src/main/java/net/minestom/server/network/packet/client/play/ClientAnimationPacket.java b/src/main/java/net/minestom/server/network/packet/client/play/ClientAnimationPacket.java index 6ec5446b8..03a255517 100644 --- a/src/main/java/net/minestom/server/network/packet/client/play/ClientAnimationPacket.java +++ b/src/main/java/net/minestom/server/network/packet/client/play/ClientAnimationPacket.java @@ -1,18 +1,17 @@ package net.minestom.server.network.packet.client.play; import net.minestom.server.entity.Player; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.client.ClientPacket; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; public record ClientAnimationPacket(@NotNull Player.Hand hand) implements ClientPacket { - public ClientAnimationPacket(BinaryReader reader) { - this(Player.Hand.values()[reader.readVarInt()]); + public ClientAnimationPacket(@NotNull NetworkBuffer reader) { + this(reader.readEnum(Player.Hand.class)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeVarInt(hand.ordinal()); + public void write(@NotNull NetworkBuffer writer) { + writer.writeEnum(Player.Hand.class, hand); } } diff --git a/src/main/java/net/minestom/server/network/packet/client/play/ClientChatAckPacket.java b/src/main/java/net/minestom/server/network/packet/client/play/ClientChatAckPacket.java index 4032d1932..b5cc19c9e 100644 --- a/src/main/java/net/minestom/server/network/packet/client/play/ClientChatAckPacket.java +++ b/src/main/java/net/minestom/server/network/packet/client/play/ClientChatAckPacket.java @@ -1,18 +1,17 @@ package net.minestom.server.network.packet.client.play; import net.minestom.server.crypto.LastSeenMessages; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.client.ClientPacket; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; public record ClientChatAckPacket(@NotNull LastSeenMessages.Update update) implements ClientPacket { - public ClientChatAckPacket(BinaryReader reader) { + public ClientChatAckPacket(@NotNull NetworkBuffer reader) { this(new LastSeenMessages.Update(reader)); } @Override - public void write(@NotNull BinaryWriter writer) { + public void write(@NotNull NetworkBuffer writer) { writer.write(update); } } diff --git a/src/main/java/net/minestom/server/network/packet/client/play/ClientChatMessagePacket.java b/src/main/java/net/minestom/server/network/packet/client/play/ClientChatMessagePacket.java index 73192ad85..8f79ee147 100644 --- a/src/main/java/net/minestom/server/network/packet/client/play/ClientChatMessagePacket.java +++ b/src/main/java/net/minestom/server/network/packet/client/play/ClientChatMessagePacket.java @@ -2,11 +2,12 @@ package net.minestom.server.network.packet.client.play; import net.minestom.server.crypto.LastSeenMessages; import net.minestom.server.crypto.MessageSignature; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.client.ClientPacket; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.*; + public record ClientChatMessagePacket(@NotNull String message, long timestamp, long salt, @NotNull MessageSignature signature, boolean signedPreview, @@ -17,20 +18,20 @@ public record ClientChatMessagePacket(@NotNull String message, } } - public ClientChatMessagePacket(BinaryReader reader) { - this(reader.readSizedString(256), - reader.readLong(), reader.readLong(), new MessageSignature(reader), - reader.readBoolean(), + public ClientChatMessagePacket(@NotNull NetworkBuffer reader) { + this(reader.read(STRING), + reader.read(LONG), reader.read(LONG), new MessageSignature(reader), + reader.read(BOOLEAN), new LastSeenMessages.Update(reader)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeSizedString(message); - writer.writeLong(timestamp); - writer.writeLong(salt); + public void write(@NotNull NetworkBuffer writer) { + writer.write(STRING, message); + writer.write(LONG, timestamp); + writer.write(LONG, salt); writer.write(signature); - writer.writeBoolean(signedPreview); + writer.write(BOOLEAN, signedPreview); writer.write(lastSeenMessages); } } diff --git a/src/main/java/net/minestom/server/network/packet/client/play/ClientChatPreviewPacket.java b/src/main/java/net/minestom/server/network/packet/client/play/ClientChatPreviewPacket.java index d2ec1fc09..9586976dc 100644 --- a/src/main/java/net/minestom/server/network/packet/client/play/ClientChatPreviewPacket.java +++ b/src/main/java/net/minestom/server/network/packet/client/play/ClientChatPreviewPacket.java @@ -1,18 +1,26 @@ package net.minestom.server.network.packet.client.play; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.client.ClientPacket; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.INT; +import static net.minestom.server.network.NetworkBuffer.STRING; + public record ClientChatPreviewPacket(int queryId, @NotNull String query) implements ClientPacket { - public ClientChatPreviewPacket(BinaryReader reader) { - this(reader.readInt(), reader.readSizedString(256)); + public ClientChatPreviewPacket { + if (query.length() > 256) { + throw new IllegalArgumentException("Query length cannot be greater than 256"); + } + } + + public ClientChatPreviewPacket(@NotNull NetworkBuffer reader) { + this(reader.read(INT), reader.read(STRING)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeInt(queryId); - writer.writeSizedString(query); + public void write(@NotNull NetworkBuffer writer) { + writer.write(INT, queryId); + writer.write(STRING, query); } } diff --git a/src/main/java/net/minestom/server/network/packet/client/play/ClientClickWindowButtonPacket.java b/src/main/java/net/minestom/server/network/packet/client/play/ClientClickWindowButtonPacket.java index 08034505b..610625d13 100644 --- a/src/main/java/net/minestom/server/network/packet/client/play/ClientClickWindowButtonPacket.java +++ b/src/main/java/net/minestom/server/network/packet/client/play/ClientClickWindowButtonPacket.java @@ -1,18 +1,19 @@ package net.minestom.server.network.packet.client.play; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.client.ClientPacket; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.BYTE; + public record ClientClickWindowButtonPacket(byte windowId, byte buttonId) implements ClientPacket { - public ClientClickWindowButtonPacket(BinaryReader reader) { - this(reader.readByte(), reader.readByte()); + public ClientClickWindowButtonPacket(@NotNull NetworkBuffer reader) { + this(reader.read(BYTE), reader.read(BYTE)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeByte(windowId); - writer.writeByte(buttonId); + public void write(@NotNull NetworkBuffer writer) { + writer.write(BYTE, windowId); + writer.write(BYTE, buttonId); } } diff --git a/src/main/java/net/minestom/server/network/packet/client/play/ClientClickWindowPacket.java b/src/main/java/net/minestom/server/network/packet/client/play/ClientClickWindowPacket.java index fd3cc94e3..147a3d984 100644 --- a/src/main/java/net/minestom/server/network/packet/client/play/ClientClickWindowPacket.java +++ b/src/main/java/net/minestom/server/network/packet/client/play/ClientClickWindowPacket.java @@ -1,14 +1,14 @@ package net.minestom.server.network.packet.client.play; import net.minestom.server.item.ItemStack; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.client.ClientPacket; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; -import net.minestom.server.utils.binary.Writeable; import org.jetbrains.annotations.NotNull; import java.util.List; +import static net.minestom.server.network.NetworkBuffer.*; + public record ClientClickWindowPacket(byte windowId, int stateId, short slot, byte button, @NotNull ClickType clickType, @NotNull List changedSlots, @@ -17,32 +17,32 @@ public record ClientClickWindowPacket(byte windowId, int stateId, changedSlots = List.copyOf(changedSlots); } - public ClientClickWindowPacket(BinaryReader reader) { - this(reader.readByte(), reader.readVarInt(), - reader.readShort(), reader.readByte(), ClickType.values()[reader.readVarInt()], - reader.readVarIntList(ChangedSlot::new), reader.readItemStack()); + public ClientClickWindowPacket(@NotNull NetworkBuffer reader) { + this(reader.read(BYTE), reader.read(VAR_INT), + reader.read(SHORT), reader.read(BYTE), reader.readEnum(ClickType.class), + reader.readCollection(ChangedSlot::new), reader.read(ITEM)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeByte(windowId); - writer.writeVarInt(stateId); - writer.writeShort(slot); - writer.writeByte(button); - writer.writeVarInt(clickType.ordinal()); - writer.writeVarIntList(changedSlots, BinaryWriter::write); - writer.writeItemStack(clickedItem); + public void write(@NotNull NetworkBuffer writer) { + writer.write(BYTE, windowId); + writer.write(VAR_INT, stateId); + writer.write(SHORT, slot); + writer.write(BYTE, button); + writer.write(VAR_INT, clickType.ordinal()); + writer.writeCollection(changedSlots); + writer.write(ITEM, clickedItem); } - public record ChangedSlot(short slot, @NotNull ItemStack item) implements Writeable { - public ChangedSlot(BinaryReader reader) { - this(reader.readShort(), reader.readItemStack()); + public record ChangedSlot(short slot, @NotNull ItemStack item) implements NetworkBuffer.Writer { + public ChangedSlot(@NotNull NetworkBuffer reader) { + this(reader.read(SHORT), reader.read(ITEM)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeShort(slot); - writer.writeItemStack(item); + public void write(@NotNull NetworkBuffer writer) { + writer.write(SHORT, slot); + writer.write(ITEM, item); } } diff --git a/src/main/java/net/minestom/server/network/packet/client/play/ClientCloseWindowPacket.java b/src/main/java/net/minestom/server/network/packet/client/play/ClientCloseWindowPacket.java index 2c4071162..fe00a8a8f 100644 --- a/src/main/java/net/minestom/server/network/packet/client/play/ClientCloseWindowPacket.java +++ b/src/main/java/net/minestom/server/network/packet/client/play/ClientCloseWindowPacket.java @@ -1,17 +1,18 @@ package net.minestom.server.network.packet.client.play; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.client.ClientPacket; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.BYTE; + public record ClientCloseWindowPacket(byte windowId) implements ClientPacket { - public ClientCloseWindowPacket(BinaryReader reader) { - this(reader.readByte()); + public ClientCloseWindowPacket(@NotNull NetworkBuffer reader) { + this(reader.read(BYTE)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeByte(windowId); + public void write(@NotNull NetworkBuffer writer) { + writer.write(BYTE, windowId); } } diff --git a/src/main/java/net/minestom/server/network/packet/client/play/ClientCommandChatPacket.java b/src/main/java/net/minestom/server/network/packet/client/play/ClientCommandChatPacket.java index 910f16fc9..a9d481ae5 100644 --- a/src/main/java/net/minestom/server/network/packet/client/play/ClientCommandChatPacket.java +++ b/src/main/java/net/minestom/server/network/packet/client/play/ClientCommandChatPacket.java @@ -2,28 +2,34 @@ package net.minestom.server.network.packet.client.play; import net.minestom.server.crypto.ArgumentSignatures; import net.minestom.server.crypto.LastSeenMessages; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.client.ClientPacket; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.*; + public record ClientCommandChatPacket(@NotNull String message, long timestamp, long salt, @NotNull ArgumentSignatures signatures, boolean signedPreview, LastSeenMessages.@NotNull Update lastSeenMessages) implements ClientPacket { + public ClientCommandChatPacket { + if (message.length() > 256) { + throw new IllegalArgumentException("Message length cannot be greater than 256"); + } + } - public ClientCommandChatPacket(BinaryReader reader) { - this(reader.readSizedString(256), reader.readLong(), - reader.readLong(), new ArgumentSignatures(reader), reader.readBoolean(), new LastSeenMessages.Update(reader)); + public ClientCommandChatPacket(@NotNull NetworkBuffer reader) { + this(reader.read(STRING), reader.read(LONG), + reader.read(LONG), new ArgumentSignatures(reader), reader.read(BOOLEAN), new LastSeenMessages.Update(reader)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeSizedString(message); - writer.writeLong(timestamp); - writer.writeLong(salt); + public void write(@NotNull NetworkBuffer writer) { + writer.write(STRING, message); + writer.write(LONG, timestamp); + writer.write(LONG, salt); writer.write(signatures); - writer.writeBoolean(signedPreview); + writer.write(BOOLEAN, signedPreview); writer.write(lastSeenMessages); } } diff --git a/src/main/java/net/minestom/server/network/packet/client/play/ClientCraftRecipeRequest.java b/src/main/java/net/minestom/server/network/packet/client/play/ClientCraftRecipeRequest.java index 5e02579d3..5897c2928 100644 --- a/src/main/java/net/minestom/server/network/packet/client/play/ClientCraftRecipeRequest.java +++ b/src/main/java/net/minestom/server/network/packet/client/play/ClientCraftRecipeRequest.java @@ -1,10 +1,11 @@ package net.minestom.server.network.packet.client.play; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.client.ClientPacket; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.*; + public record ClientCraftRecipeRequest(byte windowId, String recipe, boolean makeAll) implements ClientPacket { public ClientCraftRecipeRequest { if (recipe.length() > 256) { @@ -12,14 +13,14 @@ public record ClientCraftRecipeRequest(byte windowId, String recipe, boolean mak } } - public ClientCraftRecipeRequest(BinaryReader reader) { - this(reader.readByte(), reader.readSizedString(256), reader.readBoolean()); + public ClientCraftRecipeRequest(@NotNull NetworkBuffer reader) { + this(reader.read(BYTE), reader.read(STRING), reader.read(BOOLEAN)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeByte(windowId); - writer.writeSizedString(recipe); - writer.writeBoolean(makeAll); + public void write(@NotNull NetworkBuffer writer) { + writer.write(BYTE, windowId); + writer.write(STRING, recipe); + writer.write(BOOLEAN, makeAll); } } diff --git a/src/main/java/net/minestom/server/network/packet/client/play/ClientCreativeInventoryActionPacket.java b/src/main/java/net/minestom/server/network/packet/client/play/ClientCreativeInventoryActionPacket.java index 5296f24f6..f1c382d6f 100644 --- a/src/main/java/net/minestom/server/network/packet/client/play/ClientCreativeInventoryActionPacket.java +++ b/src/main/java/net/minestom/server/network/packet/client/play/ClientCreativeInventoryActionPacket.java @@ -1,19 +1,21 @@ package net.minestom.server.network.packet.client.play; import net.minestom.server.item.ItemStack; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.client.ClientPacket; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.ITEM; +import static net.minestom.server.network.NetworkBuffer.SHORT; + public record ClientCreativeInventoryActionPacket(short slot, @NotNull ItemStack item) implements ClientPacket { - public ClientCreativeInventoryActionPacket(BinaryReader reader) { - this(reader.readShort(), reader.readItemStack()); + public ClientCreativeInventoryActionPacket(@NotNull NetworkBuffer reader) { + this(reader.read(SHORT), reader.read(ITEM)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeShort(slot); - writer.writeItemStack(item); + public void write(@NotNull NetworkBuffer writer) { + writer.write(SHORT, slot); + writer.write(ITEM, item); } } diff --git a/src/main/java/net/minestom/server/network/packet/client/play/ClientEditBookPacket.java b/src/main/java/net/minestom/server/network/packet/client/play/ClientEditBookPacket.java index ca8f298eb..9d2755d59 100644 --- a/src/main/java/net/minestom/server/network/packet/client/play/ClientEditBookPacket.java +++ b/src/main/java/net/minestom/server/network/packet/client/play/ClientEditBookPacket.java @@ -1,29 +1,33 @@ package net.minestom.server.network.packet.client.play; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.client.ClientPacket; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.util.List; +import static net.minestom.server.network.NetworkBuffer.STRING; +import static net.minestom.server.network.NetworkBuffer.VAR_INT; + public record ClientEditBookPacket(int slot, @NotNull List pages, @Nullable String title) implements ClientPacket { public ClientEditBookPacket { pages = List.copyOf(pages); + if (title != null && title.length() > 128) { + throw new IllegalArgumentException("Title length cannot be greater than 128"); + } } - public ClientEditBookPacket(BinaryReader reader) { - this(reader.readVarInt(), reader.readVarIntList(BinaryReader::readSizedString), - reader.readBoolean() ? reader.readSizedString(128) : null); + public ClientEditBookPacket(@NotNull NetworkBuffer reader) { + this(reader.read(VAR_INT), reader.readCollection(STRING), + reader.readOptional(STRING)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeVarInt(slot); - writer.writeVarIntList(pages, BinaryWriter::writeSizedString); - writer.writeBoolean(title != null); - if (title != null) writer.writeSizedString(title); + public void write(@NotNull NetworkBuffer writer) { + writer.write(VAR_INT, slot); + writer.writeCollection(STRING, pages); + writer.writeOptional(STRING, title); } } diff --git a/src/main/java/net/minestom/server/network/packet/client/play/ClientEntityActionPacket.java b/src/main/java/net/minestom/server/network/packet/client/play/ClientEntityActionPacket.java index 7da634ec0..80cbac342 100644 --- a/src/main/java/net/minestom/server/network/packet/client/play/ClientEntityActionPacket.java +++ b/src/main/java/net/minestom/server/network/packet/client/play/ClientEntityActionPacket.java @@ -1,22 +1,23 @@ package net.minestom.server.network.packet.client.play; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.client.ClientPacket; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.VAR_INT; + public record ClientEntityActionPacket(int playerId, @NotNull Action action, int horseJumpBoost) implements ClientPacket { - public ClientEntityActionPacket(BinaryReader reader) { - this(reader.readVarInt(), Action.values()[reader.readVarInt()], - reader.readVarInt()); + public ClientEntityActionPacket(@NotNull NetworkBuffer reader) { + this(reader.read(VAR_INT), reader.readEnum(Action.class), + reader.read(VAR_INT)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeVarInt(playerId); - writer.writeVarInt(action.ordinal()); - writer.writeVarInt(horseJumpBoost); + public void write(@NotNull NetworkBuffer writer) { + writer.write(VAR_INT, playerId); + writer.writeEnum(Action.class, action); + writer.write(VAR_INT, horseJumpBoost); } public enum Action { diff --git a/src/main/java/net/minestom/server/network/packet/client/play/ClientGenerateStructurePacket.java b/src/main/java/net/minestom/server/network/packet/client/play/ClientGenerateStructurePacket.java index 4ecf8bddd..471bc2a74 100644 --- a/src/main/java/net/minestom/server/network/packet/client/play/ClientGenerateStructurePacket.java +++ b/src/main/java/net/minestom/server/network/packet/client/play/ClientGenerateStructurePacket.java @@ -1,21 +1,22 @@ package net.minestom.server.network.packet.client.play; import net.minestom.server.coordinate.Point; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.client.ClientPacket; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.*; + public record ClientGenerateStructurePacket(@NotNull Point blockPosition, int level, boolean keepJigsaws) implements ClientPacket { - public ClientGenerateStructurePacket(BinaryReader reader) { - this(reader.readBlockPosition(), reader.readVarInt(), reader.readBoolean()); + public ClientGenerateStructurePacket(@NotNull NetworkBuffer reader) { + this(reader.read(BLOCK_POSITION), reader.read(VAR_INT), reader.read(BOOLEAN)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeBlockPosition(blockPosition); - writer.writeVarInt(level); - writer.writeBoolean(keepJigsaws); + public void write(@NotNull NetworkBuffer writer) { + writer.write(BLOCK_POSITION, blockPosition); + writer.write(VAR_INT, level); + writer.write(BOOLEAN, keepJigsaws); } } diff --git a/src/main/java/net/minestom/server/network/packet/client/play/ClientHeldItemChangePacket.java b/src/main/java/net/minestom/server/network/packet/client/play/ClientHeldItemChangePacket.java index e2d1239dd..bdac77c69 100644 --- a/src/main/java/net/minestom/server/network/packet/client/play/ClientHeldItemChangePacket.java +++ b/src/main/java/net/minestom/server/network/packet/client/play/ClientHeldItemChangePacket.java @@ -1,17 +1,18 @@ package net.minestom.server.network.packet.client.play; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.client.ClientPacket; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.SHORT; + public record ClientHeldItemChangePacket(short slot) implements ClientPacket { - public ClientHeldItemChangePacket(BinaryReader reader) { - this(reader.readShort()); + public ClientHeldItemChangePacket(@NotNull NetworkBuffer reader) { + this(reader.read(SHORT)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeShort(slot); + public void write(@NotNull NetworkBuffer writer) { + writer.write(SHORT, slot); } } diff --git a/src/main/java/net/minestom/server/network/packet/client/play/ClientInteractEntityPacket.java b/src/main/java/net/minestom/server/network/packet/client/play/ClientInteractEntityPacket.java index 9c87c876d..e16fc7b06 100644 --- a/src/main/java/net/minestom/server/network/packet/client/play/ClientInteractEntityPacket.java +++ b/src/main/java/net/minestom/server/network/packet/client/play/ClientInteractEntityPacket.java @@ -1,43 +1,44 @@ package net.minestom.server.network.packet.client.play; import net.minestom.server.entity.Player; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.client.ClientPacket; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import net.minestom.server.utils.binary.Writeable; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.*; + public record ClientInteractEntityPacket(int targetId, @NotNull Type type, boolean sneaking) implements ClientPacket { - public ClientInteractEntityPacket(BinaryReader reader) { - this(reader.readVarInt(), switch (reader.readVarInt()) { + public ClientInteractEntityPacket(@NotNull NetworkBuffer reader) { + this(reader.read(VAR_INT), switch (reader.read(VAR_INT)) { case 0 -> new Interact(reader); case 1 -> new Attack(); case 2 -> new InteractAt(reader); default -> throw new RuntimeException("Unknown action id"); - }, reader.readBoolean()); + }, reader.read(BOOLEAN)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeVarInt(targetId); - writer.writeVarInt(type.id()); + public void write(@NotNull NetworkBuffer writer) { + writer.write(VAR_INT, targetId); + writer.write(VAR_INT, type.id()); writer.write(type); - writer.writeBoolean(sneaking); + writer.write(BOOLEAN, sneaking); } - public sealed interface Type extends Writeable + public sealed interface Type extends Writer permits Interact, Attack, InteractAt { int id(); } - public record Interact(@NotNull Player.Hand hand) implements Type { - public Interact(BinaryReader reader) { - this(Player.Hand.values()[reader.readVarInt()]); + public record Interact(Player.@NotNull Hand hand) implements Type { + public Interact(@NotNull NetworkBuffer reader) { + this(reader.readEnum(Player.Hand.class)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeVarInt(hand.ordinal()); + public void write(@NotNull NetworkBuffer writer) { + writer.writeEnum(Player.Hand.class, hand); } @Override @@ -48,7 +49,7 @@ public record ClientInteractEntityPacket(int targetId, @NotNull Type type, boole public record Attack() implements Type { @Override - public void write(@NotNull BinaryWriter writer) { + public void write(@NotNull NetworkBuffer writer) { // Empty } @@ -59,18 +60,18 @@ public record ClientInteractEntityPacket(int targetId, @NotNull Type type, boole } public record InteractAt(float targetX, float targetY, float targetZ, - Player.Hand hand) implements Type { - public InteractAt(BinaryReader reader) { - this(reader.readFloat(), reader.readFloat(), reader.readFloat(), - Player.Hand.values()[reader.readVarInt()]); + Player.@NotNull Hand hand) implements Type { + public InteractAt(@NotNull NetworkBuffer reader) { + this(reader.read(FLOAT), reader.read(FLOAT), reader.read(FLOAT), + reader.readEnum(Player.Hand.class)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeFloat(targetX); - writer.writeFloat(targetY); - writer.writeFloat(targetZ); - writer.writeVarInt(hand.ordinal()); + public void write(@NotNull NetworkBuffer writer) { + writer.write(FLOAT, targetX); + writer.write(FLOAT, targetY); + writer.write(FLOAT, targetZ); + writer.writeEnum(Player.Hand.class, hand); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/client/play/ClientKeepAlivePacket.java b/src/main/java/net/minestom/server/network/packet/client/play/ClientKeepAlivePacket.java index 54cd95326..60e659678 100644 --- a/src/main/java/net/minestom/server/network/packet/client/play/ClientKeepAlivePacket.java +++ b/src/main/java/net/minestom/server/network/packet/client/play/ClientKeepAlivePacket.java @@ -1,17 +1,18 @@ package net.minestom.server.network.packet.client.play; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.client.ClientPacket; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.LONG; + public record ClientKeepAlivePacket(long id) implements ClientPacket { - public ClientKeepAlivePacket(BinaryReader reader) { - this(reader.readLong()); + public ClientKeepAlivePacket(@NotNull NetworkBuffer reader) { + this(reader.read(LONG)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeLong(id); + public void write(@NotNull NetworkBuffer writer) { + writer.write(LONG, id); } } diff --git a/src/main/java/net/minestom/server/network/packet/client/play/ClientNameItemPacket.java b/src/main/java/net/minestom/server/network/packet/client/play/ClientNameItemPacket.java index 50f66d959..01ccaefc9 100644 --- a/src/main/java/net/minestom/server/network/packet/client/play/ClientNameItemPacket.java +++ b/src/main/java/net/minestom/server/network/packet/client/play/ClientNameItemPacket.java @@ -1,10 +1,11 @@ package net.minestom.server.network.packet.client.play; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.client.ClientPacket; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.STRING; + public record ClientNameItemPacket(@NotNull String itemName) implements ClientPacket { public ClientNameItemPacket { if (itemName.length() > Short.MAX_VALUE) { @@ -12,12 +13,12 @@ public record ClientNameItemPacket(@NotNull String itemName) implements ClientPa } } - public ClientNameItemPacket(BinaryReader reader) { - this(reader.readSizedString(Short.MAX_VALUE)); + public ClientNameItemPacket(@NotNull NetworkBuffer reader) { + this(reader.read(STRING)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeSizedString(itemName); + public void write(@NotNull NetworkBuffer writer) { + writer.write(STRING, itemName); } } diff --git a/src/main/java/net/minestom/server/network/packet/client/play/ClientPickItemPacket.java b/src/main/java/net/minestom/server/network/packet/client/play/ClientPickItemPacket.java index 702156c9a..42cae8485 100644 --- a/src/main/java/net/minestom/server/network/packet/client/play/ClientPickItemPacket.java +++ b/src/main/java/net/minestom/server/network/packet/client/play/ClientPickItemPacket.java @@ -1,17 +1,18 @@ package net.minestom.server.network.packet.client.play; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.client.ClientPacket; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.VAR_INT; + public record ClientPickItemPacket(int slot) implements ClientPacket { - public ClientPickItemPacket(BinaryReader reader) { - this(reader.readVarInt()); + public ClientPickItemPacket(@NotNull NetworkBuffer reader) { + this(reader.read(VAR_INT)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeVarInt(slot); + public void write(@NotNull NetworkBuffer writer) { + writer.write(VAR_INT, slot); } } diff --git a/src/main/java/net/minestom/server/network/packet/client/play/ClientPlayerAbilitiesPacket.java b/src/main/java/net/minestom/server/network/packet/client/play/ClientPlayerAbilitiesPacket.java index 014fad3a9..9c40a6b49 100644 --- a/src/main/java/net/minestom/server/network/packet/client/play/ClientPlayerAbilitiesPacket.java +++ b/src/main/java/net/minestom/server/network/packet/client/play/ClientPlayerAbilitiesPacket.java @@ -1,17 +1,18 @@ package net.minestom.server.network.packet.client.play; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.client.ClientPacket; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.BYTE; + public record ClientPlayerAbilitiesPacket(byte flags) implements ClientPacket { - public ClientPlayerAbilitiesPacket(BinaryReader reader) { - this(reader.readByte()); + public ClientPlayerAbilitiesPacket(@NotNull NetworkBuffer reader) { + this(reader.read(BYTE)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeByte(flags); + public void write(@NotNull NetworkBuffer writer) { + writer.write(BYTE, flags); } } diff --git a/src/main/java/net/minestom/server/network/packet/client/play/ClientPlayerBlockPlacementPacket.java b/src/main/java/net/minestom/server/network/packet/client/play/ClientPlayerBlockPlacementPacket.java index d8ea14ddb..2f222f1c8 100644 --- a/src/main/java/net/minestom/server/network/packet/client/play/ClientPlayerBlockPlacementPacket.java +++ b/src/main/java/net/minestom/server/network/packet/client/play/ClientPlayerBlockPlacementPacket.java @@ -3,31 +3,32 @@ package net.minestom.server.network.packet.client.play; import net.minestom.server.coordinate.Point; import net.minestom.server.entity.Player; import net.minestom.server.instance.block.BlockFace; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.client.ClientPacket; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.*; + public record ClientPlayerBlockPlacementPacket(@NotNull Player.Hand hand, @NotNull Point blockPosition, @NotNull BlockFace blockFace, float cursorPositionX, float cursorPositionY, float cursorPositionZ, boolean insideBlock, int sequence) implements ClientPacket { - public ClientPlayerBlockPlacementPacket(BinaryReader reader) { - this(Player.Hand.values()[reader.readVarInt()], reader.readBlockPosition(), - BlockFace.values()[reader.readVarInt()], - reader.readFloat(), reader.readFloat(), reader.readFloat(), - reader.readBoolean(), reader.readVarInt()); + public ClientPlayerBlockPlacementPacket(@NotNull NetworkBuffer reader) { + this(reader.readEnum(Player.Hand.class), reader.read(BLOCK_POSITION), + reader.readEnum(BlockFace.class), + reader.read(FLOAT), reader.read(FLOAT), reader.read(FLOAT), + reader.read(BOOLEAN), reader.read(VAR_INT)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeVarInt(hand.ordinal()); - writer.writeBlockPosition(blockPosition); - writer.writeVarInt(blockFace.ordinal()); - writer.writeFloat(cursorPositionX); - writer.writeFloat(cursorPositionY); - writer.writeFloat(cursorPositionZ); - writer.writeBoolean(insideBlock); - writer.writeVarInt(sequence); + public void write(@NotNull NetworkBuffer writer) { + writer.writeEnum(Player.Hand.class, hand); + writer.write(BLOCK_POSITION, blockPosition); + writer.writeEnum(BlockFace.class, blockFace); + writer.write(FLOAT, cursorPositionX); + writer.write(FLOAT, cursorPositionY); + writer.write(FLOAT, cursorPositionZ); + writer.write(BOOLEAN, insideBlock); + writer.write(VAR_INT, sequence); } } diff --git a/src/main/java/net/minestom/server/network/packet/client/play/ClientPlayerDiggingPacket.java b/src/main/java/net/minestom/server/network/packet/client/play/ClientPlayerDiggingPacket.java index d9fc1e491..c6c8c08b1 100644 --- a/src/main/java/net/minestom/server/network/packet/client/play/ClientPlayerDiggingPacket.java +++ b/src/main/java/net/minestom/server/network/packet/client/play/ClientPlayerDiggingPacket.java @@ -2,24 +2,25 @@ package net.minestom.server.network.packet.client.play; import net.minestom.server.coordinate.Point; import net.minestom.server.instance.block.BlockFace; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.client.ClientPacket; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.*; + public record ClientPlayerDiggingPacket(@NotNull Status status, @NotNull Point blockPosition, @NotNull BlockFace blockFace, int sequence) implements ClientPacket { - public ClientPlayerDiggingPacket(BinaryReader reader) { - this(Status.values()[reader.readVarInt()], reader.readBlockPosition(), - BlockFace.values()[reader.readByte()], reader.readVarInt()); + public ClientPlayerDiggingPacket(@NotNull NetworkBuffer reader) { + this(reader.readEnum(Status.class), reader.read(BLOCK_POSITION), + BlockFace.values()[reader.read(BYTE)], reader.read(VAR_INT)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeVarInt(status.ordinal()); - writer.writeBlockPosition(blockPosition); - writer.writeByte((byte) blockFace.ordinal()); - writer.writeVarInt(sequence); + public void write(@NotNull NetworkBuffer writer) { + writer.writeEnum(Status.class, status); + writer.write(BLOCK_POSITION, blockPosition); + writer.write(BYTE, (byte) blockFace.ordinal()); + writer.write(VAR_INT, sequence); } public enum Status { diff --git a/src/main/java/net/minestom/server/network/packet/client/play/ClientPlayerPacket.java b/src/main/java/net/minestom/server/network/packet/client/play/ClientPlayerPacket.java index c495c9dbc..88b607d6d 100644 --- a/src/main/java/net/minestom/server/network/packet/client/play/ClientPlayerPacket.java +++ b/src/main/java/net/minestom/server/network/packet/client/play/ClientPlayerPacket.java @@ -1,17 +1,18 @@ package net.minestom.server.network.packet.client.play; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.client.ClientPacket; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.BOOLEAN; + public record ClientPlayerPacket(boolean onGround) implements ClientPacket { - public ClientPlayerPacket(BinaryReader reader) { - this(reader.readBoolean()); + public ClientPlayerPacket(@NotNull NetworkBuffer reader) { + this(reader.read(BOOLEAN)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeBoolean(onGround); + public void write(@NotNull NetworkBuffer writer) { + writer.write(BOOLEAN, onGround); } } diff --git a/src/main/java/net/minestom/server/network/packet/client/play/ClientPlayerPositionAndRotationPacket.java b/src/main/java/net/minestom/server/network/packet/client/play/ClientPlayerPositionAndRotationPacket.java index 12759e5de..5543dc82c 100644 --- a/src/main/java/net/minestom/server/network/packet/client/play/ClientPlayerPositionAndRotationPacket.java +++ b/src/main/java/net/minestom/server/network/packet/client/play/ClientPlayerPositionAndRotationPacket.java @@ -1,25 +1,26 @@ package net.minestom.server.network.packet.client.play; import net.minestom.server.coordinate.Pos; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.client.ClientPacket; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.*; + public record ClientPlayerPositionAndRotationPacket(@NotNull Pos position, boolean onGround) implements ClientPacket { - public ClientPlayerPositionAndRotationPacket(BinaryReader reader) { - this(new Pos(reader.readDouble(), reader.readDouble(), reader.readDouble(), - reader.readFloat(), reader.readFloat()), reader.readBoolean()); + public ClientPlayerPositionAndRotationPacket(@NotNull NetworkBuffer reader) { + this(new Pos(reader.read(DOUBLE), reader.read(DOUBLE), reader.read(DOUBLE), + reader.read(FLOAT), reader.read(FLOAT)), reader.read(BOOLEAN)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeDouble(position.x()); - writer.writeDouble(position.y()); - writer.writeDouble(position.z()); - writer.writeFloat(position.yaw()); - writer.writeFloat(position.pitch()); - writer.writeBoolean(onGround); + public void write(@NotNull NetworkBuffer writer) { + writer.write(DOUBLE, position.x()); + writer.write(DOUBLE, position.y()); + writer.write(DOUBLE, position.z()); + writer.write(FLOAT, position.yaw()); + writer.write(FLOAT, position.pitch()); + writer.write(BOOLEAN, onGround); } } diff --git a/src/main/java/net/minestom/server/network/packet/client/play/ClientPlayerPositionPacket.java b/src/main/java/net/minestom/server/network/packet/client/play/ClientPlayerPositionPacket.java index 1e1197ff7..a258ab5f7 100644 --- a/src/main/java/net/minestom/server/network/packet/client/play/ClientPlayerPositionPacket.java +++ b/src/main/java/net/minestom/server/network/packet/client/play/ClientPlayerPositionPacket.java @@ -2,23 +2,25 @@ package net.minestom.server.network.packet.client.play; import net.minestom.server.coordinate.Point; import net.minestom.server.coordinate.Vec; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.client.ClientPacket; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.BOOLEAN; +import static net.minestom.server.network.NetworkBuffer.DOUBLE; + public record ClientPlayerPositionPacket(@NotNull Point position, boolean onGround) implements ClientPacket { - public ClientPlayerPositionPacket(BinaryReader reader) { - this(new Vec(reader.readDouble(), reader.readDouble(), reader.readDouble()), - reader.readBoolean()); + public ClientPlayerPositionPacket(@NotNull NetworkBuffer reader) { + this(new Vec(reader.read(DOUBLE), reader.read(DOUBLE), reader.read(DOUBLE)), + reader.read(BOOLEAN)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeDouble(position.x()); - writer.writeDouble(position.y()); - writer.writeDouble(position.z()); - writer.writeBoolean(onGround); + public void write(@NotNull NetworkBuffer writer) { + writer.write(DOUBLE, position.x()); + writer.write(DOUBLE, position.y()); + writer.write(DOUBLE, position.z()); + writer.write(BOOLEAN, onGround); } } diff --git a/src/main/java/net/minestom/server/network/packet/client/play/ClientPlayerRotationPacket.java b/src/main/java/net/minestom/server/network/packet/client/play/ClientPlayerRotationPacket.java index 5e90b17a2..23e8e46b3 100644 --- a/src/main/java/net/minestom/server/network/packet/client/play/ClientPlayerRotationPacket.java +++ b/src/main/java/net/minestom/server/network/packet/client/play/ClientPlayerRotationPacket.java @@ -1,19 +1,21 @@ package net.minestom.server.network.packet.client.play; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.client.ClientPacket; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.BOOLEAN; +import static net.minestom.server.network.NetworkBuffer.FLOAT; + public record ClientPlayerRotationPacket(float yaw, float pitch, boolean onGround) implements ClientPacket { - public ClientPlayerRotationPacket(BinaryReader reader) { - this(reader.readFloat(), reader.readFloat(), reader.readBoolean()); + public ClientPlayerRotationPacket(@NotNull NetworkBuffer reader) { + this(reader.read(FLOAT), reader.read(FLOAT), reader.read(BOOLEAN)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeFloat(yaw); - writer.writeFloat(pitch); - writer.writeBoolean(onGround); + public void write(@NotNull NetworkBuffer writer) { + writer.write(FLOAT, yaw); + writer.write(FLOAT, pitch); + writer.write(BOOLEAN, onGround); } } diff --git a/src/main/java/net/minestom/server/network/packet/client/play/ClientPluginMessagePacket.java b/src/main/java/net/minestom/server/network/packet/client/play/ClientPluginMessagePacket.java index 98dde8bad..16ac11275 100644 --- a/src/main/java/net/minestom/server/network/packet/client/play/ClientPluginMessagePacket.java +++ b/src/main/java/net/minestom/server/network/packet/client/play/ClientPluginMessagePacket.java @@ -1,23 +1,25 @@ package net.minestom.server.network.packet.client.play; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.client.ClientPacket; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.RAW_BYTES; +import static net.minestom.server.network.NetworkBuffer.STRING; + public record ClientPluginMessagePacket(@NotNull String channel, byte[] data) implements ClientPacket { public ClientPluginMessagePacket { if (channel.length() > 256) throw new IllegalArgumentException("Channel cannot be more than 256 characters long"); } - public ClientPluginMessagePacket(BinaryReader reader) { - this(reader.readSizedString(256), reader.readRemainingBytes()); + public ClientPluginMessagePacket(@NotNull NetworkBuffer reader) { + this(reader.read(STRING), reader.read(RAW_BYTES)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeSizedString(channel); - writer.writeBytes(data); + public void write(@NotNull NetworkBuffer writer) { + writer.write(STRING, channel); + writer.write(RAW_BYTES, data); } } diff --git a/src/main/java/net/minestom/server/network/packet/client/play/ClientPongPacket.java b/src/main/java/net/minestom/server/network/packet/client/play/ClientPongPacket.java index 9198bb708..d3ef187e4 100644 --- a/src/main/java/net/minestom/server/network/packet/client/play/ClientPongPacket.java +++ b/src/main/java/net/minestom/server/network/packet/client/play/ClientPongPacket.java @@ -1,17 +1,18 @@ package net.minestom.server.network.packet.client.play; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.client.ClientPacket; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.INT; + public record ClientPongPacket(int id) implements ClientPacket { - public ClientPongPacket(BinaryReader reader) { - this(reader.readInt()); + public ClientPongPacket(@NotNull NetworkBuffer reader) { + this(reader.read(INT)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeInt(id); + public void write(@NotNull NetworkBuffer writer) { + writer.write(INT, id); } } diff --git a/src/main/java/net/minestom/server/network/packet/client/play/ClientQueryBlockNbtPacket.java b/src/main/java/net/minestom/server/network/packet/client/play/ClientQueryBlockNbtPacket.java index 37abbb140..24a815183 100644 --- a/src/main/java/net/minestom/server/network/packet/client/play/ClientQueryBlockNbtPacket.java +++ b/src/main/java/net/minestom/server/network/packet/client/play/ClientQueryBlockNbtPacket.java @@ -1,19 +1,21 @@ package net.minestom.server.network.packet.client.play; import net.minestom.server.coordinate.Point; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.client.ClientPacket; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.BLOCK_POSITION; +import static net.minestom.server.network.NetworkBuffer.VAR_INT; + public record ClientQueryBlockNbtPacket(int transactionId, @NotNull Point blockPosition) implements ClientPacket { - public ClientQueryBlockNbtPacket(BinaryReader reader) { - this(reader.readVarInt(), reader.readBlockPosition()); + public ClientQueryBlockNbtPacket(@NotNull NetworkBuffer reader) { + this(reader.read(VAR_INT), reader.read(BLOCK_POSITION)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeVarInt(transactionId); - writer.writeBlockPosition(blockPosition); + public void write(@NotNull NetworkBuffer writer) { + writer.write(VAR_INT, transactionId); + writer.write(BLOCK_POSITION, blockPosition); } } diff --git a/src/main/java/net/minestom/server/network/packet/client/play/ClientQueryEntityNbtPacket.java b/src/main/java/net/minestom/server/network/packet/client/play/ClientQueryEntityNbtPacket.java index c650f7207..007dd64c1 100644 --- a/src/main/java/net/minestom/server/network/packet/client/play/ClientQueryEntityNbtPacket.java +++ b/src/main/java/net/minestom/server/network/packet/client/play/ClientQueryEntityNbtPacket.java @@ -1,18 +1,19 @@ package net.minestom.server.network.packet.client.play; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.client.ClientPacket; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.VAR_INT; + public record ClientQueryEntityNbtPacket(int transactionId, int entityId) implements ClientPacket { - public ClientQueryEntityNbtPacket(BinaryReader reader) { - this(reader.readVarInt(), reader.readVarInt()); + public ClientQueryEntityNbtPacket(@NotNull NetworkBuffer reader) { + this(reader.read(VAR_INT), reader.read(VAR_INT)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeVarInt(transactionId); - writer.writeVarInt(entityId); + public void write(@NotNull NetworkBuffer writer) { + writer.write(VAR_INT, transactionId); + writer.write(VAR_INT, entityId); } } diff --git a/src/main/java/net/minestom/server/network/packet/client/play/ClientResourcePackStatusPacket.java b/src/main/java/net/minestom/server/network/packet/client/play/ClientResourcePackStatusPacket.java index 49bcccd7e..f998c3a18 100644 --- a/src/main/java/net/minestom/server/network/packet/client/play/ClientResourcePackStatusPacket.java +++ b/src/main/java/net/minestom/server/network/packet/client/play/ClientResourcePackStatusPacket.java @@ -1,18 +1,17 @@ package net.minestom.server.network.packet.client.play; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.client.ClientPacket; import net.minestom.server.resourcepack.ResourcePackStatus; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; public record ClientResourcePackStatusPacket(@NotNull ResourcePackStatus status) implements ClientPacket { - public ClientResourcePackStatusPacket(BinaryReader reader) { - this(ResourcePackStatus.values()[reader.readVarInt()]); + public ClientResourcePackStatusPacket(@NotNull NetworkBuffer reader) { + this(reader.readEnum(ResourcePackStatus.class)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeVarInt(status.ordinal()); + public void write(@NotNull NetworkBuffer writer) { + writer.writeEnum(ResourcePackStatus.class, status); } } diff --git a/src/main/java/net/minestom/server/network/packet/client/play/ClientSelectTradePacket.java b/src/main/java/net/minestom/server/network/packet/client/play/ClientSelectTradePacket.java index 6c99fa530..20cba35cf 100644 --- a/src/main/java/net/minestom/server/network/packet/client/play/ClientSelectTradePacket.java +++ b/src/main/java/net/minestom/server/network/packet/client/play/ClientSelectTradePacket.java @@ -1,17 +1,18 @@ package net.minestom.server.network.packet.client.play; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.client.ClientPacket; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.VAR_INT; + public record ClientSelectTradePacket(int selectedSlot) implements ClientPacket { - public ClientSelectTradePacket(BinaryReader reader) { - this(reader.readVarInt()); + public ClientSelectTradePacket(@NotNull NetworkBuffer reader) { + this(reader.read(VAR_INT)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeVarInt(selectedSlot); + public void write(@NotNull NetworkBuffer writer) { + writer.write(VAR_INT, selectedSlot); } } diff --git a/src/main/java/net/minestom/server/network/packet/client/play/ClientSetBeaconEffectPacket.java b/src/main/java/net/minestom/server/network/packet/client/play/ClientSetBeaconEffectPacket.java index f532ad54c..e9676740a 100644 --- a/src/main/java/net/minestom/server/network/packet/client/play/ClientSetBeaconEffectPacket.java +++ b/src/main/java/net/minestom/server/network/packet/client/play/ClientSetBeaconEffectPacket.java @@ -1,24 +1,26 @@ package net.minestom.server.network.packet.client.play; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.client.ClientPacket; import net.minestom.server.potion.PotionType; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import static net.minestom.server.network.NetworkBuffer.BOOLEAN; +import static net.minestom.server.network.NetworkBuffer.VAR_INT; + public record ClientSetBeaconEffectPacket(@Nullable PotionType primaryEffect, @Nullable PotionType secondaryEffect) implements ClientPacket { - public ClientSetBeaconEffectPacket(BinaryReader reader) { - this(reader.readBoolean() ? PotionType.fromId(reader.readVarInt()) : null, - reader.readBoolean() ? PotionType.fromId(reader.readVarInt()) : null); + public ClientSetBeaconEffectPacket(@NotNull NetworkBuffer reader) { + this(reader.read(BOOLEAN) ? PotionType.fromId(reader.read(VAR_INT)) : null, + reader.read(BOOLEAN) ? PotionType.fromId(reader.read(VAR_INT)) : null); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeBoolean(primaryEffect != null); - if (primaryEffect != null) writer.writeVarInt(primaryEffect.id()); - writer.writeBoolean(secondaryEffect != null); - if (secondaryEffect != null) writer.writeVarInt(secondaryEffect.id()); + public void write(@NotNull NetworkBuffer writer) { + writer.write(BOOLEAN, primaryEffect != null); + if (primaryEffect != null) writer.write(VAR_INT, primaryEffect.id()); + writer.write(BOOLEAN, secondaryEffect != null); + if (secondaryEffect != null) writer.write(VAR_INT, secondaryEffect.id()); } } diff --git a/src/main/java/net/minestom/server/network/packet/client/play/ClientSetDisplayedRecipePacket.java b/src/main/java/net/minestom/server/network/packet/client/play/ClientSetDisplayedRecipePacket.java index 827496ae8..f534f9c62 100644 --- a/src/main/java/net/minestom/server/network/packet/client/play/ClientSetDisplayedRecipePacket.java +++ b/src/main/java/net/minestom/server/network/packet/client/play/ClientSetDisplayedRecipePacket.java @@ -1,17 +1,24 @@ package net.minestom.server.network.packet.client.play; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.client.ClientPacket; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.STRING; + public record ClientSetDisplayedRecipePacket(@NotNull String recipeId) implements ClientPacket { - public ClientSetDisplayedRecipePacket(BinaryReader reader) { - this(reader.readSizedString(256)); + public ClientSetDisplayedRecipePacket { + if (recipeId.length() > 256) { + throw new IllegalArgumentException("'recipeId' cannot be longer than 256 characters."); + } + } + + public ClientSetDisplayedRecipePacket(@NotNull NetworkBuffer reader) { + this(reader.read(STRING)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeSizedString(recipeId); + public void write(@NotNull NetworkBuffer writer) { + writer.write(STRING, recipeId); } } \ No newline at end of file diff --git a/src/main/java/net/minestom/server/network/packet/client/play/ClientSetRecipeBookStatePacket.java b/src/main/java/net/minestom/server/network/packet/client/play/ClientSetRecipeBookStatePacket.java index e1ccc84ea..f9109124a 100644 --- a/src/main/java/net/minestom/server/network/packet/client/play/ClientSetRecipeBookStatePacket.java +++ b/src/main/java/net/minestom/server/network/packet/client/play/ClientSetRecipeBookStatePacket.java @@ -1,21 +1,22 @@ package net.minestom.server.network.packet.client.play; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.client.ClientPacket; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.BOOLEAN; + public record ClientSetRecipeBookStatePacket(@NotNull BookType bookType, boolean bookOpen, boolean filterActive) implements ClientPacket { - public ClientSetRecipeBookStatePacket(BinaryReader reader) { - this(BookType.values()[reader.readVarInt()], reader.readBoolean(), reader.readBoolean()); + public ClientSetRecipeBookStatePacket(@NotNull NetworkBuffer reader) { + this(reader.readEnum(BookType.class), reader.read(BOOLEAN), reader.read(BOOLEAN)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeVarInt(bookType.ordinal()); - writer.writeBoolean(bookOpen); - writer.writeBoolean(filterActive); + public void write(@NotNull NetworkBuffer writer) { + writer.writeEnum(BookType.class, bookType); + writer.write(BOOLEAN, bookOpen); + writer.write(BOOLEAN, filterActive); } public enum BookType { diff --git a/src/main/java/net/minestom/server/network/packet/client/play/ClientSettingsPacket.java b/src/main/java/net/minestom/server/network/packet/client/play/ClientSettingsPacket.java index 88746dd48..f02b383ca 100644 --- a/src/main/java/net/minestom/server/network/packet/client/play/ClientSettingsPacket.java +++ b/src/main/java/net/minestom/server/network/packet/client/play/ClientSettingsPacket.java @@ -2,11 +2,12 @@ package net.minestom.server.network.packet.client.play; import net.minestom.server.entity.Player; import net.minestom.server.message.ChatMessageType; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.client.ClientPacket; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.*; + public record ClientSettingsPacket(@NotNull String locale, byte viewDistance, @NotNull ChatMessageType chatMessageType, boolean chatColors, byte displayedSkinParts, @NotNull Player.MainHand mainHand, @@ -16,22 +17,22 @@ public record ClientSettingsPacket(@NotNull String locale, byte viewDistance, throw new IllegalArgumentException("Locale cannot be longer than 128 characters."); } - public ClientSettingsPacket(BinaryReader reader) { - this(reader.readSizedString(128), reader.readByte(), - ChatMessageType.fromPacketID(reader.readVarInt()), reader.readBoolean(), - reader.readByte(), Player.MainHand.values()[reader.readVarInt()], - reader.readBoolean(), reader.readBoolean()); + public ClientSettingsPacket(@NotNull NetworkBuffer reader) { + this(reader.read(STRING), reader.read(BYTE), + ChatMessageType.fromPacketID(reader.read(VAR_INT)), reader.read(BOOLEAN), + reader.read(BYTE), reader.readEnum(Player.MainHand.class), + reader.read(BOOLEAN), reader.read(BOOLEAN)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeSizedString(locale); - writer.writeByte(viewDistance); - writer.writeVarInt(chatMessageType.getPacketID()); - writer.writeBoolean(chatColors); - writer.writeByte(displayedSkinParts); - writer.writeVarInt(mainHand.ordinal()); - writer.writeBoolean(enableTextFiltering); - writer.writeBoolean(allowsListing); + public void write(@NotNull NetworkBuffer writer) { + writer.write(STRING, locale); + writer.write(BYTE, viewDistance); + writer.write(VAR_INT, chatMessageType.getPacketID()); + writer.write(BOOLEAN, chatColors); + writer.write(BYTE, displayedSkinParts); + writer.write(VAR_INT, mainHand.ordinal()); + writer.write(BOOLEAN, enableTextFiltering); + writer.write(BOOLEAN, allowsListing); } } diff --git a/src/main/java/net/minestom/server/network/packet/client/play/ClientSpectatePacket.java b/src/main/java/net/minestom/server/network/packet/client/play/ClientSpectatePacket.java index 33e689633..cfdc5dbb8 100644 --- a/src/main/java/net/minestom/server/network/packet/client/play/ClientSpectatePacket.java +++ b/src/main/java/net/minestom/server/network/packet/client/play/ClientSpectatePacket.java @@ -1,8 +1,7 @@ package net.minestom.server.network.packet.client.play; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.client.ClientPacket; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; import java.util.UUID; @@ -13,12 +12,12 @@ import java.util.UUID; * rather than spectating them. */ public record ClientSpectatePacket(@NotNull UUID target) implements ClientPacket { - public ClientSpectatePacket(BinaryReader reader) { - this(reader.readUuid()); + public ClientSpectatePacket(@NotNull NetworkBuffer reader) { + this(reader.read(NetworkBuffer.UUID)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeUuid(target); + public void write(@NotNull NetworkBuffer writer) { + writer.write(NetworkBuffer.UUID, target); } } diff --git a/src/main/java/net/minestom/server/network/packet/client/play/ClientStatusPacket.java b/src/main/java/net/minestom/server/network/packet/client/play/ClientStatusPacket.java index 57640b449..079aeb3f1 100644 --- a/src/main/java/net/minestom/server/network/packet/client/play/ClientStatusPacket.java +++ b/src/main/java/net/minestom/server/network/packet/client/play/ClientStatusPacket.java @@ -1,18 +1,17 @@ package net.minestom.server.network.packet.client.play; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.client.ClientPacket; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; public record ClientStatusPacket(@NotNull Action action) implements ClientPacket { - public ClientStatusPacket(BinaryReader reader) { - this(Action.values()[reader.readVarInt()]); + public ClientStatusPacket(@NotNull NetworkBuffer reader) { + this(reader.readEnum(Action.class)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeVarInt(action.ordinal()); + public void write(@NotNull NetworkBuffer writer) { + writer.writeEnum(Action.class, action); } public enum Action { diff --git a/src/main/java/net/minestom/server/network/packet/client/play/ClientSteerBoatPacket.java b/src/main/java/net/minestom/server/network/packet/client/play/ClientSteerBoatPacket.java index d7343c953..05ed9b36d 100644 --- a/src/main/java/net/minestom/server/network/packet/client/play/ClientSteerBoatPacket.java +++ b/src/main/java/net/minestom/server/network/packet/client/play/ClientSteerBoatPacket.java @@ -1,18 +1,19 @@ package net.minestom.server.network.packet.client.play; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.client.ClientPacket; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.BOOLEAN; + public record ClientSteerBoatPacket(boolean leftPaddleTurning, boolean rightPaddleTurning) implements ClientPacket { - public ClientSteerBoatPacket(BinaryReader reader) { - this(reader.readBoolean(), reader.readBoolean()); + public ClientSteerBoatPacket(@NotNull NetworkBuffer reader) { + this(reader.read(BOOLEAN), reader.read(BOOLEAN)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeBoolean(leftPaddleTurning); - writer.writeBoolean(rightPaddleTurning); + public void write(@NotNull NetworkBuffer writer) { + writer.write(BOOLEAN, leftPaddleTurning); + writer.write(BOOLEAN, rightPaddleTurning); } } diff --git a/src/main/java/net/minestom/server/network/packet/client/play/ClientSteerVehiclePacket.java b/src/main/java/net/minestom/server/network/packet/client/play/ClientSteerVehiclePacket.java index 609819dcf..95cb55f81 100644 --- a/src/main/java/net/minestom/server/network/packet/client/play/ClientSteerVehiclePacket.java +++ b/src/main/java/net/minestom/server/network/packet/client/play/ClientSteerVehiclePacket.java @@ -1,20 +1,22 @@ package net.minestom.server.network.packet.client.play; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.client.ClientPacket; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.BYTE; +import static net.minestom.server.network.NetworkBuffer.FLOAT; + public record ClientSteerVehiclePacket(float sideways, float forward, byte flags) implements ClientPacket { - public ClientSteerVehiclePacket(BinaryReader reader) { - this(reader.readFloat(), reader.readFloat(), reader.readByte()); + public ClientSteerVehiclePacket(@NotNull NetworkBuffer reader) { + this(reader.read(FLOAT), reader.read(FLOAT), reader.read(BYTE)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeFloat(sideways); - writer.writeFloat(forward); - writer.writeByte(flags); + public void write(@NotNull NetworkBuffer writer) { + writer.write(FLOAT, sideways); + writer.write(FLOAT, forward); + writer.write(BYTE, flags); } } diff --git a/src/main/java/net/minestom/server/network/packet/client/play/ClientTabCompletePacket.java b/src/main/java/net/minestom/server/network/packet/client/play/ClientTabCompletePacket.java index e1089b6b5..857d7b370 100644 --- a/src/main/java/net/minestom/server/network/packet/client/play/ClientTabCompletePacket.java +++ b/src/main/java/net/minestom/server/network/packet/client/play/ClientTabCompletePacket.java @@ -1,18 +1,20 @@ package net.minestom.server.network.packet.client.play; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.client.ClientPacket; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.STRING; +import static net.minestom.server.network.NetworkBuffer.VAR_INT; + public record ClientTabCompletePacket(int transactionId, @NotNull String text) implements ClientPacket { - public ClientTabCompletePacket(BinaryReader reader) { - this(reader.readVarInt(), reader.readSizedString(Short.MAX_VALUE)); + public ClientTabCompletePacket(@NotNull NetworkBuffer reader) { + this(reader.read(VAR_INT), reader.read(STRING)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeVarInt(transactionId); - writer.writeSizedString(text); + public void write(@NotNull NetworkBuffer writer) { + writer.write(VAR_INT, transactionId); + writer.write(STRING, text); } } diff --git a/src/main/java/net/minestom/server/network/packet/client/play/ClientTeleportConfirmPacket.java b/src/main/java/net/minestom/server/network/packet/client/play/ClientTeleportConfirmPacket.java index 234908526..374f8853d 100644 --- a/src/main/java/net/minestom/server/network/packet/client/play/ClientTeleportConfirmPacket.java +++ b/src/main/java/net/minestom/server/network/packet/client/play/ClientTeleportConfirmPacket.java @@ -1,17 +1,18 @@ package net.minestom.server.network.packet.client.play; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.client.ClientPacket; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.VAR_INT; + public record ClientTeleportConfirmPacket(int teleportId) implements ClientPacket { - public ClientTeleportConfirmPacket(BinaryReader reader) { - this(reader.readVarInt()); + public ClientTeleportConfirmPacket(@NotNull NetworkBuffer reader) { + this(reader.read(VAR_INT)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeVarInt(teleportId); + public void write(@NotNull NetworkBuffer writer) { + writer.write(VAR_INT, teleportId); } } diff --git a/src/main/java/net/minestom/server/network/packet/client/play/ClientUpdateCommandBlockMinecartPacket.java b/src/main/java/net/minestom/server/network/packet/client/play/ClientUpdateCommandBlockMinecartPacket.java index f084c22da..0a87117b9 100644 --- a/src/main/java/net/minestom/server/network/packet/client/play/ClientUpdateCommandBlockMinecartPacket.java +++ b/src/main/java/net/minestom/server/network/packet/client/play/ClientUpdateCommandBlockMinecartPacket.java @@ -1,20 +1,21 @@ package net.minestom.server.network.packet.client.play; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.client.ClientPacket; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.*; + public record ClientUpdateCommandBlockMinecartPacket(int entityId, @NotNull String command, boolean trackOutput) implements ClientPacket { - public ClientUpdateCommandBlockMinecartPacket(BinaryReader reader) { - this(reader.readVarInt(), reader.readSizedString(Short.MAX_VALUE), reader.readBoolean()); + public ClientUpdateCommandBlockMinecartPacket(@NotNull NetworkBuffer reader) { + this(reader.read(VAR_INT), reader.read(STRING), reader.read(BOOLEAN)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeVarInt(entityId); - writer.writeSizedString(command); - writer.writeBoolean(trackOutput); + public void write(@NotNull NetworkBuffer writer) { + writer.write(VAR_INT, entityId); + writer.write(STRING, command); + writer.write(BOOLEAN, trackOutput); } } diff --git a/src/main/java/net/minestom/server/network/packet/client/play/ClientUpdateCommandBlockPacket.java b/src/main/java/net/minestom/server/network/packet/client/play/ClientUpdateCommandBlockPacket.java index 096769d1c..5c8890f27 100644 --- a/src/main/java/net/minestom/server/network/packet/client/play/ClientUpdateCommandBlockPacket.java +++ b/src/main/java/net/minestom/server/network/packet/client/play/ClientUpdateCommandBlockPacket.java @@ -1,24 +1,25 @@ package net.minestom.server.network.packet.client.play; import net.minestom.server.coordinate.Point; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.client.ClientPacket; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.*; + public record ClientUpdateCommandBlockPacket(@NotNull Point blockPosition, @NotNull String command, @NotNull Mode mode, byte flags) implements ClientPacket { - public ClientUpdateCommandBlockPacket(BinaryReader reader) { - this(reader.readBlockPosition(), reader.readSizedString(Short.MAX_VALUE), - Mode.values()[reader.readVarInt()], reader.readByte()); + public ClientUpdateCommandBlockPacket(@NotNull NetworkBuffer reader) { + this(reader.read(BLOCK_POSITION), reader.read(STRING), + Mode.values()[reader.read(VAR_INT)], reader.read(BYTE)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeBlockPosition(blockPosition); - writer.writeSizedString(command); - writer.writeVarInt(mode.ordinal()); - writer.writeByte(flags); + public void write(@NotNull NetworkBuffer writer) { + writer.write(BLOCK_POSITION, blockPosition); + writer.write(STRING, command); + writer.write(VAR_INT, mode.ordinal()); + writer.write(BYTE, flags); } public enum Mode { diff --git a/src/main/java/net/minestom/server/network/packet/client/play/ClientUpdateSignPacket.java b/src/main/java/net/minestom/server/network/packet/client/play/ClientUpdateSignPacket.java index 55c733924..2c7aaec2e 100644 --- a/src/main/java/net/minestom/server/network/packet/client/play/ClientUpdateSignPacket.java +++ b/src/main/java/net/minestom/server/network/packet/client/play/ClientUpdateSignPacket.java @@ -1,13 +1,15 @@ package net.minestom.server.network.packet.client.play; import net.minestom.server.coordinate.Point; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.client.ClientPacket; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; import java.util.List; +import static net.minestom.server.network.NetworkBuffer.BLOCK_POSITION; +import static net.minestom.server.network.NetworkBuffer.STRING; + public record ClientUpdateSignPacket(@NotNull Point blockPosition, @NotNull List lines) implements ClientPacket { public ClientUpdateSignPacket { @@ -22,21 +24,21 @@ public record ClientUpdateSignPacket(@NotNull Point blockPosition, } } - public ClientUpdateSignPacket(BinaryReader reader) { - this(reader.readBlockPosition(), readLines(reader)); + public ClientUpdateSignPacket(@NotNull NetworkBuffer reader) { + this(reader.read(BLOCK_POSITION), readLines(reader)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeBlockPosition(blockPosition); - writer.writeSizedString(lines.get(0)); - writer.writeSizedString(lines.get(1)); - writer.writeSizedString(lines.get(2)); - writer.writeSizedString(lines.get(3)); + public void write(@NotNull NetworkBuffer writer) { + writer.write(BLOCK_POSITION, blockPosition); + writer.write(STRING, lines.get(0)); + writer.write(STRING, lines.get(1)); + writer.write(STRING, lines.get(2)); + writer.write(STRING, lines.get(3)); } - private static List readLines(BinaryReader reader) { - return List.of(reader.readSizedString(384), reader.readSizedString(384), - reader.readSizedString(384), reader.readSizedString(384)); + private static List readLines(@NotNull NetworkBuffer reader) { + return List.of(reader.read(STRING), reader.read(STRING), + reader.read(STRING), reader.read(STRING)); } } diff --git a/src/main/java/net/minestom/server/network/packet/client/play/ClientUpdateStructureBlockPacket.java b/src/main/java/net/minestom/server/network/packet/client/play/ClientUpdateStructureBlockPacket.java index d8a8366c0..aa4daa072 100644 --- a/src/main/java/net/minestom/server/network/packet/client/play/ClientUpdateStructureBlockPacket.java +++ b/src/main/java/net/minestom/server/network/packet/client/play/ClientUpdateStructureBlockPacket.java @@ -2,25 +2,26 @@ package net.minestom.server.network.packet.client.play; import net.minestom.server.coordinate.Point; import net.minestom.server.coordinate.Vec; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.client.ClientPacket; import net.minestom.server.utils.Rotation; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.*; + public record ClientUpdateStructureBlockPacket(Point location, Action action, Mode mode, String name, Point offset, Point size, Mirror mirror, Rotation rotation, String metadata, float integrity, long seed, byte flags) implements ClientPacket { - public ClientUpdateStructureBlockPacket(BinaryReader reader) { - this(reader.readBlockPosition(), Action.values()[reader.readVarInt()], - Mode.values()[reader.readVarInt()], reader.readSizedString(Short.MAX_VALUE), - new Vec(reader.readByte(), reader.readByte(), reader.readByte()), new Vec(reader.readByte(), reader.readByte(), reader.readByte()), - Mirror.values()[reader.readVarInt()], fromRestrictedRotation(reader.readVarInt()), - reader.readSizedString(Short.MAX_VALUE), reader.readFloat(), - reader.readVarLong(), reader.readByte()); + public ClientUpdateStructureBlockPacket(@NotNull NetworkBuffer reader) { + this(reader.read(BLOCK_POSITION), reader.readEnum(Action.class), + reader.readEnum(Mode.class), reader.read(STRING), + new Vec(reader.read(BYTE), reader.read(BYTE), reader.read(BYTE)), new Vec(reader.read(BYTE), reader.read(BYTE), reader.read(BYTE)), + Mirror.values()[reader.read(VAR_INT)], fromRestrictedRotation(reader.read(VAR_INT)), + reader.read(STRING), reader.read(FLOAT), + reader.read(VAR_LONG), reader.read(BYTE)); } // Flag values @@ -32,23 +33,23 @@ public record ClientUpdateStructureBlockPacket(Point location, Action action, public static final byte SHOW_BOUNDING_BOX = 0x4; @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeBlockPosition(location); - writer.writeVarInt(action.ordinal()); - writer.writeVarInt(mode.ordinal()); - writer.writeSizedString(name); - writer.writeByte((byte) offset.x()); - writer.writeByte((byte) offset.y()); - writer.writeByte((byte) offset.z()); - writer.writeByte((byte) size.x()); - writer.writeByte((byte) size.y()); - writer.writeByte((byte) size.z()); - writer.writeVarInt(mirror.ordinal()); - writer.writeVarInt(toRestrictedRotation(rotation)); - writer.writeSizedString(metadata); - writer.writeFloat(integrity); - writer.writeVarLong(seed); - writer.writeByte(flags); + public void write(@NotNull NetworkBuffer writer) { + writer.write(BLOCK_POSITION, location); + writer.writeEnum(Action.class, action); + writer.writeEnum(Mode.class, mode); + writer.write(STRING, name); + writer.write(BYTE, (byte) offset.x()); + writer.write(BYTE, (byte) offset.y()); + writer.write(BYTE, (byte) offset.z()); + writer.write(BYTE, (byte) size.x()); + writer.write(BYTE, (byte) size.y()); + writer.write(BYTE, (byte) size.z()); + writer.write(VAR_INT, mirror.ordinal()); + writer.write(VAR_INT, toRestrictedRotation(rotation)); + writer.write(STRING, metadata); + writer.write(FLOAT, integrity); + writer.write(VAR_LONG, seed); + writer.write(BYTE, flags); } /** @@ -72,7 +73,8 @@ public record ClientUpdateStructureBlockPacket(Point location, Action action, case CLOCKWISE -> 1; case FLIPPED -> 2; case COUNTER_CLOCKWISE -> 3; - default -> throw new IllegalArgumentException("ClientUpdateStructurePacket#rotation must be a valid 90-degree rotation."); + default -> + throw new IllegalArgumentException("ClientUpdateStructurePacket#rotation must be a valid 90-degree rotation."); }; } @@ -82,7 +84,8 @@ public record ClientUpdateStructureBlockPacket(Point location, Action action, case 1 -> Rotation.CLOCKWISE; case 2 -> Rotation.FLIPPED; case 3 -> Rotation.COUNTER_CLOCKWISE; - default -> throw new IllegalArgumentException("ClientUpdateStructurePacket#rotation must be a valid 90-degree rotation."); + default -> + throw new IllegalArgumentException("ClientUpdateStructurePacket#rotation must be a valid 90-degree rotation."); }; } } diff --git a/src/main/java/net/minestom/server/network/packet/client/play/ClientUseItemPacket.java b/src/main/java/net/minestom/server/network/packet/client/play/ClientUseItemPacket.java index 3ea3af30b..178cc2b50 100644 --- a/src/main/java/net/minestom/server/network/packet/client/play/ClientUseItemPacket.java +++ b/src/main/java/net/minestom/server/network/packet/client/play/ClientUseItemPacket.java @@ -1,19 +1,20 @@ package net.minestom.server.network.packet.client.play; import net.minestom.server.entity.Player; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.client.ClientPacket; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.VAR_INT; + public record ClientUseItemPacket(@NotNull Player.Hand hand, int sequence) implements ClientPacket { - public ClientUseItemPacket(BinaryReader reader) { - this(Player.Hand.values()[reader.readVarInt()], reader.readVarInt()); + public ClientUseItemPacket(@NotNull NetworkBuffer reader) { + this(reader.readEnum(Player.Hand.class), reader.read(VAR_INT)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeVarInt(hand.ordinal()); - writer.writeVarInt(sequence); + public void write(@NotNull NetworkBuffer writer) { + writer.writeEnum(Player.Hand.class, hand); + writer.write(VAR_INT, sequence); } } diff --git a/src/main/java/net/minestom/server/network/packet/client/play/ClientVehicleMovePacket.java b/src/main/java/net/minestom/server/network/packet/client/play/ClientVehicleMovePacket.java index 0c6901846..ffbba4177 100644 --- a/src/main/java/net/minestom/server/network/packet/client/play/ClientVehicleMovePacket.java +++ b/src/main/java/net/minestom/server/network/packet/client/play/ClientVehicleMovePacket.java @@ -1,23 +1,25 @@ package net.minestom.server.network.packet.client.play; import net.minestom.server.coordinate.Pos; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.client.ClientPacket; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.DOUBLE; +import static net.minestom.server.network.NetworkBuffer.FLOAT; + public record ClientVehicleMovePacket(@NotNull Pos position) implements ClientPacket { - public ClientVehicleMovePacket(BinaryReader reader) { - this(new Pos(reader.readDouble(), reader.readDouble(), reader.readDouble(), - reader.readFloat(), reader.readFloat())); + public ClientVehicleMovePacket(@NotNull NetworkBuffer reader) { + this(new Pos(reader.read(DOUBLE), reader.read(DOUBLE), reader.read(DOUBLE), + reader.read(FLOAT), reader.read(FLOAT))); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeDouble(position.x()); - writer.writeDouble(position.y()); - writer.writeDouble(position.z()); - writer.writeFloat(position.yaw()); - writer.writeFloat(position.pitch()); + public void write(@NotNull NetworkBuffer writer) { + writer.write(DOUBLE, position.x()); + writer.write(DOUBLE, position.y()); + writer.write(DOUBLE, position.z()); + writer.write(FLOAT, position.yaw()); + writer.write(FLOAT, position.pitch()); } } diff --git a/src/main/java/net/minestom/server/network/packet/client/status/LegacyServerListPingPacket.java b/src/main/java/net/minestom/server/network/packet/client/status/LegacyServerListPingPacket.java index 2ea6fd49b..9fc42ade7 100644 --- a/src/main/java/net/minestom/server/network/packet/client/status/LegacyServerListPingPacket.java +++ b/src/main/java/net/minestom/server/network/packet/client/status/LegacyServerListPingPacket.java @@ -1,14 +1,15 @@ package net.minestom.server.network.packet.client.status; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.client.ClientPreplayPacket; import net.minestom.server.network.player.PlayerConnection; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.BYTE; + public record LegacyServerListPingPacket(byte payload) implements ClientPreplayPacket { - public LegacyServerListPingPacket(BinaryReader reader) { - this(reader.readByte()); + public LegacyServerListPingPacket(@NotNull NetworkBuffer reader) { + this(reader.read(BYTE)); } @Override @@ -17,7 +18,7 @@ public record LegacyServerListPingPacket(byte payload) implements ClientPreplayP } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeByte(payload); + public void write(@NotNull NetworkBuffer writer) { + writer.write(BYTE, payload); } } diff --git a/src/main/java/net/minestom/server/network/packet/client/status/PingPacket.java b/src/main/java/net/minestom/server/network/packet/client/status/PingPacket.java index 7c4c42350..56c11029c 100644 --- a/src/main/java/net/minestom/server/network/packet/client/status/PingPacket.java +++ b/src/main/java/net/minestom/server/network/packet/client/status/PingPacket.java @@ -3,16 +3,17 @@ package net.minestom.server.network.packet.client.status; import net.minestom.server.MinecraftServer; import net.minestom.server.event.EventDispatcher; import net.minestom.server.event.server.ClientPingServerEvent; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.client.ClientPreplayPacket; import net.minestom.server.network.packet.server.status.PongPacket; import net.minestom.server.network.player.PlayerConnection; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.LONG; + public record PingPacket(long number) implements ClientPreplayPacket { - public PingPacket(BinaryReader reader) { - this(reader.readLong()); + public PingPacket(@NotNull NetworkBuffer reader) { + this(reader.read(LONG)); } @Override @@ -36,7 +37,7 @@ public record PingPacket(long number) implements ClientPreplayPacket { } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeLong(number); + public void write(@NotNull NetworkBuffer writer) { + writer.write(LONG, number); } } diff --git a/src/main/java/net/minestom/server/network/packet/client/status/StatusRequestPacket.java b/src/main/java/net/minestom/server/network/packet/client/status/StatusRequestPacket.java index 1f3130fae..e24411a9a 100644 --- a/src/main/java/net/minestom/server/network/packet/client/status/StatusRequestPacket.java +++ b/src/main/java/net/minestom/server/network/packet/client/status/StatusRequestPacket.java @@ -2,16 +2,15 @@ package net.minestom.server.network.packet.client.status; import net.minestom.server.event.EventDispatcher; import net.minestom.server.event.server.ServerListPingEvent; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.client.ClientPreplayPacket; import net.minestom.server.network.packet.server.handshake.ResponsePacket; import net.minestom.server.network.player.PlayerConnection; import net.minestom.server.ping.ServerListPingType; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; public record StatusRequestPacket() implements ClientPreplayPacket { - public StatusRequestPacket(BinaryReader reader) { + public StatusRequestPacket(@NotNull NetworkBuffer reader) { this(); } @@ -24,7 +23,7 @@ public record StatusRequestPacket() implements ClientPreplayPacket { } @Override - public void write(@NotNull BinaryWriter writer) { + public void write(@NotNull NetworkBuffer writer) { // Empty } } diff --git a/src/main/java/net/minestom/server/network/packet/server/ServerPacket.java b/src/main/java/net/minestom/server/network/packet/server/ServerPacket.java index 271d7aa32..f82edb800 100644 --- a/src/main/java/net/minestom/server/network/packet/server/ServerPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/ServerPacket.java @@ -1,14 +1,14 @@ package net.minestom.server.network.packet.server; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.player.PlayerConnection; -import net.minestom.server.utils.binary.Writeable; /** * Represents a packet which can be sent to a player using {@link PlayerConnection#sendPacket(SendablePacket)}. *

* Packets are value-based, and should therefore not be reliant on identity. */ -public non-sealed interface ServerPacket extends Writeable, SendablePacket { +public non-sealed interface ServerPacket extends NetworkBuffer.Writer, SendablePacket { /** * Gets the id of this packet. diff --git a/src/main/java/net/minestom/server/network/packet/server/handshake/ResponsePacket.java b/src/main/java/net/minestom/server/network/packet/server/handshake/ResponsePacket.java index 4b199afd5..4895ffcdb 100644 --- a/src/main/java/net/minestom/server/network/packet/server/handshake/ResponsePacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/handshake/ResponsePacket.java @@ -1,18 +1,19 @@ package net.minestom.server.network.packet.server.handshake; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ServerPacket; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.STRING; + public record ResponsePacket(@NotNull String jsonResponse) implements ServerPacket { - public ResponsePacket(BinaryReader reader) { - this(reader.readSizedString()); + public ResponsePacket(@NotNull NetworkBuffer reader) { + this(reader.read(STRING)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeSizedString(jsonResponse); + public void write(@NotNull NetworkBuffer writer) { + writer.write(STRING, jsonResponse); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/login/EncryptionRequestPacket.java b/src/main/java/net/minestom/server/network/packet/server/login/EncryptionRequestPacket.java index d82407fe0..119195f79 100644 --- a/src/main/java/net/minestom/server/network/packet/server/login/EncryptionRequestPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/login/EncryptionRequestPacket.java @@ -1,25 +1,27 @@ package net.minestom.server.network.packet.server.login; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.BYTE_ARRAY; +import static net.minestom.server.network.NetworkBuffer.STRING; + public record EncryptionRequestPacket(@NotNull String serverId, byte @NotNull [] publicKey, byte @NotNull [] verifyToken) implements ServerPacket { - public EncryptionRequestPacket(BinaryReader reader) { - this(reader.readSizedString(), - reader.readByteArray(), - reader.readByteArray()); + public EncryptionRequestPacket(@NotNull NetworkBuffer reader) { + this(reader.read(STRING), + reader.read(BYTE_ARRAY), + reader.read(BYTE_ARRAY)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeSizedString(serverId); - writer.writeByteArray(publicKey); - writer.writeByteArray(verifyToken); + public void write(@NotNull NetworkBuffer writer) { + writer.write(STRING, serverId); + writer.write(BYTE_ARRAY, publicKey); + writer.write(BYTE_ARRAY, verifyToken); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/login/LoginDisconnectPacket.java b/src/main/java/net/minestom/server/network/packet/server/login/LoginDisconnectPacket.java index e53af9325..a9a5e5772 100644 --- a/src/main/java/net/minestom/server/network/packet/server/login/LoginDisconnectPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/login/LoginDisconnectPacket.java @@ -1,24 +1,25 @@ package net.minestom.server.network.packet.server.login; import net.kyori.adventure.text.Component; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ComponentHoldingServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; import java.util.Collection; import java.util.List; import java.util.function.UnaryOperator; +import static net.minestom.server.network.NetworkBuffer.COMPONENT; + public record LoginDisconnectPacket(@NotNull Component kickMessage) implements ComponentHoldingServerPacket { - public LoginDisconnectPacket(BinaryReader reader) { - this(reader.readComponent()); + public LoginDisconnectPacket(@NotNull NetworkBuffer reader) { + this(reader.read(COMPONENT)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeComponent(kickMessage); + public void write(@NotNull NetworkBuffer writer) { + writer.write(COMPONENT, kickMessage); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/login/LoginPluginRequestPacket.java b/src/main/java/net/minestom/server/network/packet/server/login/LoginPluginRequestPacket.java index e4ee1f2c8..f6cf0fa27 100644 --- a/src/main/java/net/minestom/server/network/packet/server/login/LoginPluginRequestPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/login/LoginPluginRequestPacket.java @@ -1,25 +1,26 @@ package net.minestom.server.network.packet.server.login; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import static net.minestom.server.network.NetworkBuffer.*; + public record LoginPluginRequestPacket(int messageId, @NotNull String channel, byte @Nullable [] data) implements ServerPacket { - public LoginPluginRequestPacket(BinaryReader reader) { - this(reader.readVarInt(), reader.readSizedString(), - reader.readRemainingBytes()); + public LoginPluginRequestPacket(@NotNull NetworkBuffer reader) { + this(reader.read(VAR_INT), reader.read(STRING), + reader.read(RAW_BYTES)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeVarInt(messageId); - writer.writeSizedString(channel); + public void write(@NotNull NetworkBuffer writer) { + writer.write(VAR_INT, messageId); + writer.write(STRING, channel); if (data != null && data.length > 0) { - writer.writeBytes(data); + writer.write(RAW_BYTES, data); } } diff --git a/src/main/java/net/minestom/server/network/packet/server/login/LoginSuccessPacket.java b/src/main/java/net/minestom/server/network/packet/server/login/LoginSuccessPacket.java index 85ecb01d5..3de0135d7 100644 --- a/src/main/java/net/minestom/server/network/packet/server/login/LoginSuccessPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/login/LoginSuccessPacket.java @@ -1,23 +1,25 @@ package net.minestom.server.network.packet.server.login; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; import java.util.UUID; +import static net.minestom.server.network.NetworkBuffer.STRING; +import static net.minestom.server.network.NetworkBuffer.VAR_INT; + public record LoginSuccessPacket(@NotNull UUID uuid, @NotNull String username, int properties) implements ServerPacket { - public LoginSuccessPacket(BinaryReader reader) { - this(reader.readUuid(), reader.readSizedString(), reader.readVarInt()); + public LoginSuccessPacket(@NotNull NetworkBuffer reader) { + this(reader.read(NetworkBuffer.UUID), reader.read(STRING), reader.read(VAR_INT)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeUuid(uuid); - writer.writeSizedString(username); - writer.writeVarInt(properties); + public void write(@NotNull NetworkBuffer writer) { + writer.write(NetworkBuffer.UUID, uuid); + writer.write(STRING, username); + writer.write(VAR_INT, properties); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/login/SetCompressionPacket.java b/src/main/java/net/minestom/server/network/packet/server/login/SetCompressionPacket.java index 4aca8785d..f5a890e80 100644 --- a/src/main/java/net/minestom/server/network/packet/server/login/SetCompressionPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/login/SetCompressionPacket.java @@ -1,19 +1,20 @@ package net.minestom.server.network.packet.server.login; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.VAR_INT; + public record SetCompressionPacket(int threshold) implements ServerPacket { - public SetCompressionPacket(BinaryReader reader) { - this(reader.readVarInt()); + public SetCompressionPacket(@NotNull NetworkBuffer reader) { + this(reader.read(VAR_INT)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeVarInt(threshold); + public void write(@NotNull NetworkBuffer writer) { + writer.write(VAR_INT, threshold); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/play/AcknowledgeBlockChangePacket.java b/src/main/java/net/minestom/server/network/packet/server/play/AcknowledgeBlockChangePacket.java index 1f11ccde8..3a5fd6ce8 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/AcknowledgeBlockChangePacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/AcknowledgeBlockChangePacket.java @@ -1,19 +1,20 @@ package net.minestom.server.network.packet.server.play; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.VAR_INT; + public record AcknowledgeBlockChangePacket(int sequence) implements ServerPacket { - public AcknowledgeBlockChangePacket(BinaryReader reader) { - this(reader.readVarInt()); + public AcknowledgeBlockChangePacket(@NotNull NetworkBuffer reader) { + this(reader.read(VAR_INT)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeVarInt(sequence); + public void write(@NotNull NetworkBuffer writer) { + writer.write(VAR_INT, sequence); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/play/ActionBarPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/ActionBarPacket.java index 68c3ccae9..353ac2869 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/ActionBarPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/ActionBarPacket.java @@ -1,25 +1,26 @@ package net.minestom.server.network.packet.server.play; import net.kyori.adventure.text.Component; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ComponentHoldingServerPacket; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; import java.util.Collection; import java.util.List; import java.util.function.UnaryOperator; +import static net.minestom.server.network.NetworkBuffer.COMPONENT; + public record ActionBarPacket(@NotNull Component text) implements ComponentHoldingServerPacket { - public ActionBarPacket(BinaryReader reader) { - this(reader.readComponent()); + public ActionBarPacket(@NotNull NetworkBuffer reader) { + this(reader.read(COMPONENT)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeComponent(text); + public void write(@NotNull NetworkBuffer writer) { + writer.write(COMPONENT, text); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/play/AdvancementsPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/AdvancementsPacket.java index ad8f71e55..64ae00571 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/AdvancementsPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/AdvancementsPacket.java @@ -4,18 +4,21 @@ import net.kyori.adventure.text.Component; import net.minestom.server.advancements.FrameType; import net.minestom.server.adventure.ComponentHolder; import net.minestom.server.item.ItemStack; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ComponentHoldingServerPacket; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; -import net.minestom.server.utils.binary.Writeable; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.util.*; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.Objects; import java.util.function.UnaryOperator; +import static net.minestom.server.network.NetworkBuffer.*; + public record AdvancementsPacket(boolean reset, @NotNull List advancementMappings, @NotNull List identifiersToRemove, @NotNull List progressMappings) implements ComponentHoldingServerPacket { @@ -25,18 +28,18 @@ public record AdvancementsPacket(boolean reset, @NotNull List { - public AdvancementMapping(BinaryReader reader) { - this(reader.readSizedString(), new Advancement(reader)); + public record AdvancementMapping(@NotNull String key, + @NotNull Advancement value) implements NetworkBuffer.Writer, ComponentHolder { + public AdvancementMapping(@NotNull NetworkBuffer reader) { + this(reader.read(STRING), new Advancement(reader)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeSizedString(key); + public void write(@NotNull NetworkBuffer writer) { + writer.write(STRING, key); writer.write(value); } @@ -96,27 +100,25 @@ public record AdvancementsPacket(boolean reset, @NotNull List criteria, - @NotNull List requirements) implements Writeable, ComponentHolder { + @NotNull List requirements) implements NetworkBuffer.Writer, ComponentHolder { public Advancement { criteria = List.copyOf(criteria); requirements = List.copyOf(requirements); } - public Advancement(BinaryReader reader) { - this(reader.readBoolean() ? reader.readSizedString() : null, - reader.readBoolean() ? new DisplayData(reader) : null, - reader.readVarIntList(BinaryReader::readSizedString), - reader.readVarIntList(Requirement::new)); + public Advancement(@NotNull NetworkBuffer reader) { + this(reader.read(BOOLEAN) ? reader.read(STRING) : null, + reader.read(BOOLEAN) ? new DisplayData(reader) : null, + reader.readCollection(STRING), + reader.readCollection(Requirement::new)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeBoolean(parentIdentifier != null); - if (parentIdentifier != null) writer.writeSizedString(parentIdentifier); - writer.writeBoolean(displayData != null); - if (displayData != null) writer.write(displayData); - writer.writeVarIntList(criteria, BinaryWriter::writeSizedString); - writer.writeVarIntList(requirements, BinaryWriter::write); + public void write(@NotNull NetworkBuffer writer) { + writer.writeOptional(STRING, parentIdentifier); + writer.writeOptional(displayData); + writer.writeCollection(STRING, criteria); + writer.writeCollection(requirements); } @Override @@ -130,26 +132,26 @@ public record AdvancementsPacket(boolean reset, @NotNull List requirements) implements Writeable { + public record Requirement(@NotNull List requirements) implements NetworkBuffer.Writer { public Requirement { requirements = List.copyOf(requirements); } - public Requirement(BinaryReader reader) { - this(reader.readVarIntList(BinaryReader::readSizedString)); + public Requirement(@NotNull NetworkBuffer reader) { + this(reader.readCollection(STRING)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeVarIntList(requirements, BinaryWriter::writeSizedString); + public void write(@NotNull NetworkBuffer writer) { + writer.writeCollection(STRING, requirements); } } public record DisplayData(@NotNull Component title, @NotNull Component description, @NotNull ItemStack icon, @NotNull FrameType frameType, int flags, @Nullable String backgroundTexture, - float x, float y) implements Writeable, ComponentHolder { - public DisplayData(BinaryReader reader) { + float x, float y) implements NetworkBuffer.Writer, ComponentHolder { + public DisplayData(@NotNull NetworkBuffer reader) { this(read(reader)); } @@ -160,15 +162,15 @@ public record AdvancementsPacket(boolean reset, @NotNull List criteria) implements Writeable { + public record AdvancementProgress(@NotNull List criteria) implements NetworkBuffer.Writer { public AdvancementProgress { criteria = List.copyOf(criteria); } - public AdvancementProgress(BinaryReader reader) { - this(reader.readVarIntList(Criteria::new)); + public AdvancementProgress(@NotNull NetworkBuffer reader) { + this(reader.readCollection(Criteria::new)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeVarIntList(criteria, BinaryWriter::write); + public void write(@NotNull NetworkBuffer writer) { + writer.writeCollection(criteria); } } public record Criteria(@NotNull String criterionIdentifier, - @NotNull CriterionProgress criterionProgress) implements Writeable { - public Criteria(BinaryReader reader) { - this(reader.readSizedString(), new CriterionProgress(reader)); + @NotNull CriterionProgress criterionProgress) implements NetworkBuffer.Writer { + public Criteria(@NotNull NetworkBuffer reader) { + this(reader.read(STRING), new CriterionProgress(reader)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeSizedString(criterionIdentifier); + public void write(@NotNull NetworkBuffer writer) { + writer.write(STRING, criterionIdentifier); writer.write(criterionProgress); } } - public record CriterionProgress(@Nullable Long dateOfAchieving) implements Writeable { - public CriterionProgress(BinaryReader reader) { - this(reader.readBoolean() ? reader.readLong() : null); + public record CriterionProgress(@Nullable Long dateOfAchieving) implements NetworkBuffer.Writer { + public CriterionProgress(@NotNull NetworkBuffer reader) { + this(reader.readOptional(LONG)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeBoolean(dateOfAchieving != null); - if (dateOfAchieving != null) writer.writeLong(dateOfAchieving); + public void write(@NotNull NetworkBuffer writer) { + writer.writeOptional(LONG, dateOfAchieving); } } } \ No newline at end of file diff --git a/src/main/java/net/minestom/server/network/packet/server/play/AttachEntityPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/AttachEntityPacket.java index 538d2ec45..10a96c65e 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/AttachEntityPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/AttachEntityPacket.java @@ -1,26 +1,27 @@ package net.minestom.server.network.packet.server.play; import net.minestom.server.entity.Entity; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import static net.minestom.server.network.NetworkBuffer.INT; + public record AttachEntityPacket(int attachedEntityId, int holdingEntityId) implements ServerPacket { public AttachEntityPacket(@NotNull Entity attachedEntity, @Nullable Entity holdingEntity) { this(attachedEntity.getEntityId(), holdingEntity != null ? holdingEntity.getEntityId() : -1); } - public AttachEntityPacket(BinaryReader reader) { - this(reader.readInt(), reader.readInt()); + public AttachEntityPacket(@NotNull NetworkBuffer reader) { + this(reader.read(INT), reader.read(INT)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeInt(attachedEntityId); - writer.writeInt(holdingEntityId); + public void write(@NotNull NetworkBuffer writer) { + writer.write(INT, attachedEntityId); + writer.write(INT, holdingEntityId); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/play/BlockActionPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/BlockActionPacket.java index 7e1842d69..6f5c55e26 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/BlockActionPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/BlockActionPacket.java @@ -2,29 +2,30 @@ package net.minestom.server.network.packet.server.play; import net.minestom.server.coordinate.Point; import net.minestom.server.instance.block.Block; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.*; + public record BlockActionPacket(@NotNull Point blockPosition, byte actionId, byte actionParam, int blockId) implements ServerPacket { public BlockActionPacket(Point blockPosition, byte actionId, byte actionParam, Block block) { this(blockPosition, actionId, actionParam, block.id()); } - public BlockActionPacket(BinaryReader reader) { - this(reader.readBlockPosition(), reader.readByte(), - reader.readByte(), reader.readVarInt()); + public BlockActionPacket(@NotNull NetworkBuffer reader) { + this(reader.read(BLOCK_POSITION), reader.read(BYTE), + reader.read(BYTE), reader.read(VAR_INT)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeBlockPosition(blockPosition); - writer.writeByte(actionId); - writer.writeByte(actionParam); - writer.writeVarInt(blockId); + public void write(@NotNull NetworkBuffer writer) { + writer.write(BLOCK_POSITION, blockPosition); + writer.write(BYTE, actionId); + writer.write(BYTE, actionParam); + writer.write(VAR_INT, blockId); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/play/BlockBreakAnimationPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/BlockBreakAnimationPacket.java index f1c8594d1..cee2c6b24 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/BlockBreakAnimationPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/BlockBreakAnimationPacket.java @@ -1,23 +1,24 @@ package net.minestom.server.network.packet.server.play; import net.minestom.server.coordinate.Point; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.*; + public record BlockBreakAnimationPacket(int entityId, @NotNull Point blockPosition, byte destroyStage) implements ServerPacket { - public BlockBreakAnimationPacket(BinaryReader reader) { - this(reader.readVarInt(), reader.readBlockPosition(), reader.readByte()); + public BlockBreakAnimationPacket(@NotNull NetworkBuffer reader) { + this(reader.read(VAR_INT), reader.read(BLOCK_POSITION), reader.read(BYTE)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeVarInt(entityId); - writer.writeBlockPosition(blockPosition); - writer.writeByte(destroyStage); + public void write(@NotNull NetworkBuffer writer) { + writer.write(VAR_INT, entityId); + writer.write(BLOCK_POSITION, blockPosition); + writer.write(BYTE, destroyStage); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/play/BlockChangePacket.java b/src/main/java/net/minestom/server/network/packet/server/play/BlockChangePacket.java index 004592116..64d3bfb8d 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/BlockChangePacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/BlockChangePacket.java @@ -2,25 +2,27 @@ package net.minestom.server.network.packet.server.play; import net.minestom.server.coordinate.Point; import net.minestom.server.instance.block.Block; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.BLOCK_POSITION; +import static net.minestom.server.network.NetworkBuffer.VAR_INT; + public record BlockChangePacket(@NotNull Point blockPosition, int blockStateId) implements ServerPacket { public BlockChangePacket(@NotNull Point blockPosition, @NotNull Block block) { this(blockPosition, block.stateId()); } - public BlockChangePacket(BinaryReader reader) { - this(reader.readBlockPosition(), reader.readVarInt()); + public BlockChangePacket(@NotNull NetworkBuffer reader) { + this(reader.read(BLOCK_POSITION), reader.read(VAR_INT)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeBlockPosition(blockPosition); - writer.writeVarInt(blockStateId); + public void write(@NotNull NetworkBuffer writer) { + writer.write(BLOCK_POSITION, blockPosition); + writer.write(VAR_INT, blockStateId); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/play/BlockEntityDataPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/BlockEntityDataPacket.java index e36e49572..1deff5cb3 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/BlockEntityDataPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/BlockEntityDataPacket.java @@ -1,29 +1,30 @@ package net.minestom.server.network.packet.server.play; import net.minestom.server.coordinate.Point; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jglrxavpok.hephaistos.nbt.NBTCompound; +import static net.minestom.server.network.NetworkBuffer.*; + public record BlockEntityDataPacket(@NotNull Point blockPosition, int action, @Nullable NBTCompound data) implements ServerPacket { - public BlockEntityDataPacket(BinaryReader reader) { - this(reader.readBlockPosition(), reader.readVarInt(), (NBTCompound) reader.readTag()); + public BlockEntityDataPacket(@NotNull NetworkBuffer reader) { + this(reader.read(BLOCK_POSITION), reader.read(VAR_INT), (NBTCompound) reader.read(NBT)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeBlockPosition(blockPosition); - writer.writeVarInt(action); + public void write(@NotNull NetworkBuffer writer) { + writer.write(BLOCK_POSITION, blockPosition); + writer.write(VAR_INT, action); if (data != null) { - writer.writeNBT("", data); + writer.write(NBT, data); } else { // TAG_End - writer.writeByte((byte) 0x00); + writer.write(BYTE, (byte) 0x00); } } diff --git a/src/main/java/net/minestom/server/network/packet/server/play/BossBarPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/BossBarPacket.java index 2acd63bd0..073ef983a 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/BossBarPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/BossBarPacket.java @@ -4,12 +4,10 @@ import net.kyori.adventure.bossbar.BossBar; import net.kyori.adventure.text.Component; import net.minestom.server.adventure.AdventurePacketConvertor; import net.minestom.server.adventure.ComponentHolder; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ComponentHoldingServerPacket; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; -import net.minestom.server.utils.binary.Writeable; import org.jetbrains.annotations.NotNull; import java.util.Collection; @@ -17,9 +15,11 @@ import java.util.List; import java.util.UUID; import java.util.function.UnaryOperator; +import static net.minestom.server.network.NetworkBuffer.*; + public record BossBarPacket(@NotNull UUID uuid, @NotNull Action action) implements ComponentHoldingServerPacket { - public BossBarPacket(BinaryReader reader) { - this(reader.readUuid(), switch (reader.readVarInt()) { + public BossBarPacket(@NotNull NetworkBuffer reader) { + this(reader.read(NetworkBuffer.UUID), switch (reader.read(VAR_INT)) { case 0 -> new AddAction(reader); case 1 -> new RemoveAction(); case 2 -> new UpdateHealthAction(reader); @@ -31,9 +31,9 @@ public record BossBarPacket(@NotNull UUID uuid, @NotNull Action action) implemen } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeUuid(uuid); - writer.writeVarInt(action.id()); + public void write(@NotNull NetworkBuffer writer) { + writer.write(NetworkBuffer.UUID, uuid); + writer.write(VAR_INT, action.id()); writer.write(action); } @@ -51,31 +51,32 @@ public record BossBarPacket(@NotNull UUID uuid, @NotNull Action action) implemen : this; } - public sealed interface Action extends Writeable + public sealed interface Action extends NetworkBuffer.Writer permits AddAction, RemoveAction, UpdateHealthAction, UpdateTitleAction, UpdateStyleAction, UpdateFlagsAction { int id(); } public record AddAction(@NotNull Component title, float health, @NotNull BossBar.Color color, - @NotNull BossBar.Overlay overlay, byte flags) implements Action, ComponentHolder { + @NotNull BossBar.Overlay overlay, + byte flags) implements Action, ComponentHolder { public AddAction(@NotNull BossBar bar) { this(bar.name(), bar.progress(), bar.color(), bar.overlay(), AdventurePacketConvertor.getBossBarFlagValue(bar.flags())); } - public AddAction(BinaryReader reader) { - this(reader.readComponent(), reader.readFloat(), - BossBar.Color.values()[reader.readVarInt()], - BossBar.Overlay.values()[reader.readVarInt()], reader.readByte()); + public AddAction(@NotNull NetworkBuffer reader) { + this(reader.read(COMPONENT), reader.read(FLOAT), + BossBar.Color.values()[reader.read(VAR_INT)], + BossBar.Overlay.values()[reader.read(VAR_INT)], reader.read(BYTE)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeComponent(title); - writer.writeFloat(health); - writer.writeVarInt(AdventurePacketConvertor.getBossBarColorValue(color)); - writer.writeVarInt(AdventurePacketConvertor.getBossBarOverlayValue(overlay)); - writer.writeByte(flags); + public void write(@NotNull NetworkBuffer writer) { + writer.write(COMPONENT, title); + writer.write(FLOAT, health); + writer.write(VAR_INT, AdventurePacketConvertor.getBossBarColorValue(color)); + writer.write(VAR_INT, AdventurePacketConvertor.getBossBarOverlayValue(overlay)); + writer.write(BYTE, flags); } @Override @@ -96,7 +97,7 @@ public record BossBarPacket(@NotNull UUID uuid, @NotNull Action action) implemen public record RemoveAction() implements Action { @Override - public void write(@NotNull BinaryWriter writer) { + public void write(@NotNull NetworkBuffer writer) { } @Override @@ -110,13 +111,13 @@ public record BossBarPacket(@NotNull UUID uuid, @NotNull Action action) implemen this(bar.progress()); } - public UpdateHealthAction(BinaryReader reader) { - this(reader.readFloat()); + public UpdateHealthAction(@NotNull NetworkBuffer reader) { + this(reader.read(FLOAT)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeFloat(health); + public void write(@NotNull NetworkBuffer writer) { + writer.write(FLOAT, health); } @Override @@ -130,13 +131,13 @@ public record BossBarPacket(@NotNull UUID uuid, @NotNull Action action) implemen this(bar.name()); } - public UpdateTitleAction(BinaryReader reader) { - this(reader.readComponent()); + public UpdateTitleAction(@NotNull NetworkBuffer reader) { + this(reader.read(COMPONENT)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeComponent(title); + public void write(@NotNull NetworkBuffer writer) { + writer.write(COMPONENT, title); } @Override @@ -161,14 +162,14 @@ public record BossBarPacket(@NotNull UUID uuid, @NotNull Action action) implemen this(bar.color(), bar.overlay()); } - public UpdateStyleAction(BinaryReader reader) { - this(BossBar.Color.values()[reader.readVarInt()], BossBar.Overlay.values()[reader.readVarInt()]); + public UpdateStyleAction(@NotNull NetworkBuffer reader) { + this(BossBar.Color.values()[reader.read(VAR_INT)], BossBar.Overlay.values()[reader.read(VAR_INT)]); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeVarInt(AdventurePacketConvertor.getBossBarColorValue(color)); - writer.writeVarInt(AdventurePacketConvertor.getBossBarOverlayValue(overlay)); + public void write(@NotNull NetworkBuffer writer) { + writer.write(VAR_INT, AdventurePacketConvertor.getBossBarColorValue(color)); + writer.write(VAR_INT, AdventurePacketConvertor.getBossBarOverlayValue(overlay)); } @Override @@ -182,13 +183,13 @@ public record BossBarPacket(@NotNull UUID uuid, @NotNull Action action) implemen this(AdventurePacketConvertor.getBossBarFlagValue(bar.flags())); } - public UpdateFlagsAction(BinaryReader reader) { - this(reader.readByte()); + public UpdateFlagsAction(@NotNull NetworkBuffer reader) { + this(reader.read(BYTE)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeByte(flags); + public void write(@NotNull NetworkBuffer writer) { + writer.write(BYTE, flags); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/play/CameraPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/CameraPacket.java index 3cd8e40dd..4090653e8 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/CameraPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/CameraPacket.java @@ -1,15 +1,16 @@ package net.minestom.server.network.packet.server.play; import net.minestom.server.entity.Entity; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.VAR_INT; + public record CameraPacket(int cameraId) implements ServerPacket { - public CameraPacket(BinaryReader reader) { - this(reader.readVarInt()); + public CameraPacket(@NotNull NetworkBuffer reader) { + this(reader.read(VAR_INT)); } public CameraPacket(@NotNull Entity camera) { @@ -17,8 +18,8 @@ public record CameraPacket(int cameraId) implements ServerPacket { } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeVarInt(cameraId); + public void write(@NotNull NetworkBuffer writer) { + writer.write(VAR_INT, cameraId); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/play/ChangeGameStatePacket.java b/src/main/java/net/minestom/server/network/packet/server/play/ChangeGameStatePacket.java index 97cdb7776..82549fd94 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/ChangeGameStatePacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/ChangeGameStatePacket.java @@ -1,20 +1,22 @@ package net.minestom.server.network.packet.server.play; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.BYTE; +import static net.minestom.server.network.NetworkBuffer.FLOAT; + public record ChangeGameStatePacket(@NotNull Reason reason, float value) implements ServerPacket { - public ChangeGameStatePacket(BinaryReader reader) { - this(Reason.values()[reader.readByte()], reader.readFloat()); + public ChangeGameStatePacket(@NotNull NetworkBuffer reader) { + this(Reason.values()[reader.read(BYTE)], reader.read(FLOAT)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeByte((byte) reason.ordinal()); - writer.writeFloat(value); + public void write(@NotNull NetworkBuffer writer) { + writer.write(BYTE, (byte) reason.ordinal()); + writer.write(FLOAT, value); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/play/ChatPreviewPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/ChatPreviewPacket.java index 171560184..a197df2ac 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/ChatPreviewPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/ChatPreviewPacket.java @@ -1,11 +1,10 @@ package net.minestom.server.network.packet.server.play; import net.kyori.adventure.text.Component; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ComponentHoldingServerPacket; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -13,16 +12,17 @@ import java.util.Collection; import java.util.List; import java.util.function.UnaryOperator; +import static net.minestom.server.network.NetworkBuffer.*; + public record ChatPreviewPacket(int queryId, @Nullable Component preview) implements ComponentHoldingServerPacket { - public ChatPreviewPacket(BinaryReader reader) { - this(reader.readInt(), reader.readBoolean() ? reader.readComponent() : null); + public ChatPreviewPacket(@NotNull NetworkBuffer reader) { + this(reader.read(INT), reader.readOptional(COMPONENT)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeInt(queryId); - writer.writeBoolean(preview != null); - if (preview != null) writer.writeComponent(preview); + public void write(@NotNull NetworkBuffer writer) { + writer.write(INT, queryId); + writer.writeOptional(COMPONENT, preview); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/play/ChunkDataPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/ChunkDataPacket.java index f7253b708..1e5ccecba 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/ChunkDataPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/ChunkDataPacket.java @@ -1,26 +1,27 @@ package net.minestom.server.network.packet.server.play; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; import net.minestom.server.network.packet.server.play.data.ChunkData; import net.minestom.server.network.packet.server.play.data.LightData; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.INT; + public record ChunkDataPacket(int chunkX, int chunkZ, @NotNull ChunkData chunkData, @NotNull LightData lightData) implements ServerPacket { - public ChunkDataPacket(BinaryReader reader) { - this(reader.readInt(), reader.readInt(), + public ChunkDataPacket(@NotNull NetworkBuffer reader) { + this(reader.read(INT), reader.read(INT), new ChunkData(reader), new LightData(reader)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeInt(chunkX); - writer.writeInt(chunkZ); + public void write(@NotNull NetworkBuffer writer) { + writer.write(INT, chunkX); + writer.write(INT, chunkZ); writer.write(chunkData); writer.write(lightData); } diff --git a/src/main/java/net/minestom/server/network/packet/server/play/ClearTitlesPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/ClearTitlesPacket.java index 947ee4ee2..dfb4e511d 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/ClearTitlesPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/ClearTitlesPacket.java @@ -1,19 +1,20 @@ package net.minestom.server.network.packet.server.play; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.BOOLEAN; + public record ClearTitlesPacket(boolean reset) implements ServerPacket { - public ClearTitlesPacket(BinaryReader reader) { - this(reader.readBoolean()); + public ClearTitlesPacket(@NotNull NetworkBuffer reader) { + this(reader.read(BOOLEAN)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeBoolean(reset); + public void write(@NotNull NetworkBuffer writer) { + writer.write(BOOLEAN, reset); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/play/CloseWindowPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/CloseWindowPacket.java index d34c0db49..e0e64ca63 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/CloseWindowPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/CloseWindowPacket.java @@ -1,19 +1,20 @@ package net.minestom.server.network.packet.server.play; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.BYTE; + public record CloseWindowPacket(byte windowId) implements ServerPacket { - public CloseWindowPacket(BinaryReader reader) { - this(reader.readByte()); + public CloseWindowPacket(@NotNull NetworkBuffer reader) { + this(reader.read(BYTE)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeByte(windowId); + public void write(@NotNull NetworkBuffer writer) { + writer.write(BYTE, windowId); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/play/CollectItemPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/CollectItemPacket.java index 1db805e43..fd4c202fc 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/CollectItemPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/CollectItemPacket.java @@ -1,22 +1,23 @@ package net.minestom.server.network.packet.server.play; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.VAR_INT; + public record CollectItemPacket(int collectedEntityId, int collectorEntityId, int pickupItemCount) implements ServerPacket { - public CollectItemPacket(BinaryReader reader) { - this(reader.readVarInt(), reader.readVarInt(), reader.readVarInt()); + public CollectItemPacket(@NotNull NetworkBuffer reader) { + this(reader.read(VAR_INT), reader.read(VAR_INT), reader.read(VAR_INT)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeVarInt(collectedEntityId); - writer.writeVarInt(collectorEntityId); - writer.writeVarInt(pickupItemCount); + public void write(@NotNull NetworkBuffer writer) { + writer.write(VAR_INT, collectedEntityId); + writer.write(VAR_INT, collectorEntityId); + writer.write(VAR_INT, pickupItemCount); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/play/CraftRecipeResponse.java b/src/main/java/net/minestom/server/network/packet/server/play/CraftRecipeResponse.java index a84c143e3..c673d08bc 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/CraftRecipeResponse.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/CraftRecipeResponse.java @@ -1,20 +1,22 @@ package net.minestom.server.network.packet.server.play; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.BYTE; +import static net.minestom.server.network.NetworkBuffer.STRING; + public record CraftRecipeResponse(byte windowId, String recipe) implements ServerPacket { - public CraftRecipeResponse(BinaryReader reader) { - this(reader.readByte(), reader.readSizedString()); + public CraftRecipeResponse(@NotNull NetworkBuffer reader) { + this(reader.read(BYTE), reader.read(STRING)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeByte(windowId); - writer.writeSizedString(recipe); + public void write(@NotNull NetworkBuffer writer) { + writer.write(BYTE, windowId); + writer.write(STRING, recipe); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/play/CustomChatCompletionPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/CustomChatCompletionPacket.java index dd22d8927..a1461feac 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/CustomChatCompletionPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/CustomChatCompletionPacket.java @@ -1,27 +1,28 @@ package net.minestom.server.network.packet.server.play; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; import java.util.List; +import static net.minestom.server.network.NetworkBuffer.STRING; + public record CustomChatCompletionPacket(@NotNull Action action, @NotNull List<@NotNull String> entries) implements ServerPacket { public CustomChatCompletionPacket { entries = List.copyOf(entries); } - public CustomChatCompletionPacket(BinaryReader reader) { - this(Action.values()[reader.readVarInt()], reader.readVarIntList(BinaryReader::readSizedString)); + public CustomChatCompletionPacket(@NotNull NetworkBuffer reader) { + this(reader.readEnum(Action.class), reader.readCollection(STRING)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeVarInt(action.ordinal()); - writer.writeVarIntList(entries, BinaryWriter::writeSizedString); + public void write(@NotNull NetworkBuffer writer) { + writer.writeEnum(Action.class, action); + writer.writeCollection(STRING, entries); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/play/DeathCombatEventPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/DeathCombatEventPacket.java index 4c39a021d..534c3592d 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/DeathCombatEventPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/DeathCombatEventPacket.java @@ -1,28 +1,29 @@ package net.minestom.server.network.packet.server.play; import net.kyori.adventure.text.Component; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ComponentHoldingServerPacket; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; import java.util.Collection; import java.util.List; import java.util.function.UnaryOperator; +import static net.minestom.server.network.NetworkBuffer.*; + public record DeathCombatEventPacket(int playerId, int entityId, @NotNull Component message) implements ComponentHoldingServerPacket { - public DeathCombatEventPacket(BinaryReader reader) { - this(reader.readVarInt(), reader.readInt(), reader.readComponent()); + public DeathCombatEventPacket(@NotNull NetworkBuffer reader) { + this(reader.read(VAR_INT), reader.read(INT), reader.read(COMPONENT)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeVarInt(playerId); - writer.writeInt(entityId); - writer.writeComponent(message); + public void write(@NotNull NetworkBuffer writer) { + writer.write(VAR_INT, playerId); + writer.write(INT, entityId); + writer.write(COMPONENT, message); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/play/DeclareCommandsPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/DeclareCommandsPacket.java index 10a4571a1..1bac5331f 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/DeclareCommandsPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/DeclareCommandsPacket.java @@ -1,36 +1,35 @@ package net.minestom.server.network.packet.server.play; import net.minestom.server.command.builder.arguments.Argument; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; import net.minestom.server.registry.ProtocolObject; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; -import net.minestom.server.utils.binary.Readable; -import net.minestom.server.utils.binary.Writeable; import org.jetbrains.annotations.NotNull; import java.util.List; import java.util.function.Function; +import static net.minestom.server.network.NetworkBuffer.*; + public record DeclareCommandsPacket(@NotNull List nodes, int rootIndex) implements ServerPacket { public DeclareCommandsPacket { nodes = List.copyOf(nodes); } - public DeclareCommandsPacket(@NotNull BinaryReader reader) { - this(reader.readVarIntList(r -> { + public DeclareCommandsPacket(@NotNull NetworkBuffer reader) { + this(reader.readCollection(r -> { Node node = new Node(); node.read(r); return node; - }), reader.readVarInt()); + }), reader.read(VAR_INT)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeVarIntList(nodes, BinaryWriter::write); - writer.writeVarInt(rootIndex); + public void write(@NotNull NetworkBuffer writer) { + writer.writeCollection(nodes); + writer.write(VAR_INT, rootIndex); } @Override @@ -38,7 +37,7 @@ public record DeclareCommandsPacket(@NotNull List nodes, return ServerPacketIdentifier.DECLARE_COMMANDS; } - public static final class Node implements Writeable, Readable { + public static final class Node implements NetworkBuffer.Writer { public byte flags; public int[] children = new int[0]; public int redirectedNode; // Only if flags & 0x08 @@ -48,77 +47,77 @@ public record DeclareCommandsPacket(@NotNull List nodes, public String suggestionsType = ""; // Only if flags 0x10 @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeByte(flags); + public void write(@NotNull NetworkBuffer writer) { + writer.write(BYTE, flags); if (children != null && children.length > 262114) { throw new RuntimeException("Children length " + children.length + " is bigger than the maximum allowed " + 262114); } - writer.writeVarIntArray(children); + writer.write(VAR_INT_ARRAY, children); if ((flags & 0x08) != 0) { - writer.writeVarInt(redirectedNode); + writer.write(VAR_INT, redirectedNode); } if (isLiteral() || isArgument()) { - writer.writeSizedString(name); + writer.write(STRING, name); } if (isArgument()) { final int parserId = Argument.CONTAINER.toId(parser); - writer.writeVarInt(parserId); + writer.write(VAR_INT, parserId); if (properties != null) { - writer.writeBytes(properties); + writer.write(RAW_BYTES, properties); } } if ((flags & 0x10) != 0) { - writer.writeSizedString(suggestionsType); + writer.write(STRING, suggestionsType); } } - @Override - public void read(@NotNull BinaryReader reader) { - flags = reader.readByte(); - children = reader.readVarIntArray(); + public void read(@NotNull NetworkBuffer reader) { + flags = reader.read(BYTE); + children = reader.read(VAR_INT_ARRAY); if ((flags & 0x08) != 0) { - redirectedNode = reader.readVarInt(); + redirectedNode = reader.read(VAR_INT); } if (isLiteral() || isArgument()) { - name = reader.readSizedString(); + name = reader.read(STRING); } if (isArgument()) { - final ProtocolObject object = Argument.CONTAINER.getId(reader.readVarInt()); + final ProtocolObject object = Argument.CONTAINER.getId(reader.read(VAR_INT)); parser = object.name(); properties = getProperties(reader, parser); } if ((flags & 0x10) != 0) { - suggestionsType = reader.readSizedString(); + suggestionsType = reader.read(STRING); } } - private byte[] getProperties(BinaryReader reader, String parser) { - final Function, byte[]> minMaxExtractor = (via) -> reader.extractBytes(() -> { - byte flags = reader.readByte(); + private byte[] getProperties(@NotNull NetworkBuffer reader, String parser) { + final Function, byte[]> minMaxExtractor = (via) -> reader.extractBytes((extractor) -> { + byte flags = extractor.read(BYTE); if ((flags & 0x01) == 0x01) { - via.apply(reader); // min + via.apply(extractor); // min } if ((flags & 0x02) == 0x02) { - via.apply(reader); // max + via.apply(extractor); // max } }); return switch (parser) { - case "brigadier:double" -> minMaxExtractor.apply(BinaryReader::readDouble); - case "brigadier:integer" -> minMaxExtractor.apply(BinaryReader::readInt); - case "brigadier:float" -> minMaxExtractor.apply(BinaryReader::readFloat); - case "brigadier:long" -> minMaxExtractor.apply(BinaryReader::readLong); - case "brigadier:string" -> reader.extractBytes(reader::readVarInt); - case "minecraft:entity", "minecraft:score_holder" -> reader.extractBytes(reader::readByte); - case "minecraft:range" -> reader.extractBytes(reader::readBoolean); // https://wiki.vg/Command_Data#minecraft:range, looks fishy - case "minecraft:resource_or_tag", "minecraft:registry" -> reader.extractBytes(reader::readSizedString); + case "brigadier:double" -> minMaxExtractor.apply(b -> b.read(DOUBLE)); + case "brigadier:integer" -> minMaxExtractor.apply(b -> b.read(INT)); + case "brigadier:float" -> minMaxExtractor.apply(b -> b.read(FLOAT)); + case "brigadier:long" -> minMaxExtractor.apply(b -> b.read(LONG)); + case "brigadier:string" -> reader.extractBytes(b -> b.read(VAR_INT)); + case "minecraft:entity", "minecraft:score_holder" -> reader.extractBytes(b -> b.read(BYTE)); + case "minecraft:range" -> + reader.extractBytes(b -> b.read(BOOLEAN)); // https://wiki.vg/Command_Data#minecraft:range, looks fishy + case "minecraft:resource_or_tag", "minecraft:registry" -> reader.extractBytes(b -> b.read(STRING)); default -> new byte[0]; // unknown }; } diff --git a/src/main/java/net/minestom/server/network/packet/server/play/DeclareRecipesPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/DeclareRecipesPacket.java index d5ec38f84..b3181d99a 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/DeclareRecipesPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/DeclareRecipesPacket.java @@ -1,24 +1,24 @@ package net.minestom.server.network.packet.server.play; import net.minestom.server.item.ItemStack; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; -import net.minestom.server.utils.binary.Writeable; import org.jetbrains.annotations.NotNull; -import java.util.ArrayList; +import java.util.ArrayList; import java.util.List; +import static net.minestom.server.network.NetworkBuffer.*; + public record DeclareRecipesPacket(@NotNull List recipes) implements ServerPacket { public DeclareRecipesPacket { recipes = List.copyOf(recipes); } - public DeclareRecipesPacket(BinaryReader reader) { - this(reader.readVarIntList(r -> { - final String type = r.readSizedString(); + public DeclareRecipesPacket(@NotNull NetworkBuffer reader) { + this(reader.readCollection(r -> { + final String type = r.read(STRING); return switch (type) { case "crafting_shapeless" -> new DeclaredShapelessCraftingRecipe(reader); case "crafting_shaped" -> new DeclaredShapedCraftingRecipe(reader); @@ -34,10 +34,10 @@ public record DeclareRecipesPacket(@NotNull List recipes) implem } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeVarIntList(recipes, (bWriter, recipe)->{ - bWriter.writeSizedString(recipe.type()); - bWriter.writeSizedString(recipe.recipeId()); + public void write(@NotNull NetworkBuffer writer) { + writer.writeCollection(recipes, (bWriter, recipe) -> { + bWriter.write(STRING, recipe.type()); + bWriter.write(STRING, recipe.recipeId()); bWriter.write(recipe); }); } @@ -47,7 +47,7 @@ public record DeclareRecipesPacket(@NotNull List recipes) implem return ServerPacketIdentifier.DECLARE_RECIPES; } - public sealed interface DeclaredRecipe extends Writeable + public sealed interface DeclaredRecipe extends NetworkBuffer.Writer permits DeclaredShapelessCraftingRecipe, DeclaredShapedCraftingRecipe, DeclaredSmeltingRecipe, DeclaredBlastingRecipe, DeclaredSmokingRecipe, DeclaredCampfireCookingRecipe, DeclaredStonecutterRecipe, DeclaredSmithingRecipe { @@ -59,16 +59,16 @@ public record DeclareRecipesPacket(@NotNull List recipes) implem public record DeclaredShapelessCraftingRecipe(String recipeId, String group, List ingredients, ItemStack result) implements DeclaredRecipe { - private DeclaredShapelessCraftingRecipe(@NotNull BinaryReader reader) { - this(reader.readSizedString(), reader.readSizedString(), - reader.readVarIntList(Ingredient::new), reader.readItemStack()); + private DeclaredShapelessCraftingRecipe(@NotNull NetworkBuffer reader) { + this(reader.read(STRING), reader.read(STRING), + reader.readCollection(Ingredient::new), reader.read(ITEM)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeSizedString(group); - writer.writeVarIntList(ingredients, BinaryWriter::write); - writer.writeItemStack(result); + public void write(@NotNull NetworkBuffer writer) { + writer.write(STRING, group); + writer.writeCollection(ingredients); + writer.write(ITEM, result); } @Override @@ -88,33 +88,33 @@ public record DeclareRecipesPacket(@NotNull List recipes) implem this(packet.recipeId, packet.width, packet.height, packet.group, packet.ingredients, packet.result); } - public DeclaredShapedCraftingRecipe(BinaryReader reader) { + public DeclaredShapedCraftingRecipe(@NotNull NetworkBuffer reader) { this(read(reader)); } - private static DeclaredShapedCraftingRecipe read(BinaryReader reader) { + private static DeclaredShapedCraftingRecipe read(@NotNull NetworkBuffer reader) { - String recipeId = reader.readSizedString(); - int width = reader.readVarInt(); - int height = reader.readVarInt(); - String group = reader.readSizedString(); + String recipeId = reader.read(STRING); + int width = reader.read(VAR_INT); + int height = reader.read(VAR_INT); + String group = reader.read(STRING); List ingredients = new ArrayList<>(); for (int slot = 0; slot < width * height; slot++) { ingredients.add(new Ingredient(reader)); } - ItemStack result = reader.readItemStack(); + ItemStack result = reader.read(ITEM); return new DeclaredShapedCraftingRecipe(recipeId, width, height, group, ingredients, result); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeVarInt(width); - writer.writeVarInt(height); - writer.writeSizedString(group); + public void write(@NotNull NetworkBuffer writer) { + writer.write(VAR_INT, width); + writer.write(VAR_INT, height); + writer.write(STRING, group); for (Ingredient ingredient : ingredients) { ingredient.write(writer); } - writer.writeItemStack(result); + writer.write(ITEM, result); } @Override @@ -126,19 +126,19 @@ public record DeclareRecipesPacket(@NotNull List recipes) implem public record DeclaredSmeltingRecipe(@NotNull String recipeId, @NotNull String group, @NotNull Ingredient ingredient, @NotNull ItemStack result, float experience, int cookingTime) implements DeclaredRecipe { - public DeclaredSmeltingRecipe(BinaryReader reader) { - this(reader.readSizedString(), reader.readSizedString(), - new Ingredient(reader), reader.readItemStack(), - reader.readFloat(), reader.readVarInt()); + public DeclaredSmeltingRecipe(@NotNull NetworkBuffer reader) { + this(reader.read(STRING), reader.read(STRING), + new Ingredient(reader), reader.read(ITEM), + reader.read(FLOAT), reader.read(VAR_INT)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeSizedString(group); + public void write(@NotNull NetworkBuffer writer) { + writer.write(STRING, group); writer.write(ingredient); - writer.writeItemStack(result); - writer.writeFloat(experience); - writer.writeVarInt(cookingTime); + writer.write(ITEM, result); + writer.write(FLOAT, experience); + writer.write(VAR_INT, cookingTime); } @Override @@ -150,19 +150,19 @@ public record DeclareRecipesPacket(@NotNull List recipes) implem public record DeclaredBlastingRecipe(@NotNull String recipeId, @NotNull String group, @NotNull Ingredient ingredient, @NotNull ItemStack result, float experience, int cookingTime) implements DeclaredRecipe { - public DeclaredBlastingRecipe(BinaryReader reader) { - this(reader.readSizedString(), reader.readSizedString(), - new Ingredient(reader), reader.readItemStack(), - reader.readFloat(), reader.readVarInt()); + public DeclaredBlastingRecipe(@NotNull NetworkBuffer reader) { + this(reader.read(STRING), reader.read(STRING), + new Ingredient(reader), reader.read(ITEM), + reader.read(FLOAT), reader.read(VAR_INT)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeSizedString(group); + public void write(@NotNull NetworkBuffer writer) { + writer.write(STRING, group); writer.write(ingredient); - writer.writeItemStack(result); - writer.writeFloat(experience); - writer.writeVarInt(cookingTime); + writer.write(ITEM, result); + writer.write(FLOAT, experience); + writer.write(VAR_INT, cookingTime); } @Override @@ -174,19 +174,19 @@ public record DeclareRecipesPacket(@NotNull List recipes) implem public record DeclaredSmokingRecipe(@NotNull String recipeId, @NotNull String group, @NotNull Ingredient ingredient, @NotNull ItemStack result, float experience, int cookingTime) implements DeclaredRecipe { - public DeclaredSmokingRecipe(BinaryReader reader) { - this(reader.readSizedString(), reader.readSizedString(), - new Ingredient(reader), reader.readItemStack(), - reader.readFloat(), reader.readVarInt()); + public DeclaredSmokingRecipe(@NotNull NetworkBuffer reader) { + this(reader.read(STRING), reader.read(STRING), + new Ingredient(reader), reader.read(ITEM), + reader.read(FLOAT), reader.read(VAR_INT)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeSizedString(group); + public void write(@NotNull NetworkBuffer writer) { + writer.write(STRING, group); writer.write(ingredient); - writer.writeItemStack(result); - writer.writeFloat(experience); - writer.writeVarInt(cookingTime); + writer.write(ITEM, result); + writer.write(FLOAT, experience); + writer.write(VAR_INT, cookingTime); } @Override @@ -198,19 +198,19 @@ public record DeclareRecipesPacket(@NotNull List recipes) implem public record DeclaredCampfireCookingRecipe(@NotNull String recipeId, @NotNull String group, @NotNull Ingredient ingredient, @NotNull ItemStack result, float experience, int cookingTime) implements DeclaredRecipe { - public DeclaredCampfireCookingRecipe(BinaryReader reader) { - this(reader.readSizedString(), reader.readSizedString(), - new Ingredient(reader), reader.readItemStack(), - reader.readFloat(), reader.readVarInt()); + public DeclaredCampfireCookingRecipe(@NotNull NetworkBuffer reader) { + this(reader.read(STRING), reader.read(STRING), + new Ingredient(reader), reader.read(ITEM), + reader.read(FLOAT), reader.read(VAR_INT)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeSizedString(group); + public void write(@NotNull NetworkBuffer writer) { + writer.write(STRING, group); writer.write(ingredient); - writer.writeItemStack(result); - writer.writeFloat(experience); - writer.writeVarInt(cookingTime); + writer.write(ITEM, result); + writer.write(FLOAT, experience); + writer.write(VAR_INT, cookingTime); } @Override @@ -221,16 +221,16 @@ public record DeclareRecipesPacket(@NotNull List recipes) implem public record DeclaredStonecutterRecipe(String recipeId, String group, Ingredient ingredient, ItemStack result) implements DeclaredRecipe { - public DeclaredStonecutterRecipe(@NotNull BinaryReader reader) { - this(reader.readSizedString(), reader.readSizedString(), - new Ingredient(reader), reader.readItemStack()); + public DeclaredStonecutterRecipe(@NotNull NetworkBuffer reader) { + this(reader.read(STRING), reader.read(STRING), + new Ingredient(reader), reader.read(ITEM)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeSizedString(group); + public void write(@NotNull NetworkBuffer writer) { + writer.write(STRING, group); writer.write(ingredient); - writer.writeItemStack(result); + writer.write(ITEM, result); } @Override @@ -241,15 +241,15 @@ public record DeclareRecipesPacket(@NotNull List recipes) implem public record DeclaredSmithingRecipe(String recipeId, Ingredient base, Ingredient addition, ItemStack result) implements DeclaredRecipe { - public DeclaredSmithingRecipe(@NotNull BinaryReader reader) { - this(reader.readSizedString(), new Ingredient(reader), new Ingredient(reader), reader.readItemStack()); + public DeclaredSmithingRecipe(@NotNull NetworkBuffer reader) { + this(reader.read(STRING), new Ingredient(reader), new Ingredient(reader), reader.read(ITEM)); } @Override - public void write(@NotNull BinaryWriter writer) { + public void write(@NotNull NetworkBuffer writer) { writer.write(base); writer.write(addition); - writer.writeItemStack(result); + writer.write(ITEM, result); } @Override @@ -258,17 +258,17 @@ public record DeclareRecipesPacket(@NotNull List recipes) implem } } - public record Ingredient(@NotNull List items) implements Writeable { + public record Ingredient(@NotNull List items) implements NetworkBuffer.Writer { public Ingredient { items = List.copyOf(items); } - public Ingredient(BinaryReader reader) { - this(reader.readVarIntList(BinaryReader::readItemStack)); + public Ingredient(@NotNull NetworkBuffer reader) { + this(reader.readCollection(ITEM)); } - public void write(BinaryWriter writer) { - writer.writeVarIntList(items, BinaryWriter::writeItemStack); + public void write(@NotNull NetworkBuffer writer) { + writer.writeCollection(ITEM, items); } } } diff --git a/src/main/java/net/minestom/server/network/packet/server/play/DeleteChatPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/DeleteChatPacket.java index 7796c6c9d..a277ad89e 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/DeleteChatPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/DeleteChatPacket.java @@ -1,19 +1,18 @@ package net.minestom.server.network.packet.server.play; import net.minestom.server.crypto.MessageSignature; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; public record DeleteChatPacket(@NotNull MessageSignature signature) implements ServerPacket { - public DeleteChatPacket(BinaryReader reader) { + public DeleteChatPacket(@NotNull NetworkBuffer reader) { this(new MessageSignature(reader)); } @Override - public void write(@NotNull BinaryWriter writer) { + public void write(@NotNull NetworkBuffer writer) { writer.write(signature); } diff --git a/src/main/java/net/minestom/server/network/packet/server/play/DestroyEntitiesPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/DestroyEntitiesPacket.java index a2853d9ee..8c0a4062e 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/DestroyEntitiesPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/DestroyEntitiesPacket.java @@ -1,13 +1,14 @@ package net.minestom.server.network.packet.server.play; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; import java.util.List; +import static net.minestom.server.network.NetworkBuffer.VAR_INT; + public record DestroyEntitiesPacket(@NotNull List entityIds) implements ServerPacket { public DestroyEntitiesPacket { entityIds = List.copyOf(entityIds); @@ -17,13 +18,13 @@ public record DestroyEntitiesPacket(@NotNull List entityIds) implements this(List.of(entityId)); } - public DestroyEntitiesPacket(BinaryReader reader) { - this(reader.readVarIntList(BinaryReader::readVarInt)); + public DestroyEntitiesPacket(@NotNull NetworkBuffer reader) { + this(reader.readCollection(VAR_INT)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeVarIntList(entityIds, BinaryWriter::writeVarInt); + public void write(@NotNull NetworkBuffer writer) { + writer.writeCollection(VAR_INT, entityIds); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/play/DisconnectPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/DisconnectPacket.java index 35232ba85..036112abb 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/DisconnectPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/DisconnectPacket.java @@ -1,25 +1,26 @@ package net.minestom.server.network.packet.server.play; import net.kyori.adventure.text.Component; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ComponentHoldingServerPacket; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; import java.util.Collection; import java.util.List; import java.util.function.UnaryOperator; +import static net.minestom.server.network.NetworkBuffer.COMPONENT; + public record DisconnectPacket(@NotNull Component message) implements ComponentHoldingServerPacket { - public DisconnectPacket(BinaryReader reader) { - this(reader.readComponent()); + public DisconnectPacket(@NotNull NetworkBuffer reader) { + this(reader.read(COMPONENT)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeComponent(message); + public void write(@NotNull NetworkBuffer writer) { + writer.write(COMPONENT, message); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/play/DisplayScoreboardPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/DisplayScoreboardPacket.java index 620eb5380..db83202da 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/DisplayScoreboardPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/DisplayScoreboardPacket.java @@ -1,20 +1,22 @@ package net.minestom.server.network.packet.server.play; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.BYTE; +import static net.minestom.server.network.NetworkBuffer.STRING; + public record DisplayScoreboardPacket(byte position, String scoreName) implements ServerPacket { - public DisplayScoreboardPacket(BinaryReader reader) { - this(reader.readByte(), reader.readSizedString()); + public DisplayScoreboardPacket(@NotNull NetworkBuffer reader) { + this(reader.read(BYTE), reader.read(STRING)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeByte(position); - writer.writeSizedString(scoreName); + public void write(@NotNull NetworkBuffer writer) { + writer.write(BYTE, position); + writer.write(STRING, scoreName); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/play/EffectPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/EffectPacket.java index 5e5bf4aa5..bf5fb4f29 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/EffectPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/EffectPacket.java @@ -1,24 +1,25 @@ package net.minestom.server.network.packet.server.play; import net.minestom.server.coordinate.Point; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.*; + public record EffectPacket(int effectId, Point position, int data, boolean disableRelativeVolume) implements ServerPacket { - public EffectPacket(BinaryReader reader) { - this(reader.readInt(), reader.readBlockPosition(), reader.readInt(), reader.readBoolean()); + public EffectPacket(@NotNull NetworkBuffer reader) { + this(reader.read(INT), reader.read(BLOCK_POSITION), reader.read(INT), reader.read(BOOLEAN)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeInt(effectId); - writer.writeBlockPosition(position); - writer.writeInt(data); - writer.writeBoolean(disableRelativeVolume); + public void write(@NotNull NetworkBuffer writer) { + writer.write(INT, effectId); + writer.write(BLOCK_POSITION, position); + writer.write(INT, data); + writer.write(BOOLEAN, disableRelativeVolume); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/play/EndCombatEventPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/EndCombatEventPacket.java index 80e847aa9..0f385b015 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/EndCombatEventPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/EndCombatEventPacket.java @@ -1,20 +1,22 @@ package net.minestom.server.network.packet.server.play; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.INT; +import static net.minestom.server.network.NetworkBuffer.VAR_INT; + public record EndCombatEventPacket(int duration, int entityId) implements ServerPacket { - public EndCombatEventPacket(BinaryReader reader) { - this(reader.readVarInt(), reader.readInt()); + public EndCombatEventPacket(@NotNull NetworkBuffer reader) { + this(reader.read(VAR_INT), reader.read(INT)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeVarInt(duration); - writer.writeInt(entityId); + public void write(@NotNull NetworkBuffer writer) { + writer.write(VAR_INT, duration); + writer.write(INT, entityId); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/play/EnterCombatEventPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/EnterCombatEventPacket.java index 3278f976b..5d6bedcb4 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/EnterCombatEventPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/EnterCombatEventPacket.java @@ -1,18 +1,17 @@ package net.minestom.server.network.packet.server.play; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; public record EnterCombatEventPacket() implements ServerPacket { - public EnterCombatEventPacket(BinaryReader reader) { + public EnterCombatEventPacket(@NotNull NetworkBuffer reader) { this(); } @Override - public void write(@NotNull BinaryWriter writer) { + public void write(@NotNull NetworkBuffer writer) { // Empty } diff --git a/src/main/java/net/minestom/server/network/packet/server/play/EntityAnimationPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/EntityAnimationPacket.java index be3b84b87..b555e003d 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/EntityAnimationPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/EntityAnimationPacket.java @@ -1,20 +1,22 @@ package net.minestom.server.network.packet.server.play; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.BYTE; +import static net.minestom.server.network.NetworkBuffer.VAR_INT; + public record EntityAnimationPacket(int entityId, @NotNull Animation animation) implements ServerPacket { - public EntityAnimationPacket(BinaryReader reader) { - this(reader.readVarInt(), Animation.values()[reader.readByte()]); + public EntityAnimationPacket(@NotNull NetworkBuffer reader) { + this(reader.read(VAR_INT), Animation.values()[reader.read(BYTE)]); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeVarInt(entityId); - writer.writeByte((byte) animation.ordinal()); + public void write(@NotNull NetworkBuffer writer) { + writer.write(VAR_INT, entityId); + writer.write(BYTE, (byte) animation.ordinal()); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/play/EntityEffectPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/EntityEffectPacket.java index 451e1a954..898298bfe 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/EntityEffectPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/EntityEffectPacket.java @@ -1,27 +1,27 @@ package net.minestom.server.network.packet.server.play; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; import net.minestom.server.potion.Potion; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jglrxavpok.hephaistos.nbt.NBTCompound; +import static net.minestom.server.network.NetworkBuffer.*; + public record EntityEffectPacket(int entityId, @NotNull Potion potion, @Nullable NBTCompound factorCodec) implements ServerPacket { - public EntityEffectPacket(BinaryReader reader) { - this(reader.readVarInt(), new Potion(reader), - reader.readBoolean() ? (NBTCompound) reader.readTag() : null); + public EntityEffectPacket(@NotNull NetworkBuffer reader) { + this(reader.read(VAR_INT), new Potion(reader), + reader.read(BOOLEAN) ? (NBTCompound) reader.read(NBT) : null); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeVarInt(entityId); + public void write(@NotNull NetworkBuffer writer) { + writer.write(VAR_INT, entityId); writer.write(potion); - writer.writeBoolean(factorCodec != null); - if (factorCodec != null) writer.writeNBT("", factorCodec); + writer.writeOptional(NBT, factorCodec); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/play/EntityEquipmentPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/EntityEquipmentPacket.java index e433392f3..3a773f2a6 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/EntityEquipmentPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/EntityEquipmentPacket.java @@ -3,11 +3,10 @@ package net.minestom.server.network.packet.server.play; import net.kyori.adventure.text.Component; import net.minestom.server.entity.EquipmentSlot; import net.minestom.server.item.ItemStack; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ComponentHoldingServerPacket; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; import java.util.Collection; @@ -17,6 +16,8 @@ import java.util.Objects; import java.util.function.UnaryOperator; import java.util.stream.Collectors; +import static net.minestom.server.network.NetworkBuffer.*; + public record EntityEquipmentPacket(int entityId, @NotNull Map equipments) implements ComponentHoldingServerPacket { public EntityEquipmentPacket { @@ -25,20 +26,20 @@ public record EntityEquipmentPacket(int entityId, throw new IllegalArgumentException("Equipments cannot be empty"); } - public EntityEquipmentPacket(BinaryReader reader) { - this(reader.readVarInt(), readEquipments(reader)); + public EntityEquipmentPacket(@NotNull NetworkBuffer reader) { + this(reader.read(VAR_INT), readEquipments(reader)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeVarInt(entityId); + public void write(@NotNull NetworkBuffer writer) { + writer.write(VAR_INT, entityId); int index = 0; for (var entry : equipments.entrySet()) { final boolean last = index++ == equipments.size() - 1; byte slotEnum = (byte) entry.getKey().ordinal(); if (!last) slotEnum |= 0x80; - writer.writeByte(slotEnum); - writer.writeItemStack(entry.getValue()); + writer.write(BYTE, slotEnum); + writer.write(ITEM, entry.getValue()); } } @@ -64,12 +65,12 @@ public record EntityEquipmentPacket(int entityId, return new EntityEquipmentPacket(this.entityId, map); } - private static Map readEquipments(BinaryReader reader) { + private static Map readEquipments(@NotNull NetworkBuffer reader) { Map equipments = new EnumMap<>(EquipmentSlot.class); byte slot; do { - slot = reader.readByte(); - equipments.put(EquipmentSlot.values()[slot & 0x7F], reader.readItemStack()); + slot = reader.read(BYTE); + equipments.put(EquipmentSlot.values()[slot & 0x7F], reader.read(ITEM)); } while ((slot & 0x80) == 0x80); return equipments; } diff --git a/src/main/java/net/minestom/server/network/packet/server/play/EntityHeadLookPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/EntityHeadLookPacket.java index 64b0caa90..f01622a5b 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/EntityHeadLookPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/EntityHeadLookPacket.java @@ -1,20 +1,22 @@ package net.minestom.server.network.packet.server.play; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.BYTE; +import static net.minestom.server.network.NetworkBuffer.VAR_INT; + public record EntityHeadLookPacket(int entityId, float yaw) implements ServerPacket { - public EntityHeadLookPacket(BinaryReader reader) { - this(reader.readVarInt(), reader.readByte() * 360f / 256f); + public EntityHeadLookPacket(@NotNull NetworkBuffer reader) { + this(reader.read(VAR_INT), (reader.read(BYTE) * 360f) / 256f); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeVarInt(entityId); - writer.writeByte((byte) (this.yaw * 256 / 360)); + public void write(@NotNull NetworkBuffer writer) { + writer.write(VAR_INT, entityId); + writer.write(BYTE, (byte) (this.yaw * 256 / 360)); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/play/EntityMetaDataPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/EntityMetaDataPacket.java index 1d70eac73..61384e66b 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/EntityMetaDataPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/EntityMetaDataPacket.java @@ -2,11 +2,10 @@ package net.minestom.server.network.packet.server.play; import net.kyori.adventure.text.Component; import net.minestom.server.entity.Metadata; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ComponentHoldingServerPacket; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; import java.util.Collection; @@ -14,34 +13,37 @@ import java.util.HashMap; import java.util.Map; import java.util.function.UnaryOperator; +import static net.minestom.server.network.NetworkBuffer.BYTE; +import static net.minestom.server.network.NetworkBuffer.VAR_INT; + public record EntityMetaDataPacket(int entityId, @NotNull Map> entries) implements ComponentHoldingServerPacket { public EntityMetaDataPacket { entries = Map.copyOf(entries); } - public EntityMetaDataPacket(BinaryReader reader) { - this(reader.readVarInt(), readEntries(reader)); + public EntityMetaDataPacket(@NotNull NetworkBuffer reader) { + this(reader.read(VAR_INT), readEntries(reader)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeVarInt(entityId); + public void write(@NotNull NetworkBuffer writer) { + writer.write(VAR_INT, entityId); for (var entry : entries.entrySet()) { - writer.writeByte(entry.getKey().byteValue()); + writer.write(BYTE, entry.getKey().byteValue()); writer.write(entry.getValue()); } - writer.writeByte((byte) 0xFF); // End + writer.write(BYTE, (byte) 0xFF); // End } - private static Map> readEntries(BinaryReader reader) { + private static Map> readEntries(@NotNull NetworkBuffer reader) { Map> entries = new HashMap<>(); while (true) { - final byte index = reader.readByte(); + final byte index = reader.read(BYTE); if (index == (byte) 0xFF) { // reached the end break; } - final int type = reader.readVarInt(); + final int type = reader.read(VAR_INT); entries.put((int) index, Metadata.Entry.read(type, reader)); } return entries; diff --git a/src/main/java/net/minestom/server/network/packet/server/play/EntityPositionAndRotationPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/EntityPositionAndRotationPacket.java index 59a256060..dda775d62 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/EntityPositionAndRotationPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/EntityPositionAndRotationPacket.java @@ -1,28 +1,29 @@ package net.minestom.server.network.packet.server.play; import net.minestom.server.coordinate.Pos; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.*; + public record EntityPositionAndRotationPacket(int entityId, short deltaX, short deltaY, short deltaZ, float yaw, float pitch, boolean onGround) implements ServerPacket { - public EntityPositionAndRotationPacket(BinaryReader reader) { - this(reader.readVarInt(), reader.readShort(), reader.readShort(), reader.readShort(), - reader.readByte() * 360f / 256f, reader.readByte() * 360f / 256f, reader.readBoolean()); + public EntityPositionAndRotationPacket(@NotNull NetworkBuffer reader) { + this(reader.read(VAR_INT), reader.read(SHORT), reader.read(SHORT), reader.read(SHORT), + reader.read(BYTE) * 360f / 256f, reader.read(BYTE) * 360f / 256f, reader.read(BOOLEAN)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeVarInt(entityId); - writer.writeShort(deltaX); - writer.writeShort(deltaY); - writer.writeShort(deltaZ); - writer.writeByte((byte) (yaw * 256 / 360)); - writer.writeByte((byte) (pitch * 256 / 360)); - writer.writeBoolean(onGround); + public void write(@NotNull NetworkBuffer writer) { + writer.write(VAR_INT, entityId); + writer.write(SHORT, deltaX); + writer.write(SHORT, deltaY); + writer.write(SHORT, deltaZ); + writer.write(BYTE, (byte) (yaw * 256 / 360)); + writer.write(BYTE, (byte) (pitch * 256 / 360)); + writer.write(BOOLEAN, onGround); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/play/EntityPositionPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/EntityPositionPacket.java index c097cd87f..ca7c5914d 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/EntityPositionPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/EntityPositionPacket.java @@ -1,26 +1,27 @@ package net.minestom.server.network.packet.server.play; import net.minestom.server.coordinate.Pos; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.*; + public record EntityPositionPacket(int entityId, short deltaX, short deltaY, short deltaZ, boolean onGround) implements ServerPacket { - public EntityPositionPacket(BinaryReader reader) { - this(reader.readVarInt(), reader.readShort(), reader.readShort(), reader.readShort(), reader.readBoolean()); + public EntityPositionPacket(@NotNull NetworkBuffer reader) { + this(reader.read(VAR_INT), reader.read(SHORT), reader.read(SHORT), reader.read(SHORT), reader.read(BOOLEAN)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeVarInt(entityId); - writer.writeShort(deltaX); - writer.writeShort(deltaY); - writer.writeShort(deltaZ); - writer.writeBoolean(onGround); + public void write(@NotNull NetworkBuffer writer) { + writer.write(VAR_INT, entityId); + writer.write(SHORT, deltaX); + writer.write(SHORT, deltaY); + writer.write(SHORT, deltaZ); + writer.write(BOOLEAN, onGround); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/play/EntityPropertiesPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/EntityPropertiesPacket.java index ce2360d5a..7bf0594f1 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/EntityPropertiesPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/EntityPropertiesPacket.java @@ -4,28 +4,29 @@ import net.minestom.server.attribute.Attribute; import net.minestom.server.attribute.AttributeInstance; import net.minestom.server.attribute.AttributeModifier; import net.minestom.server.attribute.AttributeOperation; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; import java.util.Collection; import java.util.List; +import static net.minestom.server.network.NetworkBuffer.*; + public record EntityPropertiesPacket(int entityId, List properties) implements ServerPacket { public EntityPropertiesPacket { properties = List.copyOf(properties); } - public EntityPropertiesPacket(BinaryReader reader) { - this(reader.readVarInt(), reader.readVarIntList(r -> { - final Attribute attribute = Attribute.fromKey(reader.readSizedString()); - final double value = reader.readDouble(); - int modifierCount = reader.readVarInt(); + public EntityPropertiesPacket(@NotNull NetworkBuffer reader) { + this(reader.read(VAR_INT), reader.readCollection(r -> { + final Attribute attribute = Attribute.fromKey(reader.read(STRING)); + final double value = reader.read(DOUBLE); + int modifierCount = reader.read(VAR_INT); AttributeInstance instance = new AttributeInstance(attribute, null); for (int i = 0; i < modifierCount; i++) { - AttributeModifier modifier = new AttributeModifier(reader.readUuid(), "", (float) reader.readDouble(), AttributeOperation.fromId(reader.readByte())); + AttributeModifier modifier = new AttributeModifier(reader.read(UUID), "", reader.read(DOUBLE), AttributeOperation.fromId(reader.read(BYTE))); instance.addModifier(modifier); } return instance; @@ -33,23 +34,23 @@ public record EntityPropertiesPacket(int entityId, List prope } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeVarInt(entityId); - writer.writeVarInt(properties.size()); + public void write(@NotNull NetworkBuffer writer) { + writer.write(VAR_INT, entityId); + writer.write(VAR_INT, properties.size()); for (AttributeInstance instance : properties) { final Attribute attribute = instance.getAttribute(); - writer.writeSizedString(attribute.key()); - writer.writeDouble(instance.getBaseValue()); + writer.write(STRING, attribute.key()); + writer.write(DOUBLE, (double) instance.getBaseValue()); { Collection modifiers = instance.getModifiers(); - writer.writeVarInt(modifiers.size()); + writer.write(VAR_INT, modifiers.size()); for (var modifier : modifiers) { - writer.writeUuid(modifier.getId()); - writer.writeDouble(modifier.getAmount()); - writer.writeByte((byte) modifier.getOperation().getId()); + writer.write(UUID, modifier.getId()); + writer.write(DOUBLE, modifier.getAmount()); + writer.write(BYTE, (byte) modifier.getOperation().getId()); } } } diff --git a/src/main/java/net/minestom/server/network/packet/server/play/EntityRotationPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/EntityRotationPacket.java index 34ffa3a9d..d14e760f4 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/EntityRotationPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/EntityRotationPacket.java @@ -1,22 +1,23 @@ package net.minestom.server.network.packet.server.play; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.*; + public record EntityRotationPacket(int entityId, float yaw, float pitch, boolean onGround) implements ServerPacket { - public EntityRotationPacket(BinaryReader reader) { - this(reader.readVarInt(), reader.readByte() * 360f / 256f, reader.readByte() * 360f / 256f, reader.readBoolean()); + public EntityRotationPacket(@NotNull NetworkBuffer reader) { + this(reader.read(VAR_INT), reader.read(BYTE) * 360f / 256f, reader.read(BYTE) * 360f / 256f, reader.read(BOOLEAN)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeVarInt(entityId); - writer.writeByte((byte) (yaw * 256 / 360)); - writer.writeByte((byte) (pitch * 256 / 360)); - writer.writeBoolean(onGround); + public void write(@NotNull NetworkBuffer writer) { + writer.write(VAR_INT, entityId); + writer.write(BYTE, (byte) (yaw * 256 / 360)); + writer.write(BYTE, (byte) (pitch * 256 / 360)); + writer.write(BOOLEAN, onGround); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/play/EntitySoundEffectPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/EntitySoundEffectPacket.java index 08db2a702..6d93af459 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/EntitySoundEffectPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/EntitySoundEffectPacket.java @@ -2,27 +2,28 @@ package net.minestom.server.network.packet.server.play; import net.kyori.adventure.sound.Sound; import net.minestom.server.adventure.AdventurePacketConvertor; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.*; + public record EntitySoundEffectPacket(int soundId, Sound.Source source, int entityId, float volume, float pitch, long seed) implements ServerPacket { - public EntitySoundEffectPacket(BinaryReader reader) { - this(reader.readVarInt(), Sound.Source.values()[reader.readVarInt()], reader.readVarInt(), - reader.readFloat(), reader.readFloat(), reader.readLong()); + public EntitySoundEffectPacket(@NotNull NetworkBuffer reader) { + this(reader.read(VAR_INT), Sound.Source.values()[reader.read(VAR_INT)], reader.read(VAR_INT), + reader.read(FLOAT), reader.read(FLOAT), reader.read(LONG)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeVarInt(soundId); - writer.writeVarInt(AdventurePacketConvertor.getSoundSourceValue(source)); - writer.writeVarInt(entityId); - writer.writeFloat(volume); - writer.writeFloat(pitch); - writer.writeLong(seed); + public void write(@NotNull NetworkBuffer writer) { + writer.write(VAR_INT, soundId); + writer.write(VAR_INT, AdventurePacketConvertor.getSoundSourceValue(source)); + writer.write(VAR_INT, entityId); + writer.write(FLOAT, volume); + writer.write(FLOAT, pitch); + writer.write(LONG, seed); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/play/EntityStatusPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/EntityStatusPacket.java index cf5bfc39a..bf341db8c 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/EntityStatusPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/EntityStatusPacket.java @@ -1,20 +1,22 @@ package net.minestom.server.network.packet.server.play; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.BYTE; +import static net.minestom.server.network.NetworkBuffer.INT; + public record EntityStatusPacket(int entityId, byte status) implements ServerPacket { - public EntityStatusPacket(BinaryReader reader) { - this(reader.readInt(), reader.readByte()); + public EntityStatusPacket(@NotNull NetworkBuffer reader) { + this(reader.read(INT), reader.read(BYTE)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeInt(entityId); - writer.writeByte(status); + public void write(@NotNull NetworkBuffer writer) { + writer.write(INT, entityId); + writer.write(BYTE, status); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/play/EntityTeleportPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/EntityTeleportPacket.java index 479ec169d..b4e46c0e0 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/EntityTeleportPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/EntityTeleportPacket.java @@ -1,28 +1,29 @@ package net.minestom.server.network.packet.server.play; import net.minestom.server.coordinate.Pos; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.*; + public record EntityTeleportPacket(int entityId, Pos position, boolean onGround) implements ServerPacket { - public EntityTeleportPacket(BinaryReader reader) { - this(reader.readVarInt(), new Pos(reader.readDouble(), reader.readDouble(), reader.readDouble(), - reader.readByte() * 360f / 256f, reader.readByte() * 360f / 256f), - reader.readBoolean()); + public EntityTeleportPacket(@NotNull NetworkBuffer reader) { + this(reader.read(VAR_INT), new Pos(reader.read(DOUBLE), reader.read(DOUBLE), reader.read(DOUBLE), + reader.read(BYTE) * 360f / 256f, reader.read(BYTE) * 360f / 256f), + reader.read(BOOLEAN)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeVarInt(entityId); - writer.writeDouble(position.x()); - writer.writeDouble(position.y()); - writer.writeDouble(position.z()); - writer.writeByte((byte) (position.yaw() * 256f / 360f)); - writer.writeByte((byte) (position.pitch() * 256f / 360f)); - writer.writeBoolean(onGround); + public void write(@NotNull NetworkBuffer writer) { + writer.write(VAR_INT, entityId); + writer.write(DOUBLE, position.x()); + writer.write(DOUBLE, position.y()); + writer.write(DOUBLE, position.z()); + writer.write(BYTE, (byte) (position.yaw() * 256f / 360f)); + writer.write(BYTE, (byte) (position.pitch() * 256f / 360f)); + writer.write(BOOLEAN, onGround); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/play/EntityVelocityPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/EntityVelocityPacket.java index b66ecc83a..29d9faa18 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/EntityVelocityPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/EntityVelocityPacket.java @@ -1,16 +1,18 @@ package net.minestom.server.network.packet.server.play; import net.minestom.server.coordinate.Point; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.SHORT; +import static net.minestom.server.network.NetworkBuffer.VAR_INT; + public record EntityVelocityPacket(int entityId, short velocityX, short velocityY, short velocityZ) implements ServerPacket { - public EntityVelocityPacket(BinaryReader reader) { - this(reader.readVarInt(), reader.readShort(), reader.readShort(), reader.readShort()); + public EntityVelocityPacket(@NotNull NetworkBuffer reader) { + this(reader.read(VAR_INT), reader.read(SHORT), reader.read(SHORT), reader.read(SHORT)); } public EntityVelocityPacket(int entityId, Point velocity) { @@ -18,11 +20,11 @@ public record EntityVelocityPacket(int entityId, short velocityX, short velocity } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeVarInt(entityId); - writer.writeShort(velocityX); - writer.writeShort(velocityY); - writer.writeShort(velocityZ); + public void write(@NotNull NetworkBuffer writer) { + writer.write(VAR_INT, entityId); + writer.write(SHORT, velocityX); + writer.write(SHORT, velocityY); + writer.write(SHORT, velocityZ); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/play/ExplosionPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/ExplosionPacket.java index 6c4c450cb..3ff4ddaea 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/ExplosionPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/ExplosionPacket.java @@ -1,30 +1,31 @@ package net.minestom.server.network.packet.server.play; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.*; + public record ExplosionPacket(float x, float y, float z, float radius, byte @NotNull [] records, float playerMotionX, float playerMotionY, float playerMotionZ) implements ServerPacket { - public ExplosionPacket(BinaryReader reader) { - this(reader.readFloat(), reader.readFloat(), reader.readFloat(), - reader.readFloat(), reader.readBytes(reader.readVarInt() * 3), - reader.readFloat(), reader.readFloat(), reader.readFloat()); + public ExplosionPacket(@NotNull NetworkBuffer reader) { + this(reader.read(FLOAT), reader.read(FLOAT), reader.read(FLOAT), + reader.read(FLOAT), reader.readBytes(reader.read(VAR_INT) * 3), + reader.read(FLOAT), reader.read(FLOAT), reader.read(FLOAT)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeFloat(x); - writer.writeFloat(y); - writer.writeFloat(z); - writer.writeFloat(radius); - writer.writeVarInt(records.length / 3); // each record is 3 bytes long - writer.writeBytes(records); - writer.writeFloat(playerMotionX); - writer.writeFloat(playerMotionY); - writer.writeFloat(playerMotionZ); + public void write(@NotNull NetworkBuffer writer) { + writer.write(FLOAT, x); + writer.write(FLOAT, y); + writer.write(FLOAT, z); + writer.write(FLOAT, radius); + writer.write(VAR_INT, records.length / 3); // each record is 3 bytes long + writer.write(RAW_BYTES, records); + writer.write(FLOAT, playerMotionX); + writer.write(FLOAT, playerMotionY); + writer.write(FLOAT, playerMotionZ); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/play/FacePlayerPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/FacePlayerPacket.java index c42d4354f..d4a032143 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/FacePlayerPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/FacePlayerPacket.java @@ -2,32 +2,33 @@ package net.minestom.server.network.packet.server.play; import net.minestom.server.coordinate.Point; import net.minestom.server.coordinate.Vec; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.*; + public record FacePlayerPacket(FacePosition facePosition, Point target, int entityId, FacePosition entityFacePosition) implements ServerPacket { - public FacePlayerPacket(BinaryReader reader) { - this(FacePosition.values()[reader.readVarInt()], - new Vec(reader.readDouble(), reader.readDouble(), reader.readDouble()), - reader.readBoolean() ? reader.readVarInt() : 0, - reader.available() > 0 ? FacePosition.values()[reader.readVarInt()] : null); + public FacePlayerPacket(@NotNull NetworkBuffer reader) { + this(FacePosition.values()[reader.read(VAR_INT)], + new Vec(reader.read(DOUBLE), reader.read(DOUBLE), reader.read(DOUBLE)), + reader.read(BOOLEAN) ? reader.read(VAR_INT) : 0, + reader.readableBytes() > 0 ? reader.readEnum(FacePosition.class) : null); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeVarInt(facePosition.ordinal()); - writer.writeDouble(target.x()); - writer.writeDouble(target.y()); - writer.writeDouble(target.z()); + public void write(@NotNull NetworkBuffer writer) { + writer.write(VAR_INT, facePosition.ordinal()); + writer.write(DOUBLE, target.x()); + writer.write(DOUBLE, target.y()); + writer.write(DOUBLE, target.z()); final boolean isEntity = entityId > 0; - writer.writeBoolean(isEntity); + writer.write(BOOLEAN, isEntity); if (isEntity) { - writer.writeVarInt(entityId); - writer.writeVarInt(entityFacePosition.ordinal()); + writer.write(VAR_INT, entityId); + writer.writeEnum(FacePosition.class, entityFacePosition); } } diff --git a/src/main/java/net/minestom/server/network/packet/server/play/HeldItemChangePacket.java b/src/main/java/net/minestom/server/network/packet/server/play/HeldItemChangePacket.java index 5ffb134e6..9bb5fd2ab 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/HeldItemChangePacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/HeldItemChangePacket.java @@ -1,19 +1,20 @@ package net.minestom.server.network.packet.server.play; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.BYTE; + public record HeldItemChangePacket(byte slot) implements ServerPacket { - public HeldItemChangePacket(BinaryReader reader) { - this(reader.readByte()); + public HeldItemChangePacket(@NotNull NetworkBuffer reader) { + this(reader.read(BYTE)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeByte(slot); + public void write(@NotNull NetworkBuffer writer) { + writer.write(BYTE, slot); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/play/InitializeWorldBorderPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/InitializeWorldBorderPacket.java index 36e6e42af..caee0a8c2 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/InitializeWorldBorderPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/InitializeWorldBorderPacket.java @@ -1,30 +1,32 @@ package net.minestom.server.network.packet.server.play; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; +import org.jetbrains.annotations.NotNull; + +import static net.minestom.server.network.NetworkBuffer.*; public record InitializeWorldBorderPacket(double x, double z, double oldDiameter, double newDiameter, long speed, int portalTeleportBoundary, int warningTime, int warningBlocks) implements ServerPacket { - public InitializeWorldBorderPacket(BinaryReader reader) { - this(reader.readDouble(), reader.readDouble(), - reader.readDouble(), reader.readDouble(), - reader.readVarLong(), reader.readVarInt(), reader.readVarInt(), reader.readVarInt()); + public InitializeWorldBorderPacket(@NotNull NetworkBuffer reader) { + this(reader.read(DOUBLE), reader.read(DOUBLE), + reader.read(DOUBLE), reader.read(DOUBLE), + reader.read(VAR_LONG), reader.read(VAR_INT), reader.read(VAR_INT), reader.read(VAR_INT)); } @Override - public void write(BinaryWriter writer) { - writer.writeDouble(x); - writer.writeDouble(z); - writer.writeDouble(oldDiameter); - writer.writeDouble(newDiameter); - writer.writeVarLong(speed); - writer.writeVarInt(portalTeleportBoundary); - writer.writeVarInt(warningTime); - writer.writeVarInt(warningBlocks); + public void write(@NotNull NetworkBuffer writer) { + writer.write(DOUBLE, x); + writer.write(DOUBLE, z); + writer.write(DOUBLE, oldDiameter); + writer.write(DOUBLE, newDiameter); + writer.write(VAR_LONG, speed); + writer.write(VAR_INT, portalTeleportBoundary); + writer.write(VAR_INT, warningTime); + writer.write(VAR_INT, warningBlocks); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/play/JoinGamePacket.java b/src/main/java/net/minestom/server/network/packet/server/play/JoinGamePacket.java index f1e7496c7..6935794b6 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/JoinGamePacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/JoinGamePacket.java @@ -1,15 +1,16 @@ package net.minestom.server.network.packet.server.play; import net.minestom.server.entity.GameMode; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; import org.jglrxavpok.hephaistos.nbt.NBTCompound; import java.util.List; +import static net.minestom.server.network.NetworkBuffer.*; + public record JoinGamePacket(int entityId, boolean isHardcore, GameMode gameMode, GameMode previousGameMode, List worlds, NBTCompound dimensionCodec, String dimensionType, String world, long hashedSeed, int maxPlayers, int viewDistance, int simulationDistance, @@ -19,41 +20,41 @@ public record JoinGamePacket(int entityId, boolean isHardcore, GameMode gameMode worlds = List.copyOf(worlds); } - public JoinGamePacket(BinaryReader reader) { - this(reader.readVarInt(), reader.readBoolean(), GameMode.fromId(reader.readByte()), GameMode.fromId(reader.readByte()), - List.of(reader.readSizedStringArray()), (NBTCompound) reader.readTag(), reader.readSizedString(), reader.readSizedString(), - reader.readLong(), reader.readVarInt(), reader.readVarInt(), reader.readVarInt(), - reader.readBoolean(), reader.readBoolean(), reader.readBoolean(), reader.readBoolean()); + public JoinGamePacket(@NotNull NetworkBuffer reader) { + this(reader.read(VAR_INT), reader.read(BOOLEAN), GameMode.fromId(reader.read(BYTE)), GameMode.fromId(reader.read(BYTE)), + reader.readCollection(STRING), (NBTCompound) reader.read(NBT), reader.read(STRING), reader.read(STRING), + reader.read(LONG), reader.read(VAR_INT), reader.read(VAR_INT), reader.read(VAR_INT), + reader.read(BOOLEAN), reader.read(BOOLEAN), reader.read(BOOLEAN), reader.read(BOOLEAN)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeInt(entityId); - writer.writeBoolean(isHardcore); - writer.writeByte(gameMode.id()); + public void write(@NotNull NetworkBuffer writer) { + writer.write(INT, entityId); + writer.write(BOOLEAN, isHardcore); + writer.write(BYTE, gameMode.id()); if (previousGameMode != null) { - writer.writeByte(previousGameMode.id()); + writer.write(BYTE, previousGameMode.id()); } else { - writer.writeByte((byte) -1); + writer.write(BYTE, (byte) -1); } - writer.writeVarIntList(worlds, BinaryWriter::writeSizedString); - writer.writeNBT("", dimensionCodec); + writer.writeCollection(STRING, worlds); + writer.write(NBT, dimensionCodec); - writer.writeSizedString(dimensionType); - writer.writeSizedString(world); - writer.writeLong(hashedSeed); - writer.writeVarInt(maxPlayers); - writer.writeVarInt(viewDistance); - writer.writeVarInt(simulationDistance); - writer.writeBoolean(reducedDebugInfo); - writer.writeBoolean(enableRespawnScreen); + writer.write(STRING, dimensionType); + writer.write(STRING, world); + writer.write(LONG, hashedSeed); + writer.write(VAR_INT, maxPlayers); + writer.write(VAR_INT, viewDistance); + writer.write(VAR_INT, simulationDistance); + writer.write(BOOLEAN, reducedDebugInfo); + writer.write(BOOLEAN, enableRespawnScreen); //debug - writer.writeBoolean(isDebug); + writer.write(BOOLEAN, isDebug); //is flat - writer.writeBoolean(isFlat); + writer.write(BOOLEAN, isFlat); - writer.writeBoolean(false); + writer.write(BOOLEAN, false); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/play/KeepAlivePacket.java b/src/main/java/net/minestom/server/network/packet/server/play/KeepAlivePacket.java index 80cb08c41..7fc79fd26 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/KeepAlivePacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/KeepAlivePacket.java @@ -1,19 +1,20 @@ package net.minestom.server.network.packet.server.play; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.LONG; + public record KeepAlivePacket(long id) implements ServerPacket { - public KeepAlivePacket(BinaryReader reader) { - this(reader.readLong()); + public KeepAlivePacket(@NotNull NetworkBuffer reader) { + this(reader.read(LONG)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeLong(id); + public void write(@NotNull NetworkBuffer writer) { + writer.write(LONG, id); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/play/MapDataPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/MapDataPacket.java index c6f0fb1fc..d06ea31b2 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/MapDataPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/MapDataPacket.java @@ -1,16 +1,16 @@ package net.minestom.server.network.packet.server.play; import net.kyori.adventure.text.Component; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; -import net.minestom.server.utils.binary.Writeable; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.util.List; +import static net.minestom.server.network.NetworkBuffer.*; + public record MapDataPacket(int mapId, byte scale, boolean locked, boolean trackingPosition, @NotNull List icons, @Nullable MapDataPacket.ColorContent colorContent) implements ServerPacket { @@ -18,7 +18,7 @@ public record MapDataPacket(int mapId, byte scale, boolean locked, icons = List.copyOf(icons); } - public MapDataPacket(BinaryReader reader) { + public MapDataPacket(@NotNull NetworkBuffer reader) { this(read(reader)); } @@ -28,35 +28,35 @@ public record MapDataPacket(int mapId, byte scale, boolean locked, packet.colorContent); } - private static MapDataPacket read(BinaryReader reader) { - var mapId = reader.readVarInt(); - var scale = reader.readByte(); - var locked = reader.readBoolean(); - var trackingPosition = reader.readBoolean(); - List icons = trackingPosition ? reader.readVarIntList(Icon::new) : List.of(); + private static MapDataPacket read(@NotNull NetworkBuffer reader) { + var mapId = reader.read(VAR_INT); + var scale = reader.read(BYTE); + var locked = reader.read(BOOLEAN); + var trackingPosition = reader.read(BOOLEAN); + List icons = trackingPosition ? reader.readCollection(Icon::new) : List.of(); - var columns = reader.readByte(); + var columns = reader.read(BYTE); if (columns <= 0) return new MapDataPacket(mapId, scale, locked, trackingPosition, icons, null); - byte rows = reader.readByte(); - byte x = reader.readByte(); - byte z = reader.readByte(); - byte[] data = reader.readByteArray(); + byte rows = reader.read(BYTE); + byte x = reader.read(BYTE); + byte z = reader.read(BYTE); + byte[] data = reader.read(BYTE_ARRAY); return new MapDataPacket(mapId, scale, locked, trackingPosition, icons, new ColorContent(columns, rows, x, z, data)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeVarInt(mapId); - writer.writeByte(scale); - writer.writeBoolean(locked); - writer.writeBoolean(trackingPosition); - if (trackingPosition) writer.writeVarIntList(icons, BinaryWriter::write); + public void write(@NotNull NetworkBuffer writer) { + writer.write(VAR_INT, mapId); + writer.write(BYTE, scale); + writer.write(BOOLEAN, locked); + writer.write(BOOLEAN, trackingPosition); + if (trackingPosition) writer.writeCollection(icons); if (colorContent != null) { writer.write(colorContent); } else { - writer.writeByte((byte) 0); + writer.write(BYTE, (byte) 0); } } @@ -66,35 +66,35 @@ public record MapDataPacket(int mapId, byte scale, boolean locked, } public record Icon(int type, byte x, byte z, byte direction, - @Nullable Component displayName) implements Writeable { - public Icon(BinaryReader reader) { - this(reader.readVarInt(), reader.readByte(), reader.readByte(), reader.readByte(), - reader.readBoolean() ? reader.readComponent() : null); + @Nullable Component displayName) implements NetworkBuffer.Writer { + public Icon(@NotNull NetworkBuffer reader) { + this(reader.read(VAR_INT), reader.read(BYTE), reader.read(BYTE), reader.read(BYTE), + reader.read(BOOLEAN) ? reader.read(COMPONENT) : null); } - public void write(BinaryWriter writer) { - writer.writeVarInt(type); - writer.writeByte(x); - writer.writeByte(z); - writer.writeByte(direction); - writer.writeBoolean(displayName != null); - if (displayName != null) writer.writeComponent(displayName); + public void write(@NotNull NetworkBuffer writer) { + writer.write(VAR_INT, type); + writer.write(BYTE, x); + writer.write(BYTE, z); + writer.write(BYTE, direction); + writer.write(BOOLEAN, displayName != null); + if (displayName != null) writer.write(COMPONENT, displayName); } } public record ColorContent(byte columns, byte rows, byte x, byte z, - byte @NotNull [] data) implements Writeable { - public ColorContent(BinaryReader reader) { - this(reader.readByte(), reader.readByte(), reader.readByte(), reader.readByte(), - reader.readByteArray()); + byte @NotNull [] data) implements NetworkBuffer.Writer { + public ColorContent(@NotNull NetworkBuffer reader) { + this(reader.read(BYTE), reader.read(BYTE), reader.read(BYTE), reader.read(BYTE), + reader.read(BYTE_ARRAY)); } - public void write(BinaryWriter writer) { - writer.writeByte(columns); - writer.writeByte(rows); - writer.writeByte(x); - writer.writeByte(z); - writer.writeByteArray(data); + public void write(@NotNull NetworkBuffer writer) { + writer.write(BYTE, columns); + writer.write(BYTE, rows); + writer.write(BYTE, x); + writer.write(BYTE, z); + writer.write(BYTE_ARRAY, data); } } } diff --git a/src/main/java/net/minestom/server/network/packet/server/play/MultiBlockChangePacket.java b/src/main/java/net/minestom/server/network/packet/server/play/MultiBlockChangePacket.java index a59eecedb..b9b7740eb 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/MultiBlockChangePacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/MultiBlockChangePacket.java @@ -1,11 +1,12 @@ package net.minestom.server.network.packet.server.play; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.*; + public record MultiBlockChangePacket(long chunkSectionPosition, boolean suppressLightUpdates, long[] blocks) implements ServerPacket { @@ -16,15 +17,15 @@ public record MultiBlockChangePacket(long chunkSectionPosition, suppressLightUpdates, blocks); } - public MultiBlockChangePacket(BinaryReader reader) { - this(reader.readLong(), reader.readBoolean(), reader.readVarLongArray()); + public MultiBlockChangePacket(@NotNull NetworkBuffer reader) { + this(reader.read(LONG), reader.read(BOOLEAN), reader.read(VAR_LONG_ARRAY)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeLong(chunkSectionPosition); - writer.writeBoolean(suppressLightUpdates); - writer.writeVarLongArray(blocks); + public void write(@NotNull NetworkBuffer writer) { + writer.write(LONG, chunkSectionPosition); + writer.write(BOOLEAN, suppressLightUpdates); + writer.write(VAR_LONG_ARRAY, blocks); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/play/NamedSoundEffectPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/NamedSoundEffectPacket.java index f45b95a4b..7ceaea8ec 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/NamedSoundEffectPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/NamedSoundEffectPacket.java @@ -2,30 +2,31 @@ package net.minestom.server.network.packet.server.play; import net.kyori.adventure.sound.Sound.Source; import net.minestom.server.adventure.AdventurePacketConvertor; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.*; + public record NamedSoundEffectPacket(String soundName, Source source, int x, int y, int z, float volume, float pitch, long seed) implements ServerPacket { - public NamedSoundEffectPacket(BinaryReader reader) { - this(reader.readSizedString(), Source.values()[reader.readVarInt()], - reader.readInt() / 8, reader.readInt() / 8, reader.readInt() / 8, - reader.readFloat(), reader.readFloat(), reader.readLong()); + public NamedSoundEffectPacket(@NotNull NetworkBuffer reader) { + this(reader.read(STRING), Source.values()[reader.read(VAR_INT)], + reader.read(INT) / 8, reader.read(INT) / 8, reader.read(INT) / 8, + reader.read(FLOAT), reader.read(FLOAT), reader.read(LONG)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeSizedString(soundName); - writer.writeVarInt(AdventurePacketConvertor.getSoundSourceValue(source)); - writer.writeInt(x * 8); - writer.writeInt(y * 8); - writer.writeInt(z * 8); - writer.writeFloat(volume); - writer.writeFloat(pitch); - writer.writeLong(seed); + public void write(@NotNull NetworkBuffer writer) { + writer.write(STRING, soundName); + writer.write(VAR_INT, AdventurePacketConvertor.getSoundSourceValue(source)); + writer.write(INT, x * 8); + writer.write(INT, y * 8); + writer.write(INT, z * 8); + writer.write(FLOAT, volume); + writer.write(FLOAT, pitch); + writer.write(LONG, seed); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/play/NbtQueryResponsePacket.java b/src/main/java/net/minestom/server/network/packet/server/play/NbtQueryResponsePacket.java index ff3f38601..c7fb8042c 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/NbtQueryResponsePacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/NbtQueryResponsePacket.java @@ -1,25 +1,26 @@ package net.minestom.server.network.packet.server.play; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; import org.jglrxavpok.hephaistos.nbt.NBTCompound; +import static net.minestom.server.network.NetworkBuffer.*; + public record NbtQueryResponsePacket(int transactionId, NBTCompound data) implements ServerPacket { - public NbtQueryResponsePacket(BinaryReader reader) { - this(reader.readVarInt(), (NBTCompound) reader.readTag()); + public NbtQueryResponsePacket(@NotNull NetworkBuffer reader) { + this(reader.read(VAR_INT), (NBTCompound) reader.read(NBT)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeVarInt(transactionId); + public void write(@NotNull NetworkBuffer writer) { + writer.write(VAR_INT, transactionId); if (data != null) { - writer.writeNBT("", data); + writer.write(NBT, data); } else { // TAG_End - writer.writeByte((byte) 0x00); + writer.write(BYTE, (byte) 0x00); } } diff --git a/src/main/java/net/minestom/server/network/packet/server/play/OpenBookPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/OpenBookPacket.java index 150d07aac..b4a78c6bc 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/OpenBookPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/OpenBookPacket.java @@ -1,20 +1,19 @@ package net.minestom.server.network.packet.server.play; import net.minestom.server.entity.Player; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; public record OpenBookPacket(@NotNull Player.Hand hand) implements ServerPacket { - public OpenBookPacket(BinaryReader reader) { - this(Player.Hand.values()[reader.readVarInt()]); + public OpenBookPacket(@NotNull NetworkBuffer reader) { + this(reader.readEnum(Player.Hand.class)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeVarInt(hand.ordinal()); + public void write(@NotNull NetworkBuffer writer) { + writer.writeEnum(Player.Hand.class, hand); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/play/OpenHorseWindowPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/OpenHorseWindowPacket.java index 295bf6a1e..03ce46f6d 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/OpenHorseWindowPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/OpenHorseWindowPacket.java @@ -1,21 +1,22 @@ package net.minestom.server.network.packet.server.play; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.*; + public record OpenHorseWindowPacket(byte windowId, int slotCount, int entityId) implements ServerPacket { - public OpenHorseWindowPacket(BinaryReader reader) { - this(reader.readByte(), reader.readVarInt(), reader.readInt()); + public OpenHorseWindowPacket(@NotNull NetworkBuffer reader) { + this(reader.read(BYTE), reader.read(VAR_INT), reader.read(INT)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeByte(windowId); - writer.writeVarInt(slotCount); - writer.writeInt(entityId); + public void write(@NotNull NetworkBuffer writer) { + writer.write(BYTE, windowId); + writer.write(VAR_INT, slotCount); + writer.write(INT, entityId); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/play/OpenSignEditorPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/OpenSignEditorPacket.java index 9bd522bb4..8f695caff 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/OpenSignEditorPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/OpenSignEditorPacket.java @@ -1,20 +1,21 @@ package net.minestom.server.network.packet.server.play; import net.minestom.server.coordinate.Point; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.BLOCK_POSITION; + public record OpenSignEditorPacket(@NotNull Point position) implements ServerPacket { - public OpenSignEditorPacket(BinaryReader reader) { - this(reader.readBlockPosition()); + public OpenSignEditorPacket(@NotNull NetworkBuffer reader) { + this(reader.read(BLOCK_POSITION)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeBlockPosition(position); + public void write(@NotNull NetworkBuffer writer) { + writer.write(BLOCK_POSITION, position); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/play/OpenWindowPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/OpenWindowPacket.java index 71c35a83d..839ec7efd 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/OpenWindowPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/OpenWindowPacket.java @@ -1,28 +1,30 @@ package net.minestom.server.network.packet.server.play; import net.kyori.adventure.text.Component; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ComponentHoldingServerPacket; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; import java.util.Collection; import java.util.List; import java.util.function.UnaryOperator; +import static net.minestom.server.network.NetworkBuffer.COMPONENT; +import static net.minestom.server.network.NetworkBuffer.VAR_INT; + public record OpenWindowPacket(int windowId, int windowType, @NotNull Component title) implements ComponentHoldingServerPacket { - public OpenWindowPacket(BinaryReader reader) { - this(reader.readVarInt(), reader.readVarInt(), reader.readComponent()); + public OpenWindowPacket(@NotNull NetworkBuffer reader) { + this(reader.read(VAR_INT), reader.read(VAR_INT), reader.read(COMPONENT)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeVarInt(windowId); - writer.writeVarInt(windowType); - writer.writeComponent(title); + public void write(@NotNull NetworkBuffer writer) { + writer.write(VAR_INT, windowId); + writer.write(VAR_INT, windowType); + writer.write(COMPONENT, title); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/play/ParticlePacket.java b/src/main/java/net/minestom/server/network/packet/server/play/ParticlePacket.java index c557cb542..b5e7411d5 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/ParticlePacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/ParticlePacket.java @@ -1,36 +1,37 @@ package net.minestom.server.network.packet.server.play; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.*; + public record ParticlePacket(int particleId, boolean longDistance, double x, double y, double z, float offsetX, float offsetY, float offsetZ, float particleData, int particleCount, byte[] data) implements ServerPacket { - public ParticlePacket(BinaryReader reader) { - this(reader.readVarInt(), reader.readBoolean(), - reader.readDouble(), reader.readDouble(), reader.readDouble(), - reader.readFloat(), reader.readFloat(), reader.readFloat(), - reader.readFloat(), reader.readInt(), reader.readRemainingBytes()); + public ParticlePacket(@NotNull NetworkBuffer reader) { + this(reader.read(VAR_INT), reader.read(BOOLEAN), + reader.read(DOUBLE), reader.read(DOUBLE), reader.read(DOUBLE), + reader.read(FLOAT), reader.read(FLOAT), reader.read(FLOAT), + reader.read(FLOAT), reader.read(INT), reader.read(RAW_BYTES)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeVarInt(particleId); - writer.writeBoolean(longDistance); - writer.writeDouble(x); - writer.writeDouble(y); - writer.writeDouble(z); - writer.writeFloat(offsetX); - writer.writeFloat(offsetY); - writer.writeFloat(offsetZ); - writer.writeFloat(particleData); - writer.writeInt(particleCount); + public void write(@NotNull NetworkBuffer writer) { + writer.write(VAR_INT, particleId); + writer.write(BOOLEAN, longDistance); + writer.write(DOUBLE, x); + writer.write(DOUBLE, y); + writer.write(DOUBLE, z); + writer.write(FLOAT, offsetX); + writer.write(FLOAT, offsetY); + writer.write(FLOAT, offsetZ); + writer.write(FLOAT, particleData); + writer.write(INT, particleCount); - writer.writeBytes(data); + writer.write(RAW_BYTES, data); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/play/PingPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/PingPacket.java index 8c4ad6723..6f2e18ec9 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/PingPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/PingPacket.java @@ -1,19 +1,20 @@ package net.minestom.server.network.packet.server.play; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.INT; + public record PingPacket(int id) implements ServerPacket { - public PingPacket(BinaryReader reader) { - this(reader.readInt()); + public PingPacket(@NotNull NetworkBuffer reader) { + this(reader.read(INT)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeInt(id); + public void write(@NotNull NetworkBuffer writer) { + writer.write(INT, id); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/play/PlayerAbilitiesPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/PlayerAbilitiesPacket.java index 2fed46fdf..4150dfd26 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/PlayerAbilitiesPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/PlayerAbilitiesPacket.java @@ -1,26 +1,28 @@ package net.minestom.server.network.packet.server.play; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.BYTE; +import static net.minestom.server.network.NetworkBuffer.FLOAT; + public record PlayerAbilitiesPacket(byte flags, float flyingSpeed, float fieldViewModifier) implements ServerPacket { public static final byte FLAG_INVULNERABLE = 0x01; public static final byte FLAG_FLYING = 0x02; public static final byte FLAG_ALLOW_FLYING = 0x04; public static final byte FLAG_INSTANT_BREAK = 0x08; - public PlayerAbilitiesPacket(BinaryReader reader) { - this(reader.readByte(), reader.readFloat(), reader.readFloat()); + public PlayerAbilitiesPacket(@NotNull NetworkBuffer reader) { + this(reader.read(BYTE), reader.read(FLOAT), reader.read(FLOAT)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeByte(flags); - writer.writeFloat(flyingSpeed); - writer.writeFloat(fieldViewModifier); + public void write(@NotNull NetworkBuffer writer) { + writer.write(BYTE, flags); + writer.write(FLOAT, flyingSpeed); + writer.write(FLOAT, fieldViewModifier); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/play/PlayerChatHeaderPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/PlayerChatHeaderPacket.java index 65741a7ce..2a13bab72 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/PlayerChatHeaderPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/PlayerChatHeaderPacket.java @@ -2,23 +2,24 @@ package net.minestom.server.network.packet.server.play; import net.minestom.server.crypto.MessageSignature; import net.minestom.server.crypto.SignedMessageHeader; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.BYTE_ARRAY; + public record PlayerChatHeaderPacket(@NotNull SignedMessageHeader messageHeader, @NotNull MessageSignature signature, byte[] bodyDigest) implements ServerPacket { - public PlayerChatHeaderPacket(BinaryReader reader) { - this(new SignedMessageHeader(reader), new MessageSignature(reader), reader.readByteArray()); + public PlayerChatHeaderPacket(@NotNull NetworkBuffer reader) { + this(new SignedMessageHeader(reader), new MessageSignature(reader), reader.read(BYTE_ARRAY)); } @Override - public void write(@NotNull BinaryWriter writer) { + public void write(@NotNull NetworkBuffer writer) { writer.write(messageHeader); writer.write(signature); - writer.writeByteArray(bodyDigest); + writer.write(BYTE_ARRAY, bodyDigest); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/play/PlayerChatMessagePacket.java b/src/main/java/net/minestom/server/network/packet/server/play/PlayerChatMessagePacket.java index bae83de66..143867aef 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/PlayerChatMessagePacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/PlayerChatMessagePacket.java @@ -2,11 +2,10 @@ package net.minestom.server.network.packet.server.play; import net.kyori.adventure.text.Component; import net.minestom.server.crypto.MessageSignature; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ComponentHoldingServerPacket; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -15,6 +14,8 @@ import java.util.List; import java.util.UUID; import java.util.function.UnaryOperator; +import static net.minestom.server.network.NetworkBuffer.*; + /** * Represents an outgoing chat message packet. */ @@ -22,23 +23,21 @@ public record PlayerChatMessagePacket(@NotNull Component signedContent, @Nullabl int type, @NotNull UUID uuid, @NotNull Component displayName, @Nullable Component teamDisplayName, @NotNull MessageSignature signature) implements ComponentHoldingServerPacket { - public PlayerChatMessagePacket(BinaryReader reader) { - this(reader.readComponent(), reader.readBoolean() ? reader.readComponent() : null, - reader.readVarInt(), reader.readUuid(), - reader.readComponent(), reader.readBoolean() ? reader.readComponent() : null, + public PlayerChatMessagePacket(@NotNull NetworkBuffer reader) { + this(reader.read(COMPONENT), reader.readOptional(COMPONENT), + reader.read(VAR_INT), reader.read(NetworkBuffer.UUID), + reader.read(COMPONENT), reader.readOptional(COMPONENT), new MessageSignature(reader)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeComponent(signedContent); - writer.writeBoolean(unsignedContent != null); - if (unsignedContent != null) writer.writeComponent(unsignedContent); - writer.writeVarInt(type); - writer.writeUuid(uuid); - writer.writeComponent(displayName); - writer.writeBoolean(teamDisplayName != null); - if (teamDisplayName != null) writer.writeComponent(teamDisplayName); + public void write(@NotNull NetworkBuffer writer) { + writer.write(COMPONENT, signedContent); + writer.writeOptional(COMPONENT, unsignedContent); + writer.write(VAR_INT, type); + writer.write(UUID, uuid); + writer.write(COMPONENT, displayName); + writer.writeOptional(COMPONENT, teamDisplayName); writer.write(signature); } diff --git a/src/main/java/net/minestom/server/network/packet/server/play/PlayerInfoPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/PlayerInfoPacket.java index 2621e2dae..5e21cc4e8 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/PlayerInfoPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/PlayerInfoPacket.java @@ -4,18 +4,21 @@ import net.kyori.adventure.text.Component; import net.minestom.server.adventure.ComponentHolder; import net.minestom.server.crypto.PlayerPublicKey; import net.minestom.server.entity.GameMode; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ComponentHoldingServerPacket; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; -import net.minestom.server.utils.binary.Writeable; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.util.*; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.UUID; import java.util.function.UnaryOperator; +import static net.minestom.server.network.NetworkBuffer.*; + public record PlayerInfoPacket(@NotNull Action action, @NotNull List entries) implements ComponentHoldingServerPacket { public PlayerInfoPacket { @@ -30,7 +33,7 @@ public record PlayerInfoPacket(@NotNull Action action, this(action, List.of(entry)); } - public PlayerInfoPacket(BinaryReader reader) { + public PlayerInfoPacket(@NotNull NetworkBuffer reader) { this(read(reader)); } @@ -38,12 +41,12 @@ public record PlayerInfoPacket(@NotNull Action action, this(packet.action, packet.entries); } - private static PlayerInfoPacket read(BinaryReader reader) { - var action = Action.values()[reader.readVarInt()]; - final int playerInfoCount = reader.readVarInt(); + private static PlayerInfoPacket read(@NotNull NetworkBuffer reader) { + var action = Action.values()[reader.read(VAR_INT)]; + final int playerInfoCount = reader.read(VAR_INT); List entries = new ArrayList<>(playerInfoCount); for (int i = 0; i < playerInfoCount; i++) { - final UUID uuid = reader.readUuid(); + final UUID uuid = reader.read(UUID); entries.add(switch (action) { case ADD_PLAYER -> new AddPlayer(uuid, reader); case UPDATE_GAMEMODE -> new UpdateGameMode(uuid, reader); @@ -56,10 +59,10 @@ public record PlayerInfoPacket(@NotNull Action action, } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeVarInt(action.ordinal()); - writer.writeVarIntList(entries, (w, entry) -> { - w.writeUuid(entry.uuid()); + public void write(@NotNull NetworkBuffer writer) { + writer.write(VAR_INT, action.ordinal()); + writer.writeCollection(entries, (w, entry) -> { + w.write(UUID, entry.uuid()); entry.write(w); }); } @@ -72,8 +75,7 @@ public record PlayerInfoPacket(@NotNull Action action, @Override public @NotNull Collection components() { switch (this.action) { - case ADD_PLAYER: - case UPDATE_DISPLAY_NAME: + case ADD_PLAYER, UPDATE_DISPLAY_NAME -> { List components = new ArrayList<>(); for (Entry entry : entries) { if (entry instanceof ComponentHolder) { @@ -81,8 +83,10 @@ public record PlayerInfoPacket(@NotNull Action action, } } return components; - default: + } + default -> { return List.of(); + } } } @@ -123,7 +127,7 @@ public record PlayerInfoPacket(@NotNull Action action, } } - public sealed interface Entry extends Writeable + public sealed interface Entry extends NetworkBuffer.Writer permits AddPlayer, UpdateGameMode, UpdateLatency, UpdateDisplayName, RemovePlayer { UUID uuid(); } @@ -135,24 +139,22 @@ public record PlayerInfoPacket(@NotNull Action action, properties = List.copyOf(properties); } - public AddPlayer(UUID uuid, BinaryReader reader) { - this(uuid, reader.readSizedString(), - reader.readVarIntList(Property::new), - GameMode.values()[reader.readVarInt()], reader.readVarInt(), - reader.readBoolean() ? reader.readComponent() : null, - reader.readBoolean() ? new PlayerPublicKey(reader) : null); + public AddPlayer(UUID uuid, NetworkBuffer reader) { + this(uuid, reader.read(STRING), + reader.readCollection(Property::new), + GameMode.values()[reader.read(VAR_INT)], reader.read(VAR_INT), + reader.read(BOOLEAN) ? reader.read(COMPONENT) : null, + reader.read(BOOLEAN) ? new PlayerPublicKey(reader) : null); } @Override - public void write(BinaryWriter writer) { - writer.writeSizedString(name); - writer.writeVarIntList(properties, BinaryWriter::write); - writer.writeVarInt(gameMode.id()); - writer.writeVarInt(ping); - writer.writeBoolean(displayName != null); - if (displayName != null) writer.writeComponent(displayName); - writer.writeBoolean(playerPublicKey != null); - if (playerPublicKey != null) writer.write(playerPublicKey); + public void write(@NotNull NetworkBuffer writer) { + writer.write(STRING, name); + writer.writeCollection(properties); + writer.write(VAR_INT, (int) gameMode.id()); + writer.write(VAR_INT, ping); + writer.writeOptional(COMPONENT, displayName); + writer.writeOptional(playerPublicKey); } @Override @@ -167,58 +169,58 @@ public record PlayerInfoPacket(@NotNull Action action, } public record Property(@NotNull String name, @NotNull String value, - @Nullable String signature) implements Writeable { + @Nullable String signature) implements NetworkBuffer.Writer { public Property(String name, String value) { this(name, value, null); } - public Property(BinaryReader reader) { - this(reader.readSizedString(), reader.readSizedString(), - reader.readBoolean() ? reader.readSizedString() : null); + public Property(@NotNull NetworkBuffer reader) { + this(reader.read(STRING), reader.read(STRING), + reader.read(BOOLEAN) ? reader.read(STRING) : null); } @Override - public void write(BinaryWriter writer) { - writer.writeSizedString(name); - writer.writeSizedString(value); - writer.writeBoolean(signature != null); - if (signature != null) writer.writeSizedString(signature); + public void write(@NotNull NetworkBuffer writer) { + writer.write(STRING, name); + writer.write(STRING, value); + writer.write(BOOLEAN, signature != null); + if (signature != null) writer.write(STRING, signature); } } } public record UpdateGameMode(UUID uuid, GameMode gameMode) implements Entry { - public UpdateGameMode(UUID uuid, BinaryReader reader) { - this(uuid, GameMode.fromId((byte) reader.readVarInt())); + public UpdateGameMode(UUID uuid, NetworkBuffer reader) { + this(uuid, GameMode.fromId(reader.read(VAR_INT).byteValue())); } @Override - public void write(BinaryWriter writer) { - writer.writeVarInt(gameMode.id()); + public void write(@NotNull NetworkBuffer writer) { + writer.write(VAR_INT, (int) gameMode.id()); } } public record UpdateLatency(UUID uuid, int ping) implements Entry { - public UpdateLatency(UUID uuid, BinaryReader reader) { - this(uuid, reader.readVarInt()); + public UpdateLatency(UUID uuid, NetworkBuffer reader) { + this(uuid, reader.read(VAR_INT)); } @Override - public void write(BinaryWriter writer) { - writer.writeVarInt(ping); + public void write(@NotNull NetworkBuffer writer) { + writer.write(VAR_INT, ping); } } public record UpdateDisplayName(@NotNull UUID uuid, @Nullable Component displayName) implements Entry, ComponentHolder { - public UpdateDisplayName(UUID uuid, BinaryReader reader) { - this(uuid, reader.readBoolean() ? reader.readComponent() : null); + public UpdateDisplayName(UUID uuid, NetworkBuffer reader) { + this(uuid, reader.read(BOOLEAN) ? reader.read(COMPONENT) : null); } @Override - public void write(BinaryWriter writer) { - writer.writeBoolean(displayName != null); - if (displayName != null) writer.writeComponent(displayName); + public void write(@NotNull NetworkBuffer writer) { + writer.write(BOOLEAN, displayName != null); + if (displayName != null) writer.write(COMPONENT, displayName); } @Override @@ -234,7 +236,7 @@ public record PlayerInfoPacket(@NotNull Action action, public record RemovePlayer(@NotNull UUID uuid) implements Entry { @Override - public void write(BinaryWriter writer) { + public void write(@NotNull NetworkBuffer writer) { } } } diff --git a/src/main/java/net/minestom/server/network/packet/server/play/PlayerListHeaderAndFooterPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/PlayerListHeaderAndFooterPacket.java index f938458f1..0d9327c34 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/PlayerListHeaderAndFooterPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/PlayerListHeaderAndFooterPacket.java @@ -1,27 +1,28 @@ package net.minestom.server.network.packet.server.play; import net.kyori.adventure.text.Component; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ComponentHoldingServerPacket; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; import java.util.Collection; import java.util.List; import java.util.function.UnaryOperator; +import static net.minestom.server.network.NetworkBuffer.COMPONENT; + public record PlayerListHeaderAndFooterPacket(@NotNull Component header, @NotNull Component footer) implements ComponentHoldingServerPacket { - public PlayerListHeaderAndFooterPacket(BinaryReader reader) { - this(reader.readComponent(), reader.readComponent()); + public PlayerListHeaderAndFooterPacket(@NotNull NetworkBuffer reader) { + this(reader.read(COMPONENT), reader.read(COMPONENT)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeComponent(header); - writer.writeComponent(footer); + public void write(@NotNull NetworkBuffer writer) { + writer.write(COMPONENT, header); + writer.write(COMPONENT, footer); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/play/PlayerPositionAndLookPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/PlayerPositionAndLookPacket.java index 6e1409e52..16aefb061 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/PlayerPositionAndLookPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/PlayerPositionAndLookPacket.java @@ -1,31 +1,32 @@ package net.minestom.server.network.packet.server.play; import net.minestom.server.coordinate.Pos; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.*; + public record PlayerPositionAndLookPacket(Pos position, byte flags, int teleportId, boolean dismountVehicle) implements ServerPacket { - public PlayerPositionAndLookPacket(BinaryReader reader) { - this(new Pos(reader.readDouble(), reader.readDouble(), reader.readDouble(), reader.readFloat(), reader.readFloat()), - reader.readByte(), reader.readVarInt(), reader.readBoolean()); + public PlayerPositionAndLookPacket(@NotNull NetworkBuffer reader) { + this(new Pos(reader.read(DOUBLE), reader.read(DOUBLE), reader.read(DOUBLE), reader.read(FLOAT), reader.read(FLOAT)), + reader.read(BYTE), reader.read(VAR_INT), reader.read(BOOLEAN)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeDouble(position.x()); - writer.writeDouble(position.y()); - writer.writeDouble(position.z()); + public void write(@NotNull NetworkBuffer writer) { + writer.write(DOUBLE, position.x()); + writer.write(DOUBLE, position.y()); + writer.write(DOUBLE, position.z()); - writer.writeFloat(position.yaw()); - writer.writeFloat(position.pitch()); + writer.write(FLOAT, position.yaw()); + writer.write(FLOAT, position.pitch()); - writer.writeByte(flags); - writer.writeVarInt(teleportId); - writer.writeBoolean(dismountVehicle); + writer.write(BYTE, flags); + writer.write(VAR_INT, teleportId); + writer.write(BOOLEAN, dismountVehicle); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/play/PluginMessagePacket.java b/src/main/java/net/minestom/server/network/packet/server/play/PluginMessagePacket.java index 115a005a7..880029156 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/PluginMessagePacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/PluginMessagePacket.java @@ -1,21 +1,23 @@ package net.minestom.server.network.packet.server.play; import net.minestom.server.MinecraftServer; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.RAW_BYTES; +import static net.minestom.server.network.NetworkBuffer.STRING; + public record PluginMessagePacket(String channel, byte[] data) implements ServerPacket { - public PluginMessagePacket(BinaryReader reader) { - this(reader.readSizedString(), reader.readRemainingBytes()); + public PluginMessagePacket(@NotNull NetworkBuffer reader) { + this(reader.read(STRING), reader.read(RAW_BYTES)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeSizedString(channel); - writer.writeBytes(data); + public void write(@NotNull NetworkBuffer writer) { + writer.write(STRING, channel); + writer.write(RAW_BYTES, data); } @Override @@ -32,8 +34,7 @@ public record PluginMessagePacket(String channel, byte[] data) implements Server */ public static @NotNull PluginMessagePacket getBrandPacket() { final String brandName = MinecraftServer.getBrandName(); - BinaryWriter writer = new BinaryWriter(4 + brandName.length()); - writer.writeSizedString(brandName); - return new PluginMessagePacket("minecraft:brand", writer.toByteArray()); + final byte[] data = NetworkBuffer.makeArray(networkBuffer -> networkBuffer.write(STRING, brandName)); + return new PluginMessagePacket("minecraft:brand", data); } } diff --git a/src/main/java/net/minestom/server/network/packet/server/play/RemoveEntityEffectPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/RemoveEntityEffectPacket.java index a69b1acab..d3140c19e 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/RemoveEntityEffectPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/RemoveEntityEffectPacket.java @@ -1,23 +1,24 @@ package net.minestom.server.network.packet.server.play; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; import net.minestom.server.potion.PotionEffect; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; import java.util.Objects; +import static net.minestom.server.network.NetworkBuffer.VAR_INT; + public record RemoveEntityEffectPacket(int entityId, @NotNull PotionEffect potionEffect) implements ServerPacket { - public RemoveEntityEffectPacket(BinaryReader reader) { - this(reader.readVarInt(), Objects.requireNonNull(PotionEffect.fromId(reader.readVarInt()))); + public RemoveEntityEffectPacket(@NotNull NetworkBuffer reader) { + this(reader.read(VAR_INT), Objects.requireNonNull(PotionEffect.fromId(reader.read(VAR_INT)))); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeVarInt(entityId); - writer.writeVarInt(potionEffect.id()); + public void write(@NotNull NetworkBuffer writer) { + writer.write(VAR_INT, entityId); + writer.write(VAR_INT, potionEffect.id()); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/play/ResourcePackSendPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/ResourcePackSendPacket.java index c73f604c8..a2ea7dae5 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/ResourcePackSendPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/ResourcePackSendPacket.java @@ -1,23 +1,24 @@ package net.minestom.server.network.packet.server.play; import net.kyori.adventure.text.Component; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ComponentHoldingServerPacket; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; import net.minestom.server.resourcepack.ResourcePack; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; import java.util.Collection; import java.util.List; import java.util.function.UnaryOperator; +import static net.minestom.server.network.NetworkBuffer.*; + public record ResourcePackSendPacket(String url, String hash, boolean forced, Component prompt) implements ComponentHoldingServerPacket { - public ResourcePackSendPacket(BinaryReader reader) { - this(reader.readSizedString(), reader.readSizedString(), reader.readBoolean(), - reader.readBoolean() ? reader.readComponent() : null); + public ResourcePackSendPacket(@NotNull NetworkBuffer reader) { + this(reader.read(STRING), reader.read(STRING), reader.read(BOOLEAN), + reader.read(BOOLEAN) ? reader.read(COMPONENT) : null); } public ResourcePackSendPacket(@NotNull ResourcePack resourcePack) { @@ -26,15 +27,15 @@ public record ResourcePackSendPacket(String url, String hash, boolean forced, } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeSizedString(url); - writer.writeSizedString(hash); - writer.writeBoolean(forced); + public void write(@NotNull NetworkBuffer writer) { + writer.write(STRING, url); + writer.write(STRING, hash); + writer.write(BOOLEAN, forced); if (prompt != null) { - writer.writeBoolean(true); - writer.writeComponent(prompt); + writer.write(BOOLEAN, true); + writer.write(COMPONENT, prompt); } else { - writer.writeBoolean(false); + writer.write(BOOLEAN, false); } } diff --git a/src/main/java/net/minestom/server/network/packet/server/play/RespawnPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/RespawnPacket.java index eeb9ecaab..e8f7c5817 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/RespawnPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/RespawnPacket.java @@ -1,33 +1,34 @@ package net.minestom.server.network.packet.server.play; import net.minestom.server.entity.GameMode; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.*; + public record RespawnPacket(String dimensionType, String worldName, long hashedSeed, GameMode gameMode, GameMode previousGameMode, boolean isDebug, boolean isFlat, boolean copyMeta) implements ServerPacket { - public RespawnPacket(BinaryReader reader) { - this(reader.readSizedString(), reader.readSizedString(), - reader.readLong(), GameMode.values()[reader.readByte()], GameMode.values()[reader.readByte()], - reader.readBoolean(), reader.readBoolean(), reader.readBoolean()); + public RespawnPacket(@NotNull NetworkBuffer reader) { + this(reader.read(STRING), reader.read(STRING), + reader.read(LONG), GameMode.values()[reader.read(BYTE)], GameMode.values()[reader.read(BYTE)], + reader.read(BOOLEAN), reader.read(BOOLEAN), reader.read(BOOLEAN)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeSizedString(dimensionType); - writer.writeSizedString(worldName); - writer.writeLong(hashedSeed); - writer.writeByte(gameMode.id()); - writer.writeByte(previousGameMode.id()); - writer.writeBoolean(isDebug); - writer.writeBoolean(isFlat); - writer.writeBoolean(copyMeta); + public void write(@NotNull NetworkBuffer writer) { + writer.write(STRING, dimensionType); + writer.write(STRING, worldName); + writer.write(LONG, hashedSeed); + writer.write(BYTE, gameMode.id()); + writer.write(BYTE, previousGameMode.id()); + writer.write(BOOLEAN, isDebug); + writer.write(BOOLEAN, isFlat); + writer.write(BOOLEAN, copyMeta); - writer.writeBoolean(false); + writer.write(BOOLEAN, false); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/play/ScoreboardObjectivePacket.java b/src/main/java/net/minestom/server/network/packet/server/play/ScoreboardObjectivePacket.java index 4ad2c3d47..58c949a35 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/ScoreboardObjectivePacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/ScoreboardObjectivePacket.java @@ -1,11 +1,10 @@ package net.minestom.server.network.packet.server.play; import net.kyori.adventure.text.Component; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ComponentHoldingServerPacket; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -13,10 +12,12 @@ import java.util.Collection; import java.util.List; import java.util.function.UnaryOperator; +import static net.minestom.server.network.NetworkBuffer.*; + public record ScoreboardObjectivePacket(@NotNull String objectiveName, byte mode, @Nullable Component objectiveValue, @Nullable Type type) implements ComponentHoldingServerPacket { - public ScoreboardObjectivePacket(BinaryReader reader) { + public ScoreboardObjectivePacket(@NotNull NetworkBuffer reader) { this(read(reader)); } @@ -24,27 +25,27 @@ public record ScoreboardObjectivePacket(@NotNull String objectiveName, byte mode this(packet.objectiveName, packet.mode, packet.objectiveValue, packet.type); } - private static ScoreboardObjectivePacket read(BinaryReader reader) { - var objectiveName = reader.readSizedString(); - var mode = reader.readByte(); + private static ScoreboardObjectivePacket read(@NotNull NetworkBuffer reader) { + var objectiveName = reader.read(STRING); + var mode = reader.read(BYTE); Component objectiveValue = null; Type type = null; if (mode == 0 || mode == 2) { - objectiveValue = reader.readComponent(); - type = Type.values()[reader.readVarInt()]; + objectiveValue = reader.read(COMPONENT); + type = Type.values()[reader.read(VAR_INT)]; } return new ScoreboardObjectivePacket(objectiveName, mode, objectiveValue, type); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeSizedString(objectiveName); - writer.writeByte(mode); + public void write(@NotNull NetworkBuffer writer) { + writer.write(STRING, objectiveName); + writer.write(BYTE, mode); if (mode == 0 || mode == 2) { assert objectiveValue != null; - writer.writeComponent(objectiveValue); + writer.write(COMPONENT, objectiveValue); assert type != null; - writer.writeVarInt(type.ordinal()); + writer.write(VAR_INT, type.ordinal()); } } diff --git a/src/main/java/net/minestom/server/network/packet/server/play/SelectAdvancementTabPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/SelectAdvancementTabPacket.java index f4ff0a8df..ccb1610dc 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/SelectAdvancementTabPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/SelectAdvancementTabPacket.java @@ -1,21 +1,21 @@ package net.minestom.server.network.packet.server.play; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import static net.minestom.server.network.NetworkBuffer.STRING; + public record SelectAdvancementTabPacket(@Nullable String identifier) implements ServerPacket { - public SelectAdvancementTabPacket(BinaryReader reader) { - this(reader.readBoolean() ? reader.readSizedString() : null); + public SelectAdvancementTabPacket(@NotNull NetworkBuffer reader) { + this(reader.readOptional(STRING)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeBoolean(identifier != null); - if (identifier != null) writer.writeSizedString(identifier); + public void write(@NotNull NetworkBuffer writer) { + writer.writeOptional(STRING, identifier); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/play/ServerDataPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/ServerDataPacket.java index a38e59e99..35c7ad6d7 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/ServerDataPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/ServerDataPacket.java @@ -1,28 +1,27 @@ package net.minestom.server.network.packet.server.play; import net.kyori.adventure.text.Component; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import static net.minestom.server.network.NetworkBuffer.*; + public record ServerDataPacket(@Nullable Component motd, @Nullable String iconBase64, boolean previewsChat, boolean enforcesSecureChat) implements ServerPacket { - public ServerDataPacket(BinaryReader reader) { - this(reader.readBoolean() ? reader.readComponent() : null, reader.readBoolean() ? reader.readSizedString() : null, - reader.readBoolean(), reader.readBoolean()); + public ServerDataPacket(@NotNull NetworkBuffer reader) { + this(reader.readOptional(COMPONENT), reader.readOptional(STRING), + reader.read(BOOLEAN), reader.read(BOOLEAN)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeBoolean(motd != null); - if (motd != null) writer.writeComponent(motd); - writer.writeBoolean(iconBase64 != null); - if (iconBase64 != null) writer.writeSizedString(iconBase64); - writer.writeBoolean(previewsChat); - writer.writeBoolean(enforcesSecureChat); + public void write(@NotNull NetworkBuffer writer) { + writer.writeOptional(COMPONENT, this.motd); + writer.writeOptional(STRING, this.iconBase64); + writer.write(BOOLEAN, previewsChat); + writer.write(BOOLEAN, enforcesSecureChat); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/play/ServerDifficultyPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/ServerDifficultyPacket.java index 7a5f2a715..2fab78f71 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/ServerDifficultyPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/ServerDifficultyPacket.java @@ -1,21 +1,22 @@ package net.minestom.server.network.packet.server.play; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import net.minestom.server.world.Difficulty; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.BOOLEAN; + public record ServerDifficultyPacket(@NotNull Difficulty difficulty, boolean locked) implements ServerPacket { - public ServerDifficultyPacket(BinaryReader reader) { - this(Difficulty.values()[reader.readByte()], reader.readBoolean()); + public ServerDifficultyPacket(@NotNull NetworkBuffer reader) { + this(reader.readEnum(Difficulty.class), reader.read(BOOLEAN)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeByte((byte) difficulty.ordinal()); - writer.writeBoolean(locked); + public void write(@NotNull NetworkBuffer writer) { + writer.writeEnum(Difficulty.class, difficulty); + writer.write(BOOLEAN, locked); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/play/SetChatPreviewPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/SetChatPreviewPacket.java index 903dc468e..3bcae23e9 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/SetChatPreviewPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/SetChatPreviewPacket.java @@ -1,19 +1,20 @@ package net.minestom.server.network.packet.server.play; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.BOOLEAN; + public record SetChatPreviewPacket(boolean enable) implements ServerPacket { - public SetChatPreviewPacket(BinaryReader reader) { - this(reader.readBoolean()); + public SetChatPreviewPacket(@NotNull NetworkBuffer reader) { + this(reader.read(BOOLEAN)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeBoolean(enable); + public void write(@NotNull NetworkBuffer writer) { + writer.write(BOOLEAN, enable); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/play/SetCooldownPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/SetCooldownPacket.java index 01ecefa8c..c05f8b402 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/SetCooldownPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/SetCooldownPacket.java @@ -1,20 +1,21 @@ package net.minestom.server.network.packet.server.play; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.VAR_INT; + public record SetCooldownPacket(int itemId, int cooldownTicks) implements ServerPacket { - public SetCooldownPacket(BinaryReader reader) { - this(reader.readVarInt(), reader.readVarInt()); + public SetCooldownPacket(@NotNull NetworkBuffer reader) { + this(reader.read(VAR_INT), reader.read(VAR_INT)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeVarInt(itemId); - writer.writeVarInt(cooldownTicks); + public void write(@NotNull NetworkBuffer writer) { + writer.write(VAR_INT, itemId); + writer.write(VAR_INT, cooldownTicks); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/play/SetExperiencePacket.java b/src/main/java/net/minestom/server/network/packet/server/play/SetExperiencePacket.java index 4c455a70c..006d1a555 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/SetExperiencePacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/SetExperiencePacket.java @@ -1,21 +1,23 @@ package net.minestom.server.network.packet.server.play; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.FLOAT; +import static net.minestom.server.network.NetworkBuffer.VAR_INT; + public record SetExperiencePacket(float percentage, int level, int totalExperience) implements ServerPacket { - public SetExperiencePacket(BinaryReader reader) { - this(reader.readFloat(), reader.readVarInt(), reader.readVarInt()); + public SetExperiencePacket(@NotNull NetworkBuffer reader) { + this(reader.read(FLOAT), reader.read(VAR_INT), reader.read(VAR_INT)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeFloat(percentage); - writer.writeVarInt(level); - writer.writeVarInt(totalExperience); + public void write(@NotNull NetworkBuffer writer) { + writer.write(FLOAT, percentage); + writer.write(VAR_INT, level); + writer.write(VAR_INT, totalExperience); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/play/SetPassengersPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/SetPassengersPacket.java index a1e3a2e20..de3372751 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/SetPassengersPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/SetPassengersPacket.java @@ -1,27 +1,28 @@ package net.minestom.server.network.packet.server.play; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; import java.util.List; +import static net.minestom.server.network.NetworkBuffer.VAR_INT; + public record SetPassengersPacket(int vehicleEntityId, @NotNull List passengersId) implements ServerPacket { public SetPassengersPacket { passengersId = List.copyOf(passengersId); } - public SetPassengersPacket(BinaryReader reader) { - this(reader.readVarInt(), reader.readVarIntList(BinaryReader::readVarInt)); + public SetPassengersPacket(@NotNull NetworkBuffer reader) { + this(reader.read(VAR_INT), reader.readCollection(VAR_INT)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeVarInt(vehicleEntityId); - writer.writeVarIntList(passengersId, BinaryWriter::writeVarInt); + public void write(@NotNull NetworkBuffer writer) { + writer.write(VAR_INT, vehicleEntityId); + writer.writeCollection(VAR_INT, passengersId); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/play/SetSlotPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/SetSlotPacket.java index d4d26abb3..b851b52d5 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/SetSlotPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/SetSlotPacket.java @@ -2,11 +2,10 @@ package net.minestom.server.network.packet.server.play; import net.kyori.adventure.text.Component; import net.minestom.server.item.ItemStack; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ComponentHoldingServerPacket; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; import java.util.ArrayList; @@ -14,19 +13,21 @@ import java.util.Collection; import java.util.List; import java.util.function.UnaryOperator; +import static net.minestom.server.network.NetworkBuffer.*; + public record SetSlotPacket(byte windowId, int stateId, short slot, @NotNull ItemStack itemStack) implements ComponentHoldingServerPacket { - public SetSlotPacket(BinaryReader reader) { - this(reader.readByte(), reader.readVarInt(), reader.readShort(), - reader.readItemStack()); + public SetSlotPacket(@NotNull NetworkBuffer reader) { + this(reader.read(BYTE), reader.read(VAR_INT), reader.read(SHORT), + reader.read(ITEM)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeByte(windowId); - writer.writeVarInt(stateId); - writer.writeShort(slot); - writer.writeItemStack(itemStack); + public void write(@NotNull NetworkBuffer writer) { + writer.write(BYTE, windowId); + writer.write(VAR_INT, stateId); + writer.write(SHORT, slot); + writer.write(ITEM, itemStack); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/play/SetTitleSubTitlePacket.java b/src/main/java/net/minestom/server/network/packet/server/play/SetTitleSubTitlePacket.java index 9f6720fc9..dc9f83a92 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/SetTitleSubTitlePacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/SetTitleSubTitlePacket.java @@ -1,25 +1,26 @@ package net.minestom.server.network.packet.server.play; import net.kyori.adventure.text.Component; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ComponentHoldingServerPacket; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; import java.util.Collection; import java.util.List; import java.util.function.UnaryOperator; +import static net.minestom.server.network.NetworkBuffer.COMPONENT; + public record SetTitleSubTitlePacket(@NotNull Component subtitle) implements ComponentHoldingServerPacket { - public SetTitleSubTitlePacket(BinaryReader reader) { - this(reader.readComponent()); + public SetTitleSubTitlePacket(@NotNull NetworkBuffer reader) { + this(reader.read(COMPONENT)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeComponent(subtitle); + public void write(@NotNull NetworkBuffer writer) { + writer.write(COMPONENT, subtitle); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/play/SetTitleTextPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/SetTitleTextPacket.java index ed4a844c2..c449f8a82 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/SetTitleTextPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/SetTitleTextPacket.java @@ -1,25 +1,26 @@ package net.minestom.server.network.packet.server.play; import net.kyori.adventure.text.Component; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ComponentHoldingServerPacket; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; import java.util.Collection; import java.util.List; import java.util.function.UnaryOperator; +import static net.minestom.server.network.NetworkBuffer.COMPONENT; + public record SetTitleTextPacket(@NotNull Component title) implements ComponentHoldingServerPacket { - public SetTitleTextPacket(BinaryReader reader) { - this(reader.readComponent()); + public SetTitleTextPacket(@NotNull NetworkBuffer reader) { + this(reader.read(COMPONENT)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeComponent(title); + public void write(@NotNull NetworkBuffer writer) { + writer.write(COMPONENT, title); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/play/SetTitleTimePacket.java b/src/main/java/net/minestom/server/network/packet/server/play/SetTitleTimePacket.java index 1e10ff2c4..95fa019d0 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/SetTitleTimePacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/SetTitleTimePacket.java @@ -1,21 +1,22 @@ package net.minestom.server.network.packet.server.play; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.INT; + public record SetTitleTimePacket(int fadeIn, int stay, int fadeOut) implements ServerPacket { - public SetTitleTimePacket(BinaryReader reader) { - this(reader.readInt(), reader.readInt(), reader.readInt()); + public SetTitleTimePacket(@NotNull NetworkBuffer reader) { + this(reader.read(INT), reader.read(INT), reader.read(INT)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeInt(fadeIn); - writer.writeInt(stay); - writer.writeInt(fadeOut); + public void write(@NotNull NetworkBuffer writer) { + writer.write(INT, fadeIn); + writer.write(INT, stay); + writer.write(INT, fadeOut); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/play/SoundEffectPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/SoundEffectPacket.java index 50c629f71..efa75ce7a 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/SoundEffectPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/SoundEffectPacket.java @@ -3,20 +3,21 @@ package net.minestom.server.network.packet.server.play; import net.kyori.adventure.sound.Sound.Source; import net.minestom.server.adventure.AdventurePacketConvertor; import net.minestom.server.coordinate.Point; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; import net.minestom.server.sound.SoundEvent; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.*; + public record SoundEffectPacket(int soundId, @NotNull Source source, int x, int y, int z, float volume, float pitch, long seed) implements ServerPacket { - public SoundEffectPacket(BinaryReader reader) { - this(reader.readVarInt(), Source.values()[reader.readVarInt()], - reader.readInt() * 8, reader.readInt() * 8, reader.readInt() * 8, - reader.readFloat(), reader.readFloat(), reader.readLong()); + public SoundEffectPacket(@NotNull NetworkBuffer reader) { + this(reader.read(VAR_INT), reader.readEnum(Source.class), + reader.read(INT) * 8, reader.read(INT) * 8, reader.read(INT) * 8, + reader.read(FLOAT), reader.read(FLOAT), reader.read(LONG)); } public SoundEffectPacket(@NotNull SoundEvent sound, @NotNull Source source, @@ -26,15 +27,15 @@ public record SoundEffectPacket(int soundId, @NotNull Source source, } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeVarInt(soundId); - writer.writeVarInt(AdventurePacketConvertor.getSoundSourceValue(source)); - writer.writeInt(x * 8); - writer.writeInt(y * 8); - writer.writeInt(z * 8); - writer.writeFloat(volume); - writer.writeFloat(pitch); - writer.writeLong(seed); + public void write(@NotNull NetworkBuffer writer) { + writer.write(VAR_INT, soundId); + writer.write(VAR_INT, AdventurePacketConvertor.getSoundSourceValue(source)); + writer.write(INT, x * 8); + writer.write(INT, y * 8); + writer.write(INT, z * 8); + writer.write(FLOAT, volume); + writer.write(FLOAT, pitch); + writer.write(LONG, seed); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/play/SpawnEntityPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/SpawnEntityPacket.java index 372d1acc1..1e22c06f9 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/SpawnEntityPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/SpawnEntityPacket.java @@ -1,43 +1,44 @@ package net.minestom.server.network.packet.server.play; import net.minestom.server.coordinate.Pos; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; import java.util.UUID; +import static net.minestom.server.network.NetworkBuffer.*; + public record SpawnEntityPacket(int entityId, @NotNull UUID uuid, int type, @NotNull Pos position, float headRot, int data, short velocityX, short velocityY, short velocityZ) implements ServerPacket { - public SpawnEntityPacket(BinaryReader reader) { - this(reader.readVarInt(), reader.readUuid(), reader.readVarInt(), - new Pos(reader.readDouble(), reader.readDouble(), reader.readDouble(), - reader.readByte() * 360f / 256f, reader.readByte() * 360f / 256f), reader.readByte() * 360f / 256f, - reader.readVarInt(), reader.readShort(), reader.readShort(), reader.readShort()); + public SpawnEntityPacket(@NotNull NetworkBuffer reader) { + this(reader.read(VAR_INT), reader.read(UUID), reader.read(VAR_INT), + new Pos(reader.read(DOUBLE), reader.read(DOUBLE), reader.read(DOUBLE), + reader.read(BYTE) * 360f / 256f, reader.read(BYTE) * 360f / 256f), reader.read(BYTE) * 360f / 256f, + reader.read(VAR_INT), reader.read(SHORT), reader.read(SHORT), reader.read(SHORT)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeVarInt(entityId); - writer.writeUuid(uuid); - writer.writeVarInt(type); + public void write(@NotNull NetworkBuffer writer) { + writer.write(VAR_INT, entityId); + writer.write(UUID, uuid); + writer.write(VAR_INT, type); - writer.writeDouble(position.x()); - writer.writeDouble(position.y()); - writer.writeDouble(position.z()); + writer.write(DOUBLE, position.x()); + writer.write(DOUBLE, position.y()); + writer.write(DOUBLE, position.z()); - writer.writeByte((byte) (position.pitch() * 256 / 360)); - writer.writeByte((byte) (position.yaw() * 256 / 360)); - writer.writeByte((byte) (headRot * 256 / 360)); + writer.write(BYTE, (byte) (position.pitch() * 256 / 360)); + writer.write(BYTE, (byte) (position.yaw() * 256 / 360)); + writer.write(BYTE, (byte) (headRot * 256 / 360)); - writer.writeVarInt(data); + writer.write(VAR_INT, data); - writer.writeShort(velocityX); - writer.writeShort(velocityY); - writer.writeShort(velocityZ); + writer.write(SHORT, velocityX); + writer.write(SHORT, velocityY); + writer.write(SHORT, velocityZ); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/play/SpawnExperienceOrbPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/SpawnExperienceOrbPacket.java index 6e3da4ccd..325ad70a3 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/SpawnExperienceOrbPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/SpawnExperienceOrbPacket.java @@ -1,26 +1,27 @@ package net.minestom.server.network.packet.server.play; import net.minestom.server.coordinate.Pos; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.*; + public record SpawnExperienceOrbPacket(int entityId, @NotNull Pos position, short expCount) implements ServerPacket { - public SpawnExperienceOrbPacket(BinaryReader reader) { - this(reader.readVarInt(), - new Pos(reader.readDouble(), reader.readDouble(), reader.readDouble()), reader.readShort()); + public SpawnExperienceOrbPacket(@NotNull NetworkBuffer reader) { + this(reader.read(VAR_INT), + new Pos(reader.read(DOUBLE), reader.read(DOUBLE), reader.read(DOUBLE)), reader.read(SHORT)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeVarInt(entityId); - writer.writeDouble(position.x()); - writer.writeDouble(position.y()); - writer.writeDouble(position.z()); - writer.writeShort(expCount); + public void write(@NotNull NetworkBuffer writer) { + writer.write(VAR_INT, entityId); + writer.write(DOUBLE, position.x()); + writer.write(DOUBLE, position.y()); + writer.write(DOUBLE, position.z()); + writer.write(SHORT, expCount); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/play/SpawnPlayerPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/SpawnPlayerPacket.java index 8ea6cc899..ddaeb7782 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/SpawnPlayerPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/SpawnPlayerPacket.java @@ -1,31 +1,32 @@ package net.minestom.server.network.packet.server.play; import net.minestom.server.coordinate.Pos; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; import java.util.UUID; +import static net.minestom.server.network.NetworkBuffer.*; + public record SpawnPlayerPacket(int entityId, @NotNull UUID playerUuid, @NotNull Pos position) implements ServerPacket { - public SpawnPlayerPacket(BinaryReader reader) { - this(reader.readVarInt(), reader.readUuid(), - new Pos(reader.readDouble(), reader.readDouble(), reader.readDouble(), - (reader.readByte() * 360f) / 256f, (reader.readByte() * 360f) / 256f)); + public SpawnPlayerPacket(@NotNull NetworkBuffer reader) { + this(reader.read(VAR_INT), reader.read(UUID), + new Pos(reader.read(DOUBLE), reader.read(DOUBLE), reader.read(DOUBLE), + (reader.read(BYTE) * 360f) / 256f, (reader.read(BYTE) * 360f) / 256f)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeVarInt(entityId); - writer.writeUuid(playerUuid); - writer.writeDouble(position.x()); - writer.writeDouble(position.y()); - writer.writeDouble(position.z()); - writer.writeByte((byte) (position.yaw() * 256f / 360f)); - writer.writeByte((byte) (position.pitch() * 256f / 360f)); + public void write(@NotNull NetworkBuffer writer) { + writer.write(VAR_INT, entityId); + writer.write(UUID, playerUuid); + writer.write(DOUBLE, position.x()); + writer.write(DOUBLE, position.y()); + writer.write(DOUBLE, position.z()); + writer.write(BYTE, (byte) (position.yaw() * 256f / 360f)); + writer.write(BYTE, (byte) (position.pitch() * 256f / 360f)); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/play/SpawnPositionPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/SpawnPositionPacket.java index 3f966f137..905b47a3a 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/SpawnPositionPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/SpawnPositionPacket.java @@ -1,21 +1,23 @@ package net.minestom.server.network.packet.server.play; import net.minestom.server.coordinate.Point; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.BLOCK_POSITION; +import static net.minestom.server.network.NetworkBuffer.FLOAT; + public record SpawnPositionPacket(@NotNull Point position, float angle) implements ServerPacket { - public SpawnPositionPacket(BinaryReader reader) { - this(reader.readBlockPosition(), reader.readFloat()); + public SpawnPositionPacket(@NotNull NetworkBuffer reader) { + this(reader.read(BLOCK_POSITION), reader.read(FLOAT)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeBlockPosition(position); - writer.writeFloat(angle); + public void write(@NotNull NetworkBuffer writer) { + writer.write(BLOCK_POSITION, position); + writer.write(FLOAT, angle); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/play/StatisticsPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/StatisticsPacket.java index 6e9589d88..65b6e60b4 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/StatisticsPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/StatisticsPacket.java @@ -1,27 +1,27 @@ package net.minestom.server.network.packet.server.play; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; import net.minestom.server.statistic.StatisticCategory; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; -import net.minestom.server.utils.binary.Writeable; import org.jetbrains.annotations.NotNull; import java.util.List; +import static net.minestom.server.network.NetworkBuffer.VAR_INT; + public record StatisticsPacket(@NotNull List statistics) implements ServerPacket { public StatisticsPacket { statistics = List.copyOf(statistics); } - public StatisticsPacket(BinaryReader reader) { - this(reader.readVarIntList(Statistic::new)); + public StatisticsPacket(@NotNull NetworkBuffer reader) { + this(reader.readCollection(Statistic::new)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeVarIntList(statistics, BinaryWriter::write); + public void write(@NotNull NetworkBuffer writer) { + writer.writeCollection(statistics); } @Override @@ -30,17 +30,17 @@ public record StatisticsPacket(@NotNull List statistics) implements S } public record Statistic(@NotNull StatisticCategory category, - int statisticId, int value) implements Writeable { - public Statistic(BinaryReader reader) { - this(StatisticCategory.values()[reader.readVarInt()], - reader.readVarInt(), reader.readVarInt()); + int statisticId, int value) implements NetworkBuffer.Writer { + public Statistic(@NotNull NetworkBuffer reader) { + this(reader.readEnum(StatisticCategory.class), + reader.read(VAR_INT), reader.read(VAR_INT)); } @Override - public void write(BinaryWriter writer) { - writer.writeVarInt(category.ordinal()); - writer.writeVarInt(statisticId); - writer.writeVarInt(value); + public void write(@NotNull NetworkBuffer writer) { + writer.write(VAR_INT, category.ordinal()); + writer.write(VAR_INT, statisticId); + writer.write(VAR_INT, value); } } } diff --git a/src/main/java/net/minestom/server/network/packet/server/play/StopSoundPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/StopSoundPacket.java index e32ffa595..d1ababbb9 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/StopSoundPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/StopSoundPacket.java @@ -2,16 +2,17 @@ package net.minestom.server.network.packet.server.play; import net.kyori.adventure.sound.Sound; import net.minestom.server.adventure.AdventurePacketConvertor; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import static net.minestom.server.network.NetworkBuffer.*; + public record StopSoundPacket(byte flags, @Nullable Sound.Source source, @Nullable String sound) implements ServerPacket { - public StopSoundPacket(BinaryReader reader) { + public StopSoundPacket(@NotNull NetworkBuffer reader) { this(read(reader)); } @@ -19,23 +20,23 @@ public record StopSoundPacket(byte flags, @Nullable Sound.Source source, this(packet.flags, packet.source, packet.sound); } - private static StopSoundPacket read(BinaryReader reader) { - var flags = reader.readByte(); - var source = flags == 3 || flags == 1 ? Sound.Source.values()[reader.readVarInt()] : null; - var sound = flags == 2 || flags == 3 ? reader.readSizedString() : null; + private static StopSoundPacket read(@NotNull NetworkBuffer reader) { + byte flags = reader.read(BYTE); + var source = flags == 3 || flags == 1 ? reader.readEnum(Sound.Source.class) : null; + var sound = flags == 2 || flags == 3 ? reader.read(STRING) : null; return new StopSoundPacket(flags, source, sound); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeByte(flags); + public void write(@NotNull NetworkBuffer writer) { + writer.write(BYTE, flags); if (flags == 3 || flags == 1) { assert source != null; - writer.writeVarInt(AdventurePacketConvertor.getSoundSourceValue(source)); + writer.write(VAR_INT, AdventurePacketConvertor.getSoundSourceValue(source)); } if (flags == 2 || flags == 3) { assert sound != null; - writer.writeSizedString(sound); + writer.write(STRING, sound); } } diff --git a/src/main/java/net/minestom/server/network/packet/server/play/SystemChatPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/SystemChatPacket.java index 99b67614e..04277f98d 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/SystemChatPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/SystemChatPacket.java @@ -1,26 +1,28 @@ package net.minestom.server.network.packet.server.play; import net.kyori.adventure.text.Component; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ComponentHoldingServerPacket; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; import java.util.Collection; import java.util.List; import java.util.function.UnaryOperator; +import static net.minestom.server.network.NetworkBuffer.BOOLEAN; +import static net.minestom.server.network.NetworkBuffer.COMPONENT; + public record SystemChatPacket(@NotNull Component message, boolean overlay) implements ComponentHoldingServerPacket { - public SystemChatPacket(BinaryReader reader) { - this(reader.readComponent(), reader.readBoolean()); + public SystemChatPacket(@NotNull NetworkBuffer reader) { + this(reader.read(COMPONENT), reader.read(BOOLEAN)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeComponent(message); - writer.writeBoolean(overlay); + public void write(@NotNull NetworkBuffer writer) { + writer.write(COMPONENT, message); + writer.write(BOOLEAN, overlay); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/play/TabCompletePacket.java b/src/main/java/net/minestom/server/network/packet/server/play/TabCompletePacket.java index b7549730f..48a220735 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/TabCompletePacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/TabCompletePacket.java @@ -2,12 +2,10 @@ package net.minestom.server.network.packet.server.play; import net.kyori.adventure.text.Component; import net.minestom.server.adventure.ComponentHolder; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ComponentHoldingServerPacket; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; -import net.minestom.server.utils.binary.Writeable; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -16,22 +14,24 @@ import java.util.Collection; import java.util.List; import java.util.function.UnaryOperator; +import static net.minestom.server.network.NetworkBuffer.*; + public record TabCompletePacket(int transactionId, int start, int length, @NotNull List matches) implements ComponentHoldingServerPacket { public TabCompletePacket { matches = List.copyOf(matches); } - public TabCompletePacket(BinaryReader reader) { - this(reader.readVarInt(), reader.readVarInt(), reader.readVarInt(), reader.readVarIntList(Match::new)); + public TabCompletePacket(@NotNull NetworkBuffer reader) { + this(reader.read(VAR_INT), reader.read(VAR_INT), reader.read(VAR_INT), reader.readCollection(Match::new)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeVarInt(transactionId); - writer.writeVarInt(start); - writer.writeVarInt(length); - writer.writeVarIntList(matches, BinaryWriter::write); + public void write(@NotNull NetworkBuffer writer) { + writer.write(VAR_INT, transactionId); + writer.write(VAR_INT, start); + writer.write(VAR_INT, length); + writer.writeCollection(matches); } @Override @@ -60,16 +60,15 @@ public record TabCompletePacket(int transactionId, int start, int length, } public record Match(@NotNull String match, - @Nullable Component tooltip) implements Writeable, ComponentHolder { - public Match(BinaryReader reader) { - this(reader.readSizedString(), reader.readBoolean() ? reader.readComponent() : null); + @Nullable Component tooltip) implements NetworkBuffer.Writer, ComponentHolder { + public Match(@NotNull NetworkBuffer reader) { + this(reader.read(STRING), reader.read(BOOLEAN) ? reader.read(COMPONENT) : null); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeSizedString(match); - writer.writeBoolean(tooltip != null); - if (tooltip != null) writer.writeComponent(tooltip); + public void write(@NotNull NetworkBuffer writer) { + writer.write(STRING, match); + writer.writeOptional(COMPONENT, tooltip); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/play/TagsPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/TagsPacket.java index 007d35d2a..39a970c3a 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/TagsPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/TagsPacket.java @@ -2,11 +2,10 @@ package net.minestom.server.network.packet.server.play; import net.minestom.server.MinecraftServer; import net.minestom.server.gamedata.tags.Tag; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.CachedPacket; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.NotNull; @@ -14,6 +13,8 @@ import java.util.EnumMap; import java.util.List; import java.util.Map; +import static net.minestom.server.network.NetworkBuffer.*; + public record TagsPacket(@NotNull Map> tagsMap) implements ServerPacket { @ApiStatus.Internal public static final CachedPacket DEFAULT_TAGS = new CachedPacket(new TagsPacket(MinecraftServer.getTagManager().getTagMap())); @@ -22,24 +23,24 @@ public record TagsPacket(@NotNull Map> tagsMap) impleme tagsMap = Map.copyOf(tagsMap); } - public TagsPacket(BinaryReader reader) { + public TagsPacket(@NotNull NetworkBuffer reader) { this(readTagsMap(reader)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeVarInt(tagsMap.size()); + public void write(@NotNull NetworkBuffer writer) { + writer.write(VAR_INT, tagsMap.size()); for (var entry : tagsMap.entrySet()) { final var type = entry.getKey(); final var tags = entry.getValue(); - writer.writeSizedString(type.getIdentifier()); - writer.writeVarInt(tags.size()); + writer.write(STRING, type.getIdentifier()); + writer.write(VAR_INT, tags.size()); for (var tag : tags) { - writer.writeSizedString(tag.getName().asString()); + writer.write(STRING, tag.getName().asString()); final var values = tag.getValues(); - writer.writeVarInt(values.size()); + writer.write(VAR_INT, values.size()); for (var name : values) { - writer.writeVarInt(type.getFunction().apply(name.asString())); + writer.write(VAR_INT, type.getFunction().apply(name.asString())); } } } @@ -50,21 +51,21 @@ public record TagsPacket(@NotNull Map> tagsMap) impleme return ServerPacketIdentifier.TAGS; } - private static Map> readTagsMap(BinaryReader reader) { + private static Map> readTagsMap(@NotNull NetworkBuffer reader) { Map> tagsMap = new EnumMap<>(Tag.BasicType.class); // Read amount of tag types - final int typeCount = reader.readVarInt(); + final int typeCount = reader.read(VAR_INT); for (int i = 0; i < typeCount; i++) { // Read tag type - final Tag.BasicType tagType = Tag.BasicType.fromIdentifer(reader.readSizedString()); + final Tag.BasicType tagType = Tag.BasicType.fromIdentifer(reader.read(STRING)); if (tagType == null) { throw new IllegalArgumentException("Tag type could not be resolved"); } - final int tagCount = reader.readVarInt(); + final int tagCount = reader.read(VAR_INT); for (int j = 0; j < tagCount; j++) { - final String tagName = reader.readSizedString(); - final int[] entries = reader.readVarIntArray(); + final String tagName = reader.read(STRING); + final int[] entries = reader.read(VAR_INT_ARRAY); // TODO convert } } diff --git a/src/main/java/net/minestom/server/network/packet/server/play/TeamsPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/TeamsPacket.java index b571b6193..18cf76425 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/TeamsPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/TeamsPacket.java @@ -4,12 +4,10 @@ import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.NamedTextColor; import net.minestom.server.adventure.AdventurePacketConvertor; import net.minestom.server.adventure.ComponentHolder; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ComponentHoldingServerPacket; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; -import net.minestom.server.utils.binary.Writeable; import net.minestom.server.utils.validate.Check; import org.jetbrains.annotations.NotNull; @@ -17,12 +15,14 @@ import java.util.Collection; import java.util.List; import java.util.function.UnaryOperator; +import static net.minestom.server.network.NetworkBuffer.*; + /** * The packet creates or updates teams */ public record TeamsPacket(String teamName, Action action) implements ComponentHoldingServerPacket { - public TeamsPacket(BinaryReader reader) { - this(reader.readSizedString(), switch (reader.readByte()) { + public TeamsPacket(@NotNull NetworkBuffer reader) { + this(reader.read(STRING), switch (reader.read(BYTE)) { case 0 -> new CreateTeamAction(reader); case 1 -> new RemoveTeamAction(); case 2 -> new UpdateTeamAction(reader); @@ -33,9 +33,9 @@ public record TeamsPacket(String teamName, Action action) implements ComponentHo } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeSizedString(teamName); - writer.writeByte((byte) action.id()); + public void write(@NotNull NetworkBuffer writer) { + writer.write(STRING, teamName); + writer.write(BYTE, (byte) action.id()); writer.write(action); } @@ -54,7 +54,7 @@ public record TeamsPacket(String teamName, Action action) implements ComponentHo ); } - public sealed interface Action extends Writeable + public sealed interface Action extends NetworkBuffer.Writer permits CreateTeamAction, RemoveTeamAction, UpdateTeamAction, AddEntitiesToTeamAction, RemoveEntitiesToTeamAction { int id(); } @@ -67,23 +67,23 @@ public record TeamsPacket(String teamName, Action action) implements ComponentHo entities = List.copyOf(entities); } - public CreateTeamAction(BinaryReader reader) { - this(reader.readComponent(), reader.readByte(), - NameTagVisibility.fromIdentifier(reader.readSizedString()), CollisionRule.fromIdentifier(reader.readSizedString()), - NamedTextColor.ofExact(reader.readVarInt()), reader.readComponent(), reader.readComponent(), - reader.readVarIntList(BinaryReader::readSizedString)); + public CreateTeamAction(@NotNull NetworkBuffer reader) { + this(reader.read(COMPONENT), reader.read(BYTE), + NameTagVisibility.fromIdentifier(reader.read(STRING)), CollisionRule.fromIdentifier(reader.read(STRING)), + NamedTextColor.namedColor(reader.read(VAR_INT)), reader.read(COMPONENT), reader.read(COMPONENT), + reader.readCollection(STRING)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeComponent(displayName); - writer.writeByte(friendlyFlags); - writer.writeSizedString(nameTagVisibility.getIdentifier()); - writer.writeSizedString(collisionRule.getIdentifier()); - writer.writeVarInt(AdventurePacketConvertor.getNamedTextColorValue(teamColor)); - writer.writeComponent(teamPrefix); - writer.writeComponent(teamSuffix); - writer.writeVarIntList(entities, BinaryWriter::writeSizedString); + public void write(@NotNull NetworkBuffer writer) { + writer.write(COMPONENT, displayName); + writer.write(BYTE, friendlyFlags); + writer.write(STRING, nameTagVisibility.getIdentifier()); + writer.write(STRING, collisionRule.getIdentifier()); + writer.write(VAR_INT, AdventurePacketConvertor.getNamedTextColorValue(teamColor)); + writer.write(COMPONENT, teamPrefix); + writer.write(COMPONENT, teamSuffix); + writer.writeCollection(STRING, entities); } @Override @@ -113,7 +113,7 @@ public record TeamsPacket(String teamName, Action action) implements ComponentHo public record RemoveTeamAction() implements Action { @Override - public void write(@NotNull BinaryWriter writer) { + public void write(@NotNull NetworkBuffer writer) { } @Override @@ -125,24 +125,25 @@ public record TeamsPacket(String teamName, Action action) implements ComponentHo public record UpdateTeamAction(Component displayName, byte friendlyFlags, NameTagVisibility nameTagVisibility, CollisionRule collisionRule, NamedTextColor teamColor, - Component teamPrefix, Component teamSuffix) implements Action, ComponentHolder { + Component teamPrefix, + Component teamSuffix) implements Action, ComponentHolder { - public UpdateTeamAction(BinaryReader reader) { - this(reader.readComponent(), reader.readByte(), - NameTagVisibility.fromIdentifier(reader.readSizedString()), CollisionRule.fromIdentifier(reader.readSizedString()), - NamedTextColor.ofExact(reader.readVarInt()), - reader.readComponent(), reader.readComponent()); + public UpdateTeamAction(@NotNull NetworkBuffer reader) { + this(reader.read(COMPONENT), reader.read(BYTE), + NameTagVisibility.fromIdentifier(reader.read(STRING)), CollisionRule.fromIdentifier(reader.read(STRING)), + NamedTextColor.namedColor(reader.read(VAR_INT)), + reader.read(COMPONENT), reader.read(COMPONENT)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeComponent(displayName); - writer.writeByte(friendlyFlags); - writer.writeSizedString(nameTagVisibility.getIdentifier()); - writer.writeSizedString(collisionRule.getIdentifier()); - writer.writeVarInt(AdventurePacketConvertor.getNamedTextColorValue(teamColor)); - writer.writeComponent(teamPrefix); - writer.writeComponent(teamSuffix); + public void write(@NotNull NetworkBuffer writer) { + writer.write(COMPONENT, displayName); + writer.write(BYTE, friendlyFlags); + writer.write(STRING, nameTagVisibility.getIdentifier()); + writer.write(STRING, collisionRule.getIdentifier()); + writer.write(VAR_INT, AdventurePacketConvertor.getNamedTextColorValue(teamColor)); + writer.write(COMPONENT, teamPrefix); + writer.write(COMPONENT, teamSuffix); } @Override @@ -174,13 +175,13 @@ public record TeamsPacket(String teamName, Action action) implements ComponentHo entities = List.copyOf(entities); } - public AddEntitiesToTeamAction(BinaryReader reader) { - this(reader.readVarIntList(BinaryReader::readSizedString)); + public AddEntitiesToTeamAction(@NotNull NetworkBuffer reader) { + this(reader.readCollection(STRING)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeVarIntList(entities, BinaryWriter::writeSizedString); + public void write(@NotNull NetworkBuffer writer) { + writer.writeCollection(STRING, entities); } @Override @@ -189,14 +190,18 @@ public record TeamsPacket(String teamName, Action action) implements ComponentHo } } - public record RemoveEntitiesToTeamAction(String[] entities) implements Action { - public RemoveEntitiesToTeamAction(BinaryReader reader) { - this(reader.readSizedStringArray()); + public record RemoveEntitiesToTeamAction(@NotNull List<@NotNull String> entities) implements Action { + public RemoveEntitiesToTeamAction { + entities = List.copyOf(entities); + } + + public RemoveEntitiesToTeamAction(@NotNull NetworkBuffer reader) { + this(reader.readCollection(STRING)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeStringArray(entities); + public void write(@NotNull NetworkBuffer writer) { + writer.writeCollection(STRING, entities); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/play/TimeUpdatePacket.java b/src/main/java/net/minestom/server/network/packet/server/play/TimeUpdatePacket.java index 3ee90655d..681e5f254 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/TimeUpdatePacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/TimeUpdatePacket.java @@ -1,20 +1,21 @@ package net.minestom.server.network.packet.server.play; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.LONG; + public record TimeUpdatePacket(long worldAge, long timeOfDay) implements ServerPacket { - public TimeUpdatePacket(BinaryReader reader) { - this(reader.readLong(), reader.readLong()); + public TimeUpdatePacket(@NotNull NetworkBuffer reader) { + this(reader.read(LONG), reader.read(LONG)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeLong(worldAge); - writer.writeLong(timeOfDay); + public void write(@NotNull NetworkBuffer writer) { + writer.write(LONG, worldAge); + writer.write(LONG, timeOfDay); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/play/TradeListPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/TradeListPacket.java index 043da29ff..b3c3d9655 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/TradeListPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/TradeListPacket.java @@ -1,15 +1,15 @@ package net.minestom.server.network.packet.server.play; import net.minestom.server.item.ItemStack; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; -import net.minestom.server.utils.binary.Writeable; import org.jetbrains.annotations.NotNull; import java.util.List; +import static net.minestom.server.network.NetworkBuffer.*; + public record TradeListPacket(int windowId, @NotNull List trades, int villagerLevel, int experience, boolean regularVillager, boolean canRestock) implements ServerPacket { @@ -17,20 +17,20 @@ public record TradeListPacket(int windowId, @NotNull List trades, trades = List.copyOf(trades); } - public TradeListPacket(BinaryReader reader) { - this(reader.readVarInt(), reader.readByteList(Trade::new), - reader.readVarInt(), reader.readVarInt(), - reader.readBoolean(), reader.readBoolean()); + public TradeListPacket(@NotNull NetworkBuffer reader) { + this(reader.read(VAR_INT), reader.readCollection(Trade::new), + reader.read(VAR_INT), reader.read(VAR_INT), + reader.read(BOOLEAN), reader.read(BOOLEAN)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeVarInt(windowId); - writer.writeByteList(trades, BinaryWriter::write); - writer.writeVarInt(villagerLevel); - writer.writeVarInt(experience); - writer.writeBoolean(regularVillager); - writer.writeBoolean(canRestock); + public void write(@NotNull NetworkBuffer writer) { + writer.write(VAR_INT, windowId); + writer.writeCollection(trades); + writer.write(VAR_INT, villagerLevel); + writer.write(VAR_INT, experience); + writer.write(BOOLEAN, regularVillager); + writer.write(BOOLEAN, canRestock); } @Override @@ -41,29 +41,26 @@ public record TradeListPacket(int windowId, @NotNull List trades, public record Trade(ItemStack inputItem1, ItemStack result, ItemStack inputItem2, boolean tradeDisabled, int tradeUsesNumber, int maxTradeUsesNumber, int exp, - int specialPrice, float priceMultiplier, int demand) implements Writeable { - public Trade(BinaryReader reader) { - this(reader.readItemStack(), reader.readItemStack(), - reader.readBoolean() ? reader.readItemStack() : null, reader.readBoolean(), - reader.readInt(), reader.readInt(), reader.readInt(), - reader.readInt(), reader.readFloat(), reader.readInt()); + int specialPrice, float priceMultiplier, int demand) implements NetworkBuffer.Writer { + public Trade(@NotNull NetworkBuffer reader) { + this(reader.read(ITEM), reader.read(ITEM), + reader.readOptional(ITEM), reader.read(BOOLEAN), + reader.read(INT), reader.read(INT), reader.read(INT), + reader.read(INT), reader.read(FLOAT), reader.read(INT)); } @Override - public void write(BinaryWriter writer) { - boolean hasSecondItem = inputItem2 != null; - - writer.writeItemStack(inputItem1); - writer.writeItemStack(result); - writer.writeBoolean(hasSecondItem); - if (hasSecondItem) writer.writeItemStack(inputItem2); - writer.writeBoolean(tradeDisabled); - writer.writeInt(tradeUsesNumber); - writer.writeInt(maxTradeUsesNumber); - writer.writeInt(exp); - writer.writeInt(specialPrice); - writer.writeFloat(priceMultiplier); - writer.writeInt(demand); + public void write(@NotNull NetworkBuffer writer) { + writer.write(ITEM, inputItem1); + writer.write(ITEM, result); + writer.writeOptional(ITEM, inputItem2); + writer.write(BOOLEAN, tradeDisabled); + writer.write(INT, tradeUsesNumber); + writer.write(INT, maxTradeUsesNumber); + writer.write(INT, exp); + writer.write(INT, specialPrice); + writer.write(FLOAT, priceMultiplier); + writer.write(INT, demand); } } } diff --git a/src/main/java/net/minestom/server/network/packet/server/play/UnloadChunkPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/UnloadChunkPacket.java index 961c0586d..2856d0846 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/UnloadChunkPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/UnloadChunkPacket.java @@ -1,20 +1,21 @@ package net.minestom.server.network.packet.server.play; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.INT; + public record UnloadChunkPacket(int chunkX, int chunkZ) implements ServerPacket { - public UnloadChunkPacket(BinaryReader reader) { - this(reader.readInt(), reader.readInt()); + public UnloadChunkPacket(@NotNull NetworkBuffer reader) { + this(reader.read(INT), reader.read(INT)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeInt(chunkX); - writer.writeInt(chunkZ); + public void write(@NotNull NetworkBuffer writer) { + writer.write(INT, chunkX); + writer.write(INT, chunkZ); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/play/UnlockRecipesPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/UnlockRecipesPacket.java index 95b75ade4..9c36cfda4 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/UnlockRecipesPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/UnlockRecipesPacket.java @@ -1,14 +1,15 @@ package net.minestom.server.network.packet.server.play; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.UnknownNullability; import java.util.List; +import static net.minestom.server.network.NetworkBuffer.*; + public record UnlockRecipesPacket(int mode, boolean craftingRecipeBookOpen, boolean craftingRecipeBookFilterActive, boolean smeltingRecipeBookOpen, boolean smeltingRecipeBookFilterActive, @@ -23,7 +24,7 @@ public record UnlockRecipesPacket(int mode, } } - public UnlockRecipesPacket(BinaryReader reader) { + public UnlockRecipesPacket(@NotNull NetworkBuffer reader) { this(read(reader)); } @@ -36,18 +37,18 @@ public record UnlockRecipesPacket(int mode, packet.recipeIds, packet.initRecipeIds); } - private static UnlockRecipesPacket read(BinaryReader reader) { - var mode = reader.readVarInt(); - var craftingRecipeBookOpen = reader.readBoolean(); - var craftingRecipeBookFilterActive = reader.readBoolean(); - var smeltingRecipeBookOpen = reader.readBoolean(); - var smeltingRecipeBookFilterActive = reader.readBoolean(); - var blastFurnaceRecipeBookOpen = reader.readBoolean(); - var blastFurnaceRecipeBookFilterActive = reader.readBoolean(); - var smokerRecipeBookOpen = reader.readBoolean(); - var smokerRecipeBookFilterActive = reader.readBoolean(); - var recipeIds = reader.readVarIntList(BinaryReader::readSizedString); - var initRecipeIds = mode == 0 ? reader.readVarIntList(BinaryReader::readSizedString) : null; + private static UnlockRecipesPacket read(@NotNull NetworkBuffer reader) { + var mode = reader.read(VAR_INT); + var craftingRecipeBookOpen = reader.read(BOOLEAN); + var craftingRecipeBookFilterActive = reader.read(BOOLEAN); + var smeltingRecipeBookOpen = reader.read(BOOLEAN); + var smeltingRecipeBookFilterActive = reader.read(BOOLEAN); + var blastFurnaceRecipeBookOpen = reader.read(BOOLEAN); + var blastFurnaceRecipeBookFilterActive = reader.read(BOOLEAN); + var smokerRecipeBookOpen = reader.read(BOOLEAN); + var smokerRecipeBookFilterActive = reader.read(BOOLEAN); + var recipeIds = reader.readCollection(STRING); + var initRecipeIds = mode == 0 ? reader.readCollection(STRING) : null; return new UnlockRecipesPacket(mode, craftingRecipeBookOpen, craftingRecipeBookFilterActive, smeltingRecipeBookOpen, smeltingRecipeBookFilterActive, @@ -57,20 +58,20 @@ public record UnlockRecipesPacket(int mode, } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeVarInt(mode); - writer.writeBoolean(craftingRecipeBookOpen); - writer.writeBoolean(craftingRecipeBookFilterActive); - writer.writeBoolean(smeltingRecipeBookOpen); - writer.writeBoolean(smeltingRecipeBookFilterActive); - writer.writeBoolean(blastFurnaceRecipeBookOpen); - writer.writeBoolean(blastFurnaceRecipeBookFilterActive); - writer.writeBoolean(smokerRecipeBookOpen); - writer.writeBoolean(smokerRecipeBookFilterActive); + public void write(@NotNull NetworkBuffer writer) { + writer.write(VAR_INT, mode); + writer.write(BOOLEAN, craftingRecipeBookOpen); + writer.write(BOOLEAN, craftingRecipeBookFilterActive); + writer.write(BOOLEAN, smeltingRecipeBookOpen); + writer.write(BOOLEAN, smeltingRecipeBookFilterActive); + writer.write(BOOLEAN, blastFurnaceRecipeBookOpen); + writer.write(BOOLEAN, blastFurnaceRecipeBookFilterActive); + writer.write(BOOLEAN, smokerRecipeBookOpen); + writer.write(BOOLEAN, smokerRecipeBookFilterActive); - writer.writeVarIntList(recipeIds, BinaryWriter::writeSizedString); + writer.writeCollection(STRING, recipeIds); if (mode == 0) { - writer.writeVarIntList(initRecipeIds, BinaryWriter::writeSizedString); + writer.writeCollection(STRING, initRecipeIds); } } diff --git a/src/main/java/net/minestom/server/network/packet/server/play/UpdateHealthPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/UpdateHealthPacket.java index bcfae9628..2c5f0c391 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/UpdateHealthPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/UpdateHealthPacket.java @@ -1,21 +1,23 @@ package net.minestom.server.network.packet.server.play; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.FLOAT; +import static net.minestom.server.network.NetworkBuffer.VAR_INT; + public record UpdateHealthPacket(float health, int food, float foodSaturation) implements ServerPacket { - public UpdateHealthPacket(BinaryReader reader) { - this(reader.readFloat(), reader.readVarInt(), reader.readFloat()); + public UpdateHealthPacket(@NotNull NetworkBuffer reader) { + this(reader.read(FLOAT), reader.read(VAR_INT), reader.read(FLOAT)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeFloat(health); - writer.writeVarInt(food); - writer.writeFloat(foodSaturation); + public void write(@NotNull NetworkBuffer writer) { + writer.write(FLOAT, health); + writer.write(VAR_INT, food); + writer.write(FLOAT, foodSaturation); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/play/UpdateLightPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/UpdateLightPacket.java index ba896ed64..07499cd8a 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/UpdateLightPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/UpdateLightPacket.java @@ -1,22 +1,23 @@ package net.minestom.server.network.packet.server.play; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; import net.minestom.server.network.packet.server.play.data.LightData; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.VAR_INT; + public record UpdateLightPacket(int chunkX, int chunkZ, @NotNull LightData lightData) implements ServerPacket { - public UpdateLightPacket(BinaryReader reader) { - this(reader.readVarInt(), reader.readVarInt(), new LightData(reader)); + public UpdateLightPacket(@NotNull NetworkBuffer reader) { + this(reader.read(VAR_INT), reader.read(VAR_INT), new LightData(reader)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeVarInt(chunkX); - writer.writeVarInt(chunkZ); + public void write(@NotNull NetworkBuffer writer) { + writer.write(VAR_INT, chunkX); + writer.write(VAR_INT, chunkZ); writer.write(lightData); } diff --git a/src/main/java/net/minestom/server/network/packet/server/play/UpdateScorePacket.java b/src/main/java/net/minestom/server/network/packet/server/play/UpdateScorePacket.java index 9771e9174..ce2150a27 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/UpdateScorePacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/UpdateScorePacket.java @@ -1,14 +1,15 @@ package net.minestom.server.network.packet.server.play; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.*; + public record UpdateScorePacket(@NotNull String entityName, byte action, @NotNull String objectiveName, int value) implements ServerPacket { - public UpdateScorePacket(BinaryReader reader) { + public UpdateScorePacket(@NotNull NetworkBuffer reader) { this(read(reader)); } @@ -16,20 +17,20 @@ public record UpdateScorePacket(@NotNull String entityName, byte action, this(packet.entityName, packet.action, packet.objectiveName, packet.value); } - private static UpdateScorePacket read(BinaryReader reader) { - var entityName = reader.readSizedString(); - var action = reader.readByte(); - var objectiveName = reader.readSizedString(); - var value = action != 1 ? reader.readVarInt() : 0; + private static UpdateScorePacket read(@NotNull NetworkBuffer reader) { + var entityName = reader.read(STRING); + var action = reader.read(BYTE); + var objectiveName = reader.read(STRING); + var value = action != 1 ? reader.read(VAR_INT) : 0; return new UpdateScorePacket(entityName, action, objectiveName, value); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeSizedString(entityName); - writer.writeByte(action); - writer.writeSizedString(objectiveName); - if (action != 1) writer.writeVarInt(value); + public void write(@NotNull NetworkBuffer writer) { + writer.write(STRING, entityName); + writer.write(BYTE, action); + writer.write(STRING, objectiveName); + if (action != 1) writer.write(VAR_INT, value); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/play/UpdateSimulationDistancePacket.java b/src/main/java/net/minestom/server/network/packet/server/play/UpdateSimulationDistancePacket.java index 1eaf166a4..8fbf700f9 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/UpdateSimulationDistancePacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/UpdateSimulationDistancePacket.java @@ -1,19 +1,20 @@ package net.minestom.server.network.packet.server.play; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.VAR_INT; + public record UpdateSimulationDistancePacket(int simulationDistance) implements ServerPacket { - public UpdateSimulationDistancePacket(BinaryReader reader) { - this(reader.readVarInt()); + public UpdateSimulationDistancePacket(@NotNull NetworkBuffer reader) { + this(reader.read(VAR_INT)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeVarInt(simulationDistance); + public void write(@NotNull NetworkBuffer writer) { + writer.write(VAR_INT, simulationDistance); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/play/UpdateViewDistancePacket.java b/src/main/java/net/minestom/server/network/packet/server/play/UpdateViewDistancePacket.java index f6439d094..f7aa01c53 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/UpdateViewDistancePacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/UpdateViewDistancePacket.java @@ -1,19 +1,20 @@ package net.minestom.server.network.packet.server.play; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.VAR_INT; + public record UpdateViewDistancePacket(int viewDistance) implements ServerPacket { - public UpdateViewDistancePacket(BinaryReader reader) { - this(reader.readVarInt()); + public UpdateViewDistancePacket(@NotNull NetworkBuffer reader) { + this(reader.read(VAR_INT)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeVarInt(viewDistance); + public void write(@NotNull NetworkBuffer writer) { + writer.write(VAR_INT, viewDistance); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/play/UpdateViewPositionPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/UpdateViewPositionPacket.java index 4c729b766..a8c006b67 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/UpdateViewPositionPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/UpdateViewPositionPacket.java @@ -1,20 +1,21 @@ package net.minestom.server.network.packet.server.play; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.VAR_INT; + public record UpdateViewPositionPacket(int chunkX, int chunkZ) implements ServerPacket { - public UpdateViewPositionPacket(BinaryReader reader) { - this(reader.readVarInt(), reader.readVarInt()); + public UpdateViewPositionPacket(@NotNull NetworkBuffer reader) { + this(reader.read(VAR_INT), reader.read(VAR_INT)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeVarInt(chunkX); - writer.writeVarInt(chunkZ); + public void write(@NotNull NetworkBuffer writer) { + writer.write(VAR_INT, chunkX); + writer.write(VAR_INT, chunkZ); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/play/VehicleMovePacket.java b/src/main/java/net/minestom/server/network/packet/server/play/VehicleMovePacket.java index fdecdd947..71a381796 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/VehicleMovePacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/VehicleMovePacket.java @@ -1,25 +1,27 @@ package net.minestom.server.network.packet.server.play; import net.minestom.server.coordinate.Pos; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.DOUBLE; +import static net.minestom.server.network.NetworkBuffer.FLOAT; + public record VehicleMovePacket(@NotNull Pos position) implements ServerPacket { - public VehicleMovePacket(BinaryReader reader) { - this(new Pos(reader.readDouble(), reader.readDouble(), reader.readDouble(), - reader.readFloat(), reader.readFloat())); + public VehicleMovePacket(@NotNull NetworkBuffer reader) { + this(new Pos(reader.read(DOUBLE), reader.read(DOUBLE), reader.read(DOUBLE), + reader.read(FLOAT), reader.read(FLOAT))); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeDouble(position.x()); - writer.writeDouble(position.y()); - writer.writeDouble(position.z()); - writer.writeFloat(position.yaw()); - writer.writeFloat(position.pitch()); + public void write(@NotNull NetworkBuffer writer) { + writer.write(DOUBLE, position.x()); + writer.write(DOUBLE, position.y()); + writer.write(DOUBLE, position.z()); + writer.write(FLOAT, position.yaw()); + writer.write(FLOAT, position.pitch()); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/play/WindowItemsPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/WindowItemsPacket.java index a2aad16e8..aae88fa3e 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/WindowItemsPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/WindowItemsPacket.java @@ -2,11 +2,10 @@ package net.minestom.server.network.packet.server.play; import net.kyori.adventure.text.Component; import net.minestom.server.item.ItemStack; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ComponentHoldingServerPacket; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; import java.util.ArrayList; @@ -14,23 +13,25 @@ import java.util.Collection; import java.util.List; import java.util.function.UnaryOperator; +import static net.minestom.server.network.NetworkBuffer.*; + public record WindowItemsPacket(byte windowId, int stateId, @NotNull List items, @NotNull ItemStack carriedItem) implements ComponentHoldingServerPacket { public WindowItemsPacket { items = List.copyOf(items); } - public WindowItemsPacket(BinaryReader reader) { - this(reader.readByte(), reader.readVarInt(), reader.readVarIntList(BinaryReader::readItemStack), - reader.readItemStack()); + public WindowItemsPacket(@NotNull NetworkBuffer reader) { + this(reader.read(BYTE), reader.read(VAR_INT), reader.readCollection(ITEM), + reader.read(ITEM)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeByte(windowId); - writer.writeVarInt(stateId); - writer.writeVarIntList(items, BinaryWriter::writeItemStack); - writer.writeItemStack(carriedItem); + public void write(@NotNull NetworkBuffer writer) { + writer.write(BYTE, windowId); + writer.write(VAR_INT, stateId); + writer.writeCollection(ITEM, items); + writer.write(ITEM, carriedItem); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/play/WindowPropertyPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/WindowPropertyPacket.java index 85695b09c..788d80eae 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/WindowPropertyPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/WindowPropertyPacket.java @@ -1,21 +1,23 @@ package net.minestom.server.network.packet.server.play; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.BYTE; +import static net.minestom.server.network.NetworkBuffer.SHORT; + public record WindowPropertyPacket(byte windowId, short property, short value) implements ServerPacket { - public WindowPropertyPacket(BinaryReader reader) { - this(reader.readByte(), reader.readShort(), reader.readShort()); + public WindowPropertyPacket(@NotNull NetworkBuffer reader) { + this(reader.read(BYTE), reader.read(SHORT), reader.read(SHORT)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeByte(windowId); - writer.writeShort(property); - writer.writeShort(value); + public void write(@NotNull NetworkBuffer writer) { + writer.write(BYTE, windowId); + writer.write(SHORT, property); + writer.write(SHORT, value); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/play/WorldBorderCenterPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/WorldBorderCenterPacket.java index de1885cc2..d93b00264 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/WorldBorderCenterPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/WorldBorderCenterPacket.java @@ -1,20 +1,21 @@ package net.minestom.server.network.packet.server.play; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.DOUBLE; + public record WorldBorderCenterPacket(double x, double z) implements ServerPacket { - public WorldBorderCenterPacket(BinaryReader reader) { - this(reader.readDouble(), reader.readDouble()); + public WorldBorderCenterPacket(@NotNull NetworkBuffer reader) { + this(reader.read(DOUBLE), reader.read(DOUBLE)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeDouble(x); - writer.writeDouble(z); + public void write(@NotNull NetworkBuffer writer) { + writer.write(DOUBLE, x); + writer.write(DOUBLE, z); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/play/WorldBorderLerpSizePacket.java b/src/main/java/net/minestom/server/network/packet/server/play/WorldBorderLerpSizePacket.java index 6576af521..ec44103d5 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/WorldBorderLerpSizePacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/WorldBorderLerpSizePacket.java @@ -1,21 +1,23 @@ package net.minestom.server.network.packet.server.play; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.DOUBLE; +import static net.minestom.server.network.NetworkBuffer.VAR_LONG; + public record WorldBorderLerpSizePacket(double oldDiameter, double newDiameter, long speed) implements ServerPacket { - public WorldBorderLerpSizePacket(BinaryReader reader) { - this(reader.readDouble(), reader.readDouble(), reader.readVarLong()); + public WorldBorderLerpSizePacket(@NotNull NetworkBuffer reader) { + this(reader.read(DOUBLE), reader.read(DOUBLE), reader.read(VAR_LONG)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeDouble(oldDiameter); - writer.writeDouble(newDiameter); - writer.writeVarLong(speed); + public void write(@NotNull NetworkBuffer writer) { + writer.write(DOUBLE, oldDiameter); + writer.write(DOUBLE, newDiameter); + writer.write(VAR_LONG, speed); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/play/WorldBorderSizePacket.java b/src/main/java/net/minestom/server/network/packet/server/play/WorldBorderSizePacket.java index 2d72ab41b..289806200 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/WorldBorderSizePacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/WorldBorderSizePacket.java @@ -1,19 +1,20 @@ package net.minestom.server.network.packet.server.play; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.DOUBLE; + public record WorldBorderSizePacket(double diameter) implements ServerPacket { - public WorldBorderSizePacket(BinaryReader reader) { - this(reader.readDouble()); + public WorldBorderSizePacket(@NotNull NetworkBuffer reader) { + this(reader.read(DOUBLE)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeDouble(diameter); + public void write(@NotNull NetworkBuffer writer) { + writer.write(DOUBLE, diameter); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/play/WorldBorderWarningDelayPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/WorldBorderWarningDelayPacket.java index 8e0bc2e68..383ab9300 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/WorldBorderWarningDelayPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/WorldBorderWarningDelayPacket.java @@ -1,19 +1,20 @@ package net.minestom.server.network.packet.server.play; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.VAR_INT; + public record WorldBorderWarningDelayPacket(int warningTime) implements ServerPacket { - public WorldBorderWarningDelayPacket(BinaryReader reader) { - this(reader.readVarInt()); + public WorldBorderWarningDelayPacket(@NotNull NetworkBuffer reader) { + this(reader.read(VAR_INT)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeVarInt(warningTime); + public void write(@NotNull NetworkBuffer writer) { + writer.write(VAR_INT, warningTime); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/play/WorldBorderWarningReachPacket.java b/src/main/java/net/minestom/server/network/packet/server/play/WorldBorderWarningReachPacket.java index 62d4d5f15..26d060c26 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/WorldBorderWarningReachPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/WorldBorderWarningReachPacket.java @@ -1,19 +1,20 @@ package net.minestom.server.network.packet.server.play; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.network.packet.server.ServerPacketIdentifier; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.VAR_INT; + public record WorldBorderWarningReachPacket(int warningBlocks) implements ServerPacket { - public WorldBorderWarningReachPacket(BinaryReader reader) { - this(reader.readVarInt()); + public WorldBorderWarningReachPacket(@NotNull NetworkBuffer reader) { + this(reader.read(VAR_INT)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeVarInt(warningBlocks); + public void write(@NotNull NetworkBuffer writer) { + writer.write(VAR_INT, warningBlocks); } @Override diff --git a/src/main/java/net/minestom/server/network/packet/server/play/data/ChunkData.java b/src/main/java/net/minestom/server/network/packet/server/play/data/ChunkData.java index 2b91109c9..c0126205d 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/data/ChunkData.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/data/ChunkData.java @@ -2,9 +2,7 @@ package net.minestom.server.network.packet.server.play.data; import net.minestom.server.coordinate.Point; import net.minestom.server.instance.block.Block; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; -import net.minestom.server.utils.binary.Writeable; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.utils.block.BlockUtils; import net.minestom.server.utils.chunk.ChunkUtils; import org.jetbrains.annotations.NotNull; @@ -14,8 +12,10 @@ import java.util.HashMap; import java.util.Map; import java.util.stream.Collectors; +import static net.minestom.server.network.NetworkBuffer.*; + public record ChunkData(@NotNull NBTCompound heightmaps, byte @NotNull [] data, - @NotNull Map blockEntities) implements Writeable { + @NotNull Map blockEntities) implements NetworkBuffer.Writer { public ChunkData { blockEntities = blockEntities.entrySet() .stream() @@ -23,44 +23,43 @@ public record ChunkData(@NotNull NBTCompound heightmaps, byte @NotNull [] data, .collect(Collectors.toUnmodifiableMap(Map.Entry::getKey, Map.Entry::getValue)); } - public ChunkData(BinaryReader reader) { - this((NBTCompound) reader.readTag(), reader.readByteArray(), + public ChunkData(@NotNull NetworkBuffer reader) { + this((NBTCompound) reader.read(NBT), reader.read(BYTE_ARRAY), readBlockEntities(reader)); } @Override - public void write(@NotNull BinaryWriter writer) { + public void write(@NotNull NetworkBuffer writer) { // Heightmaps - writer.writeNBT("", this.heightmaps); + writer.write(NBT, this.heightmaps); // Data - writer.writeVarInt(data.length); - writer.writeBytes(data); + writer.write(BYTE_ARRAY, data); // Block entities - writer.writeVarInt(blockEntities.size()); + writer.write(VAR_INT, blockEntities.size()); for (var entry : blockEntities.entrySet()) { final int index = entry.getKey(); final Block block = entry.getValue(); final var registry = block.registry(); final Point point = ChunkUtils.getBlockPosition(index, 0, 0); - writer.writeByte((byte) ((point.blockX() & 15) << 4 | point.blockZ() & 15)); // xz - writer.writeShort((short) point.blockY()); // y + writer.write(BYTE, (byte) ((point.blockX() & 15) << 4 | point.blockZ() & 15)); // xz + writer.write(SHORT, (short) point.blockY()); // y - writer.writeVarInt(registry.blockEntityId()); + writer.write(VAR_INT, registry.blockEntityId()); final NBTCompound nbt = BlockUtils.extractClientNbt(block); assert nbt != null; - writer.writeNBT("", nbt); // block nbt + writer.write(NBT, nbt); // block nbt } } - private static Map readBlockEntities(BinaryReader reader) { + private static Map readBlockEntities(@NotNull NetworkBuffer reader) { final Map blockEntities = new HashMap<>(); - final int size = reader.readVarInt(); + final int size = reader.read(VAR_INT); for (int i = 0; i < size; i++) { - final byte xz = reader.readByte(); - final short y = reader.readShort(); - final int blockEntityId = reader.readVarInt(); - final NBTCompound nbt = (NBTCompound) reader.readTag(); + final byte xz = reader.read(BYTE); + final short y = reader.read(SHORT); + final int blockEntityId = reader.read(VAR_INT); + final NBTCompound nbt = (NBTCompound) reader.read(NBT); // TODO create block object } return blockEntities; diff --git a/src/main/java/net/minestom/server/network/packet/server/play/data/LightData.java b/src/main/java/net/minestom/server/network/packet/server/play/data/LightData.java index 373f0fdca..7ca1b4a59 100644 --- a/src/main/java/net/minestom/server/network/packet/server/play/data/LightData.java +++ b/src/main/java/net/minestom/server/network/packet/server/play/data/LightData.java @@ -1,35 +1,36 @@ package net.minestom.server.network.packet.server.play.data; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; -import net.minestom.server.utils.binary.Writeable; +import net.minestom.server.network.NetworkBuffer; import org.jetbrains.annotations.NotNull; import java.util.BitSet; import java.util.List; +import static net.minestom.server.network.NetworkBuffer.*; + public record LightData(boolean trustEdges, @NotNull BitSet skyMask, @NotNull BitSet blockMask, @NotNull BitSet emptySkyMask, @NotNull BitSet emptyBlockMask, - @NotNull List skyLight, @NotNull List blockLight) implements Writeable { - public LightData(BinaryReader reader) { - this(reader.readBoolean(), - BitSet.valueOf(reader.readLongArray()), BitSet.valueOf(reader.readLongArray()), - BitSet.valueOf(reader.readLongArray()), BitSet.valueOf(reader.readLongArray()), - reader.readVarIntList(r -> r.readBytes(r.readVarInt())), reader.readVarIntList(r -> r.readBytes(r.readVarInt()))); + @NotNull List skyLight, + @NotNull List blockLight) implements NetworkBuffer.Writer { + public LightData(@NotNull NetworkBuffer reader) { + this(reader.read(BOOLEAN), + BitSet.valueOf(reader.read(LONG_ARRAY)), BitSet.valueOf(reader.read(LONG_ARRAY)), + BitSet.valueOf(reader.read(LONG_ARRAY)), BitSet.valueOf(reader.read(LONG_ARRAY)), + reader.readCollection(BYTE_ARRAY), reader.readCollection(BYTE_ARRAY)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeBoolean(trustEdges); + public void write(@NotNull NetworkBuffer writer) { + writer.write(BOOLEAN, trustEdges); - writer.writeLongArray(skyMask.toLongArray()); - writer.writeLongArray(blockMask.toLongArray()); + writer.write(LONG_ARRAY, skyMask.toLongArray()); + writer.write(LONG_ARRAY, blockMask.toLongArray()); - writer.writeLongArray(emptySkyMask.toLongArray()); - writer.writeLongArray(emptyBlockMask.toLongArray()); + writer.write(LONG_ARRAY, emptySkyMask.toLongArray()); + writer.write(LONG_ARRAY, emptyBlockMask.toLongArray()); - writer.writeVarIntList(skyLight, BinaryWriter::writeByteArray); - writer.writeVarIntList(blockLight, BinaryWriter::writeByteArray); + writer.writeCollection(BYTE_ARRAY, skyLight); + writer.writeCollection(BYTE_ARRAY, blockLight); } } diff --git a/src/main/java/net/minestom/server/network/packet/server/status/PongPacket.java b/src/main/java/net/minestom/server/network/packet/server/status/PongPacket.java index f4948a43b..077a3d4cd 100644 --- a/src/main/java/net/minestom/server/network/packet/server/status/PongPacket.java +++ b/src/main/java/net/minestom/server/network/packet/server/status/PongPacket.java @@ -1,18 +1,19 @@ package net.minestom.server.network.packet.server.status; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.ServerPacket; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import static net.minestom.server.network.NetworkBuffer.LONG; + public record PongPacket(long number) implements ServerPacket { - public PongPacket(BinaryReader reader) { - this(reader.readLong()); + public PongPacket(@NotNull NetworkBuffer reader) { + this(reader.read(LONG)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeLong(number); + public void write(@NotNull NetworkBuffer writer) { + writer.write(LONG, number); } @Override diff --git a/src/main/java/net/minestom/server/network/player/GameProfile.java b/src/main/java/net/minestom/server/network/player/GameProfile.java index fb4f165cf..5cd2731b3 100644 --- a/src/main/java/net/minestom/server/network/player/GameProfile.java +++ b/src/main/java/net/minestom/server/network/player/GameProfile.java @@ -1,8 +1,6 @@ package net.minestom.server.network.player; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; -import net.minestom.server.utils.binary.Writeable; +import net.minestom.server.network.NetworkBuffer; import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -10,9 +8,11 @@ import org.jetbrains.annotations.Nullable; import java.util.List; import java.util.UUID; +import static net.minestom.server.network.NetworkBuffer.STRING; + @ApiStatus.Experimental public record GameProfile(@NotNull UUID uuid, @NotNull String name, - @NotNull List<@NotNull Property> properties) implements Writeable { + @NotNull List<@NotNull Property> properties) implements NetworkBuffer.Writer { public GameProfile { if (name.isBlank()) throw new IllegalArgumentException("Name cannot be blank"); @@ -21,34 +21,33 @@ public record GameProfile(@NotNull UUID uuid, @NotNull String name, properties = List.copyOf(properties); } - public GameProfile(@NotNull BinaryReader reader) { - this(reader.readUuid(), reader.readSizedString(16), reader.readVarIntList(Property::new)); + public GameProfile(@NotNull NetworkBuffer reader) { + this(reader.read(NetworkBuffer.UUID), reader.read(STRING), reader.readCollection(Property::new)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeUuid(uuid); - writer.writeSizedString(name); - writer.writeVarIntList(properties, BinaryWriter::write); + public void write(@NotNull NetworkBuffer writer) { + writer.write(NetworkBuffer.UUID, uuid); + writer.write(STRING, name); + writer.writeCollection(properties); } public record Property(@NotNull String name, @NotNull String value, - @Nullable String signature) implements Writeable { + @Nullable String signature) implements NetworkBuffer.Writer { public Property(@NotNull String name, @NotNull String value) { this(name, value, null); } - public Property(@NotNull BinaryReader reader) { - this(reader.readSizedString(), reader.readSizedString(), - reader.readBoolean() ? reader.readSizedString() : null); + public Property(@NotNull NetworkBuffer reader) { + this(reader.read(STRING), reader.read(STRING), + reader.readOptional(STRING)); } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeSizedString(name); - writer.writeSizedString(value); - writer.writeBoolean(signature != null); - if (signature != null) writer.writeSizedString(signature); + public void write(@NotNull NetworkBuffer writer) { + writer.write(STRING, name); + writer.write(STRING, value); + writer.writeOptional(STRING, signature); } } } diff --git a/src/main/java/net/minestom/server/potion/Potion.java b/src/main/java/net/minestom/server/potion/Potion.java index e7ddecf47..2e0159086 100644 --- a/src/main/java/net/minestom/server/potion/Potion.java +++ b/src/main/java/net/minestom/server/potion/Potion.java @@ -1,15 +1,18 @@ package net.minestom.server.potion; import net.minestom.server.entity.Entity; +import net.minestom.server.network.NetworkBuffer; import net.minestom.server.network.packet.server.play.EntityEffectPacket; import net.minestom.server.network.packet.server.play.RemoveEntityEffectPacket; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; -import net.minestom.server.utils.binary.Writeable; import org.jetbrains.annotations.NotNull; +import java.util.Objects; + +import static net.minestom.server.network.NetworkBuffer.BYTE; +import static net.minestom.server.network.NetworkBuffer.VAR_INT; + public record Potion(@NotNull PotionEffect effect, byte amplifier, - int duration, byte flags) implements Writeable { + int duration, byte flags) implements NetworkBuffer.Writer { /** * A flag indicating that this Potion is ambient (it came from a beacon). * @@ -41,9 +44,9 @@ public record Potion(@NotNull PotionEffect effect, byte amplifier, this(effect, amplifier, duration, (byte) 0); } - public Potion(BinaryReader reader) { - this(PotionEffect.fromId(reader.readVarInt()), reader.readByte(), - reader.readVarInt(), reader.readByte()); + public Potion(@NotNull NetworkBuffer reader) { + this(Objects.requireNonNull(PotionEffect.fromId(reader.read(VAR_INT))), reader.read(BYTE), + reader.read(VAR_INT), reader.read(BYTE)); } /** @@ -108,10 +111,10 @@ public record Potion(@NotNull PotionEffect effect, byte amplifier, } @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeVarInt(effect.id()); - writer.writeByte(amplifier); - writer.writeVarInt(duration); - writer.writeByte(flags); + public void write(@NotNull NetworkBuffer writer) { + writer.write(VAR_INT, effect.id()); + writer.write(BYTE, amplifier); + writer.write(VAR_INT, duration); + writer.write(BYTE, flags); } } diff --git a/src/main/java/net/minestom/server/scoreboard/Team.java b/src/main/java/net/minestom/server/scoreboard/Team.java index 33ed22106..9d570b725 100644 --- a/src/main/java/net/minestom/server/scoreboard/Team.java +++ b/src/main/java/net/minestom/server/scoreboard/Team.java @@ -16,6 +16,7 @@ import org.jetbrains.annotations.NotNull; import java.util.Collection; import java.util.Collections; +import java.util.List; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.CopyOnWriteArraySet; @@ -128,7 +129,7 @@ public class Team implements PacketGroupingAudience { public void removeMember(@NotNull String member) { // Initializes remove player packet final TeamsPacket removePlayerPacket = new TeamsPacket(teamName, - new TeamsPacket.RemoveEntitiesToTeamAction(new String[]{member})); + new TeamsPacket.RemoveEntitiesToTeamAction(List.of(member))); // Sends to all online player teh remove player packet PacketUtils.broadcastPacket(removePlayerPacket); diff --git a/src/main/java/net/minestom/server/utils/PacketUtils.java b/src/main/java/net/minestom/server/utils/PacketUtils.java index 8f3772dbd..f4fb64ee7 100644 --- a/src/main/java/net/minestom/server/utils/PacketUtils.java +++ b/src/main/java/net/minestom/server/utils/PacketUtils.java @@ -22,8 +22,6 @@ import net.minestom.server.network.packet.server.*; import net.minestom.server.network.player.PlayerConnection; import net.minestom.server.network.player.PlayerSocketConnection; import net.minestom.server.utils.binary.BinaryBuffer; -import net.minestom.server.utils.binary.BinaryWriter; -import net.minestom.server.utils.binary.Writeable; import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -254,15 +252,14 @@ public final class PacketUtils { public static void writeFramedPacket(@NotNull ByteBuffer buffer, int id, - @NotNull Writeable writeable, + @NotNull NetworkBuffer.Writer writer, int compressionThreshold) { NetworkBuffer networkBuffer = new NetworkBuffer(buffer, false); - BinaryWriter legacyWriter = new BinaryWriter(networkBuffer); if (compressionThreshold <= 0) { // Uncompressed format https://wiki.vg/Protocol#Without_compression final int lengthIndex = networkBuffer.skipWrite(3); networkBuffer.write(NetworkBuffer.VAR_INT, id); - writeable.write(legacyWriter); + networkBuffer.write(writer); final int finalSize = networkBuffer.writeIndex() - (lengthIndex + 3); Utils.writeVarIntHeader(buffer, lengthIndex, finalSize); buffer.position(networkBuffer.writeIndex()); @@ -274,7 +271,7 @@ public final class PacketUtils { final int contentStart = networkBuffer.writeIndex(); networkBuffer.write(NetworkBuffer.VAR_INT, id); - writeable.write(legacyWriter); + networkBuffer.write(writer); final int packetSize = networkBuffer.writeIndex() - contentStart; final boolean compressed = packetSize >= compressionThreshold; if (compressed) { diff --git a/src/main/java/net/minestom/server/utils/binary/BinaryReader.java b/src/main/java/net/minestom/server/utils/binary/BinaryReader.java index a3cf3b520..dbcaef4cb 100644 --- a/src/main/java/net/minestom/server/utils/binary/BinaryReader.java +++ b/src/main/java/net/minestom/server/utils/binary/BinaryReader.java @@ -30,7 +30,7 @@ import static net.minestom.server.network.NetworkBuffer.*; public class BinaryReader extends InputStream { private final NetworkBuffer buffer; - public BinaryReader(NetworkBuffer buffer) { + public BinaryReader(@NotNull NetworkBuffer buffer) { this.buffer = buffer; } diff --git a/src/main/java/net/minestom/server/utils/binary/BinaryWriter.java b/src/main/java/net/minestom/server/utils/binary/BinaryWriter.java index 54b417cd2..c6ab077ae 100644 --- a/src/main/java/net/minestom/server/utils/binary/BinaryWriter.java +++ b/src/main/java/net/minestom/server/utils/binary/BinaryWriter.java @@ -27,7 +27,7 @@ import static net.minestom.server.network.NetworkBuffer.*; public class BinaryWriter extends OutputStream { private final NetworkBuffer buffer; - public BinaryWriter(NetworkBuffer buffer) { + public BinaryWriter(@NotNull NetworkBuffer buffer) { this.buffer = buffer; } diff --git a/src/test/java/net/minestom/server/instance/AnvilLoaderIntegrationTest.java b/src/test/java/net/minestom/server/instance/AnvilLoaderIntegrationTest.java index 859cc49ce..1e8f352c6 100644 --- a/src/test/java/net/minestom/server/instance/AnvilLoaderIntegrationTest.java +++ b/src/test/java/net/minestom/server/instance/AnvilLoaderIntegrationTest.java @@ -1,11 +1,11 @@ package net.minestom.server.instance; +import net.minestom.server.instance.block.Block; +import net.minestom.server.network.NetworkBuffer; +import net.minestom.server.utils.NamespaceID; +import net.minestom.server.world.biomes.Biome; import net.minestom.testing.Env; import net.minestom.testing.EnvTest; -import net.minestom.server.instance.block.Block; -import net.minestom.server.utils.NamespaceID; -import net.minestom.server.utils.binary.BinaryWriter; -import net.minestom.server.world.biomes.Biome; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeAll; @@ -47,7 +47,6 @@ public class AnvilLoaderIntegrationTest { } - @Test public void loadHouse(Env env) { // load a world that contains only a basic house and make sure it is loaded properly @@ -103,7 +102,7 @@ public class AnvilLoaderIntegrationTest { // side walls for (int x = -2; x <= 3; x++) { - if(x != 0) { // bedrock block at center + if (x != 0) { // bedrock block at center assertEquals(Block.NETHERRACK, instance.getBlock(x, 0, 0)); } assertEquals(Block.NETHERRACK, instance.getBlock(x, 0, -4)); @@ -127,8 +126,7 @@ public class AnvilLoaderIntegrationTest { .withProperty("facing", "west") .withProperty("hinge", "left") .withProperty("open", "false") - .withProperty("powered", "false") - ; + .withProperty("powered", "false"); Block bottomDoorPart = baseDoor.withProperty("half", "lower"); Block topDoorPart = baseDoor.withProperty("half", "upper"); assertEquals(bottomDoorPart, instance.getBlock(3, 1, -3)); @@ -160,28 +158,25 @@ public class AnvilLoaderIntegrationTest { return false; } }); - Chunk originalChunk = instance.loadChunk(0,0).join(); + Chunk originalChunk = instance.loadChunk(0, 0).join(); synchronized (originalChunk) { instance.saveChunkToStorage(originalChunk); instance.unloadChunk(originalChunk); - while(originalChunk.isLoaded()) { + while (originalChunk.isLoaded()) { Thread.sleep(1); } } - Chunk reloadedChunk = instance.loadChunk(0,0).join(); - for(int section = reloadedChunk.getMinSection(); section < reloadedChunk.getMaxSection(); section++) { + Chunk reloadedChunk = instance.loadChunk(0, 0).join(); + for (int section = reloadedChunk.getMinSection(); section < reloadedChunk.getMaxSection(); section++) { Section originalSection = originalChunk.getSection(section); Section reloadedSection = reloadedChunk.getSection(section); // easiest equality check to write is a memory compare on written output - BinaryWriter originalWriter = new BinaryWriter(); - BinaryWriter reloadedWriter = new BinaryWriter(); - originalSection.write(originalWriter); - reloadedSection.write(reloadedWriter); - - Assertions.assertArrayEquals(originalWriter.toByteArray(), reloadedWriter.toByteArray()); + var original = NetworkBuffer.makeArray(networkBuffer -> networkBuffer.write(originalSection)); + var reloaded = NetworkBuffer.makeArray(networkBuffer -> networkBuffer.write(reloadedSection)); + Assertions.assertArrayEquals(original, reloaded); } env.destroyInstance(instance); diff --git a/src/test/java/net/minestom/server/instance/palette/PaletteOptimizationTest.java b/src/test/java/net/minestom/server/instance/palette/PaletteOptimizationTest.java index 02d7e02c5..9916d52ab 100644 --- a/src/test/java/net/minestom/server/instance/palette/PaletteOptimizationTest.java +++ b/src/test/java/net/minestom/server/instance/palette/PaletteOptimizationTest.java @@ -1,6 +1,6 @@ package net.minestom.server.instance.palette; -import net.minestom.server.utils.binary.BinaryWriter; +import net.minestom.server.network.NetworkBuffer; import org.junit.jupiter.api.Test; import java.util.Random; @@ -60,14 +60,10 @@ public class PaletteOptimizationTest { } // Verify size { - var writer = new BinaryWriter(4096); - palette.write(writer); - int length1 = writer.toByteArray().length; - writer = new BinaryWriter(4096); - optimized.write(writer); - int length2 = writer.toByteArray().length; - - //System.out.println("debug: " + Thread.currentThread().getStackTrace()[2].getMethodName() + " " + length1 + " " + length2); + var array = NetworkBuffer.makeArray(networkBuffer -> networkBuffer.write(palette)); + int length1 = array.length; + array = NetworkBuffer.makeArray(networkBuffer -> networkBuffer.write(optimized)); + int length2 = array.length; assertTrue(length1 >= length2, "Optimized palette is bigger than the original one: " + length1 + " : " + length2); } } diff --git a/src/test/java/net/minestom/server/network/NetworkBufferTest.java b/src/test/java/net/minestom/server/network/NetworkBufferTest.java index 13126fc4b..ae0be9437 100644 --- a/src/test/java/net/minestom/server/network/NetworkBufferTest.java +++ b/src/test/java/net/minestom/server/network/NetworkBufferTest.java @@ -6,11 +6,12 @@ import net.minestom.server.item.Material; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.UnknownNullability; -import org.jglrxavpok.hephaistos.nbt.NBT; import org.junit.jupiter.api.Test; +import java.util.UUID; import java.util.*; +import static net.minestom.server.network.NetworkBuffer.*; import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -21,135 +22,181 @@ public class NetworkBufferTest { var buffer = new NetworkBuffer(); assertEquals(0, buffer.readableBytes()); - buffer.write(NetworkBuffer.BYTE, (byte) 0); + buffer.write(BYTE, (byte) 0); assertEquals(1, buffer.readableBytes()); - buffer.write(NetworkBuffer.LONG, 50L); + buffer.write(LONG, 50L); assertEquals(9, buffer.readableBytes()); - assertEquals((byte) 0, buffer.read(NetworkBuffer.BYTE)); + assertEquals((byte) 0, buffer.read(BYTE)); assertEquals(8, buffer.readableBytes()); - assertEquals(50L, buffer.read(NetworkBuffer.LONG)); + assertEquals(50L, buffer.read(LONG)); assertEquals(0, buffer.readableBytes()); } + @Test + public void extractBytes() { + var buffer = new NetworkBuffer(); + + buffer.write(BYTE, (byte) 25); + assertEquals(1, buffer.writeIndex()); + assertEquals(0, buffer.readIndex()); + + var array = buffer.extractBytes(extractor -> extractor.read(BYTE)); + assertArrayEquals(new byte[]{25}, array, "Unequal array: " + Arrays.toString(array)); + assertEquals(1, buffer.writeIndex()); + assertEquals(1, buffer.readIndex()); + + buffer.write(BYTE, (byte) 25); + buffer.write(LONG, 50L); + assertEquals(10, buffer.writeIndex()); + assertEquals(1, buffer.readIndex()); + + array = buffer.extractBytes(extractor -> { + extractor.read(BYTE); + extractor.read(LONG); + }); + assertArrayEquals(new byte[]{25, 0, 0, 0, 0, 0, 0, 0, 50}, array, "Unequal array: " + Arrays.toString(array)); + assertEquals(10, buffer.writeIndex()); + assertEquals(10, buffer.readIndex()); + } + + @Test + public void makeArray() { + assertArrayEquals(new byte[0], NetworkBuffer.makeArray(buffer -> { + })); + + assertArrayEquals(new byte[]{1}, NetworkBuffer.makeArray(buffer -> buffer.write(BYTE, (byte) 1))); + + assertArrayEquals(new byte[]{1, 0, 0, 0, 0, 0, 0, 0, 50}, NetworkBuffer.makeArray(buffer -> { + buffer.write(BYTE, (byte) 1); + buffer.write(LONG, 50L); + })); + } + @Test public void numbers() { - assertBufferType(NetworkBuffer.BOOLEAN, false, new byte[]{0x00}); - assertBufferType(NetworkBuffer.BOOLEAN, true, new byte[]{0x01}); + assertBufferType(BOOLEAN, false, new byte[]{0x00}); + assertBufferType(BOOLEAN, true, new byte[]{0x01}); - assertBufferType(NetworkBuffer.BYTE, (byte) 0x00, new byte[]{0x00}); - assertBufferType(NetworkBuffer.BYTE, (byte) 0x01, new byte[]{0x01}); - assertBufferType(NetworkBuffer.BYTE, (byte) 0x7F, new byte[]{0x7F}); - assertBufferType(NetworkBuffer.BYTE, (byte) 0x80, new byte[]{(byte) 0x80}); - assertBufferType(NetworkBuffer.BYTE, (byte) 0xFF, new byte[]{(byte) 0xFF}); + assertBufferType(BYTE, (byte) 0x00, new byte[]{0x00}); + assertBufferType(BYTE, (byte) 0x01, new byte[]{0x01}); + assertBufferType(BYTE, (byte) 0x7F, new byte[]{0x7F}); + assertBufferType(BYTE, (byte) 0x80, new byte[]{(byte) 0x80}); + assertBufferType(BYTE, (byte) 0xFF, new byte[]{(byte) 0xFF}); - assertBufferType(NetworkBuffer.SHORT, (short) 0x0000, new byte[]{0x00, 0x00}); - assertBufferType(NetworkBuffer.SHORT, (short) 0x0001, new byte[]{0x00, 0x01}); - assertBufferType(NetworkBuffer.SHORT, (short) 0x7FFF, new byte[]{0x7F, (byte) 0xFF}); - assertBufferType(NetworkBuffer.SHORT, (short) 0x8000, new byte[]{(byte) 0x80, 0x00}); - assertBufferType(NetworkBuffer.SHORT, (short) 0xFFFF, new byte[]{(byte) 0xFF, (byte) 0xFF}); + assertBufferType(SHORT, (short) 0x0000, new byte[]{0x00, 0x00}); + assertBufferType(SHORT, (short) 0x0001, new byte[]{0x00, 0x01}); + assertBufferType(SHORT, (short) 0x7FFF, new byte[]{0x7F, (byte) 0xFF}); + assertBufferType(SHORT, (short) 0x8000, new byte[]{(byte) 0x80, 0x00}); + assertBufferType(SHORT, (short) 0xFFFF, new byte[]{(byte) 0xFF, (byte) 0xFF}); - assertBufferType(NetworkBuffer.INT, 0, new byte[]{0x00, 0x00, 0x00, 0x00}); - assertBufferType(NetworkBuffer.INT, 1, new byte[]{0x00, 0x00, 0x00, 0x01}); - assertBufferType(NetworkBuffer.INT, 2, new byte[]{0x00, 0x00, 0x00, 0x02}); - assertBufferType(NetworkBuffer.INT, 127, new byte[]{0x00, 0x00, 0x00, 0x7F}); - assertBufferType(NetworkBuffer.INT, 128, new byte[]{0x00, 0x00, 0x00, (byte) 0x80}); - assertBufferType(NetworkBuffer.INT, 255, new byte[]{0x00, 0x00, 0x00, (byte) 0xFF}); - assertBufferType(NetworkBuffer.INT, 256, new byte[]{0x00, 0x00, 0x01, 0x00}); - assertBufferType(NetworkBuffer.INT, 25565, new byte[]{0x00, 0x00, 0x63, (byte) 0xDD}); - assertBufferType(NetworkBuffer.INT, 32767, new byte[]{0x00, 0x00, 0x7F, (byte) 0xFF}); - assertBufferType(NetworkBuffer.INT, 32768, new byte[]{0x00, 0x00, (byte) 0x80, 0x00}); - assertBufferType(NetworkBuffer.INT, 65535, new byte[]{0x00, 0x00, (byte) 0xFF, (byte) 0xFF}); - assertBufferType(NetworkBuffer.INT, 65536, new byte[]{0x00, 0x01, 0x00, 0x00}); - assertBufferType(NetworkBuffer.INT, 2147483647, new byte[]{0x7F, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF}); - assertBufferType(NetworkBuffer.INT, -1, new byte[]{(byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF}); - assertBufferType(NetworkBuffer.INT, -2147483648, new byte[]{(byte) 0x80, 0x00, 0x00, 0x00}); + assertBufferType(UNSIGNED_SHORT, 0x0000, new byte[]{0x00, 0x00}); + assertBufferType(UNSIGNED_SHORT, 0x0001, new byte[]{0x00, 0x01}); + assertBufferType(UNSIGNED_SHORT, 0x7FFF, new byte[]{0x7F, (byte) 0xFF}); + assertBufferType(UNSIGNED_SHORT, 0x8000, new byte[]{(byte) 0x80, 0x00}); + assertBufferType(UNSIGNED_SHORT, 0xFFFF, new byte[]{(byte) 0xFF, (byte) 0xFF}); - assertBufferType(NetworkBuffer.LONG, 0L, new byte[]{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}); - assertBufferType(NetworkBuffer.LONG, 1L, new byte[]{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}); - assertBufferType(NetworkBuffer.LONG, 2L, new byte[]{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02}); - assertBufferType(NetworkBuffer.LONG, 127L, new byte[]{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7F}); - assertBufferType(NetworkBuffer.LONG, 128L, new byte[]{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0x80}); - assertBufferType(NetworkBuffer.LONG, 255L, new byte[]{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0xFF}); - assertBufferType(NetworkBuffer.LONG, 256L, new byte[]{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00}); - assertBufferType(NetworkBuffer.LONG, 25565L, new byte[]{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x63, (byte) 0xDD}); - assertBufferType(NetworkBuffer.LONG, 32767L, new byte[]{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7F, (byte) 0xFF}); - assertBufferType(NetworkBuffer.LONG, 32768L, new byte[]{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0x80, 0x00}); - assertBufferType(NetworkBuffer.LONG, 65535L, new byte[]{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0xFF, (byte) 0xFF}); - assertBufferType(NetworkBuffer.LONG, 65536L, new byte[]{0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00}); - assertBufferType(NetworkBuffer.LONG, 2147483647L, new byte[]{0x00, 0x00, 0x00, 0x00, 0x7F, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF}); - assertBufferType(NetworkBuffer.LONG, 2147483648L, new byte[]{0x00, 0x00, 0x00, 0x00, (byte) 0x80, 0x00, 0x00, 0x00}); - assertBufferType(NetworkBuffer.LONG, 4294967295L, new byte[]{0x00, 0x00, 0x00, 0x00, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF}); - assertBufferType(NetworkBuffer.LONG, 4294967296L, new byte[]{0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00}); - assertBufferType(NetworkBuffer.LONG, 9223372036854775807L, new byte[]{0x7F, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF}); - assertBufferType(NetworkBuffer.LONG, -1L, new byte[]{(byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF}); - assertBufferType(NetworkBuffer.LONG, -2147483648L, new byte[]{(byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0x80, 0x00, 0x00, 0x00}); - assertBufferType(NetworkBuffer.LONG, -4294967296L, new byte[]{(byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, 0x00, 0x00, 0x00, 0x00}); - assertBufferType(NetworkBuffer.LONG, -9223372036854775808L, new byte[]{(byte) 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}); + assertBufferType(INT, 0, new byte[]{0x00, 0x00, 0x00, 0x00}); + assertBufferType(INT, 1, new byte[]{0x00, 0x00, 0x00, 0x01}); + assertBufferType(INT, 2, new byte[]{0x00, 0x00, 0x00, 0x02}); + assertBufferType(INT, 127, new byte[]{0x00, 0x00, 0x00, 0x7F}); + assertBufferType(INT, 128, new byte[]{0x00, 0x00, 0x00, (byte) 0x80}); + assertBufferType(INT, 255, new byte[]{0x00, 0x00, 0x00, (byte) 0xFF}); + assertBufferType(INT, 256, new byte[]{0x00, 0x00, 0x01, 0x00}); + assertBufferType(INT, 25565, new byte[]{0x00, 0x00, 0x63, (byte) 0xDD}); + assertBufferType(INT, 32767, new byte[]{0x00, 0x00, 0x7F, (byte) 0xFF}); + assertBufferType(INT, 32768, new byte[]{0x00, 0x00, (byte) 0x80, 0x00}); + assertBufferType(INT, 65535, new byte[]{0x00, 0x00, (byte) 0xFF, (byte) 0xFF}); + assertBufferType(INT, 65536, new byte[]{0x00, 0x01, 0x00, 0x00}); + assertBufferType(INT, 2147483647, new byte[]{0x7F, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF}); + assertBufferType(INT, -1, new byte[]{(byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF}); + assertBufferType(INT, -2147483648, new byte[]{(byte) 0x80, 0x00, 0x00, 0x00}); - assertBufferType(NetworkBuffer.FLOAT, 0f, new byte[]{0x00, 0x00, 0x00, 0x00}); - assertBufferType(NetworkBuffer.FLOAT, 1f, new byte[]{0x3F, (byte) 0x80, 0x00, 0x00}); - assertBufferType(NetworkBuffer.FLOAT, 1.1f, new byte[]{0x3F, (byte) 0x8C, (byte) 0xCC, (byte) 0xCD}); - assertBufferType(NetworkBuffer.FLOAT, 1.5f, new byte[]{0x3F, (byte) 0xC0, 0x00, 0x00}); - assertBufferType(NetworkBuffer.FLOAT, 1.6f, new byte[]{0x3F, (byte) 0xCC, (byte) 0xCC, (byte) 0xCD}); - assertBufferType(NetworkBuffer.FLOAT, 2f, new byte[]{0x40, 0x00, 0x00, 0x00}); - assertBufferType(NetworkBuffer.FLOAT, 2.5f, new byte[]{0x40, 0x20, 0x00, 0x00}); - assertBufferType(NetworkBuffer.FLOAT, 3f, new byte[]{0x40, 0x40, 0x00, 0x00}); - assertBufferType(NetworkBuffer.FLOAT, 4f, new byte[]{0x40, (byte) 0x80, 0x00, 0x00}); - assertBufferType(NetworkBuffer.FLOAT, 5f, new byte[]{0x40, (byte) 0xA0, 0x00, 0x00}); - assertBufferType(NetworkBuffer.FLOAT, 10f, new byte[]{0x41, 0x20, 0x00, 0x00}); - assertBufferType(NetworkBuffer.FLOAT, 100f, new byte[]{0x42, (byte) 0xC8, 0x00, 0x00}); - assertBufferType(NetworkBuffer.FLOAT, 1000f, new byte[]{0x44, 0x7a, 0x00, 0x00}); - assertBufferType(NetworkBuffer.FLOAT, 10000f, new byte[]{0x46, 0x1C, 0x40, 0x00}); - assertBufferType(NetworkBuffer.FLOAT, 100000f, new byte[]{0x47, (byte) 0xC3, 0x50, 0x00}); + assertBufferType(LONG, 0L, new byte[]{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}); + assertBufferType(LONG, 1L, new byte[]{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01}); + assertBufferType(LONG, 2L, new byte[]{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02}); + assertBufferType(LONG, 127L, new byte[]{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7F}); + assertBufferType(LONG, 128L, new byte[]{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0x80}); + assertBufferType(LONG, 255L, new byte[]{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0xFF}); + assertBufferType(LONG, 256L, new byte[]{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00}); + assertBufferType(LONG, 25565L, new byte[]{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x63, (byte) 0xDD}); + assertBufferType(LONG, 32767L, new byte[]{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x7F, (byte) 0xFF}); + assertBufferType(LONG, 32768L, new byte[]{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0x80, 0x00}); + assertBufferType(LONG, 65535L, new byte[]{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0xFF, (byte) 0xFF}); + assertBufferType(LONG, 65536L, new byte[]{0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00}); + assertBufferType(LONG, 2147483647L, new byte[]{0x00, 0x00, 0x00, 0x00, 0x7F, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF}); + assertBufferType(LONG, 2147483648L, new byte[]{0x00, 0x00, 0x00, 0x00, (byte) 0x80, 0x00, 0x00, 0x00}); + assertBufferType(LONG, 4294967295L, new byte[]{0x00, 0x00, 0x00, 0x00, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF}); + assertBufferType(LONG, 4294967296L, new byte[]{0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00}); + assertBufferType(LONG, 9223372036854775807L, new byte[]{0x7F, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF}); + assertBufferType(LONG, -1L, new byte[]{(byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF}); + assertBufferType(LONG, -2147483648L, new byte[]{(byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0x80, 0x00, 0x00, 0x00}); + assertBufferType(LONG, -4294967296L, new byte[]{(byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, 0x00, 0x00, 0x00, 0x00}); + assertBufferType(LONG, -9223372036854775808L, new byte[]{(byte) 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}); - assertBufferType(NetworkBuffer.DOUBLE, 0d, new byte[]{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}); - assertBufferType(NetworkBuffer.DOUBLE, 1d, new byte[]{0x3F, (byte) 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}); - assertBufferType(NetworkBuffer.DOUBLE, 1.1d, new byte[]{0x3F, (byte) 0xF1, (byte) 0x99, (byte) 0x99, (byte) 0x99, (byte) 0x99, (byte) 0x99, (byte) 0x9A}); - assertBufferType(NetworkBuffer.DOUBLE, 1.5d, new byte[]{0x3F, (byte) 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}); - assertBufferType(NetworkBuffer.DOUBLE, 1.6d, new byte[]{0x3F, (byte) 0xF9, (byte) 0x99, (byte) 0x99, (byte) 0x99, (byte) 0x99, (byte) 0x99, (byte) 0x9A}); - assertBufferType(NetworkBuffer.DOUBLE, 2d, new byte[]{0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}); - assertBufferType(NetworkBuffer.DOUBLE, 2.5d, new byte[]{0x40, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}); - assertBufferType(NetworkBuffer.DOUBLE, 3d, new byte[]{0x40, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}); - assertBufferType(NetworkBuffer.DOUBLE, 4d, new byte[]{0x40, 0x10, (byte) 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}); - assertBufferType(NetworkBuffer.DOUBLE, 5d, new byte[]{0x40, 0x14, (byte) 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}); - assertBufferType(NetworkBuffer.DOUBLE, 10d, new byte[]{0x40, 0x24, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}); - assertBufferType(NetworkBuffer.DOUBLE, 100d, new byte[]{0x40, 0x59, (byte) 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}); - assertBufferType(NetworkBuffer.DOUBLE, 1000d, new byte[]{0x40, (byte) 0x8F, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00}); - assertBufferType(NetworkBuffer.DOUBLE, 10000d, new byte[]{0x40, (byte) 0xC3, (byte) 0x88, 0x00, 0x00, 0x00, 0x00, 0x00}); + assertBufferType(FLOAT, 0f, new byte[]{0x00, 0x00, 0x00, 0x00}); + assertBufferType(FLOAT, 1f, new byte[]{0x3F, (byte) 0x80, 0x00, 0x00}); + assertBufferType(FLOAT, 1.1f, new byte[]{0x3F, (byte) 0x8C, (byte) 0xCC, (byte) 0xCD}); + assertBufferType(FLOAT, 1.5f, new byte[]{0x3F, (byte) 0xC0, 0x00, 0x00}); + assertBufferType(FLOAT, 1.6f, new byte[]{0x3F, (byte) 0xCC, (byte) 0xCC, (byte) 0xCD}); + assertBufferType(FLOAT, 2f, new byte[]{0x40, 0x00, 0x00, 0x00}); + assertBufferType(FLOAT, 2.5f, new byte[]{0x40, 0x20, 0x00, 0x00}); + assertBufferType(FLOAT, 3f, new byte[]{0x40, 0x40, 0x00, 0x00}); + assertBufferType(FLOAT, 4f, new byte[]{0x40, (byte) 0x80, 0x00, 0x00}); + assertBufferType(FLOAT, 5f, new byte[]{0x40, (byte) 0xA0, 0x00, 0x00}); + assertBufferType(FLOAT, 10f, new byte[]{0x41, 0x20, 0x00, 0x00}); + assertBufferType(FLOAT, 100f, new byte[]{0x42, (byte) 0xC8, 0x00, 0x00}); + assertBufferType(FLOAT, 1000f, new byte[]{0x44, 0x7a, 0x00, 0x00}); + assertBufferType(FLOAT, 10000f, new byte[]{0x46, 0x1C, 0x40, 0x00}); + assertBufferType(FLOAT, 100000f, new byte[]{0x47, (byte) 0xC3, 0x50, 0x00}); + + assertBufferType(DOUBLE, 0d, new byte[]{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}); + assertBufferType(DOUBLE, 1d, new byte[]{0x3F, (byte) 0xF0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}); + assertBufferType(DOUBLE, 1.1d, new byte[]{0x3F, (byte) 0xF1, (byte) 0x99, (byte) 0x99, (byte) 0x99, (byte) 0x99, (byte) 0x99, (byte) 0x9A}); + assertBufferType(DOUBLE, 1.5d, new byte[]{0x3F, (byte) 0xF8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}); + assertBufferType(DOUBLE, 1.6d, new byte[]{0x3F, (byte) 0xF9, (byte) 0x99, (byte) 0x99, (byte) 0x99, (byte) 0x99, (byte) 0x99, (byte) 0x9A}); + assertBufferType(DOUBLE, 2d, new byte[]{0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}); + assertBufferType(DOUBLE, 2.5d, new byte[]{0x40, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}); + assertBufferType(DOUBLE, 3d, new byte[]{0x40, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}); + assertBufferType(DOUBLE, 4d, new byte[]{0x40, 0x10, (byte) 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}); + assertBufferType(DOUBLE, 5d, new byte[]{0x40, 0x14, (byte) 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}); + assertBufferType(DOUBLE, 10d, new byte[]{0x40, 0x24, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}); + assertBufferType(DOUBLE, 100d, new byte[]{0x40, 0x59, (byte) 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}); + assertBufferType(DOUBLE, 1000d, new byte[]{0x40, (byte) 0x8F, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00}); + assertBufferType(DOUBLE, 10000d, new byte[]{0x40, (byte) 0xC3, (byte) 0x88, 0x00, 0x00, 0x00, 0x00, 0x00}); } @Test public void varInt() { - assertBufferType(NetworkBuffer.VAR_INT, 0, new byte[]{0}); - assertBufferType(NetworkBuffer.VAR_INT, 1, new byte[]{0x01}); - assertBufferType(NetworkBuffer.VAR_INT, 2, new byte[]{0x02}); - assertBufferType(NetworkBuffer.VAR_INT, 11, new byte[]{0x0B}); - assertBufferType(NetworkBuffer.VAR_INT, 127, new byte[]{0x7f}); - assertBufferType(NetworkBuffer.VAR_INT, 128, new byte[]{(byte) 0x80, 0x01}); - assertBufferType(NetworkBuffer.VAR_INT, 255, new byte[]{(byte) 0xff, 0x01}); - assertBufferType(NetworkBuffer.VAR_INT, 25565, new byte[]{(byte) 0xdd, (byte) 0xc7, 0x01}); - assertBufferType(NetworkBuffer.VAR_INT, 2097151, new byte[]{(byte) 0xff, (byte) 0xff, 0x7f}); - assertBufferType(NetworkBuffer.VAR_INT, 2147483647, new byte[]{(byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, 0x07}); - assertBufferType(NetworkBuffer.VAR_INT, -1, new byte[]{(byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, 0x0f}); - assertBufferType(NetworkBuffer.VAR_INT, -2147483648, new byte[]{(byte) 0x80, (byte) 0x80, (byte) 0x80, (byte) 0x80, 0x08}); + assertBufferType(VAR_INT, 0, new byte[]{0}); + assertBufferType(VAR_INT, 1, new byte[]{0x01}); + assertBufferType(VAR_INT, 2, new byte[]{0x02}); + assertBufferType(VAR_INT, 11, new byte[]{0x0B}); + assertBufferType(VAR_INT, 127, new byte[]{0x7f}); + assertBufferType(VAR_INT, 128, new byte[]{(byte) 0x80, 0x01}); + assertBufferType(VAR_INT, 255, new byte[]{(byte) 0xff, 0x01}); + assertBufferType(VAR_INT, 25565, new byte[]{(byte) 0xdd, (byte) 0xc7, 0x01}); + assertBufferType(VAR_INT, 2097151, new byte[]{(byte) 0xff, (byte) 0xff, 0x7f}); + assertBufferType(VAR_INT, 2147483647, new byte[]{(byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, 0x07}); + assertBufferType(VAR_INT, -1, new byte[]{(byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, 0x0f}); + assertBufferType(VAR_INT, -2147483648, new byte[]{(byte) 0x80, (byte) 0x80, (byte) 0x80, (byte) 0x80, 0x08}); } @Test public void varLong() { - assertBufferType(NetworkBuffer.VAR_LONG, 0L, new byte[]{0}); - assertBufferType(NetworkBuffer.VAR_LONG, 1L, new byte[]{0x01}); - assertBufferType(NetworkBuffer.VAR_LONG, 2L, new byte[]{0x02}); - assertBufferType(NetworkBuffer.VAR_LONG, 127L, new byte[]{0x7f}); - assertBufferType(NetworkBuffer.VAR_LONG, 128L, new byte[]{(byte) 0x80, 0x01}); - assertBufferType(NetworkBuffer.VAR_LONG, 255L, new byte[]{(byte) 0xff, 0x01}); - assertBufferType(NetworkBuffer.VAR_LONG, 2147483647L, new byte[]{(byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, 0x07}); - assertBufferType(NetworkBuffer.VAR_LONG, 9223372036854775807L, new byte[]{(byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, 0x7f}); - assertBufferType(NetworkBuffer.VAR_LONG, -1L, new byte[]{(byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, 0x01}); - assertBufferType(NetworkBuffer.VAR_LONG, -2147483648L, new byte[]{(byte) 0x80, (byte) 0x80, (byte) 0x80, (byte) 0x80, (byte) 0xf8, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, 0x01}); - assertBufferType(NetworkBuffer.VAR_LONG, -9223372036854775808L, new byte[]{(byte) 0x80, (byte) 0x80, (byte) 0x80, (byte) 0x80, (byte) 0x80, (byte) 0x80, (byte) 0x80, (byte) 0x80, (byte) 0x80, 0x01}); + assertBufferType(VAR_LONG, 0L, new byte[]{0}); + assertBufferType(VAR_LONG, 1L, new byte[]{0x01}); + assertBufferType(VAR_LONG, 2L, new byte[]{0x02}); + assertBufferType(VAR_LONG, 127L, new byte[]{0x7f}); + assertBufferType(VAR_LONG, 128L, new byte[]{(byte) 0x80, 0x01}); + assertBufferType(VAR_LONG, 255L, new byte[]{(byte) 0xff, 0x01}); + assertBufferType(VAR_LONG, 2147483647L, new byte[]{(byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, 0x07}); + assertBufferType(VAR_LONG, 9223372036854775807L, new byte[]{(byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, 0x7f}); + assertBufferType(VAR_LONG, -1L, new byte[]{(byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, 0x01}); + assertBufferType(VAR_LONG, -2147483648L, new byte[]{(byte) 0x80, (byte) 0x80, (byte) 0x80, (byte) 0x80, (byte) 0xf8, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, 0x01}); + assertBufferType(VAR_LONG, -9223372036854775808L, new byte[]{(byte) 0x80, (byte) 0x80, (byte) 0x80, (byte) 0x80, (byte) 0x80, (byte) 0x80, (byte) 0x80, (byte) 0x80, (byte) 0x80, 0x01}); } @Test @@ -161,43 +208,43 @@ public class NetworkBufferTest { @Test public void string() { - assertBufferType(NetworkBuffer.STRING, "Hello World", new byte[]{0x0B, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64}); + assertBufferType(STRING, "Hello World", new byte[]{0x0B, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64}); } @Test public void nbt() { - assertBufferType(NetworkBuffer.NBT, NBT.Int(5)); - assertBufferType(NetworkBuffer.NBT, NBT.Compound(Map.of("key", NBT.Int(5)))); + assertBufferType(NetworkBuffer.NBT, org.jglrxavpok.hephaistos.nbt.NBT.Int(5)); + assertBufferType(NetworkBuffer.NBT, org.jglrxavpok.hephaistos.nbt.NBT.Compound(Map.of("key", org.jglrxavpok.hephaistos.nbt.NBT.Int(5)))); } @Test public void component() { - assertBufferType(NetworkBuffer.COMPONENT, Component.text("Hello world")); + assertBufferType(COMPONENT, Component.text("Hello world")); } @Test public void uuid() { - assertBufferType(NetworkBuffer.UUID, new UUID(0, 0), new byte[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}); - assertBufferType(NetworkBuffer.UUID, new UUID(1, 1), new byte[]{0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1}); + assertBufferType(UUID, new UUID(0, 0), new byte[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}); + assertBufferType(UUID, new UUID(1, 1), new byte[]{0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1}); } @Test public void item() { - assertBufferType(NetworkBuffer.ITEM, ItemStack.AIR); - assertBufferType(NetworkBuffer.ITEM, ItemStack.of(Material.STONE, 1)); - assertBufferType(NetworkBuffer.ITEM, ItemStack.of(Material.DIAMOND_AXE, 1).withMeta(builder -> builder.damage(1))); + assertBufferType(ITEM, ItemStack.AIR); + assertBufferType(ITEM, ItemStack.of(Material.STONE, 1)); + assertBufferType(ITEM, ItemStack.of(Material.DIAMOND_AXE, 1).withMeta(builder -> builder.damage(1))); } @Test public void optional() { - assertBufferTypeOptional(NetworkBuffer.BOOLEAN, null, new byte[]{0}); - assertBufferTypeOptional(NetworkBuffer.BOOLEAN, true, new byte[]{1, 1}); + assertBufferTypeOptional(BOOLEAN, null, new byte[]{0}); + assertBufferTypeOptional(BOOLEAN, true, new byte[]{1, 1}); } @Test public void collection() { - assertBufferTypeCollection(NetworkBuffer.BOOLEAN, List.of(), new byte[]{0}); - assertBufferTypeCollection(NetworkBuffer.BOOLEAN, List.of(true), new byte[]{0x01, 0x01}); + assertBufferTypeCollection(BOOLEAN, List.of(), new byte[]{0}); + assertBufferTypeCollection(BOOLEAN, List.of(true), new byte[]{0x01, 0x01}); } static void assertBufferType(NetworkBuffer.@NotNull Type type, @UnknownNullability T value, byte[] expected, @NotNull Action action) { diff --git a/src/test/java/net/minestom/server/network/PacketWriteReadTest.java b/src/test/java/net/minestom/server/network/PacketWriteReadTest.java index 7ec686fe7..ead02aaa7 100644 --- a/src/test/java/net/minestom/server/network/PacketWriteReadTest.java +++ b/src/test/java/net/minestom/server/network/PacketWriteReadTest.java @@ -19,9 +19,6 @@ import net.minestom.server.network.packet.server.login.SetCompressionPacket; import net.minestom.server.network.packet.server.play.*; import net.minestom.server.network.packet.server.play.DeclareRecipesPacket.Ingredient; import net.minestom.server.network.packet.server.status.PongPacket; -import net.minestom.server.utils.binary.BinaryReader; -import net.minestom.server.utils.binary.BinaryWriter; -import net.minestom.server.utils.binary.Writeable; import org.jglrxavpok.hephaistos.nbt.NBT; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; @@ -144,13 +141,13 @@ public class PacketWriteReadTest { CLIENT_PACKETS.forEach(PacketWriteReadTest::testPacket); } - private static void testPacket(Writeable writeable) { + private static void testPacket(NetworkBuffer.Writer writeable) { try { - BinaryWriter writer = new BinaryWriter(); - writeable.write(writer); - var readerConstructor = writeable.getClass().getConstructor(BinaryReader.class); + byte[] bytes = NetworkBuffer.makeArray(buffer -> buffer.write(writeable)); + var readerConstructor = writeable.getClass().getConstructor(NetworkBuffer.class); - BinaryReader reader = new BinaryReader(writer.toByteArray()); + NetworkBuffer reader = new NetworkBuffer(); + reader.write(NetworkBuffer.RAW_BYTES, bytes); var createdPacket = readerConstructor.newInstance(reader); assertEquals(writeable, createdPacket); } catch (NoSuchMethodException | InvocationTargetException | InstantiationException diff --git a/src/test/java/net/minestom/server/network/SocketReadTest.java b/src/test/java/net/minestom/server/network/SocketReadTest.java index 332255649..0f168ab61 100644 --- a/src/test/java/net/minestom/server/network/SocketReadTest.java +++ b/src/test/java/net/minestom/server/network/SocketReadTest.java @@ -6,7 +6,6 @@ import net.minestom.server.utils.ObjectPool; import net.minestom.server.utils.PacketUtils; import net.minestom.server.utils.Utils; import net.minestom.server.utils.binary.BinaryBuffer; -import net.minestom.server.utils.binary.BinaryReader; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; @@ -38,7 +37,7 @@ public class SocketReadTest { assertEquals(1, packets.size()); var rawPacket = packets.get(0); assertEquals(0x0A, rawPacket.left()); - var readPacket = new ClientPluginMessagePacket(new BinaryReader(rawPacket.right())); + var readPacket = new ClientPluginMessagePacket(new NetworkBuffer(rawPacket.right())); assertEquals("channel", readPacket.channel()); assertEquals(2000, readPacket.data().length); } @@ -63,7 +62,7 @@ public class SocketReadTest { assertEquals(2, packets.size()); for (var rawPacket : packets) { assertEquals(0x0A, rawPacket.left()); - var readPacket = new ClientPluginMessagePacket(new BinaryReader(rawPacket.right())); + var readPacket = new ClientPluginMessagePacket(new NetworkBuffer(rawPacket.right())); assertEquals("channel", readPacket.channel()); assertEquals(2000, readPacket.data().length); } @@ -92,7 +91,7 @@ public class SocketReadTest { assertEquals(1, packets.size()); var rawPacket = packets.get(0); assertEquals(0x0A, rawPacket.left()); - var readPacket = new ClientPluginMessagePacket(new BinaryReader(rawPacket.right())); + var readPacket = new ClientPluginMessagePacket(new NetworkBuffer(rawPacket.right())); assertEquals("channel", readPacket.channel()); assertEquals(2000, readPacket.data().length); } @@ -120,7 +119,7 @@ public class SocketReadTest { assertEquals(1, packets.size()); var rawPacket = packets.get(0); assertEquals(0x0A, rawPacket.left()); - var readPacket = new ClientPluginMessagePacket(new BinaryReader(rawPacket.right())); + var readPacket = new ClientPluginMessagePacket(new NetworkBuffer(rawPacket.right())); assertEquals("channel", readPacket.channel()); assertEquals(2000, readPacket.data().length); } diff --git a/src/test/java/net/minestom/server/network/SocketWriteTest.java b/src/test/java/net/minestom/server/network/SocketWriteTest.java index 481305f40..1202eeb1f 100644 --- a/src/test/java/net/minestom/server/network/SocketWriteTest.java +++ b/src/test/java/net/minestom/server/network/SocketWriteTest.java @@ -4,12 +4,13 @@ import net.minestom.server.network.packet.server.ServerPacket; import net.minestom.server.utils.ObjectPool; import net.minestom.server.utils.PacketUtils; import net.minestom.server.utils.Utils; -import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; import org.junit.jupiter.api.Test; import java.nio.charset.StandardCharsets; +import static net.minestom.server.network.NetworkBuffer.INT; +import static net.minestom.server.network.NetworkBuffer.STRING; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotEquals; @@ -17,8 +18,8 @@ public class SocketWriteTest { record IntPacket(int value) implements ServerPacket { @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeInt(value); + public void write(@NotNull NetworkBuffer writer) { + writer.write(INT, value); } @Override @@ -29,8 +30,8 @@ public class SocketWriteTest { record CompressiblePacket(String value) implements ServerPacket { @Override - public void write(@NotNull BinaryWriter writer) { - writer.writeSizedString(value); + public void write(@NotNull NetworkBuffer writer) { + writer.write(STRING, value); } @Override