Improve byte buffers

This commit is contained in:
Bixilon 2020-12-09 16:37:17 +01:00
parent abcbd3fa28
commit 4c8377a1ae
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
22 changed files with 124 additions and 121 deletions

View File

@ -22,7 +22,7 @@ import javax.annotation.Nullable;
public abstract class CommandParser { public abstract class CommandParser {
@Deprecated @Deprecated
private static final DummyParser dummyParser = new DummyParser(); private static final DummyParser DUMMY_PARSER = new DummyParser();
public static HashBiMap<ModIdentifier, CommandParser> COMMAND_PARSERS = HashBiMap.create(); public static HashBiMap<ModIdentifier, CommandParser> COMMAND_PARSERS = HashBiMap.create();
static { static {
@ -37,7 +37,7 @@ public abstract class CommandParser {
} }
public static CommandParser createInstance(ModIdentifier identifier) { public static CommandParser createInstance(ModIdentifier identifier) {
return COMMAND_PARSERS.getOrDefault(identifier, dummyParser); return COMMAND_PARSERS.getOrDefault(identifier, DUMMY_PARSER);
} }
@Nullable @Nullable

View File

@ -46,7 +46,8 @@ public abstract class ChatComponent {
return new BaseComponent((String) raw); return new BaseComponent((String) raw);
} }
} else { } 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); return new BaseComponent(parent, json);
} }

View File

