Не могу понять, как выдать уровень за убийство моба

Версия Minecraft
1.16.5
API
Forge
41
1
0
Пытаюсь выдать уровень за убийство моба, но при его убийстве в конслои появляются ошибки и код не отробатывает, в чём проблема, что я сделал не так?
addExpForKillMob:
@SubscribeEvent
    public static void addExpForKillMob(LivingDeathEvent event) {
        if (event.getEntity() instanceof MobEntity) {
            if (!(event.getEntity().isAlive())) {
                StalinRpg.LOGGER.info("----------------------->Mob is death");
                PlayerEntity player = (PlayerEntity) event.getEntity();

                IBaseExperienceCapability expCap = IGenericCapability.getUnwrappedCapability(player,
                        PlayerExperienceCapabilityProvider.LEVEL_CAP);

                expCap.addLevel(1, player);
            }
        }
    }

Ошибка
ERROR:
[13:55:04] [Server thread/ERROR] [ne.mi.ev.EventBus/EVENTBUS]: Exception caught during firing event: net.minecraft.entity.monster.ZombieEntity cannot be cast to net.minecraft.entity.player.PlayerEntity
    Index: 1
    Listeners:
        0: NORMAL
        1: ASM: class net.stalin.stalinrpg.eventsubscriber.common.PlayerCapabilityForgeEventSubscriber addExpForKillMob(Lnet/minecraftforge/event/entity/living/LivingDeathEvent;)V
java.lang.ClassCastException: net.minecraft.entity.monster.ZombieEntity cannot be cast to net.minecraft.entity.player.PlayerEntity
    at net.stalin.stalinrpg.eventsubscriber.common.PlayerCapabilityForgeEventSubscriber.addExpForKillMob(PlayerCapabilityForgeEventSubscriber.java:161)
    at net.minecraftforge.eventbus.ASMEventHandler_4_PlayerCapabilityForgeEventSubscriber_addExpForKillMob_LivingDeathEvent.invoke(.dynamic)
    at net.minecraftforge.eventbus.ASMEventHandler.invoke(ASMEventHandler.java:85)
    at net.minecraftforge.eventbus.EventBus.post(EventBus.java:302)
    at net.minecraftforge.eventbus.EventBus.post(EventBus.java:283)
    at net.minecraftforge.common.ForgeHooks.onLivingDeath(ForgeHooks.java:374)
    at net.minecraft.entity.LivingEntity.die(LivingEntity.java:1183)
    at net.minecraft.entity.LivingEntity.hurt(LivingEntity.java:1059)
    at net.minecraft.entity.monster.MonsterEntity.hurt(MonsterEntity.java:64)
    at net.minecraft.entity.monster.ZombieEntity.hurt(ZombieEntity.java:262)
    at net.minecraft.entity.player.PlayerEntity.attack(PlayerEntity.java:1100)
    at net.minecraft.entity.player.ServerPlayerEntity.attack(ServerPlayerEntity.java:1292)
    at net.minecraft.network.play.ServerPlayNetHandler.handleInteract(ServerPlayNetHandler.java:1205)
    at net.minecraft.network.play.client.CUseEntityPacket.handle(CUseEntityPacket.java:80)
    at net.minecraft.network.play.client.CUseEntityPacket.handle(CUseEntityPacket.java:15)
    at net.minecraft.network.PacketThreadUtil.lambda$ensureRunningOnSameThread$0(PacketThreadUtil.java:19)
    at net.minecraft.util.concurrent.TickDelayedTask.run(TickDelayedTask.java:17)
    at net.minecraft.util.concurrent.ThreadTaskExecutor.doRunTask(ThreadTaskExecutor.java:136)
    at net.minecraft.util.concurrent.RecursiveEventLoop.doRunTask(RecursiveEventLoop.java:22)
    at net.minecraft.server.MinecraftServer.doRunTask(MinecraftServer.java:734)
    at net.minecraft.server.MinecraftServer.doRunTask(MinecraftServer.java:159)
    at net.minecraft.util.concurrent.ThreadTaskExecutor.pollTask(ThreadTaskExecutor.java:109)
    at net.minecraft.server.MinecraftServer.pollTaskInternal(MinecraftServer.java:717)
    at net.minecraft.server.MinecraftServer.pollTask(MinecraftServer.java:711)
    at net.minecraft.util.concurrent.ThreadTaskExecutor.managedBlock(ThreadTaskExecutor.java:119)
    at net.minecraft.server.MinecraftServer.waitUntilNextTick(MinecraftServer.java:697)
    at net.minecraft.server.MinecraftServer.runServer(MinecraftServer.java:646)
    at net.minecraft.server.MinecraftServer.lambda$spin$0(MinecraftServer.java:232)
    at java.lang.Thread.run(Thread.java:750)

Я понимаю, что ZombieEntity нельзя преобразовать в PlayerEntity, но как можно сделать иначе?
 
Решение
Нус рабоат вроде.
Java:
@SubscribeEvent
    public static void addExpForKillMob(LivingDeathEvent event) {
        if (event.getEntity() instanceof MobEntity) {
            MobEntity mobEntity = (MobEntity) event.getEntity();
            DamageSource damageSour = mobEntity.getLastDamageSource();
            if (damageSour != null) {
                if (damageSour.getEntity() instanceof PlayerEntity) {
                    PlayerEntity player = (PlayerEntity) damageSour.getEntity();

                    IBaseExperienceCapability expCap = IGenericCapability.getUnwrappedCapability(player,
                            PlayerExperienceCapabilityProvider.LEVEL_CAP);

                    expCap.addExperience(1.5f, player)...
145
13
42
Ты либо скопировал какую-то часть кода, либо вообще не понимаешь как работают касты. У тебя уже есть проверка, что моб наследует класс MobEntity, если тебе нужен игрок, в чём проблема проверить, что моб является экземпляром класса PlayerEntity?
 
41
1
0
Нус рабоат вроде.
Java:
@SubscribeEvent
    public static void addExpForKillMob(LivingDeathEvent event) {
        if (event.getEntity() instanceof MobEntity) {
            MobEntity mobEntity = (MobEntity) event.getEntity();
            DamageSource damageSour = mobEntity.getLastDamageSource();
            if (damageSour != null) {
                if (damageSour.getEntity() instanceof PlayerEntity) {
                    PlayerEntity player = (PlayerEntity) damageSour.getEntity();

                    IBaseExperienceCapability expCap = IGenericCapability.getUnwrappedCapability(player,
                            PlayerExperienceCapabilityProvider.LEVEL_CAP);

                    expCap.addExperience(1.5f, player);

                    player.sendMessage(new StringTextComponent("Current Experience:" + expCap.getExperience()), player.getUUID());

                    StalinRpg.LOGGER.info("------------->Exp total:" + expCap.getExperience());
                }
            }
        }
    }
 
Последнее редактирование:
Сверху