Проверка инвентаря игрока в ивенте

Версия Minecraft
1.12.2
62
1
0
Всем привет. Делаю проверку на слот в инвентаре и содержащие в нём.
Для теста накидал код, который должен работать, но краш со стороны сервера.
Java:
       private EntityPlayer player;
     
       @SubscribeEvent
       public void onUpdateEvent(LivingEvent.LivingUpdateEvent event)
       {
            ICAPCustomInventory cap = player.getCapability(CAPCustomInventoryProvider.INVENTORY_CAP, null);
            CustomInventory inv = cap.getInventory();
           
            if (inv.getStackInSlot(0).getItem() == ItemsRegister.ring) {
                System.out.println("item");
            } else {
                System.out.println("noitem");
            }
       }
}


crash-log:

---- Minecraft Crash Report ----
// This doesn't make any sense!

Time: 5/4/20 1:41 PM
Description: Ticking entity

java.lang.NullPointerException: Ticking entity
at ua.maslenok.main.EH.onPlayerUpdateEvent(EH.java:17)
at net.minecraftforge.fml.common.eventhandler.ASMEventHandler_14_EH_onPlayerUpdateEvent_LivingUpdateEvent.invoke(.dynamic)
at net.minecraftforge.fml.common.eventhandler.ASMEventHandler.invoke(ASMEventHandler.java:90)
at net.minecraftforge.fml.common.eventhandler.EventBus.post(EventBus.java:182)
at net.minecraftforge.common.ForgeHooks.onLivingUpdate(ForgeHooks.java:581)
at net.minecraft.entity.EntityLivingBase.onUpdate(EntityLivingBase.java:2321)
at net.minecraft.entity.EntityLiving.onUpdate(EntityLiving.java:346)
at net.minecraft.world.World.updateEntityWithOptionalForce(World.java:2174)
at net.minecraft.world.WorldServer.updateEntityWithOptionalForce(WorldServer.java:871)
at net.minecraft.world.World.updateEntity(World.java:2133)
at net.minecraft.world.World.updateEntities(World.java:1934)
at net.minecraft.world.WorldServer.updateEntities(WorldServer.java:643)
at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:842)
at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:743)
at net.minecraft.server.integrated.IntegratedServer.tick(IntegratedServer.java:192)
at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:592)
at java.lang.Thread.run(Unknown Source)


A detailed walkthrough of the error, its code path and all known details is as follows:
---------------------------------------------------------------------------------------

-- Head --
Thread: Server thread
Stacktrace:
at ua.maslenok.main.EH.onPlayerUpdateEvent(EH.java:17)
at net.minecraftforge.fml.common.eventhandler.ASMEventHandler_14_EH_onPlayerUpdateEvent_LivingUpdateEvent.invoke(.dynamic)
at net.minecraftforge.fml.common.eventhandler.ASMEventHandler.invoke(ASMEventHandler.java:90)
at net.minecraftforge.fml.common.eventhandler.EventBus.post(EventBus.java:182)
at net.minecraftforge.common.ForgeHooks.onLivingUpdate(ForgeHooks.java:581)
at net.minecraft.entity.EntityLivingBase.onUpdate(EntityLivingBase.java:2321)
at net.minecraft.entity.EntityLiving.onUpdate(EntityLiving.java:346)
at net.minecraft.world.World.updateEntityWithOptionalForce(World.java:2174)
at net.minecraft.world.WorldServer.updateEntityWithOptionalForce(WorldServer.java:871)
at net.minecraft.world.World.updateEntity(World.java:2133)

