ProtocolLib

3M Downloads

"channel" is null

BeBr0 opened this issue ยท 3 comments

commented

I am coding plugin that includes NPCs (walking, finding paths, etc.)

Here is NPC connection code:

bot = ServerPlayer(
        server,
        (location.world as CraftWorld).handle,
        GameProfile(npcUUID, name),
        null
    )

    bot.connection = object : ServerGamePacketListenerImpl(server, Connection(PacketFlow.CLIENTBOUND), bot) {
        override fun send(packet: Packet<*>?) {

        }
    }

But when working with packet driven plugins (e.g. HMS Cosmetics) I get an error

11:58:52 WARN]: [HMCCosmetics] Task #4263 for HMCCosmetics v2.4.10 generated an exception
java.lang.NullPointerException: Cannot invoke "io.netty.channel.Channel.attr(io.netty.util.AttributeKey)" because "channel" is null
at com.comphenix.protocol.injector.netty.channel.NettyChannelInjector.findInjector(NettyChannelInjector.java:155) ~[ProtocolLib(7).jar:?]
at com.comphenix.protocol.injector.netty.channel.InjectionFactory.fromPlayer(InjectionFactory.java:103) ~[ProtocolLib(7).jar:?]
at com.comphenix.protocol.injector.netty.manager.NetworkManagerPlayerInjector.sendServerPacket(NetworkManagerPlayerInjector.java:59) ~[ProtocolLib(7).jar:?]
at com.comphenix.protocol.injector.PacketFilterManager.sendServerPacket(PacketFilterManager.java:184) ~[ProtocolLib(7).jar:?]
at com.comphenix.protocol.injector.PacketFilterManager.sendServerPacket(PacketFilterManager.java:157) ~[ProtocolLib(7).jar:?]
at com.hibiscusmc.hmccosmetics.util.packets.BasePacket.sendPacket(BasePacket.java:11) ~[HMCCosmeticsRemapped-2.4.10.jar:?]
at com.hibiscusmc.hmccosmetics.util.packets.PacketManager.sendTeleportPacket(PacketManager.java:440) ~[HMCCosmeticsRemapped-2.4.10.jar:?]
at com.hibiscusmc.hmccosmetics.cosmetic.types.CosmeticBalloonType.update(CosmeticBalloonType.java:74) ~[HMCCosmeticsRemapped-2.4.10.jar:?]
at com.hibiscusmc.hmccosmetics.user.CosmeticUser.updateCosmetic(CosmeticUser.java:185) ~[HMCCosmeticsRemapped-2.4.10.jar:?]
at com.hibiscusmc.hmccosmetics.user.CosmeticUser.updateCosmetic(CosmeticUser.java:195) ~[HMCCosmeticsRemapped-2.4.10.jar:?]
at com.hibiscusmc.hmccosmetics.user.CosmeticUser.lambda$tick$0(CosmeticUser.java:65) ~[HMCCosmeticsRemapped-2.4.10.jar:?]
at org.bukkit.craftbukkit.v1_19_R1.scheduler.CraftTask.run(CraftTask.java:101) ~[purpur-1.19.2.jar:git-Purpur-1858]
at org.bukkit.craftbukkit.v1_19_R1.scheduler.CraftScheduler.mainThreadHeartbeat(CraftScheduler.java:483) ~[purpur-1.19.2.jar:git-Purpur-1858]
at net.minecraft.server.MinecraftServer.tickChildren(MinecraftServer.java:1500) ~[purpur-1.19.2.jar:git-Purpur-1858]
at net.minecraft.server.dedicated.DedicatedServer.tickChildren(DedicatedServer.java:486) ~[purpur-1.19.2.jar:git-Purpur-1858]
at net.minecraft.server.MinecraftServer.tickServer(MinecraftServer.java:1424) ~[purpur-1.19.2.jar:git-Purpur-1858]
at net.minecraft.server.MinecraftServer.runServer(MinecraftServer.java:1194) ~[purpur-1.19.2.jar:git-Purpur-1858]
at net.minecraft.server.MinecraftServer.lambda$spin$0(MinecraftServer.java:310) ~[purpur-1.19.2.jar:git-Purpur-1858]
at java.lang.Thread.run(Thread.java:833) ~[?:?]

Was trying to fix this for a long time, so asking for help here. I really want to know what causes this problem and how it can be connected with NPC packet channels. Thank you in advance ;)

commented

Based on the code you've given that issue shouldn't happen.
I suspect that you're interacting in some way with the network protocol or you are (maybe by accident) registering the created player instance in some online player list, causing HMS Cosmetics to try and send a packet to that player. Due to the player not having a channel, the issue occurs. So making sure that you're not leaking your player instance outside your handling scope should resolve the issue.

commented

Based on the code you've given that issue shouldn't happen. I suspect that you're interacting in some way with the network protocol or you are (maybe by accident) registering the created player instance in some online player list, causing HMS Cosmetics to try and send a packet to that player. Due to the player not having a channel, the issue occurs. So making sure that you're not leaking your player instance outside your handling scope should resolve the issue.

So you mean that adding player to the world is the problem here?

(location().world!! as CraftWorld).addEntity<Entity>(bot, CreatureSpawnEvent.SpawnReason.CUSTOM)
commented

That was the problem. I managed to just spawn them with packets. Strong thanks to @derklaro for the explanation