尝试传送播放器时出现 DecoderException

DecoderException when trying to teleport player

我为 minecraft 服务器创建了 PvP 插件,但是当我按下传送到世界时,出现错误:

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

已经检查过代码,有 0 个错误。这是我的代码:

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);

这个错误可以采取什么措施?

这个问题好像是因为一个值太高了,没法按需要写。

好像是watcher.watch(10, 127):

127 被 Java 转换为 int。因此,对于 bukkit,DataWatch 将发送一个 int。

您的客户端收到一个整数,因此一个可以达到 2147483647 的值。比如它在等待一个字节(只能到达127),它被踢了。

要修复它,例如“127”是 byte 的允许值,您只需告诉 Java 您要使用此发送 int 而不是 byte :

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