-- Entity being ticked --
Details:
Entity Type: minecraft:sheep (net.minecraft.entity.passive.EntitySheep)
Entity ID: 0
Entity Name: Овца
Entity's Exact location: 698.53, 4.00, 823.25
Entity's Block location: World: (698,4,823), Chunk: (at 10,0,7 in 43,51; contains blocks 688,0,816 to 703,255,831), Region: (1,1; contains chunks 32,32 to 63,63, blocks 512,0,512 to 1023,255,1023)
Entity's Momentum: 0.00, -0.08, 0.00
Entity's Passengers: []
Entity's Vehicle: ~~ERROR~~ NullPointerException: null
Stacktrace:
at net.minecraft.world.World.updateEntities(World.java:1934)
at net.minecraft.world.WorldServer.updateEntities(WorldServer.java:643)

-- Affected level --
Details:
Level name: Новый мир
All players: 0 total; []
Chunk stats: ServerChunkCache: 625 Drop: 0
Level seed: -875017853216645664
Level generator: ID 01 - flat, ver 0. Features enabled: true
Level generator options:
Level spawn location: World: (774,4,874), Chunk: (at 6,0,10 in 48,54; contains blocks 768,0,864 to 783,255,879), Region: (1,1; contains chunks 32,32 to 63,63, blocks 512,0,512 to 1023,255,1023)
Level time: 43909 game time, 7272 day time
Level dimension: 0
Level storage version: 0x04ABD - Anvil
Level weather: Rain time: 1301 (now: false), thunder time: 82963 (now: false)
Level game mode: Game mode: creative (ID 1). Hardcore: false. Cheats: true
Stacktrace:
at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:842)
at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:743)
at net.minecraft.server.integrated.IntegratedServer.tick(IntegratedServer.java:192)
at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:592)
at java.lang.Thread.run(Unknown Source)
 
Решение
который должен работать
Нет, не должен.
Поле "player" не имеет значение, т.е. он null.
Эвент LivingUpdateEvent берёшь метод который возвращает EntityLiving, проверяешь что он EntityPlayer, преобразуешь EntityLiving в EntityPlayer и это используешь место player.

timaxa007

Модератор
5,831
409
672
который должен работать
Нет, не должен.
Поле "player" не имеет значение, т.е. он null.
Эвент LivingUpdateEvent берёшь метод который возвращает EntityLiving, проверяешь что он EntityPlayer, преобразуешь EntityLiving в EntityPlayer и это используешь место player.
 
62
1
0
Java:
    @SubscribeEvent
    public void onEntityUpdate(LivingUpdateEvent event)
    {
        if(event.getEntityLiving() instanceof EntityPlayer)
        {
            EntityPlayer player = (EntityPlayer)event.getEntityLiving();
        }
    }

crash-log:
---- Minecraft Crash Report ----
// Daisy, daisy...

Time: 5/4/20 2:03 PM
Description: Ticking entity

java.lang.ClassCastException: net.minecraft.entity.passive.EntitySheep cannot be cast to net.minecraft.entity.player.EntityPlayer
at ua.maslenok.main.EH.onPlayerUpdateEvent(EH.java:26)
at net.minecraftforge.fml.common.eventhandler.ASMEventHandler_15_EH_onPlayerUpdateEvent_LivingUpdateEvent.invoke(.dynamic)
at net.minecraftforge.fml.common.eventhandler.ASMEventHandler.invoke(ASMEventHandler.java:90)
at net.minecraftforge.fml.common.eventhandler.EventBus.post(EventBus.java:182)
at net.minecraftforge.common.ForgeHooks.onLivingUpdate(ForgeHooks.java:581)
at net.minecraft.entity.EntityLivingBase.onUpdate(EntityLivingBase.java:2321)
at net.minecraft.entity.EntityLiving.onUpdate(EntityLiving.java:346)
at net.minecraft.world.World.updateEntityWithOptionalForce(World.java:2174)
at net.minecraft.world.WorldServer.updateEntityWithOptionalForce(WorldServer.java:871)
at net.minecraft.world.World.updateEntity(World.java:2133)
at net.minecraft.world.World.updateEntities(World.java:1934)
at net.minecraft.world.WorldServer.updateEntities(WorldServer.java:643)
at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:842)
at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:743)
at net.minecraft.server.integrated.IntegratedServer.tick(IntegratedServer.java:192)
at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:592)
at java.lang.Thread.run(Unknown Source)


