@Override public SetPassengerMessage decode(ByteBuf buffer) throws IOException { int entityId = ByteBufUtils.readVarInt(buffer); int length = ByteBufUtils.readVarInt(buffer); int[] passengers = new int[length]; for (int i = 0; i < length; i++) { passengers[i] = ByteBufUtils.readVarInt(buffer); } return new SetPassengerMessage(entityId, passengers); }
@Override public ByteBuf encode(ByteBuf buf, StatisticMessage message) throws IOException { Map<String, Integer> map = message.getValues(); ByteBufUtils.writeVarInt(buf, map.size()); for (Entry<String, Integer> entry : map.entrySet()) { ByteBufUtils.writeUTF8(buf, entry.getKey()); ByteBufUtils.writeVarInt(buf, entry.getValue()); } return buf; } }
@Override public EncryptionKeyRequestMessage decode(ByteBuf buffer) throws IOException { String sessionId = ByteBufUtils.readUTF8(buffer); byte[] publicKey = new byte[ByteBufUtils.readVarInt(buffer)]; buffer.readBytes(publicKey); byte[] verifyToken = new byte[ByteBufUtils.readVarInt(buffer)]; buffer.readBytes(verifyToken); return new EncryptionKeyRequestMessage(sessionId, publicKey, verifyToken); }
@Override protected void encode(ChannelHandlerContext ctx, ByteBuf msg, ByteBuf out) throws Exception { ByteBufUtils.writeVarInt(out, msg.readableBytes()); out.writeBytes(msg); }
/** * Write an encoded chat message to the buffer. * * @param buf The buffer. * @param text The chat message to write. * @throws IOException on write failure. */ public static void writeChat(ByteBuf buf, TextMessage text) throws IOException { ByteBufUtils.writeUTF8(buf, text.encode()); }
@Override public WorldBorderMessage decode(ByteBuf buffer) throws IOException { int actionId = ByteBufUtils.readVarInt(buffer); Action action = Action.getAction(actionId); switch (action) { double oldRadius = buffer.readDouble(); double newRadius = buffer.readDouble(); long speed = ByteBufUtils.readVarLong(buffer); return new WorldBorderMessage(action, oldRadius, newRadius, speed); case SET_CENTER: oldRadius = buffer.readDouble(); newRadius = buffer.readDouble(); speed = ByteBufUtils.readVarLong(buffer); int portalTeleportBoundary = ByteBufUtils.readVarInt(buffer); int warningTime = ByteBufUtils.readVarInt(buffer); int warningBlocks = ByteBufUtils.readVarInt(buffer); return new WorldBorderMessage(action, x, z, oldRadius, newRadius, speed, portalTeleportBoundary, warningTime, warningBlocks); case SET_WARNING_TIME: case SET_WARNING_BLOCKS: warningTime = ByteBufUtils.readVarInt(buffer); return new WorldBorderMessage(action, warningTime); default:
@Override public ByteBuf encode(ByteBuf buf, WorldBorderMessage message) throws IOException { ByteBufUtils.writeVarInt(buf, message.getAction().ordinal()); switch (message.getAction()) { case SET_SIZE: buf.writeDouble(message.getOldRadius()); buf.writeDouble(message.getNewRadius()); ByteBufUtils.writeVarLong(buf, message.getSpeed()); break; case SET_CENTER: buf.writeDouble(message.getOldRadius()); buf.writeDouble(message.getNewRadius()); ByteBufUtils.writeVarLong(buf, message.getSpeed()); ByteBufUtils.writeVarInt(buf, message.getPortalTeleportBoundary()); ByteBufUtils.writeVarInt(buf, message.getWarningTime()); ByteBufUtils.writeVarInt(buf, message.getWarningBlocks()); break; case SET_WARNING_TIME: ByteBufUtils.writeVarInt(buf, message.getWarningTime()); break; case SET_WARNING_BLOCKS: ByteBufUtils.writeVarInt(buf, message.getWarningBlocks()); break; default:
@Test public void testUtf8() throws Exception { final ByteBuf test = Unpooled.buffer(); ByteBufUtils.writeUTF8(test, "Hello"); final String utf8String = ByteBufUtils.readUTF8(test); if (!"Hello".equals(utf8String)) { fail("The buffer had hello wrote to it but received " + utf8String + " instead!"); } boolean exceptionThrown = false; try { ByteBufUtils.writeUTF8(test, new String(new byte[Short.MAX_VALUE + 1])); } catch (Exception ignore) { exceptionThrown = true; } if (!exceptionThrown) { fail("Writing more than Short.MAX_VALUE as a UTF8 String to the ByteBuf should have thrown an exception but it did not!"); } } }
@Override @Deprecated public ByteBuf writeHeader(ByteBuf out, CodecRegistration codec, ByteBuf data) { ByteBuf opcodeBuffer = Unpooled.buffer(5); ByteBufUtils.writeVarInt(opcodeBuffer, codec.getOpcode()); ByteBufUtils.writeVarInt(out, opcodeBuffer.readableBytes() + data.readableBytes()); opcodeBuffer.release(); ByteBufUtils.writeVarInt(out, codec.getOpcode()); return out; }
@Override public ByteBuf encode(ByteBuf buf, IncomingChatMessage message) throws IOException { ByteBufUtils.writeUTF8(buf, message.getText()); return buf; } }
@Override public ByteBuf encode(ByteBuf buf, TabCompleteResponseMessage message) throws IOException { ByteBufUtils.writeVarInt(buf, message.getCompletions().size()); for (String completion : message.getCompletions()) { ByteBufUtils.writeUTF8(buf, completion); } return buf; } }
@Override public CollectItemMessage decode(ByteBuf buf) throws IOException { int id = ByteBufUtils.readVarInt(buf); int collector = ByteBufUtils.readVarInt(buf); int count = ByteBufUtils.readVarInt(buf); return new CollectItemMessage(id, collector, count); }
@Override public ByteBuf encode(ByteBuf buf, CameraMessage message) throws IOException { ByteBufUtils.writeVarInt(buf, message.getCameraId()); return buf; } }
@Override public HandshakeMessage decode(ByteBuf buffer) throws IOException { int version = ByteBufUtils.readVarInt(buffer); String address = ByteBufUtils.readUTF8(buffer); int port = buffer.readUnsignedShort(); int state = ByteBufUtils.readVarInt(buffer); return new HandshakeMessage(version, address, port, state); }
@Override public RespawnMessage decode(ByteBuf buf) throws IOException { int dimension = buf.readInt(); int difficulty = buf.readByte(); int mode = buf.readByte(); String levelType = ByteBufUtils.readUTF8(buf); return new RespawnMessage(dimension, difficulty, mode, levelType); }
@Override public ByteBuf encode(ByteBuf buf, ScoreboardScoreMessage message) throws IOException { boolean remove = message.isRemove(); ByteBufUtils.writeUTF8(buf, message.getTarget()); buf.writeByte(remove ? 1 : 0); ByteBufUtils.writeUTF8(buf, message.getObjective()); if (!remove) { ByteBufUtils.writeVarInt(buf, message.getValue()); } return buf; } }
@Override public SetCooldownMessage decode(ByteBuf buffer) throws IOException { int itemId = ByteBufUtils.readVarInt(buffer); int cooldownTicks = ByteBufUtils.readVarInt(buffer); return new SetCooldownMessage(itemId, cooldownTicks); }