mirror of
https://gitlab.bixilon.de/bixilon/minosoft.git
synced 2025-09-15 18:34:56 -04:00
Improve byte buffers
This commit is contained in:
parent
abcbd3fa28
commit
4c8377a1ae
@ -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<ModIdentifier, CommandParser> 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
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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() {
|
||||
|
@ -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);
|
||||
|
@ -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?
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
|
@ -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) {
|
||||
|
@ -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;
|
||||
|
@ -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();
|
||||
}
|
||||
|
@ -49,7 +49,7 @@ public class PacketSpawnMob implements ClientboundPacket {
|
||||
Class<? extends Entity> 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();
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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) {
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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);
|
||||
|
@ -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() {
|
||||
|
@ -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()));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user