A detailed walkthrough of the error, its code path and all known details is as follows:
---------------------------------------------------------------------------------------

-- Head --
Thread: Server thread
Stacktrace:
at ua.maslenok.main.EH.onPlayerUpdateEvent(EH.java:26)
at net.minecraftforge.fml.common.eventhandler.ASMEventHandler_15_EH_onPlayerUpdateEvent_LivingUpdateEvent.invoke(.dynamic)
at net.minecraftforge.fml.common.eventhandler.ASMEventHandler.invoke(ASMEventHandler.java:90)
at net.minecraftforge.fml.common.eventhandler.EventBus.post(EventBus.java:182)
at net.minecraftforge.common.ForgeHooks.onLivingUpdate(ForgeHooks.java:581)
at net.minecraft.entity.EntityLivingBase.onUpdate(EntityLivingBase.java:2321)
at net.minecraft.entity.EntityLiving.onUpdate(EntityLiving.java:346)
at net.minecraft.world.World.updateEntityWithOptionalForce(World.java:2174)
at net.minecraft.world.WorldServer.updateEntityWithOptionalForce(WorldServer.java:871)
at net.minecraft.world.World.updateEntity(World.java:2133)

-- Entity being ticked --
Details:
Entity Type: minecraft:sheep (net.minecraft.entity.passive.EntitySheep)
Entity ID: 0
Entity Name: Овца
Entity's Exact location: 698.53, 4.00, 823.25
Entity's Block location: World: (698,4,823), Chunk: (at 10,0,7 in 43,51; contains blocks 688,0,816 to 703,255,831), Region: (1,1; contains chunks 32,32 to 63,63, blocks 512,0,512 to 1023,255,1023)
Entity's Momentum: 0.00, -0.08, 0.00
Entity's Passengers: []
Entity's Vehicle: ~~ERROR~~ NullPointerException: null
Stacktrace:
at net.minecraft.world.World.updateEntities(World.java:1934)
at net.minecraft.world.WorldServer.updateEntities(WorldServer.java:643)

-- Affected level --
Details:
Level name: Новый мир
All players: 0 total; []
Chunk stats: ServerChunkCache: 625 Drop: 0
Level seed: -875017853216645664
Level generator: ID 01 - flat, ver 0. Features enabled: true
Level generator options:
Level spawn location: World: (774,4,874), Chunk: (at 6,0,10 in 48,54; contains blocks 768,0,864 to 783,255,879), Region: (1,1; contains chunks 32,32 to 63,63, blocks 512,0,512 to 1023,255,1023)
Level time: 43911 game time, 7274 day time
Level dimension: 0
Level storage version: 0x04ABD - Anvil
Level weather: Rain time: 1299 (now: false), thunder time: 82961 (now: false)
Level game mode: Game mode: creative (ID 1). Hardcore: false. Cheats: true
Stacktrace:
at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:842)
at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:743)
at net.minecraft.server.integrated.IntegratedServer.tick(IntegratedServer.java:192)
at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:592)
at java.lang.Thread.run(Unknown Source)

попробовал так сделать, все равно
 

timaxa007

Модератор
5,831
409
672
попробовал так сделать
Просто сделал, но не используешь где надо?
Java:
       @SubscribeEvent
       public void onUpdateEvent(LivingEvent.LivingUpdateEvent event) {

        if(event.getEntityLiving() instanceof EntityPlayer) {
            EntityPlayer player = (EntityPlayer)event.getEntityLiving();

            ICAPCustomInventory cap = player.getCapability(CAPCustomInventoryProvider.INVENTORY_CAP, null);
            CustomInventory inv = cap.getInventory();
          
            if (inv.getStackInSlot(0).getItem() == ItemsRegister.ring) {
                System.out.println("item");
            } else {
                System.out.println("noitem");
            }
       }
       }
 
Сверху