@ -32,7 +32,7 @@ public class ChangeGameStateEvent extends CancelableEvent {
public ChangeGameStateEvent(Connection connection, PacketChangeGameState pkg) { public ChangeGameStateEvent(Connection connection, PacketChangeGameState pkg) {
super(connection); super(connection);
this.reason = pkg.getReason(); this.reason = pkg.getReason();
this.value = pkg.getValue(); this.value = pkg.getFloatValue();
} }
public PacketChangeGameState.Reason getReason() { public PacketChangeGameState.Reason getReason() {

View File

@ -96,7 +96,7 @@ public class SocketNetwork implements Network {
ServerboundPacket packet = queue.take(); ServerboundPacket packet = queue.take();
packet.log(); packet.log();
queue.remove(packet); queue.remove(packet);
byte[] data = packet.write(connection).getOutBytes(); byte[] data = packet.write(connection).toByteArray();
if (compressionThreshold >= 0) { if (compressionThreshold >= 0) {
// compression is enabled // compression is enabled
// check if there is a need to compress it and if so, do it! // 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); byte[] compressed = Util.compress(data);
lengthPrefixedBuffer.writeVarInt(data.length); // uncompressed length lengthPrefixedBuffer.writeVarInt(data.length); // uncompressed length
lengthPrefixedBuffer.writeBytes(compressed); lengthPrefixedBuffer.writeBytes(compressed);
outRawBuffer.prefixVarInt(lengthPrefixedBuffer.getOutBytes().length); // length of total data is uncompressed length + compressed data outRawBuffer.prefixVarInt(lengthPrefixedBuffer.toByteArray().length); // length of total data is uncompressed length + compressed data
outRawBuffer.writeBytes(lengthPrefixedBuffer.getOutBytes()); // write all bytes outRawBuffer.writeBytes(lengthPrefixedBuffer.toByteArray()); // write all bytes
} else { } else {
outRawBuffer.writeVarInt(data.length + 1); // 1 for the compressed length (0) outRawBuffer.writeVarInt(data.length + 1); // 1 for the compressed length (0)
outRawBuffer.writeVarInt(0); // data is uncompressed, compressed size is 0 outRawBuffer.writeVarInt(0); // data is uncompressed, compressed size is 0
outRawBuffer.writeBytes(data); outRawBuffer.writeBytes(data);
} }
data = outRawBuffer.getOutBytes(); data = outRawBuffer.toByteArray();
} else { } else {
// append packet length // append packet length
OutByteBuffer bufferWithLengthPrefix = new OutByteBuffer(connection); OutByteBuffer bufferWithLengthPrefix = new OutByteBuffer(connection);
bufferWithLengthPrefix.writeVarInt(data.length); bufferWithLengthPrefix.writeVarInt(data.length);
bufferWithLengthPrefix.writeBytes(data); bufferWithLengthPrefix.writeBytes(data);
data = bufferWithLengthPrefix.getOutBytes(); data = bufferWithLengthPrefix.toByteArray();
} }
outputStream.write(data); outputStream.write(data);

View File

@ -45,10 +45,14 @@ public class PacketChangeGameState implements ClientboundPacket {
return reason; return reason;
} }
public Float getValue() { public float getFloatValue() {
return value; return value;
} }
public int getIntValue() {
return (int) value;
}
public enum Reason { public enum Reason {
NO_RESPAWN_BLOCK_AVAILABLE(new MapSet[]{new MapSet<>(0, 0)}), 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? START_RAINING(new MapSet[]{new MapSet<>(0, 1), new MapSet<>(498, 2), new MapSet<>(578, 1)}), // ToDo: when exactly did these 2 switch?

View File

@ -89,7 +89,7 @@ public class PacketChunkData implements ClientboundPacket {
} }
if (groundUpContinuous) { if (groundUpContinuous) {
if (buffer.getVersionId() >= 740) { if (buffer.getVersionId() >= 740) {
biomes = buffer.readVarIntArray(buffer.readVarInt()); biomes = buffer.readVarIntArray();
} else if (buffer.getVersionId() >= 552) { } else if (buffer.getVersionId() >= 552) {
biomes = buffer.readIntArray(1024); biomes = buffer.readIntArray(1024);
} }

View File

@ -45,7 +45,7 @@ public class PacketDeclareRecipes implements ClientboundPacket {
switch (type) { switch (type) {
case SHAPELESS -> { case SHAPELESS -> {
String group = buffer.readString(); String group = buffer.readString();
Ingredient[] ingredients = buffer.readIngredientArray(buffer.readVarInt()); Ingredient[] ingredients = buffer.readIngredientArray();
Slot result = buffer.readSlot(); Slot result = buffer.readSlot();
recipe = new Recipe(type, group, ingredients, result); recipe = new Recipe(type, group, ingredients, result);
} }

View File

@ -31,7 +31,7 @@ public class PacketEntityTeleport implements ClientboundPacket {
this.entityId = buffer.readEntityId(); this.entityId = buffer.readEntityId();
if (buffer.getVersionId() < 100) { 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 { } else {
this.location = buffer.readLocation(); this.location = buffer.readLocation();
} }

View File

@ -47,7 +47,7 @@ public class PacketNamedSoundEffect implements ClientboundPacket {
category = SoundCategories.byId(buffer.readVarInt()); category = SoundCategories.byId(buffer.readVarInt());
} }
if (buffer.getVersionId() >= 95) { 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(); volume = buffer.readFloat();
if (buffer.getVersionId() < 201) { if (buffer.getVersionId() < 201) {

View File

@ -42,7 +42,7 @@ public class PacketSoundEffect implements ClientboundPacket {
if (buffer.getVersionId() >= 95 && (buffer.getVersionId() < 321 || buffer.getVersionId() >= 326)) { if (buffer.getVersionId() >= 95 && (buffer.getVersionId() < 321 || buffer.getVersionId() >= 326)) {
category = SoundCategories.byId(buffer.readVarInt()); 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(); volume = buffer.readFloat();
if (buffer.getVersionId() < 201) { if (buffer.getVersionId() < 201) {
pitch = (buffer.readByte() * pitchCalc) / 100F; pitch = (buffer.readByte() * pitchCalc) / 100F;

View File

@ -28,7 +28,7 @@ public class PacketSpawnExperienceOrb implements ClientboundPacket {
int entityId = buffer.readVarInt(); int entityId = buffer.readVarInt();
Location location; Location location;
if (buffer.getVersionId() < 100) { if (buffer.getVersionId() < 100) {
location = new Location(buffer.readFixedPointNumberInteger(), buffer.readFixedPointNumberInteger(), buffer.readFixedPointNumberInteger()); location = new Location(buffer.readFixedPointNumberInt(), buffer.readFixedPointNumberInt(), buffer.readFixedPointNumberInt());
} else { } else {
location = buffer.readLocation(); location = buffer.readLocation();
} }

View File

@ -49,7 +49,7 @@ public class PacketSpawnMob implements ClientboundPacket {
Class<? extends Entity> typeClass = buffer.getConnection().getMapping().getEntityClassById(type); Class<? extends Entity> typeClass = buffer.getConnection().getMapping().getEntityClassById(type);
Location location; Location location;
if (buffer.getVersionId() < 100) { if (buffer.getVersionId() < 100) {
location = new Location(buffer.readFixedPointNumberInteger(), buffer.readFixedPointNumberInteger(), buffer.readFixedPointNumberInteger()); location = new Location(buffer.readFixedPointNumberInt(), buffer.readFixedPointNumberInt(), buffer.readFixedPointNumberInt());
} else { } else {
location = buffer.readLocation(); location = buffer.readLocation();
} }

View File

@ -56,7 +56,7 @@ public class PacketSpawnObject implements ClientboundPacket {
Location location; Location location;
if (buffer.getVersionId() < 100) { if (buffer.getVersionId() < 100) {
location = new Location(buffer.readFixedPointNumberInteger(), buffer.readFixedPointNumberInteger(), buffer.readFixedPointNumberInteger()); location = new Location(buffer.readFixedPointNumberInt(), buffer.readFixedPointNumberInt(), buffer.readFixedPointNumberInt());
} else { } else {
location = buffer.readLocation(); location = buffer.readLocation();
} }

View File

@ -51,7 +51,7 @@ public class PacketSpawnPlayer implements ClientboundPacket {
} }
Location location; Location location;
if (buffer.getVersionId() < 100) { if (buffer.getVersionId() < 100) {
location = new Location(buffer.readFixedPointNumberInteger(), buffer.readFixedPointNumberInteger(), buffer.readFixedPointNumberInteger()); location = new Location(buffer.readFixedPointNumberInt(), buffer.readFixedPointNumberInt(), buffer.readFixedPointNumberInt());
} else { } else {
location = buffer.readLocation(); location = buffer.readLocation();
} }

View File

@ -29,7 +29,7 @@ public class PacketSpawnWeatherEntity implements ClientboundPacket {
byte type = buffer.readByte(); byte type = buffer.readByte();
Location location; Location location;
if (buffer.getVersionId() < 100) { if (buffer.getVersionId() < 100) {
location = new Location(buffer.readFixedPointNumberInteger(), buffer.readFixedPointNumberInteger(), buffer.readFixedPointNumberInteger()); location = new Location(buffer.readFixedPointNumberInt(), buffer.readFixedPointNumberInt(), buffer.readFixedPointNumberInt());
} else { } else {
location = buffer.readLocation(); location = buffer.readLocation();
} }

View File

@ -39,7 +39,7 @@ public class PacketTags implements ClientboundPacket {
private Tag[] readTags(InByteBuffer buffer) { private Tag[] readTags(InByteBuffer buffer) {
Tag[] ret = new Tag[buffer.readVarInt()]; Tag[] ret = new Tag[buffer.readVarInt()];
for (int i = 0; i < ret.length; i++) { 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; return ret;
} }

View File

@ -73,9 +73,9 @@ public class PacketInteractEntity implements ServerboundPacket {
if (buffer.getVersionId() >= 33) { if (buffer.getVersionId() >= 33) {
if (click == EntityInteractionClicks.INTERACT_AT) { if (click == EntityInteractionClicks.INTERACT_AT) {
// position // position
buffer.writeFloat((float) location.getX()); buffer.writeFloat((float) location.x());
buffer.writeFloat((float) location.getY()); buffer.writeFloat((float) location.y());
buffer.writeFloat((float) location.getZ()); buffer.writeFloat((float) location.z());
} }
if (click == EntityInteractionClicks.INTERACT_AT || click == EntityInteractionClicks.INTERACT) { if (click == EntityInteractionClicks.INTERACT_AT || click == EntityInteractionClicks.INTERACT) {

View File

@ -38,7 +38,6 @@ import de.bixilon.minosoft.util.Util;
import de.bixilon.minosoft.util.nbt.tag.*; import de.bixilon.minosoft.util.nbt.tag.*;
import java.io.IOException; import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.Base64; import java.util.Base64;
import java.util.UUID; import java.util.UUID;
@ -73,9 +72,7 @@ public class InByteBuffer {
} }
public short readShort() { public short readShort() {
ByteBuffer buffer = ByteBuffer.allocate(Short.BYTES); return (short) (((readUnsignedByte()) << 8) | (readUnsignedByte()));
buffer.put(readBytes(Short.BYTES));
return buffer.getShort(0);
} }
public int readUnsignedShort() { public int readUnsignedShort() {
@ -83,9 +80,7 @@ public class InByteBuffer {
} }
public int readInt() { public int readInt() {
ByteBuffer buffer = ByteBuffer.allocate(Integer.BYTES); return ((readUnsignedByte() << 24) | (readUnsignedByte() << 16) | (readUnsignedByte() << 8) | (readUnsignedByte()));
buffer.put(readBytes(Integer.BYTES));
return buffer.getInt(0);
} }
public byte[] readBytes(int count) { public byte[] readBytes(int count) {
@ -95,13 +90,11 @@ public class InByteBuffer {
return ret; return ret;
} }
public Long readLong() { public long readLong() {
ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES); return (((long) readUnsignedByte() << 56) | ((long) readUnsignedByte() << 48) | ((long) readUnsignedByte() << 40) | ((long) readUnsignedByte() << 32) | ((long) readUnsignedByte() << 24) | (readUnsignedByte() << 16) | (readUnsignedByte() << 8) | (readUnsignedByte()));
buffer.put(readBytes(Long.BYTES));
return buffer.getLong(0);
} }
public double readFixedPointNumberInteger() { public double readFixedPointNumberInt() {
return readInt() / 32.0D; return readInt() / 32.0D;
} }
@ -135,8 +128,7 @@ public class InByteBuffer {
public int[] readUnsignedLEShorts(int num) { public int[] readUnsignedLEShorts(int num) {
int[] ret = new int[num]; int[] ret = new int[num];
for (int i = 0; i < ret.length; i++) { for (int i = 0; i < ret.length; i++) {
ret[i] = (short) (readByte() & 0xFF); ret[i] = ((readUnsignedByte()) | (readUnsignedByte() << 8));
ret[i] |= (readByte() & 0xFF) << 8;
} }
return ret; return ret;
} }
@ -144,7 +136,7 @@ public class InByteBuffer {
public String[] readStringArray(int length) { public String[] readStringArray(int length) {
String[] ret = new String[length]; String[] ret = new String[length];
for (int i = 0; i < length; i++) { for (int i = 0; i < length; i++) {
ret[i] = new String(readBytes(readVarInt()), StandardCharsets.UTF_8); ret[i] = readString();
} }
return ret; return ret;
} }
@ -194,11 +186,11 @@ public class InByteBuffer {
long raw = readLong(); long raw = readLong();
int x = (int) (raw >> 38); int x = (int) (raw >> 38);
if (versionId < 440) { if (versionId < 440) {
short y = (short) ((raw >> 26) & 0xFFF); int y = (int) ((raw >> 26) & 0xFFF);
int z = (int) (raw & 0x3FFFFFF); int z = (int) (raw & 0x3FFFFFF);
return new BlockPosition(x, y, z); return new BlockPosition(x, y, z);
} }
short y = (short) (raw & 0xFFF); int y = (int) (raw & 0xFFF);
int z = (int) (raw << 26 >> 38); int z = (int) (raw << 26 >> 38);
return new BlockPosition(x, y, z); return new BlockPosition(x, y, z);
} }
@ -341,34 +333,30 @@ public class InByteBuffer {
return new Location(readDouble(), readDouble(), readDouble()); return new Location(readDouble(), readDouble(), readDouble());
} }
public Double readDouble() { public double readDouble() {
ByteBuffer buffer = ByteBuffer.allocate(Double.BYTES); return Double.longBitsToDouble(readLong());
buffer.put(readBytes(Double.BYTES));
return buffer.getDouble(0);
} }
public Location readSmallLocation() { public Location readSmallLocation() {
return new Location(readFloat(), readFloat(), readFloat()); return new Location(readFloat(), readFloat(), readFloat());
} }
public Float readFloat() { public float readFloat() {
ByteBuffer buffer = ByteBuffer.allocate(Float.BYTES); return Float.intBitsToFloat(readInt());
buffer.put(readBytes(Float.BYTES));
return buffer.getFloat(0);
} }
public BlockPosition readBlockPosition() { public BlockPosition readBlockPosition() {
return new BlockPosition(readInt(), readUnsignedByte(), readInt()); return new BlockPosition(readInt(), readUnsignedByte(), readInt());
} }
public BlockPosition readBlockPositionInteger() {
return new BlockPosition(readInt(), (short) (readInt()), readInt());
}
public BlockPosition readBlockPositionShort() { public BlockPosition readBlockPositionShort() {
return new BlockPosition(readInt(), readShort(), readInt()); return new BlockPosition(readInt(), readShort(), readInt());
} }
public BlockPosition readBlockPositionInteger() {
return new BlockPosition(readInt(), readInt(), readInt());
}
public byte[] readBytesLeft() { public byte[] readBytesLeft() {
return readBytes(getBytesLeft()); return readBytes(getBytesLeft());
} }
@ -398,26 +386,25 @@ public class InByteBuffer {
EntityMetaData.MetaDataHashMap sets = metaData.getSets(); EntityMetaData.MetaDataHashMap sets = metaData.getSets();
if (versionId < 48) { if (versionId < 48) {
byte item = readByte(); short item = readUnsignedByte();
while (item != 0x7F) { while (item != 0x7F) {
byte index = (byte) (item & 0x1F); byte index = (byte) (item & 0x1F);
EntityMetaData.EntityMetaDataValueTypes type = EntityMetaData.EntityMetaDataValueTypes.byId((item & 0xFF) >> 5, versionId); EntityMetaData.EntityMetaDataValueTypes type = EntityMetaData.EntityMetaDataValueTypes.byId((item & 0xFF) >> 5, versionId);
sets.put((int) index, EntityMetaData.getData(type, this)); sets.put((int) index, EntityMetaData.getData(type, this));
item = readByte(); 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 { } else {
byte index = readByte(); int index = readUnsignedByte();
while (index != (byte) 0xFF) { while (index != 0xFF) {
EntityMetaData.EntityMetaDataValueTypes type = EntityMetaData.EntityMetaDataValueTypes.byId(readVarInt(), versionId); int id;
sets.put((int) index, EntityMetaData.getData(type, this)); if (versionId < 107) {
index = readByte(); id = readUnsignedByte();
} else {
id = readVarInt();
}
EntityMetaData.EntityMetaDataValueTypes type = EntityMetaData.EntityMetaDataValueTypes.byId(id, versionId);
sets.put(index, EntityMetaData.getData(type, this));
index = readUnsignedByte();
} }
} }
return metaData; return metaData;
@ -445,7 +432,7 @@ public class InByteBuffer {
} }
public Ingredient readIngredient() { public Ingredient readIngredient() {
return new Ingredient(readSlotArray(readVarInt())); return new Ingredient(readSlotArray());
} }
public Ingredient[] readIngredientArray(int length) { public Ingredient[] readIngredientArray(int length) {
@ -456,6 +443,10 @@ public class InByteBuffer {
return ret; return ret;
} }
public Ingredient[] readIngredientArray() {
return readIngredientArray(readVarInt());
}
public Slot[] readSlotArray(int length) { public Slot[] readSlotArray(int length) {
Slot[] res = new Slot[length]; Slot[] res = new Slot[length];
for (int i = 0; i < length; i++) { for (int i = 0; i < length; i++) {
@ -464,6 +455,10 @@ public class InByteBuffer {
return res; return res;
} }
public Slot[] readSlotArray() {
return readSlotArray(readVarInt());
}
public Connection getConnection() { public Connection getConnection() {
return connection; return connection;
} }

View File

@ -13,6 +13,7 @@
package de.bixilon.minosoft.protocol.protocol; package de.bixilon.minosoft.protocol.protocol;
import com.google.gson.Gson;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import de.bixilon.minosoft.data.inventory.Slot; import de.bixilon.minosoft.data.inventory.Slot;
import de.bixilon.minosoft.data.text.ChatComponent; 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.protocol.network.Connection;
import de.bixilon.minosoft.util.nbt.tag.CompoundTag; import de.bixilon.minosoft.util.nbt.tag.CompoundTag;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.UUID; import java.util.UUID;
@ -52,36 +52,41 @@ public class OutByteBuffer {
writeBytes(data); writeBytes(data);
} }
public void writeShort(short s) { public void writeShort(short value) {
ByteBuffer buffer = ByteBuffer.allocate(Short.BYTES); writeByte(value >>> 8);
buffer.putShort(s); writeByte(value);
writeBytes(buffer.array());
} }
public void writeInt(int i) { public void writeInt(int value) {
ByteBuffer buffer = ByteBuffer.allocate(Integer.BYTES); writeByte(value >>> 24);
buffer.putInt(i); writeByte(value >>> 16);
writeBytes(buffer.array()); writeByte(value >>> 8);
writeByte(value);
} }
public void writeBytes(byte[] b) { public void writeBytes(byte[] data) {
for (byte value : b) { for (byte singleByte : data) {
bytes.add(value); bytes.add(singleByte);
} }
} }
public void writeLong(Long l) { public void writeLong(long value) {
ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES); writeByte(value >>> 56);
buffer.putLong(l); writeByte(value >>> 48);
writeBytes(buffer.array()); writeByte(value >>> 40);
writeByte(value >>> 32);
writeByte(value >>> 24);
writeByte(value >>> 16);
writeByte(value >>> 8);
writeByte(value);
} }
public void writeChatComponent(ChatComponent chatComponent) { 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) { public void writeJSON(JsonObject json) {
writeString(j.toString()); writeString(new Gson().toJson(json));
} }
public void writeString(String string) { public void writeString(String string) {
@ -103,29 +108,29 @@ public class OutByteBuffer {
} while (value != 0); } while (value != 0);
} }
public void writeByte(byte b) { public void writeByte(byte value) {
bytes.add(b); bytes.add(value);
} }
public void writeFloat(Float f) { public void writeByte(int value) {
ByteBuffer buffer = ByteBuffer.allocate(Float.BYTES); bytes.add((byte) (value & 0xFF));
buffer.putFloat(f);
for (byte b : buffer.array()) {
bytes.add(b);
}
} }
public void writeDouble(Double d) { public void writeByte(long value) {
ByteBuffer buffer = ByteBuffer.allocate(Double.BYTES); bytes.add((byte) (value & 0xFF));
buffer.putDouble(d);
writeBytes(buffer.array());
} }
public void writeUUID(UUID u) { public void writeFloat(float value) {
ByteBuffer buffer = ByteBuffer.allocate(16); // UUID.BYTES writeInt(Float.floatToIntBits(value));
buffer.putLong(u.getMostSignificantBits()); }
buffer.putLong(u.getLeastSignificantBits());
writeBytes(buffer.array()); 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) { public void writeFixedPointNumberInt(double d) {
@ -142,16 +147,15 @@ public class OutByteBuffer {
return; return;
} }
if (versionId < 440) { 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; 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) { public void writeVarInt(int value) {
// thanks https://wiki.vg/Protocol#VarInt_and_VarLong // thanks https://wiki.vg/Protocol#VarInt_and_VarLong
do do {
{
byte temp = (byte) (value & 0x7F); byte temp = (byte) (value & 0x7F);
value >>>= 7; value >>>= 7;
if (value != 0) { if (value != 0) {
@ -164,8 +168,7 @@ public class OutByteBuffer {
public void prefixVarInt(int value) { public void prefixVarInt(int value) {
int count = 0; int count = 0;
// thanks https://wiki.vg/Protocol#VarInt_and_VarLong // thanks https://wiki.vg/Protocol#VarInt_and_VarLong
do do {
{
byte temp = (byte) (value & 0x7F); byte temp = (byte) (value & 0x7F);
value >>>= 7; value >>>= 7;
if (value != 0) { if (value != 0) {
@ -200,8 +203,8 @@ public class OutByteBuffer {
nbt.writeBytes(this); nbt.writeBytes(this);
} }
public void writeBoolean(boolean b) { public void writeBoolean(boolean value) {
bytes.add((byte) ((b) ? 0x01 : 0x00)); bytes.add((byte) ((value) ? 0x01 : 0x00));
} }
public void writeStringNoLength(String string) { public void writeStringNoLength(String string) {
@ -209,12 +212,12 @@ public class OutByteBuffer {
} }
public void writeBlockPositionByte(BlockPosition pos) { public void writeBlockPositionByte(BlockPosition pos) {
writeInt(pos.getX()); writeInt(pos.x());
writeByte((byte) pos.getY()); writeByte((byte) pos.y());
writeInt(pos.getZ()); writeInt(pos.z());
} }
public byte[] getOutBytes() { public byte[] toByteArray() {
byte[] ret = new byte[bytes.size()]; byte[] ret = new byte[bytes.size()];
for (int i = 0; i < bytes.size(); i++) { for (int i = 0; i < bytes.size(); i++) {
ret[i] = bytes.get(i); ret[i] = bytes.get(i);

View File

@ -24,10 +24,10 @@ public class OutPacketBuffer extends OutByteBuffer {
} }
@Override @Override
public byte[] getOutBytes() { public byte[] toByteArray() {
OutByteBuffer ret = new OutByteBuffer(this); OutByteBuffer ret = new OutByteBuffer(this);
ret.prefixVarInt(getCommand()); ret.prefixVarInt(getCommand());
return ret.getOutBytes(); return ret.toByteArray();
} }
public int getCommand() { public int getCommand() {

View File

@ -284,14 +284,14 @@ public class PacketHandler {
Log.game(switch (pkg.getReason()) { Log.game(switch (pkg.getReason()) {
case START_RAINING -> "Received weather packet: Starting rain..."; case START_RAINING -> "Received weather packet: Starting rain...";
case STOP_RAINING -> "Received weather packet: Stopping 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 -> ""; default -> "";
}); });
switch (pkg.getReason()) { switch (pkg.getReason()) {
case STOP_RAINING -> connection.getPlayer().getWorld().setRaining(false); case STOP_RAINING -> connection.getPlayer().getWorld().setRaining(false);
case START_RAINING -> connection.getPlayer().getWorld().setRaining(true); 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()));
} }
} }

View File

@ -96,11 +96,11 @@ public class PacketSender {
} }
public void sendPluginMessageData(String channel, OutByteBuffer toSend) { 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) { 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) { public void setLocation(Location location, EntityRotation rotation, boolean onGround) {