package protocolsupport.protocol.serializer;

import io.netty.buffer.ByteBuf;
import protocolsupport.protocol.utils.types.Position;

public class PositionSerializer {

	public static Position readPosition(ByteBuf from) {
		long l = from.readLong();
		return new Position((int) (l >> 38), (int) ((l >> 26) & 0xFFFL), (int) (l & 0x3FFFFFFL));
	}

	public static void readPositionTo(ByteBuf from, Position to) {
		long l = from.readLong();
		to.setX((int) (l >> 38));
		to.setY((int) ((l >> 26) & 0xFFFL));
		to.setZ((int) (l & 0x3FFFFFFL));
	}

	public static void readLegacyPositionBTo(ByteBuf from, Position to) {
		to.setX(from.readInt());
		to.setY(from.readUnsignedByte());
		to.setZ(from.readInt());
	}

	public static void readLegacyPositionSTo(ByteBuf from, Position to) {
		to.setX(from.readInt());
		to.setY(from.readShort());
		to.setZ(from.readInt());
	}

	public static void writePosition(ByteBuf to, Position position) {
		to.writeLong(((position.getX() & 0x3FFFFFFL) << 38) | ((position.getY() & 0xFFFL) << 26) | (position.getZ() & 0x3FFFFFFL));
	}

	public static void writeLegacyPositionB(ByteBuf to, Position position) {
		to.writeInt(position.getX());
		to.writeByte(position.getY());
		to.writeInt(position.getZ());
	}

	public static void writeLegacyPositionS(ByteBuf to, Position position) {
		to.writeInt(position.getX());
		to.writeShort(position.getY());
		to.writeInt(position.getZ());
	}

	public static void writeLegacyPositionI(ByteBuf to, Position position) {
		to.writeInt(position.getX());
		to.writeInt(position.getY());
		to.writeInt(position.getZ());
	}

}

@@ -311,39 +311,40 @@
 FFL), (int) 
+(
 (l 
-& 0x3FFFFFFL
+%3C%3C 38) %3E%3E 38
 ));%0A%09%7D%0A%0A%09pub
@@ -518,23 +518,24 @@
 nt) 
+(
 (l 
-& 0x3FFFFFFL
+%3C%3C 38) %3E%3E 38
 ));%0A

Fix position decoding.