Field index 0 is out of bounds for length 0 - I'm not able to read field 0
Kaganox opened this issue · 2 comments
Make sure you're doing the following
- the server using Protoclib 5.2.0
- plugin using maven version of 5.1.0
The server is a paper 1.20.4 Server
I wanted to tried this code which i found.
ProtocolLibrary.getProtocolManager().addPacketListener(new PacketAdapter(this,
PacketType.Play.Server.ENTITY_EQUIPMENT) {
@Override
public void onPacketSending(PacketEvent event) {
PacketContainer packet = event.getPacket();
ItemStack stack = packet.getItemModifier().read(0);
// Only modify leather armor
if (stack != null && stack.getType().name().contains("LEATHER")) {
// The problem turned out to be that certain Minecraft
// functions update every player with the same packet for
// an equipment, whereas other methods update the
// equipment with a different packet per player.
// To fix this, we'll simply clone the packet before we
// modify it
packet = event.getPacket().deepClone();
event.setPacket(packet);
stack = packet.getItemModifier().read(4);
// Color that depends on the player's name
String recieverName = event.getPlayer().getName();
int color = recieverName.hashCode() & 0xFFFFFF;
// Update the color
LeatherArmorMeta meta = (LeatherArmorMeta) stack.getItemMeta();
meta.setColor(Color.fromBGR(color));
stack.setItemMeta(meta);
}
}
});
But i always get by the line ItemStack stack = packet.getItemModifier().read(0);
the error:
[16:05:04 ERROR]: [Library] Unhandled exception occurred in onPacketSending(PacketEvent) for Library
com.comphenix.protocol.reflect.FieldAccessException: Field index 4 is out of bounds for length 0
at com.comphenix.protocol.reflect.FieldAccessException.fromFormat(FieldAccessException.java:49) ~[ProtocolLib.jar:?]
at com.comphenix.protocol.reflect.StructureModifier.read(StructureModifier.java:244) ~[ProtocolLib.jar:?]
at kagano.library.Library$5.onPacketSending(Library.java:175) ~[Kagano Library.jar:?]
at com.comphenix.protocol.injector.SortedPacketListenerList.invokeSendingListener(SortedPacketListenerList.java:219) ~[ProtocolLib.jar:?]
at com.comphenix.protocol.injector.SortedPacketListenerList.invokeUnpackedPacketSending(SortedPacketListenerList.java:204) ~[ProtocolLib.jar:?]
at com.comphenix.protocol.injector.SortedPacketListenerList.invokePacketSending(SortedPacketListenerList.java:149) ~[ProtocolLib.jar:?]
at com.comphenix.protocol.injector.SortedPacketListenerList.invokePacketSending(SortedPacketListenerList.java:139) ~[ProtocolLib.jar:?]
at com.comphenix.protocol.injector.PacketFilterManager.postPacketToListeners(PacketFilterManager.java:553) ~[ProtocolLib.jar:?]
at com.comphenix.protocol.injector.PacketFilterManager.invokePacketSending(PacketFilterManager.java:526) ~[ProtocolLib.jar:?]
at com.comphenix.protocol.injector.netty.manager.NetworkManagerInjector.onPacketSending(NetworkManagerInjector.java:100) ~[ProtocolLib.jar:?]
at com.comphenix.protocol.injector.netty.channel.NettyChannelInjector.processOutbound(NettyChannelInjector.java:553) ~[ProtocolLib.jar:?]
at com.comphenix.protocol.injector.netty.channel.NettyChannelInjector$2.doProxyRunnable(NettyChannelInjector.java:456) ~[ProtocolLib.jar:?]
at com.comphenix.protocol.injector.netty.channel.NettyEventLoopProxy.proxyRunnable(NettyEventLoopProxy.java:43) ~[ProtocolLib.jar:?]
at com.comphenix.protocol.injector.netty.channel.NettyEventLoopProxy.execute(NettyEventLoopProxy.java:252) ~[ProtocolLib.jar:?]
at net.minecraft.network.Connection.sendPacket(Connection.java:432) ~[?:?]
at net.minecraft.network.Connection.send(Connection.java:392) ~[?:?]
at net.minecraft.server.network.ServerCommonPacketListenerImpl.send(ServerCommonPacketListenerImpl.java:255) ~[paper-1.20.4.jar:git-Paper-398]
at net.minecraft.server.network.ServerCommonPacketListenerImpl.send(ServerCommonPacketListenerImpl.java:240) ~[paper-1.20.4.jar:git-Paper-398]
at net.minecraft.server.level.ChunkMap$TrackedEntity.broadcast(ChunkMap.java:1378) ~[?:?]
at net.minecraft.server.level.ChunkMap.broadcast(ChunkMap.java:1226) ~[paper-1.20.4.jar:git-Paper-398]
at net.minecraft.server.level.ServerChunkCache.broadcast(ServerChunkCache.java:795) ~[?:?]
at net.minecraft.world.entity.LivingEntity.handleEquipmentChanges(LivingEntity.java:3206) ~[?:?]
at net.minecraft.world.entity.LivingEntity.detectEquipmentUpdatesPublic(LivingEntity.java:3114) ~[?:?]
at net.minecraft.server.level.ServerEntity.sendPairingData(ServerEntity.java:354) ~[?:?]
at net.minecraft.server.level.ServerEntity.addPairing(ServerEntity.java:288) ~[?:?]
at net.minecraft.server.level.ChunkMap$TrackedEntity.updatePlayer(ChunkMap.java:1442) ~[?:?]
at net.minecraft.server.level.ChunkMap$TrackedEntity.updatePlayers(ChunkMap.java:1343) ~[?:?]
at net.minecraft.server.level.ChunkMap.addEntity(ChunkMap.java:1104) ~[paper-1.20.4.jar:git-Paper-398]
at net.minecraft.server.level.ServerChunkCache.addEntity(ServerChunkCache.java:787) ~[?:?]
at net.minecraft.server.level.ServerLevel$EntityCallbacks.onTrackingStart(ServerLevel.java:2786) ~[?:?]
at net.minecraft.server.level.ServerLevel$EntityCallbacks.c(ServerLevel.java:2724) ~[?:?]
at io.papermc.paper.chunk.system.entity.EntityLookup.entityStatusChange(EntityLookup.java:281) ~[paper-1.20.4.jar:git-Paper-398]
at io.papermc.paper.chunk.system.entity.EntityLookup.addEntity(EntityLookup.java:437) ~[paper-1.20.4.jar:git-Paper-398]
at io.papermc.paper.chunk.system.entity.EntityLookup.addNewEntity(EntityLookup.java:375) ~[paper-1.20.4.jar:git-Paper-398]
at net.minecraft.server.level.ServerLevel.addEntity(ServerLevel.java:1704) ~[?:?]
at net.minecraft.server.level.ServerLevel.addFreshEntity(ServerLevel.java:1621) ~[?:?]
at net.minecraft.world.level.ServerLevelAccessor.lambda$addFreshEntityWithPassengers$0(ServerLevelAccessor.java:16) ~[?:?]
at java.util.stream.Streams$StreamBuilderImpl.forEachRemaining(Streams.java:411) ~[?:?]
at java.util.stream.ReferencePipeline$Head.forEach(ReferencePipeline.java:762) ~[?:?]
at net.minecraft.world.level.ServerLevelAccessor.addFreshEntityWithPassengers(ServerLevelAccessor.java:16) ~[?:?]
at net.minecraft.world.level.NaturalSpawner.spawnCategoryForPosition(NaturalSpawner.java:301) ~[?:?]
at net.minecraft.world.level.NaturalSpawner.spawnCategoryForChunk(NaturalSpawner.java:212) ~[?:?]
at net.minecraft.world.level.NaturalSpawner.spawnForChunk(NaturalSpawner.java:182) ~[?:?]
at net.minecraft.server.level.ServerChunkCache.tickChunks(ServerChunkCache.java:649) ~[?:?]
at net.minecraft.server.level.ServerChunkCache.tick(ServerChunkCache.java:501) ~[?:?]
at net.minecraft.server.level.ServerLevel.tick(ServerLevel.java:868) ~[?:?]
at net.minecraft.server.MinecraftServer.tickChildren(MinecraftServer.java:1709) ~[paper-1.20.4.jar:git-Paper-398]
at net.minecraft.server.dedicated.DedicatedServer.tickChildren(DedicatedServer.java:446) ~[paper-1.20.4.jar:git-Paper-398]
at net.minecraft.server.MinecraftServer.tickServer(MinecraftServer.java:1510) ~[paper-1.20.4.jar:git-Paper-398]
at net.minecraft.server.MinecraftServer.runServer(MinecraftServer.java:1211) ~[paper-1.20.4.jar:git-Paper-398]
at net.minecraft.server.MinecraftServer.lambda$spin$0(MinecraftServer.java:318) ~[paper-1.20.4.jar:git-Paper-398]
at java.lang.Thread.run(Thread.java:1583) ~[?:?]
[16:05:04 ERROR]: Parameters:
net.minecraft.network.protocol.game.PacketPlayOutEntityEquipment@4149511a[
b=515
c=[(MAINHAND, 1 bow)]
]
I have a similar question. When I was using protocolLib dev build 721 with "ItemsAdder" plugin and it was trying to reload the resource pack for the player, it threw the following error. But when i used protocolLib 5.2.0 release, it worked fine without throwing the error. 😢
[15:51:37 ERROR]: Could not pass event PlayerResourcePackStatusEvent to ItemsAdder v3.6.4-release-2
com.comphenix.protocol.reflect.FieldAccessException: Field index 0 is out of bounds for length 0
at com.comphenix.protocol.reflect.FieldAccessException.fromFormat(FieldAccessException.java:49) ~[ProtocolLib_dev_721.jar:?]
at com.comphenix.protocol.reflect.StructureModifier.write(StructureModifier.java:318) ~[ProtocolLib_dev_721.jar:?]
at ia.m.mW.a(SourceFile:267) ~[ItemsAdder_3.6.4-release-2_5.jar:?]
at ia.m.qi.at(SourceFile:189) ~[ItemsAdder_3.6.4-release-2_5.jar:?]
at ia.m.qa.a(SourceFile:209) ~[ItemsAdder_3.6.4-release-2_5.jar:?]
at com.destroystokyo.paper.event.executor.asm.generated.GeneratedEventExecutor516.execute(Unknown Source) ~[?:?]
at org.bukkit.plugin.EventExecutor$2.execute(EventExecutor.java:77) ~[paper-api-1.20.4-R0.1-SNAPSHOT.jar:?]
at co.aikar.timings.TimedEventExecutor.execute(TimedEventExecutor.java:81) ~[paper-api-1.20.4-R0.1-SNAPSHOT.jar:git-Paper-497]
at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:70) ~[paper-api-1.20.4-R0.1-SNAPSHOT.jar:?]
at io.papermc.paper.plugin.manager.PaperEventManager.callEvent(PaperEventManager.java:54) ~[paper-1.20.4.jar:git-Paper-497]
at io.papermc.paper.plugin.manager.PaperPluginManagerImpl.callEvent(PaperPluginManagerImpl.java:126) ~[paper-1.20.4.jar:git-Paper-497]
at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:615) ~[paper-api-1.20.4-R0.1-SNAPSHOT.jar:?]
at net.minecraft.server.network.ServerCommonPacketListenerImpl.handleResourcePackResponse(ServerCommonPacketListenerImpl.java:200) ~[paper-1.20.4.jar:git-Paper-497]
at net.minecraft.network.protocol.common.ServerboundResourcePackPacket.handle(ServerboundResourcePackPacket.java:20) ~[paper-1.20.4.jar:git-Paper-497]
at net.minecraft.network.protocol.common.ServerboundResourcePackPacket.a(ServerboundResourcePackPacket.java:9) ~[paper-1.20.4.jar:git-Paper-497]
at net.minecraft.network.protocol.PacketUtils.lambda$ensureRunningOnSameThread$0(PacketUtils.java:54) ~[?:?]
at net.minecraft.server.TickTask.run(TickTask.java:18) ~[paper-1.20.4.jar:git-Paper-497]
at net.minecraft.util.thread.BlockableEventLoop.doRunTask(BlockableEventLoop.java:149) ~[?:?]
at net.minecraft.util.thread.ReentrantBlockableEventLoop.doRunTask(ReentrantBlockableEventLoop.java:24) ~[?:?]
at net.minecraft.server.MinecraftServer.doRunTask(MinecraftServer.java:1465) ~[paper-1.20.4.jar:git-Paper-497]
at net.minecraft.server.MinecraftServer.d(MinecraftServer.java:194) ~[paper-1.20.4.jar:git-Paper-497]
at net.minecraft.util.thread.BlockableEventLoop.pollTask(BlockableEventLoop.java:123) ~[?:?]
at net.minecraft.server.MinecraftServer.pollTaskInternal(MinecraftServer.java:1442) ~[paper-1.20.4.jar:git-Paper-497]
at net.minecraft.server.MinecraftServer.pollTask(MinecraftServer.java:1365) ~[paper-1.20.4.jar:git-Paper-497]
at net.minecraft.util.thread.BlockableEventLoop.managedBlock(BlockableEventLoop.java:133) ~[?:?]
at net.minecraft.server.MinecraftServer.tickServer(MinecraftServer.java:1516) ~[paper-1.20.4.jar:git-Paper-497]
at net.minecraft.server.MinecraftServer.runServer(MinecraftServer.java:1226) ~[paper-1.20.4.jar:git-Paper-497]
at net.minecraft.server.MinecraftServer.lambda$spin$0(MinecraftServer.java:319) ~[paper-1.20.4.jar:git-Paper-497]
at java.lang.Thread.run(Thread.java:1583) ~[?:?]
The ClientboundSetEquipmentPacket
packet doesn't contain any ItemStack
fields but only a list of ItemSlot
and ItemStack
. Here is what it looks like in 1.20.4
:
public class ClientboundSetEquipmentPacket implements Packet<ClientGamePacketListener> {
private final int entity;
private final List<Pair<EquipmentSlot, ItemStack>> slots;
public ClientboundSetEquipmentPacket(int entity, List<Pair<EquipmentSlot, ItemStack>> slots) {
this.entity = entity;
this.slots = slots;
}
// ...
}
You need to use the getSlotStackPairLists()
method to read the items of the packet.