Search code examples
javapluginsnettyminecraftbukkit

DecoderException when trying to teleport player


I create PvP Plugin for minecraft server, but when I press teleport to the world, I get an error:

Internal Exception: io.netty.handler.codec.DecoderException: java.lang.IndexOutOfBoundsException: readerIndex(47) + lenght(1) exceeds writerIndex(47): UnpooledHeapByteBuf(ridx: 47, widx: 47, cap: 47)

Already went through the code, there are 0 errors. This is my code:

Location playerSpawn = new Location(map_world, spawn1x + moves * 200, (double)spawn1y, (double)spawn1z, (float) spawnYaw, (float) spawnPitch);
player.teleport(playerSpawn);

// then I send NPC to player
EntityPlayer npc = new EntityPlayer(server, world, profile, new PlayerInteractManager(world));
npc.setLocation(npcX + moves * 200, npcY, npcZ, 0.0f, 90.0f);
CraftLivingEntity cle = (CraftLivingEntity) npc.getBukkitEntity();
ItemStack itemstack = ItemUtils.getItem(Material.STICK, 1, Enchantment.KNOCKBACK);
cle.getEquipment().setItemInHand(itemstack);
EntityPlayer ep = ((CraftPlayer) p).getHandle();
PlayerConnection connection = ep.playerConnection;
connection.sendPacket(new PacketPlayOutPlayerInfo(PacketPlayOutPlayerInfo.EnumPlayerInfoAction.ADD_PLAYER, npc));
connection.sendPacket(new PacketPlayOutNamedEntitySpawn(npc));
connection.sendPacket(new PacketPlayOutEntityEquipment(npc.getBukkitEntity().getEntityId(), 0, CraftItemStack.asNMSCopy(itemstack)));
DataWatcher watcher = npc.getDataWatcher();
watcher.watch(10, 127);
PacketPlayOutEntityMetadata packet = new PacketPlayOutEntityMetadata(npc.getId(), watcher, false);
connection.sendPacket(packet4);

What can be taken with this error?


Solution

  • The issue seems to comes because a value is too high to be writted as needed.

    It seems to be watcher.watch(10, 127) :

    The 127 is cast as int by Java. So, for bukkit, the DataWatch will send an int.

    Your client receive an integer, so a value that can reach 2147483647. Such as it was waiting for a byte (that can reach only 127), it's kicked.

    To fix it, such as "127" is an allowed value for byte, you just have to say to Java that you want to send int and not byte by using this :

    watcher.watch(10, (byte) 127);