Как не дать мобам нападать?

Версия Minecraft
1.12.2
198
1
24
Можно ли как-то сделать, чтобы определённые мобы не нападали на игрока, находящегося под определённым эффектом? Например, в моём моде есть эффект "Некромант" - в идеале нежить не должна нападать на игрока с таким эффектом. Хотел ловить эвент LivingSetAttackTargetEvent, проверять, есть ли у игрока нужный эффект, и если да - отменять эвент. Но именно этот эвент отменить нельзя :( Есть ли вообще способ заставить определённых мобов игнорировать определённых игроков?
 

tox1cozZ

aka Agravaine
8,455
598
2,892
А, в 1.12 только одна переменная.

Нет, это EntityLiving#setAttackTarget устанавливает значение переменной attackTarget. Тебе в ивенте нужно установить ей нулл. Только не вздумай вызывать сеттер - уйдешь в бесконечную рекурсию. Либо рефлексией нулл установи, либо AT'шкой сними приват.
 
198
1
24
Добавил AT для EntityLiving.attackTarget, но вот этот код крашит:
Java:
    @SubscribeEvent
    public void onTarget(LivingSetAttackTargetEvent event) {
        if (event.getEntityLiving().isEntityUndead() &&
            event.getTarget().isPotionActive(PotionsRegistry.NECROMANCER)) {
                EntityLiving entity = (EntityLiving) event.getEntityLiving();
                entity.attackTarget = null;           
        }
    }
 
198
1
24
Краш при загрузке мира:
---- Minecraft Crash Report ----
// Hi. I'm Minecraft, and I'm a crashaholic.

Time: 2/13/21 6:21 PM
Description: Ticking entity

java.lang.NullPointerException: Ticking entity
    at com.madalchemist.godsanddemons.EventsHandler.onTarget(EventsHandler.java:171)
    at net.minecraftforge.fml.common.eventhandler.ASMEventHandler_30_EventsHandler_onTarget_LivingSetAttackTargetEvent.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.onLivingSetAttackTarget(ForgeHooks.java:576)
    at net.minecraft.entity.EntityLiving.setAttackTarget(EntityLiving.java:196)
    at net.minecraft.entity.ai.EntityAITarget.resetTask(EntityAITarget.java:136)
    at net.minecraft.entity.ai.EntityAITasks.onUpdateTasks(EntityAITasks.java:100)
    at net.minecraft.entity.EntityLiving.updateEntityActionState(EntityLiving.java:840)
    at net.minecraft.entity.EntityLivingBase.onLivingUpdate(EntityLivingBase.java:2589)
    at net.minecraft.entity.EntityLiving.onLivingUpdate(EntityLiving.java:647)
    at net.minecraft.entity.monster.EntityMob.onLivingUpdate(EntityMob.java:49)
    at net.minecraft.entity.monster.EntityZombie.onLivingUpdate(EntityZombie.java:246)
    at net.minecraft.entity.EntityLivingBase.onUpdate(EntityLivingBase.java:2405)
    at net.minecraft.entity.EntityLiving.onUpdate(EntityLiving.java:346)
    at net.minecraft.entity.monster.EntityMob.onUpdate(EntityMob.java:57)
    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(Thread.java:748)


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

-- Head --
Thread: Server thread
Stacktrace:
    at com.madalchemist.godsanddemons.EventsHandler.onTarget(EventsHandler.java:171)
    at net.minecraftforge.fml.common.eventhandler.ASMEventHandler_30_EventsHandler_onTarget_LivingSetAttackTargetEvent.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.onLivingSetAttackTarget(ForgeHooks.java:576)
    at net.minecraft.entity.EntityLiving.setAttackTarget(EntityLiving.java:196)
    at net.minecraft.entity.ai.EntityAITarget.resetTask(EntityAITarget.java:136)
    at net.minecraft.entity.ai.EntityAITasks.onUpdateTasks(EntityAITasks.java:100)
    at net.minecraft.entity.EntityLiving.updateEntityActionState(EntityLiving.java:840)
    at net.minecraft.entity.EntityLivingBase.onLivingUpdate(EntityLivingBase.java:2589)
    at net.minecraft.entity.EntityLiving.onLivingUpdate(EntityLiving.java:647)
    at net.minecraft.entity.monster.EntityMob.onLivingUpdate(EntityMob.java:49)
    at net.minecraft.entity.monster.EntityZombie.onLivingUpdate(EntityZombie.java:246)
    at net.minecraft.entity.EntityLivingBase.onUpdate(EntityLivingBase.java:2405)
    at net.minecraft.entity.EntityLiving.onUpdate(EntityLiving.java:346)
    at net.minecraft.entity.monster.EntityMob.onUpdate(EntityMob.java:57)
    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:zombie (net.minecraft.entity.monster.EntityZombie)
    Entity ID: 72
    Entity Name: Zombie
    Entity's Exact location: 1561.58, 65.00, -101.44
    Entity's Block location: World: (1561,65,-102), Chunk: (at 9,4,10 in 97,-7; contains blocks 1552,0,-112 to 1567,255,-97), Region: (3,-1; contains chunks 96,-32 to 127,-1, blocks 1536,0,-512 to 2047,255,-1)
    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: New World
    All players: 0 total; []
    Chunk stats: ServerChunkCache: 625 Drop: 0
    Level seed: 4537929924188303795
    Level generator: ID 01 - flat, ver 0. Features enabled: true
    Level generator options: 3;minecraft:bedrock,59*minecraft:stone,3*minecraft:dirt,minecraft:grass;1;village,biome_1,decoration,stronghold,mineshaft,lake,lava_lake,dungeon
    Level spawn location: World: (1541,4,-89), Chunk: (at 5,0,7 in 96,-6; contains blocks 1536,0,-96 to 1551,255,-81), Region: (3,-1; contains chunks 96,-32 to 127,-1, blocks 1536,0,-512 to 2047,255,-1)
    Level time: 136822 game time, 891 day time
    Level dimension: 0
    Level storage version: 0x04ABD - Anvil
    Level weather: Rain time: 20174 (now: true), thunder time: 6095 (now: true)
    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(Thread.java:748)

-- System Details --
Details:
    Minecraft Version: 1.12.2
    Operating System: Windows 7 (amd64) version 6.1
    Java Version: 1.8.0_131, Oracle Corporation
    Java VM Version: Java HotSpot(TM) 64-Bit Server VM (mixed mode), Oracle Corporation
    Memory: 779214184 bytes (743 MB) / 1056309248 bytes (1007 MB) up to 1056309248 bytes (1007 MB)
    JVM Flags: 3 total; -Xincgc -Xmx1024M -Xms1024M
    IntCache: cache: 0, tcache: 0, allocated: 0, tallocated: 0
    FML: MCP 9.42 Powered by Forge 14.23.5.2847 6 mods loaded, 6 mods active
    States: 'U' = Unloaded 'L' = Loaded 'C' = Constructed 'H' = Pre-initialized 'I' = Initialized 'J' = Post-initialized 'A' = Available 'D' = Disabled 'E' = Errored

    | State  | ID            | Version      | Source                                                  | Signature |
    |:------ |:------------- |:------------ |:------------------------------------------------------- |:--------- |
    | LCHIJA | minecraft     | 1.12.2       | minecraft.jar                                           | None      |
    | LCHIJA | mcp           | 9.42         | minecraft.jar                                           | None      |
    | LCHIJA | FML           | 8.0.99.99    | forgeSrc-1.12.2-14.23.5.2847-PROJECT(GodsAndDemons).jar | None      |
    | LCHIJA | forge         | 14.23.5.2847 | forgeSrc-1.12.2-14.23.5.2847-PROJECT(GodsAndDemons).jar | None      |
    | LCHIJA | godsanddemons | 0.0.1        | bin                                                     | None      |
    | LCHIJA | nbtedit       | $version     | NBTEdit-0.7.jar                                         | None      |

    Loaded coremods (and transformers):
    GL info: ~~ERROR~~ RuntimeException: No OpenGL context found in the current thread.
    Profiler Position: N/A (disabled)
    Player Count: 0 / 8; []
    Type: Integrated Server (map_client.txt)
    Is Modded: Definitely; Client brand changed to 'fml,forge'
 

tox1cozZ

aka Agravaine
8,455
598
2,892
Ну ты бы хоть голову включил :)
Если attackTarget может быть нулл, значит в ивенте наверное нужно добавить проверку на нулл? event.getTarget() у тебя нулл в данном случае, никакой проверки нет, ты достаешь из него эффект.
И вообще, тебе стактрейс даже пишет на какой строке ошибка возникла, неужели так сложно продебажить каждый параметр?

@will0376 прекрати писать чушь. Не знаешь - не пиши вообще. Только в заблуждение людей вводишь.
 
Сверху