@Override public PluginMessage decode(ByteBuf buf) throws IOException { String channel = ByteBufUtils.readUTF8(buf); // todo: maybe store a ByteBuf in the message instead? byte[] data = new byte[buf.readableBytes()]; buf.readBytes(data); return new PluginMessage(channel, data); }
@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 IncomingChatMessage decode(ByteBuf buffer) throws IOException { return new IncomingChatMessage(ByteBufUtils.readUTF8(buffer)); }
/** * Read an encoded chat message from the buffer. * * @param buf The buffer. * @return The chat message read. * @throws IOException on read failure. */ public static TextMessage readChat(ByteBuf buf) throws IOException { return TextMessage.decode(ByteBufUtils.readUTF8(buf)); }
@Override public LoginStartMessage decode(ByteBuf buffer) throws IOException { return new LoginStartMessage(ByteBufUtils.readUTF8(buffer)); }
@Override public JoinGameMessage decode(ByteBuf buffer) throws IOException { int id = buffer.readInt(); byte gameMode = buffer.readByte(); int dimension = buffer.readInt(); byte difficulty = buffer.readByte(); byte maxPlayers = buffer.readByte(); String levelType = ByteBufUtils.readUTF8(buffer); boolean reducedDebug = buffer.readBoolean(); return new JoinGameMessage(id, gameMode, dimension, difficulty, maxPlayers, levelType, reducedDebug); }
@Override public StatusResponseMessage decode(ByteBuf buf) throws IOException { String json = ByteBufUtils.readUTF8(buf); return new StatusResponseMessage((JSONObject) JSONValue.parse(json)); }
@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 public AdvancementTabMessage decode(ByteBuf buf) throws IOException { int action = ByteBufUtils.readVarInt(buf); if (action == AdvancementTabMessage.ACTION_CLOSE) { return new AdvancementTabMessage(); } String tabId = ByteBufUtils.readUTF8(buf); return new AdvancementTabMessage(action, tabId); }
@Override public ClientSettingsMessage decode(ByteBuf buf) throws IOException { String locale = ByteBufUtils.readUTF8(buf); int viewDistance = buf.readByte(); int chatFlags = ByteBufUtils.readVarInt(buf); boolean colors = buf.readBoolean(); int skinFlags = buf.readUnsignedByte(); int hand = ByteBufUtils.readVarInt(buf); return new ClientSettingsMessage(locale, viewDistance, chatFlags, colors, skinFlags, hand); }
@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 TitleMessage decode(ByteBuf buffer) throws IOException { int actionId = ByteBufUtils.readVarInt(buffer); Action action = Action.getAction(actionId); switch (action) { case TITLE: case SUBTITLE: case ACTION: String text = ByteBufUtils.readUTF8(buffer); return new TitleMessage(action, TextMessage.decode(text)); case TIMES: int fadeIn = buffer.readInt(); int stay = buffer.readInt(); int fadeOut = buffer.readInt(); return new TitleMessage(action, fadeIn, stay, fadeOut); default: return new TitleMessage(action); } }
@Override public TabCompleteMessage decode(ByteBuf buf) throws IOException { String text = ByteBufUtils.readUTF8(buf); boolean assumeCommand = buf.readBoolean(); boolean hasLocation = buf.readBoolean(); BlockVector location = null; if (hasLocation) { location = GlowBufUtils.readBlockPosition(buf); } return new TabCompleteMessage(text, assumeCommand, location); }
@Override public SpawnPaintingMessage decode(ByteBuf buf) throws IOException { int id = ByteBufUtils.readVarInt(buf); UUID uuid = GlowBufUtils.readUuid(buf); String title = ByteBufUtils.readUTF8(buf); BlockVector vector = GlowBufUtils.readBlockPosition(buf); int facing = buf.readByte(); return new SpawnPaintingMessage(id, uuid, title, vector.getBlockX(), vector.getBlockY(), vector.getBlockZ(), facing); }
break; case STRING: entries.add(new Entry(index, ByteBufUtils.readUTF8(buf))); break; case ITEM:
@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!"); } } }