@Override public RespawnMessage decode(ChannelBuffer buffer) throws IOException { int dimension = buffer.readInt(); byte difficulty = buffer.readByte(); byte mode = buffer.readByte(); int worldHeight = ChannelBufferUtils.getExpandedHeight(buffer.readShort()); String worldType = ChannelBufferUtils.readString(buffer); return new RespawnMessage(dimension, difficulty, mode, worldHeight, worldType); }
@Override public ChannelBuffer encode(RespawnMessage message) throws IOException { ChannelBuffer buffer = ChannelBuffers.dynamicBuffer(); buffer.writeInt(message.getDimension()); buffer.writeByte(message.getDifficulty()); buffer.writeByte(message.getGameMode()); buffer.writeShort(ChannelBufferUtils.getShifts(message.getWorldHeight()) - 1); ChannelBufferUtils.writeString(buffer, message.getWorldType()); return buffer; }
@Override public PlayerSpawnMessage decode(ChannelBuffer buffer) throws IOException { int id = buffer.readInt(); String name = ChannelBufferUtils.readString(buffer); int x = buffer.readInt(); int y = buffer.readInt(); int z = buffer.readInt(); int rotation = buffer.readUnsignedByte(); int pitch = buffer.readUnsignedByte(); int item = buffer.readUnsignedShort(); List<Parameter<?>> parameters = ChannelBufferUtils.readParameters(buffer); return new PlayerSpawnMessage(id, name, x, y, z, rotation, pitch, item, parameters); }
@Override public ChannelBuffer encode(PlayerSpawnMessage message) throws IOException { ChannelBuffer buffer = ChannelBuffers.dynamicBuffer(); buffer.writeInt(message.getEntityId()); ChannelBufferUtils.writeString(buffer, message.getName()); buffer.writeInt(message.getX()); buffer.writeInt(message.getY()); buffer.writeInt(message.getZ()); buffer.writeByte(message.getYaw()); buffer.writeByte(message.getPitch()); buffer.writeShort(message.getId()); ChannelBufferUtils.writeParameters(buffer, message.getParameters()); return buffer; }
@Override public PlayerListMessage decode(ChannelBuffer buffer) throws IOException { String name = ChannelBufferUtils.readString(buffer); boolean addOrRemove = buffer.readByte() == 1; short ping = buffer.readShort(); return new PlayerListMessage(name, addOrRemove, ping); }
@Override public ChannelBuffer encode(PlayerListMessage message) throws IOException { ChannelBuffer buffer = ChannelBuffers.dynamicBuffer(); ChannelBufferUtils.writeString(buffer, message.getPlayerName()); buffer.writeByte(message.playerIsOnline() ? 1 : 0); buffer.writeShort(message.getPing()); return buffer; }
@Override public OpenWindowMessage decode(ChannelBuffer buffer) throws IOException { int id = buffer.readUnsignedByte(); int type = buffer.readUnsignedByte(); String title = ChannelBufferUtils.readString(buffer); int slots = buffer.readUnsignedByte(); return new OpenWindowMessage(id, type, title, slots); }
@Override public ChannelBuffer encode(OpenWindowMessage message) throws IOException { ChannelBuffer buffer = ChannelBuffers.dynamicBuffer(); buffer.writeByte(message.getId()); buffer.writeByte(message.getType()); ChannelBufferUtils.writeString(buffer, message.getTitle()); buffer.writeByte(message.getSlots()); return buffer; }
@Override public WindowClickMessage decode(ChannelBuffer buffer) throws IOException { int id = buffer.readUnsignedByte(); int slot = buffer.readUnsignedShort(); boolean rightClick = buffer.readUnsignedByte() != 0; int transaction = buffer.readUnsignedShort(); boolean shift = buffer.readUnsignedByte() != 0; ItemStack item = ChannelBufferUtils.readItemStack(buffer); return new WindowClickMessage(id, slot, rightClick, transaction, shift, item); }
@Override public ChannelBuffer encode(WindowClickMessage message) throws IOException { ChannelBuffer buffer = ChannelBuffers.dynamicBuffer(); buffer.writeByte(message.getWindowInstanceId()); buffer.writeShort(message.getSlot()); buffer.writeByte(message.isRightClick() ? 1 : 0); buffer.writeShort(message.getTransaction()); buffer.writeByte(message.isShift() ? 1 : 0); ChannelBufferUtils.writeItemStack(buffer, message.getItem()); return buffer; }
/* This is needed for tests to complete. It is not actually used. * See the commented-out code below this function */ @Override public PlayerLoginRequestMessage decodeFromClient(ChannelBuffer buffer) { int id = buffer.readInt(); String worldType = ChannelBufferUtils.readString(buffer); byte mode = buffer.readByte(); byte dimension = buffer.readByte(); byte difficulty = buffer.readByte(); buffer.readUnsignedByte(); // not used? short maxPlayers = buffer.readUnsignedByte(); return new PlayerLoginRequestMessage(id, worldType, mode, dimension, difficulty, maxPlayers); }
@Override public ChannelBuffer encodeToServer(PlayerLoginRequestMessage message) { PlayerLoginRequestMessage server = message; ChannelBuffer buffer = ChannelBuffers.dynamicBuffer(); buffer.writeInt(server.getId()); ChannelBufferUtils.writeString(buffer, server.getWorldType()); buffer.writeByte(server.getGameMode()); buffer.writeByte(server.getDimension()); buffer.writeByte(server.getDifficulty()); buffer.writeByte(0); buffer.writeByte(server.getMaxPlayers()); return buffer; }
@Override public List<Message> getSpawnMessages(Entity entity, RepositionManager rm) { FallingBlock block = entity.get(FallingBlock.class); if (block != null) { VanillaBlockMaterial material = block.getMaterial(); int messageData = material.getMinecraftId() | (material.getMinecraftData(material.getData()) >> 16); List<Message> messages = new ArrayList<Message>(); final double p = 32d; Point pos = entity.getScene().getPosition(); int x = (int) Math.floor(pos.getX() * p); int y = (int) Math.floor(pos.getY() * p); int z = (int) Math.floor(pos.getZ() * p); byte yaw = (byte) ChannelBufferUtils.protocolifyYaw(entity.getScene().getRotation().getYaw()); byte pitch = (byte) ChannelBufferUtils.protocolifyPitch(entity.getScene().getRotation().getPitch()); short fallSpeed = (short) (block.getFallingSpeed() * 8000d); messages.add( new EntityObjectMessage( entity.getId(), (byte) typeId, x, y, z, messageData, (short) 0, fallSpeed, (short) 0, yaw, pitch, rm)); messages.add(new EntityMetadataMessage(entity.getId(), getSpawnParameters(entity))); return messages; } else { return Collections.emptyList(); } }
@Override public UpdateSignMessage decode(ChannelBuffer buffer) throws IOException { int x = buffer.readInt(); int y = buffer.readShort(); int z = buffer.readInt(); String[] message = new String[4]; for (int i = 0; i < message.length; i++) { String line = ChannelBufferUtils.readString(buffer); if (line == null) { line = ""; } message[i] = line; } return new UpdateSignMessage(x, y, z, message); }
@Override public ChannelBuffer encode(UpdateSignMessage message) throws IOException { String[] lines = message.getMessage(); ChannelBuffer buffer = ChannelBuffers.dynamicBuffer(); buffer.writeInt(message.getX()); buffer.writeShort(message.getY()); buffer.writeInt(message.getZ()); for (String line : lines) { if (line == null) { line = ""; } ChannelBufferUtils.writeString(buffer, line); } return buffer; }