From 4c8377a1ae24f584b8c0f57a8eb489eea4858818 Mon Sep 17 00:00:00 2001 From: Bixilon Date: Wed, 9 Dec 2020 16:37:17 +0100 Subject: [PATCH] Improve byte buffers --- .../data/commands/parser/CommandParser.java | 4 +- .../minosoft/data/text/ChatComponent.java | 3 +- .../event/events/ChangeGameStateEvent.java | 2 +- .../network/socket/SocketNetwork.java | 10 +- .../play/PacketChangeGameState.java | 6 +- .../clientbound/play/PacketChunkData.java | 2 +- .../play/PacketDeclareRecipes.java | 2 +- .../play/PacketEntityTeleport.java | 2 +- .../play/PacketNamedSoundEffect.java | 2 +- .../clientbound/play/PacketSoundEffect.java | 2 +- .../play/PacketSpawnExperienceOrb.java | 2 +- .../clientbound/play/PacketSpawnMob.java | 2 +- .../clientbound/play/PacketSpawnObject.java | 2 +- .../clientbound/play/PacketSpawnPlayer.java | 2 +- .../play/PacketSpawnWeatherEntity.java | 2 +- .../packets/clientbound/play/PacketTags.java | 2 +- .../play/PacketInteractEntity.java | 6 +- .../protocol/protocol/InByteBuffer.java | 81 +++++++-------- .../protocol/protocol/OutByteBuffer.java | 99 ++++++++++--------- .../protocol/protocol/OutPacketBuffer.java | 4 +- .../protocol/protocol/PacketHandler.java | 4 +- .../protocol/protocol/PacketSender.java | 4 +- 22 files changed, 124 insertions(+), 121 deletions(-) diff --git a/src/main/java/de/bixilon/minosoft/data/commands/parser/CommandParser.java b/src/main/java/de/bixilon/minosoft/data/commands/parser/CommandParser.java index 28746059c..9b505313c 100644 --- a/src/main/java/de/bixilon/minosoft/data/commands/parser/CommandParser.java +++ b/src/main/java/de/bixilon/minosoft/data/commands/parser/CommandParser.java @@ -22,7 +22,7 @@ import javax.annotation.Nullable; public abstract class CommandParser { @Deprecated - private static final DummyParser dummyParser = new DummyParser(); + private static final DummyParser DUMMY_PARSER = new DummyParser(); public static HashBiMap COMMAND_PARSERS = HashBiMap.create(); static { @@ -37,7 +37,7 @@ public abstract class CommandParser { } public static CommandParser createInstance(ModIdentifier identifier) { - return COMMAND_PARSERS.getOrDefault(identifier, dummyParser); + return COMMAND_PARSERS.getOrDefault(identifier, DUMMY_PARSER); } @Nullable diff --git a/src/main/java/de/bixilon/minosoft/data/text/ChatComponent.java b/src/main/java/de/bixilon/minosoft/data/text/ChatComponent.java index d28c6b76c..491649086 100644 --- a/src/main/java/de/bixilon/minosoft/data/text/ChatComponent.java +++ b/src/main/java/de/bixilon/minosoft/data/text/ChatComponent.java @@ -46,7 +46,8 @@ public abstract class ChatComponent { return new BaseComponent((String) raw); } } else { - throw new IllegalArgumentException(String.format("%s is not a valid type here!", raw.getClass().getSimpleName())); + return new BaseComponent(parent, raw.toString()); + // throw new IllegalArgumentException(String.format("%s is not a valid type here!", raw.getClass().getSimpleName())); } return new BaseComponent(parent, json); } diff --git a/src/main/java/de/bixilon/minosoft/modding/event/events/ChangeGameStateEvent.java b/src/main/java/de/bixilon/minosoft/modding/event/events/ChangeGameStateEvent.java index 3c66aafaf..23fc5a553 100644 --- a/src/main/java/de/bixilon/minosoft/modding/event/events/ChangeGameStateEvent.java +++ b/src/main/java/de/bixilon/minosoft/modding/event/events/ChangeGameStateEvent.java @@ -32,7 +32,7 @@ public class ChangeGameStateEvent extends CancelableEvent { public ChangeGameStateEvent(Connection connection, PacketChangeGameState pkg) { super(connection); this.reason = pkg.getReason(); - this.value = pkg.getValue(); + this.value = pkg.getFloatValue(); } public PacketChangeGameState.Reason getReason() { diff --git a/src/main/java/de/bixilon/minosoft/protocol/network/socket/SocketNetwork.java b/src/main/java/de/bixilon/minosoft/protocol/network/socket/SocketNetwork.java index 698ceeeb6..14d25568e 100644 --- a/src/main/java/de/bixilon/minosoft/protocol/network/socket/SocketNetwork.java +++ b/src/main/java/de/bixilon/minosoft/protocol/network/socket/SocketNetwork.java @@ -96,7 +96,7 @@ public class SocketNetwork implements Network { ServerboundPacket packet = queue.take(); packet.log(); queue.remove(packet); - byte[] data = packet.write(connection).getOutBytes(); + byte[] data = packet.write(connection).toByteArray(); if (compressionThreshold >= 0) { // compression is enabled // check if there is a need to compress it and if so, do it! @@ -107,20 +107,20 @@ public class SocketNetwork implements Network { byte[] compressed = Util.compress(data); lengthPrefixedBuffer.writeVarInt(data.length); // uncompressed length lengthPrefixedBuffer.writeBytes(compressed); - outRawBuffer.prefixVarInt(lengthPrefixedBuffer.getOutBytes().length); // length of total data is uncompressed length + compressed data - outRawBuffer.writeBytes(lengthPrefixedBuffer.getOutBytes()); // write all bytes + outRawBuffer.prefixVarInt(lengthPrefixedBuffer.toByteArray().length); // length of total data is uncompressed length + compressed data + outRawBuffer.writeBytes(lengthPrefixedBuffer.toByteArray()); // write all bytes } else { outRawBuffer.writeVarInt(data.length + 1); // 1 for the compressed length (0) outRawBuffer.writeVarInt(0); // data is uncompressed, compressed size is 0 outRawBuffer.writeBytes(data); } - data = outRawBuffer.getOutBytes(); + data = outRawBuffer.toByteArray(); } else { // append packet length OutByteBuffer bufferWithLengthPrefix = new OutByteBuffer(connection); bufferWithLengthPrefix.writeVarInt(data.length); bufferWithLengthPrefix.writeBytes(data); - data = bufferWithLengthPrefix.getOutBytes(); + data = bufferWithLengthPrefix.toByteArray(); } outputStream.write(data); diff --git a/src/main/java/de/bixilon/minosoft/protocol/packets/clientbound/play/PacketChangeGameState.java b/src/main/java/de/bixilon/minosoft/protocol/packets/clientbound/play/PacketChangeGameState.java index ac2288c84..559239c06 100644 --- a/src/main/java/de/bixilon/minosoft/protocol/packets/clientbound/play/PacketChangeGameState.java +++ b/src/main/java/de/bixilon/minosoft/protocol/packets/clientbound/play/PacketChangeGameState.java @@ -45,10 +45,14 @@ public class PacketChangeGameState implements ClientboundPacket { return reason; } - public Float getValue() { + public float getFloatValue() { return value; } + public int getIntValue() { + return (int) value; + } + public enum Reason { NO_RESPAWN_BLOCK_AVAILABLE(new MapSet[]{new MapSet<>(0, 0)}), START_RAINING(new MapSet[]{new MapSet<>(0, 1), new MapSet<>(498, 2), new MapSet<>(578, 1)}), // ToDo: when exactly did these 2 switch? diff --git a/src/main/java/de/bixilon/minosoft/protocol/packets/clientbound/play/PacketChunkData.java b/src/main/java/de/bixilon/minosoft/protocol/packets/clientbound/play/PacketChunkData.java index a02a20366..d82ce26bc 100644 --- a/src/main/java/de/bixilon/minosoft/protocol/packets/clientbound/play/PacketChunkData.java +++ b/src/main/java/de/bixilon/minosoft/protocol/packets/clientbound/play/PacketChunkData.java @@ -89,7 +89,7 @@ public class PacketChunkData implements ClientboundPacket { } if (groundUpContinuous) { if (buffer.getVersionId() >= 740) { - biomes = buffer.readVarIntArray(buffer.readVarInt()); + biomes = buffer.readVarIntArray(); } else if (buffer.getVersionId() >= 552) { biomes = buffer.readIntArray(1024); } diff --git a/src/main/java/de/bixilon/minosoft/protocol/packets/clientbound/play/PacketDeclareRecipes.java b/src/main/java/de/bixilon/minosoft/protocol/packets/clientbound/play/PacketDeclareRecipes.java index 05e7b64f8..8ee28a372 100644 --- a/src/main/java/de/bixilon/minosoft/protocol/packets/clientbound/play/PacketDeclareRecipes.java +++ b/src/main/java/de/bixilon/minosoft/protocol/packets/clientbound/play/PacketDeclareRecipes.java @@ -45,7 +45,7 @@ public class PacketDeclareRecipes implements ClientboundPacket { switch (type) { case SHAPELESS -> { String group = buffer.readString(); - Ingredient[] ingredients = buffer.readIngredientArray(buffer.readVarInt()); + Ingredient[] ingredients = buffer.readIngredientArray(); Slot result = buffer.readSlot(); recipe = new Recipe(type, group, ingredients, result); } diff --git a/src/main/java/de/bixilon/minosoft/protocol/packets/clientbound/play/PacketEntityTeleport.java b/src/main/java/de/bixilon/minosoft/protocol/packets/clientbound/play/PacketEntityTeleport.java index 71999616a..c4dd81ff5 100644 --- a/src/main/java/de/bixilon/minosoft/protocol/packets/clientbound/play/PacketEntityTeleport.java +++ b/src/main/java/de/bixilon/minosoft/protocol/packets/clientbound/play/PacketEntityTeleport.java @@ -31,7 +31,7 @@ public class PacketEntityTeleport implements ClientboundPacket { this.entityId = buffer.readEntityId(); if (buffer.getVersionId() < 100) { - this.location = new Location(buffer.readFixedPointNumberInteger(), buffer.readFixedPointNumberInteger(), buffer.readFixedPointNumberInteger()); + this.location = new Location(buffer.readFixedPointNumberInt(), buffer.readFixedPointNumberInt(), buffer.readFixedPointNumberInt()); } else { this.location = buffer.readLocation(); } diff --git a/src/main/java/de/bixilon/minosoft/protocol/packets/clientbound/play/PacketNamedSoundEffect.java b/src/main/java/de/bixilon/minosoft/protocol/packets/clientbound/play/PacketNamedSoundEffect.java index 15d6a830e..cf3738256 100644 --- a/src/main/java/de/bixilon/minosoft/protocol/packets/clientbound/play/PacketNamedSoundEffect.java +++ b/src/main/java/de/bixilon/minosoft/protocol/packets/clientbound/play/PacketNamedSoundEffect.java @@ -47,7 +47,7 @@ public class PacketNamedSoundEffect implements ClientboundPacket { category = SoundCategories.byId(buffer.readVarInt()); } if (buffer.getVersionId() >= 95) { - location = new Location(buffer.readFixedPointNumberInteger() * 4, buffer.readFixedPointNumberInteger() * 4, buffer.readFixedPointNumberInteger() * 4); + location = new Location(buffer.readFixedPointNumberInt() * 4, buffer.readFixedPointNumberInt() * 4, buffer.readFixedPointNumberInt() * 4); } volume = buffer.readFloat(); if (buffer.getVersionId() < 201) { diff --git a/src/main/java/de/bixilon/minosoft/protocol/packets/clientbound/play/PacketSoundEffect.java b/src/main/java/de/bixilon/minosoft/protocol/packets/clientbound/play/PacketSoundEffect.java index 060d747b6..6759e4314 100644 --- a/src/main/java/de/bixilon/minosoft/protocol/packets/clientbound/play/PacketSoundEffect.java +++ b/src/main/java/de/bixilon/minosoft/protocol/packets/clientbound/play/PacketSoundEffect.java @@ -42,7 +42,7 @@ public class PacketSoundEffect implements ClientboundPacket { if (buffer.getVersionId() >= 95 && (buffer.getVersionId() < 321 || buffer.getVersionId() >= 326)) { category = SoundCategories.byId(buffer.readVarInt()); } - location = new Location(buffer.readFixedPointNumberInteger() * 4, buffer.readFixedPointNumberInteger() * 4, buffer.readFixedPointNumberInteger() * 4); + location = new Location(buffer.readFixedPointNumberInt() * 4, buffer.readFixedPointNumberInt() * 4, buffer.readFixedPointNumberInt() * 4); volume = buffer.readFloat(); if (buffer.getVersionId() < 201) { pitch = (buffer.readByte() * pitchCalc) / 100F; diff --git a/src/main/java/de/bixilon/minosoft/protocol/packets/clientbound/play/PacketSpawnExperienceOrb.java b/src/main/java/de/bixilon/minosoft/protocol/packets/clientbound/play/PacketSpawnExperienceOrb.java index f2175bff6..6eecbcf12 100644 --- a/src/main/java/de/bixilon/minosoft/protocol/packets/clientbound/play/PacketSpawnExperienceOrb.java +++ b/src/main/java/de/bixilon/minosoft/protocol/packets/clientbound/play/PacketSpawnExperienceOrb.java @@ -28,7 +28,7 @@ public class PacketSpawnExperienceOrb implements ClientboundPacket { int entityId = buffer.readVarInt(); Location location; if (buffer.getVersionId() < 100) { - location = new Location(buffer.readFixedPointNumberInteger(), buffer.readFixedPointNumberInteger(), buffer.readFixedPointNumberInteger()); + location = new Location(buffer.readFixedPointNumberInt(), buffer.readFixedPointNumberInt(), buffer.readFixedPointNumberInt()); } else { location = buffer.readLocation(); } diff --git a/src/main/java/de/bixilon/minosoft/protocol/packets/clientbound/play/PacketSpawnMob.java b/src/main/java/de/bixilon/minosoft/protocol/packets/clientbound/play/PacketSpawnMob.java index 4d799af27..aa62fe062 100644 --- a/src/main/java/de/bixilon/minosoft/protocol/packets/clientbound/play/PacketSpawnMob.java +++ b/src/main/java/de/bixilon/minosoft/protocol/packets/clientbound/play/PacketSpawnMob.java @@ -49,7 +49,7 @@ public class PacketSpawnMob implements ClientboundPacket { Class typeClass = buffer.getConnection().getMapping().getEntityClassById(type); Location location; if (buffer.getVersionId() < 100) { - location = new Location(buffer.readFixedPointNumberInteger(), buffer.readFixedPointNumberInteger(), buffer.readFixedPointNumberInteger()); + location = new Location(buffer.readFixedPointNumberInt(), buffer.readFixedPointNumberInt(), buffer.readFixedPointNumberInt()); } else { location = buffer.readLocation(); } diff --git a/src/main/java/de/bixilon/minosoft/protocol/packets/clientbound/play/PacketSpawnObject.java b/src/main/java/de/bixilon/minosoft/protocol/packets/clientbound/play/PacketSpawnObject.java index d3f1f4475..d895be9b4 100644 --- a/src/main/java/de/bixilon/minosoft/protocol/packets/clientbound/play/PacketSpawnObject.java +++ b/src/main/java/de/bixilon/minosoft/protocol/packets/clientbound/play/PacketSpawnObject.java @@ -56,7 +56,7 @@ public class PacketSpawnObject implements ClientboundPacket { Location location; if (buffer.getVersionId() < 100) { - location = new Location(buffer.readFixedPointNumberInteger(), buffer.readFixedPointNumberInteger(), buffer.readFixedPointNumberInteger()); + location = new Location(buffer.readFixedPointNumberInt(), buffer.readFixedPointNumberInt(), buffer.readFixedPointNumberInt()); } else { location = buffer.readLocation(); } diff --git a/src/main/java/de/bixilon/minosoft/protocol/packets/clientbound/play/PacketSpawnPlayer.java b/src/main/java/de/bixilon/minosoft/protocol/packets/clientbound/play/PacketSpawnPlayer.java index aa9cf7270..a734ff0f0 100644 --- a/src/main/java/de/bixilon/minosoft/protocol/packets/clientbound/play/PacketSpawnPlayer.java +++ b/src/main/java/de/bixilon/minosoft/protocol/packets/clientbound/play/PacketSpawnPlayer.java @@ -51,7 +51,7 @@ public class PacketSpawnPlayer implements ClientboundPacket { } Location location; if (buffer.getVersionId() < 100) { - location = new Location(buffer.readFixedPointNumberInteger(), buffer.readFixedPointNumberInteger(), buffer.readFixedPointNumberInteger()); + location = new Location(buffer.readFixedPointNumberInt(), buffer.readFixedPointNumberInt(), buffer.readFixedPointNumberInt()); } else { location = buffer.readLocation(); } diff --git a/src/main/java/de/bixilon/minosoft/protocol/packets/clientbound/play/PacketSpawnWeatherEntity.java b/src/main/java/de/bixilon/minosoft/protocol/packets/clientbound/play/PacketSpawnWeatherEntity.java index 5ae39e492..0b5a7a86c 100644 --- a/src/main/java/de/bixilon/minosoft/protocol/packets/clientbound/play/PacketSpawnWeatherEntity.java +++ b/src/main/java/de/bixilon/minosoft/protocol/packets/clientbound/play/PacketSpawnWeatherEntity.java @@ -29,7 +29,7 @@ public class PacketSpawnWeatherEntity implements ClientboundPacket { byte type = buffer.readByte(); Location location; if (buffer.getVersionId() < 100) { - location = new Location(buffer.readFixedPointNumberInteger(), buffer.readFixedPointNumberInteger(), buffer.readFixedPointNumberInteger()); + location = new Location(buffer.readFixedPointNumberInt(), buffer.readFixedPointNumberInt(), buffer.readFixedPointNumberInt()); } else { location = buffer.readLocation(); } diff --git a/src/main/java/de/bixilon/minosoft/protocol/packets/clientbound/play/PacketTags.java b/src/main/java/de/bixilon/minosoft/protocol/packets/clientbound/play/PacketTags.java index 0efa36871..549941432 100644 --- a/src/main/java/de/bixilon/minosoft/protocol/packets/clientbound/play/PacketTags.java +++ b/src/main/java/de/bixilon/minosoft/protocol/packets/clientbound/play/PacketTags.java @@ -39,7 +39,7 @@ public class PacketTags implements ClientboundPacket { private Tag[] readTags(InByteBuffer buffer) { Tag[] ret = new Tag[buffer.readVarInt()]; for (int i = 0; i < ret.length; i++) { - ret[i] = new Tag(buffer.readString(), buffer.readVarIntArray(buffer.readVarInt())); + ret[i] = new Tag(buffer.readString(), buffer.readVarIntArray()); } return ret; } diff --git a/src/main/java/de/bixilon/minosoft/protocol/packets/serverbound/play/PacketInteractEntity.java b/src/main/java/de/bixilon/minosoft/protocol/packets/serverbound/play/PacketInteractEntity.java index 541ffe438..4b97c5140 100644 --- a/src/main/java/de/bixilon/minosoft/protocol/packets/serverbound/play/PacketInteractEntity.java +++ b/src/main/java/de/bixilon/minosoft/protocol/packets/serverbound/play/PacketInteractEntity.java @@ -73,9 +73,9 @@ public class PacketInteractEntity implements ServerboundPacket { if (buffer.getVersionId() >= 33) { if (click == EntityInteractionClicks.INTERACT_AT) { // position - buffer.writeFloat((float) location.getX()); - buffer.writeFloat((float) location.getY()); - buffer.writeFloat((float) location.getZ()); + buffer.writeFloat((float) location.x()); + buffer.writeFloat((float) location.y()); + buffer.writeFloat((float) location.z()); } if (click == EntityInteractionClicks.INTERACT_AT || click == EntityInteractionClicks.INTERACT) { diff --git a/src/main/java/de/bixilon/minosoft/protocol/protocol/InByteBuffer.java b/src/main/java/de/bixilon/minosoft/protocol/protocol/InByteBuffer.java index 1d140167e..3364652b0 100644 --- a/src/main/java/de/bixilon/minosoft/protocol/protocol/InByteBuffer.java +++ b/src/main/java/de/bixilon/minosoft/protocol/protocol/InByteBuffer.java @@ -38,7 +38,6 @@ import de.bixilon.minosoft.util.Util; import de.bixilon.minosoft.util.nbt.tag.*; import java.io.IOException; -import java.nio.ByteBuffer; import java.nio.charset.StandardCharsets; import java.util.Base64; import java.util.UUID; @@ -73,9 +72,7 @@ public class InByteBuffer { } public short readShort() { - ByteBuffer buffer = ByteBuffer.allocate(Short.BYTES); - buffer.put(readBytes(Short.BYTES)); - return buffer.getShort(0); + return (short) (((readUnsignedByte()) << 8) | (readUnsignedByte())); } public int readUnsignedShort() { @@ -83,9 +80,7 @@ public class InByteBuffer { } public int readInt() { - ByteBuffer buffer = ByteBuffer.allocate(Integer.BYTES); - buffer.put(readBytes(Integer.BYTES)); - return buffer.getInt(0); + return ((readUnsignedByte() << 24) | (readUnsignedByte() << 16) | (readUnsignedByte() << 8) | (readUnsignedByte())); } public byte[] readBytes(int count) { @@ -95,13 +90,11 @@ public class InByteBuffer { return ret; } - public Long readLong() { - ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES); - buffer.put(readBytes(Long.BYTES)); - return buffer.getLong(0); + public long readLong() { + return (((long) readUnsignedByte() << 56) | ((long) readUnsignedByte() << 48) | ((long) readUnsignedByte() << 40) | ((long) readUnsignedByte() << 32) | ((long) readUnsignedByte() << 24) | (readUnsignedByte() << 16) | (readUnsignedByte() << 8) | (readUnsignedByte())); } - public double readFixedPointNumberInteger() { + public double readFixedPointNumberInt() { return readInt() / 32.0D; } @@ -135,8 +128,7 @@ public class InByteBuffer { public int[] readUnsignedLEShorts(int num) { int[] ret = new int[num]; for (int i = 0; i < ret.length; i++) { - ret[i] = (short) (readByte() & 0xFF); - ret[i] |= (readByte() & 0xFF) << 8; + ret[i] = ((readUnsignedByte()) | (readUnsignedByte() << 8)); } return ret; } @@ -144,7 +136,7 @@ public class InByteBuffer { public String[] readStringArray(int length) { String[] ret = new String[length]; for (int i = 0; i < length; i++) { - ret[i] = new String(readBytes(readVarInt()), StandardCharsets.UTF_8); + ret[i] = readString(); } return ret; } @@ -194,11 +186,11 @@ public class InByteBuffer { long raw = readLong(); int x = (int) (raw >> 38); if (versionId < 440) { - short y = (short) ((raw >> 26) & 0xFFF); + int y = (int) ((raw >> 26) & 0xFFF); int z = (int) (raw & 0x3FFFFFF); return new BlockPosition(x, y, z); } - short y = (short) (raw & 0xFFF); + int y = (int) (raw & 0xFFF); int z = (int) (raw << 26 >> 38); return new BlockPosition(x, y, z); } @@ -341,34 +333,30 @@ public class InByteBuffer { return new Location(readDouble(), readDouble(), readDouble()); } - public Double readDouble() { - ByteBuffer buffer = ByteBuffer.allocate(Double.BYTES); - buffer.put(readBytes(Double.BYTES)); - return buffer.getDouble(0); + public double readDouble() { + return Double.longBitsToDouble(readLong()); } public Location readSmallLocation() { return new Location(readFloat(), readFloat(), readFloat()); } - public Float readFloat() { - ByteBuffer buffer = ByteBuffer.allocate(Float.BYTES); - buffer.put(readBytes(Float.BYTES)); - return buffer.getFloat(0); + public float readFloat() { + return Float.intBitsToFloat(readInt()); } public BlockPosition readBlockPosition() { return new BlockPosition(readInt(), readUnsignedByte(), readInt()); } - public BlockPosition readBlockPositionInteger() { - return new BlockPosition(readInt(), (short) (readInt()), readInt()); - } - public BlockPosition readBlockPositionShort() { return new BlockPosition(readInt(), readShort(), readInt()); } + public BlockPosition readBlockPositionInteger() { + return new BlockPosition(readInt(), readInt(), readInt()); + } + public byte[] readBytesLeft() { return readBytes(getBytesLeft()); } @@ -398,26 +386,25 @@ public class InByteBuffer { EntityMetaData.MetaDataHashMap sets = metaData.getSets(); if (versionId < 48) { - byte item = readByte(); + short item = readUnsignedByte(); while (item != 0x7F) { byte index = (byte) (item & 0x1F); EntityMetaData.EntityMetaDataValueTypes type = EntityMetaData.EntityMetaDataValueTypes.byId((item & 0xFF) >> 5, versionId); sets.put((int) index, EntityMetaData.getData(type, this)); item = readByte(); } - } else if (versionId < 107) { - byte index = readByte(); - while (index != (byte) 0xFF) { - EntityMetaData.EntityMetaDataValueTypes type = EntityMetaData.EntityMetaDataValueTypes.byId(readByte(), versionId); - sets.put((int) index, EntityMetaData.getData(type, this)); - index = readByte(); - } } else { - byte index = readByte(); - while (index != (byte) 0xFF) { - EntityMetaData.EntityMetaDataValueTypes type = EntityMetaData.EntityMetaDataValueTypes.byId(readVarInt(), versionId); - sets.put((int) index, EntityMetaData.getData(type, this)); - index = readByte(); + int index = readUnsignedByte(); + while (index != 0xFF) { + int id; + if (versionId < 107) { + id = readUnsignedByte(); + } else { + id = readVarInt(); + } + EntityMetaData.EntityMetaDataValueTypes type = EntityMetaData.EntityMetaDataValueTypes.byId(id, versionId); + sets.put(index, EntityMetaData.getData(type, this)); + index = readUnsignedByte(); } } return metaData; @@ -445,7 +432,7 @@ public class InByteBuffer { } public Ingredient readIngredient() { - return new Ingredient(readSlotArray(readVarInt())); + return new Ingredient(readSlotArray()); } public Ingredient[] readIngredientArray(int length) { @@ -456,6 +443,10 @@ public class InByteBuffer { return ret; } + public Ingredient[] readIngredientArray() { + return readIngredientArray(readVarInt()); + } + public Slot[] readSlotArray(int length) { Slot[] res = new Slot[length]; for (int i = 0; i < length; i++) { @@ -464,6 +455,10 @@ public class InByteBuffer { return res; } + public Slot[] readSlotArray() { + return readSlotArray(readVarInt()); + } + public Connection getConnection() { return connection; } diff --git a/src/main/java/de/bixilon/minosoft/protocol/protocol/OutByteBuffer.java b/src/main/java/de/bixilon/minosoft/protocol/protocol/OutByteBuffer.java index cbe37b255..1dce38ff7 100644 --- a/src/main/java/de/bixilon/minosoft/protocol/protocol/OutByteBuffer.java +++ b/src/main/java/de/bixilon/minosoft/protocol/protocol/OutByteBuffer.java @@ -13,6 +13,7 @@ package de.bixilon.minosoft.protocol.protocol; +import com.google.gson.Gson; import com.google.gson.JsonObject; import de.bixilon.minosoft.data.inventory.Slot; import de.bixilon.minosoft.data.text.ChatComponent; @@ -20,7 +21,6 @@ import de.bixilon.minosoft.data.world.BlockPosition; import de.bixilon.minosoft.protocol.network.Connection; import de.bixilon.minosoft.util.nbt.tag.CompoundTag; -import java.nio.ByteBuffer; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.UUID; @@ -52,36 +52,41 @@ public class OutByteBuffer { writeBytes(data); } - public void writeShort(short s) { - ByteBuffer buffer = ByteBuffer.allocate(Short.BYTES); - buffer.putShort(s); - writeBytes(buffer.array()); + public void writeShort(short value) { + writeByte(value >>> 8); + writeByte(value); } - public void writeInt(int i) { - ByteBuffer buffer = ByteBuffer.allocate(Integer.BYTES); - buffer.putInt(i); - writeBytes(buffer.array()); + public void writeInt(int value) { + writeByte(value >>> 24); + writeByte(value >>> 16); + writeByte(value >>> 8); + writeByte(value); } - public void writeBytes(byte[] b) { - for (byte value : b) { - bytes.add(value); + public void writeBytes(byte[] data) { + for (byte singleByte : data) { + bytes.add(singleByte); } } - public void writeLong(Long l) { - ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES); - buffer.putLong(l); - writeBytes(buffer.array()); + public void writeLong(long value) { + writeByte(value >>> 56); + writeByte(value >>> 48); + writeByte(value >>> 40); + writeByte(value >>> 32); + writeByte(value >>> 24); + writeByte(value >>> 16); + writeByte(value >>> 8); + writeByte(value); } public void writeChatComponent(ChatComponent chatComponent) { - writeString(chatComponent.getMessage()); // ToDo: test if this should not be json + writeString(chatComponent.getLegacyText()); // ToDo: test if this should not be json } - public void writeJSON(JsonObject j) { - writeString(j.toString()); + public void writeJSON(JsonObject json) { + writeString(new Gson().toJson(json)); } public void writeString(String string) { @@ -103,29 +108,29 @@ public class OutByteBuffer { } while (value != 0); } - public void writeByte(byte b) { - bytes.add(b); + public void writeByte(byte value) { + bytes.add(value); } - public void writeFloat(Float f) { - ByteBuffer buffer = ByteBuffer.allocate(Float.BYTES); - buffer.putFloat(f); - for (byte b : buffer.array()) { - bytes.add(b); - } + public void writeByte(int value) { + bytes.add((byte) (value & 0xFF)); } - public void writeDouble(Double d) { - ByteBuffer buffer = ByteBuffer.allocate(Double.BYTES); - buffer.putDouble(d); - writeBytes(buffer.array()); + public void writeByte(long value) { + bytes.add((byte) (value & 0xFF)); } - public void writeUUID(UUID u) { - ByteBuffer buffer = ByteBuffer.allocate(16); // UUID.BYTES - buffer.putLong(u.getMostSignificantBits()); - buffer.putLong(u.getLeastSignificantBits()); - writeBytes(buffer.array()); + public void writeFloat(float value) { + writeInt(Float.floatToIntBits(value)); + } + + public void writeDouble(double value) { + writeLong(Double.doubleToLongBits(value)); + } + + public void writeUUID(UUID uuid) { + writeLong(uuid.getMostSignificantBits()); + writeLong(uuid.getLeastSignificantBits()); } public void writeFixedPointNumberInt(double d) { @@ -142,16 +147,15 @@ public class OutByteBuffer { return; } if (versionId < 440) { - writeLong((((long) position.getX() & 0x3FFFFFF) << 38) | (((long) position.getZ() & 0x3FFFFFF)) | ((long) position.getY() & 0xFFF) << 26); + writeLong((((long) position.x() & 0x3FFFFFF) << 38) | (((long) position.z() & 0x3FFFFFF)) | ((long) position.y() & 0xFFF) << 26); return; } - writeLong((((long) (position.getX() & 0x3FFFFFF) << 38) | ((long) (position.getZ() & 0x3FFFFFF) << 12) | (long) (position.getY() & 0xFFF))); + writeLong((((long) (position.x() & 0x3FFFFFF) << 38) | ((long) (position.z() & 0x3FFFFFF) << 12) | (long) (position.y() & 0xFFF))); } public void writeVarInt(int value) { // thanks https://wiki.vg/Protocol#VarInt_and_VarLong - do - { + do { byte temp = (byte) (value & 0x7F); value >>>= 7; if (value != 0) { @@ -164,8 +168,7 @@ public class OutByteBuffer { public void prefixVarInt(int value) { int count = 0; // thanks https://wiki.vg/Protocol#VarInt_and_VarLong - do - { + do { byte temp = (byte) (value & 0x7F); value >>>= 7; if (value != 0) { @@ -200,8 +203,8 @@ public class OutByteBuffer { nbt.writeBytes(this); } - public void writeBoolean(boolean b) { - bytes.add((byte) ((b) ? 0x01 : 0x00)); + public void writeBoolean(boolean value) { + bytes.add((byte) ((value) ? 0x01 : 0x00)); } public void writeStringNoLength(String string) { @@ -209,12 +212,12 @@ public class OutByteBuffer { } public void writeBlockPositionByte(BlockPosition pos) { - writeInt(pos.getX()); - writeByte((byte) pos.getY()); - writeInt(pos.getZ()); + writeInt(pos.x()); + writeByte((byte) pos.y()); + writeInt(pos.z()); } - public byte[] getOutBytes() { + public byte[] toByteArray() { byte[] ret = new byte[bytes.size()]; for (int i = 0; i < bytes.size(); i++) { ret[i] = bytes.get(i); diff --git a/src/main/java/de/bixilon/minosoft/protocol/protocol/OutPacketBuffer.java b/src/main/java/de/bixilon/minosoft/protocol/protocol/OutPacketBuffer.java index ace429eaf..095d5f1b8 100644 --- a/src/main/java/de/bixilon/minosoft/protocol/protocol/OutPacketBuffer.java +++ b/src/main/java/de/bixilon/minosoft/protocol/protocol/OutPacketBuffer.java @@ -24,10 +24,10 @@ public class OutPacketBuffer extends OutByteBuffer { } @Override - public byte[] getOutBytes() { + public byte[] toByteArray() { OutByteBuffer ret = new OutByteBuffer(this); ret.prefixVarInt(getCommand()); - return ret.getOutBytes(); + return ret.toByteArray(); } public int getCommand() { diff --git a/src/main/java/de/bixilon/minosoft/protocol/protocol/PacketHandler.java b/src/main/java/de/bixilon/minosoft/protocol/protocol/PacketHandler.java index 10c7b24cb..1553a1b7e 100644 --- a/src/main/java/de/bixilon/minosoft/protocol/protocol/PacketHandler.java +++ b/src/main/java/de/bixilon/minosoft/protocol/protocol/PacketHandler.java @@ -284,14 +284,14 @@ public class PacketHandler { Log.game(switch (pkg.getReason()) { case START_RAINING -> "Received weather packet: Starting rain..."; case STOP_RAINING -> "Received weather packet: Stopping rain..."; - case CHANGE_GAMEMODE -> String.format("Received game mode change: Now in %s", GameModes.byId(pkg.getValue().intValue())); + case CHANGE_GAMEMODE -> String.format("Received game mode change: Now in %s", GameModes.byId(pkg.getIntValue())); default -> ""; }); switch (pkg.getReason()) { case STOP_RAINING -> connection.getPlayer().getWorld().setRaining(false); case START_RAINING -> connection.getPlayer().getWorld().setRaining(true); - case CHANGE_GAMEMODE -> connection.getPlayer().setGameMode(GameModes.byId(pkg.getValue().intValue())); + case CHANGE_GAMEMODE -> connection.getPlayer().setGameMode(GameModes.byId(pkg.getIntValue())); } } diff --git a/src/main/java/de/bixilon/minosoft/protocol/protocol/PacketSender.java b/src/main/java/de/bixilon/minosoft/protocol/protocol/PacketSender.java index 0eb94b77b..7437d09b5 100644 --- a/src/main/java/de/bixilon/minosoft/protocol/protocol/PacketSender.java +++ b/src/main/java/de/bixilon/minosoft/protocol/protocol/PacketSender.java @@ -96,11 +96,11 @@ public class PacketSender { } public void sendPluginMessageData(String channel, OutByteBuffer toSend) { - connection.sendPacket(new PacketPluginMessageSending(channel, toSend.getOutBytes())); + connection.sendPacket(new PacketPluginMessageSending(channel, toSend.toByteArray())); } public void sendLoginPluginMessageResponse(int messageId, OutByteBuffer toSend) { - connection.sendPacket(new PacketLoginPluginResponse(messageId, toSend.getOutBytes())); + connection.sendPacket(new PacketLoginPluginResponse(messageId, toSend.toByteArray())); } public void setLocation(Location location, EntityRotation rotation, boolean onGround) {