Jump to content

Recommended Posts

Posted (edited)

When the player owns certain items, prevent monsters from attacking the player

 

like this  :    look this

 

@SubscribeEvent
        public static void onTarget(final LivingSetAttackTargetEvent event) {
            LivingEntity target = event.getTarget();
            if (target instanceof PlayerEntity) {
                PlayerEntity player = (PlayerEntity) target;
                if (invalid(player)) {
                    event.setCanceled(true);
                }
            }
        }

 


[19:06:23] [Server thread/ERROR] [ne.mi.ev.EventBus/EVENTBUS]: Exception caught during firing event: Attempted to call Event#setCanceled() on a non-cancelable event of type: net.minecraftforge.event.entity.living.LivingSetAttackTargetEvent
    Index: 1
    Listeners:
        0: NORMAL
        1: ASM: class ocean.chinacraft.common.item.armor.SilkNightArmor$RenderNightArmor onPreRenderPlayer(Lnet/minecraftforge/event/entity/living/LivingSetAttackTargetEvent;)V
java.lang.UnsupportedOperationException: Attempted to call Event#setCanceled() on a non-cancelable event of type: net.minecraftforge.event.entity.living.LivingSetAttackTargetEvent
    at net.minecraftforge.eventbus.api.Event.setCanceled(Event.java:95)
    at ocean.chinacraft.common.item.armor.SilkNightArmor$RenderNightArmor.onPreRenderPlayer(SilkNightArmor.java:79)
    at net.minecraftforge.eventbus.ASMEventHandler_16_RenderNightArmor_onPreRenderPlayer_LivingSetAttackTargetEvent.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.onLivingSetAttackTarget(ForgeHooks.java:335)
    at net.minecraft.entity.MobEntity.setTarget(MobEntity.java:201)
    at net.minecraft.entity.ai.goal.TargetGoal.canContinueToUse(TargetGoal.java:65)
    at net.minecraft.entity.ai.goal.PrioritizedGoal.canContinueToUse(PrioritizedGoal.java:25)
    at net.minecraft.entity.ai.goal.GoalSelector.lambda$tick$2(GoalSelector.java:52)
    at java.base/java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:176)
    at java.base/java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:177)
    at java.base/java.util.Iterator.forEachRemaining(Iterator.java:133)
    at java.base/java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801)
    at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484)
    at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474)
    at java.base/java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:150)
    at java.base/java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:173)
    at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
    at java.base/java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:497)
    at net.minecraft.entity.ai.goal.GoalSelector.tick(GoalSelector.java:53)
    at net.minecraft.entity.MobEntity.serverAiStep(MobEntity.java:679)
    at net.minecraft.entity.LivingEntity.aiStep(LivingEntity.java:2411)
    at net.minecraft.entity.MobEntity.aiStep(MobEntity.java:488)
    at net.minecraft.entity.monster.MonsterEntity.aiStep(MonsterEntity.java:40)
    at net.minecraft.entity.monster.ZombieEntity.aiStep(ZombieEntity.java:231)
    at net.minecraft.entity.LivingEntity.tick(LivingEntity.java:2158)
    at net.minecraft.entity.MobEntity.tick(MobEntity.java:300)
    at net.minecraft.entity.monster.ZombieEntity.tick(ZombieEntity.java:205)
    at net.minecraft.world.server.ServerWorld.tickNonPassenger(ServerWorld.java:611)
    at net.minecraft.world.World.guardEntityTick(World.java:554)
    at net.minecraft.world.server.ServerWorld.tick(ServerWorld.java:404)
    at net.minecraft.server.MinecraftServer.tickChildren(MinecraftServer.java:851)
    at net.minecraft.server.MinecraftServer.tickServer(MinecraftServer.java:787)
    at net.minecraft.server.integrated.IntegratedServer.tickServer(IntegratedServer.java:78)
    at net.minecraft.server.MinecraftServer.runServer(MinecraftServer.java:642)
    at net.minecraft.server.MinecraftServer.lambda$spin$0(MinecraftServer.java:232)
    at java.base/java.lang.Thread.run(Thread.java:829)

[19:06:23] [Server thread/FATAL] [ne.mi.co.ForgeMod/]: Preparing crash report with UUID 3b7c1cee-90fc-4e08-93d0-bbc4cda9dac9
[19:06:23] [Server thread/ERROR] [minecraft/MinecraftServer]: Encountered an unexpected exception
net.minecraft.crash.ReportedException: Ticking entity
    at net.minecraft.server.MinecraftServer.tickChildren(MinecraftServer.java:855) ~[forge:?] {re:classloading,pl:accesstransformer:B}
    at net.minecraft.server.MinecraftServer.tickServer(MinecraftServer.java:787) ~[forge:?] {re:classloading,pl:accesstransformer:B}
    at net.minecraft.server.integrated.IntegratedServer.tickServer(IntegratedServer.java:78) ~[forge:?] {re:classloading,pl:runtimedistcleaner:A}
    at net.minecraft.server.MinecraftServer.runServer(MinecraftServer.java:642) ~[forge:?] {re:classloading,pl:accesstransformer:B}
    at net.minecraft.server.MinecraftServer.lambda$spin$0(MinecraftServer.java:232) ~[forge:?] {re:classloading,pl:accesstransformer:B}
    at java.lang.Thread.run(Thread.java:829) [?:?] {}
Caused by: java.lang.UnsupportedOperationException: Attempted to call Event#setCanceled() on a non-cancelable event of type: net.minecraftforge.event.entity.living.LivingSetAttackTargetEvent
    at net.minecraftforge.eventbus.api.Event.setCanceled(Event.java:95) ~[eventbus-4.0.0.jar:?] {}
    at ocean.chinacraft.common.item.armor.SilkNightArmor$RenderNightArmor.onPreRenderPlayer(SilkNightArmor.java:79) ~[main/:?] {re:classloading}
    at net.minecraftforge.eventbus.ASMEventHandler_16_RenderNightArmor_onPreRenderPlayer_LivingSetAttackTargetEvent.invoke(.dynamic) ~[?:?] {}
    at net.minecraftforge.eventbus.ASMEventHandler.invoke(ASMEventHandler.java:85) ~[eventbus-4.0.0.jar:?] {}
    at net.minecraftforge.eventbus.EventBus.post(EventBus.java:302) ~[eventbus-4.0.0.jar:?] {}
    at net.minecraftforge.eventbus.EventBus.post(EventBus.java:283) ~[eventbus-4.0.0.jar:?] {}
    at net.minecraftforge.common.ForgeHooks.onLivingSetAttackTarget(ForgeHooks.java:335) ~[forge:?] {re:classloading}
    at net.minecraft.entity.MobEntity.setTarget(MobEntity.java:201) ~[forge:?] {re:classloading,pl:accesstransformer:B}
    at net.minecraft.entity.ai.goal.TargetGoal.canContinueToUse(TargetGoal.java:65) ~[forge:?] {re:classloading}
    at net.minecraft.entity.ai.goal.PrioritizedGoal.canContinueToUse(PrioritizedGoal.java:25) ~[forge:?] {re:classloading}
    at net.minecraft.entity.ai.goal.GoalSelector.lambda$tick$2(GoalSelector.java:52) ~[forge:?] {re:classloading}
    at java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:176) ~[?:?] {}
    at java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:177) ~[?:?] {}
    at java.util.Iterator.forEachRemaining(Iterator.java:133) ~[?:?] {}
    at java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801) ~[?:?] {}
    at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484) ~[?:?] {}
    at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474) ~[?:?] {}
    at java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:150) ~[?:?] {}
    at java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:173) ~[?:?] {}
    at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) ~[?:?] {}
    at java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:497) ~[?:?] {}
    at net.minecraft.entity.ai.goal.GoalSelector.tick(GoalSelector.java:53) ~[forge:?] {re:classloading}
    at net.minecraft.entity.MobEntity.serverAiStep(MobEntity.java:679) ~[forge:?] {re:classloading,pl:accesstransformer:B}
    at net.minecraft.entity.LivingEntity.aiStep(LivingEntity.java:2411) ~[forge:?] {re:classloading}
    at net.minecraft.entity.MobEntity.aiStep(MobEntity.java:488) ~[forge:?] {re:classloading,pl:accesstransformer:B}
    at net.minecraft.entity.monster.MonsterEntity.aiStep(MonsterEntity.java:40) ~[forge:?] {re:classloading}
    at net.minecraft.entity.monster.ZombieEntity.aiStep(ZombieEntity.java:231) ~[forge:?] {re:classloading}
    at net.minecraft.entity.LivingEntity.tick(LivingEntity.java:2158) ~[forge:?] {re:classloading}
    at net.minecraft.entity.MobEntity.tick(MobEntity.java:300) ~[forge:?] {re:classloading,pl:accesstransformer:B}
    at net.minecraft.entity.monster.ZombieEntity.tick(ZombieEntity.java:205) ~[forge:?] {re:classloading}
    at net.minecraft.world.server.ServerWorld.tickNonPassenger(ServerWorld.java:611) ~[forge:?] {re:classloading}
    at net.minecraft.world.World.guardEntityTick(World.java:554) ~[forge:?] {re:classloading,pl:accesstransformer:B}
    at net.minecraft.world.server.ServerWorld.tick(ServerWorld.java:404) ~[forge:?] {re:classloading}
    at net.minecraft.server.MinecraftServer.tickChildren(MinecraftServer.java:851) ~[forge:?] {re:classloading,pl:accesstransformer:B}
    ... 5 more
Disconnected from the target VM, address: '127.0.0.1:62991', transport: 'socket'
AL lib: (EE) alc_cleanup: 1 device not closed

Process finished with exit code -1
 

Edited by Spring
Posted
  On 12/12/2021 at 11:24 AM, Luis_ST said:
Expand  

Yes, but the problem has been bothering me for a long time, I really want to know how to solve this

Posted (edited)
  On 12/12/2021 at 12:24 PM, Luis_ST said:

if you want to replace all TargetGoals, yes

Expand  

hello, I tried to do it like this, but it doesn't work, why?

 

@SubscribeEvent
  public static void onEntityJoinWorldEvent(final EntityJoinWorldEvent event) {
  Entity entity = event.getEntity();
  if (entity.isAlive() && entity instanceof MonsterEntity) {
    MonsterEntity monsterEntity = (MonsterEntity) entity;
    GoalSelector targetSelector = monsterEntity.targetSelector;
    targetSelector.addGoal(1, new MonsterCustomGoal(monsterEntity));
  }
}

public class MonsterCustomGoal extends Goal {

    private final MobEntity entity;

    public MonsterCustomGoal(MobEntity entity) {
        this.entity = entity;
    }

    @Override
    public boolean canUse() {
        return false;
    }

    @Override
    public void tick() {
        LivingEntity target = entity.getTarget();
        if (target instanceof PlayerEntity) {
            entity.setTarget(null);
        }
    }
}

tick not execute

Edited by Spring
Posted
  On 12/12/2021 at 11:35 AM, Luis_ST said:

you need to extends the vanilla Goals, overwrite the tick method and call super if you want to run the vanilla logic

Expand  

extends the vanilla TargetGoals, look into the MonsterEntities for example how to add the Goal

Posted
  On 12/13/2021 at 1:22 PM, Luis_ST said:

show your code

Expand  
 @SubscribeEvent
        public static void onEntityJoinWorldEvent(final EntityJoinWorldEvent event) {
            Entity entity = event.getEntity();
            if (entity.isAlive() && entity instanceof MonsterEntity) {
                MonsterEntity monsterEntity = (MonsterEntity) entity;
                GoalSelector targetSelector = monsterEntity.targetSelector;
                GoalSelector goalSelector = monsterEntity.goalSelector;
                targetSelector.addGoal(1, new MonsterCustomGoal(monsterEntity, false));
                goalSelector.addGoal(2, new MonsterCustomGoal.MonsterCustomLookGoal(monsterEntity,
                        PlayerEntity.class, 8.0F, 0));
            }
        }


public class MonsterCustomGoal extends TargetGoal {

    public MonsterCustomGoal(MobEntity entity, boolean mustSee) {
        super(entity, mustSee);
    }

    @Override
    public boolean canUse() {
        return true;
    }

    @Override
    public void tick() {
        LivingEntity target = mob.getTarget();
        if (target instanceof PlayerEntity) {
            PlayerEntity player = (PlayerEntity) target;
            if (SilkNightArmor.invalid(player)) {
                List<MonsterEntity> monsterEntities = SilkNightArmor.getNearbyEntities(player);
                for (MonsterEntity monster : monsterEntities) {
                    if (monster.getTarget() != null && monster.getTarget().getUUID().equals(player.getUUID())) {
                        monster.setTarget(null);
                    }
                }
            }
        }
    }

    /**
     * need this class ?
     */
    public static class MonsterCustomLookGoal extends LookAtGoal {

        public MonsterCustomLookGoal(MobEntity entity, Class<? extends LivingEntity> livingEntity, float lookDistance
                , float probability) {
            super(entity, livingEntity, lookDistance, probability);
        }

        @Override
        public void tick() {
            if (lookAt != null && lookAt.isAlive() && lookAt instanceof PlayerEntity) {
                PlayerEntity player = (PlayerEntity) lookAt;
                System.out.println(!SilkNightArmor.invalid(player));
                if (SilkNightArmor.invalid(player)) {
                    //dont not look me ....
                }
            }
        }
    }
}

 

Posted
  On 12/13/2021 at 1:59 PM, Spring said:

Does the MonsterCustomLookGoal method need to be retained, or is it to be deleted?

Expand  

okay after a look at the TargetGoals you need to create a custom HurtByTargetGoal and a custom NearestAttackableTargetGoal
in your custom HurtByTargetGoal you need to overwrite canAttack and call super if you want that the Mob can attack the Entity.
in your custom NearestAttackableTargetGoal you need to overwrite also canAttack and call super if you want that the Mob can attack the Entity.

 

  On 12/13/2021 at 1:59 PM, Spring said:

I stand in front of the monster and sometimes look at me.

Expand  

then you also need to replace the LookAtPlayerGoal

Posted (edited)
  On 12/14/2021 at 3:47 PM, Luis_ST said:

okay after a look at the TargetGoals you need to create a custom HurtByTargetGoal and a custom NearestAttackableTargetGoal
in your custom HurtByTargetGoal you need to overwrite canAttack and call super if you want that the Mob can attack the Entity.
in your custom NearestAttackableTargetGoal you need to overwrite also canAttack and call super if you want that the Mob can attack the Entity.

 

then you also need to replace the LookAtPlayerGoal

Expand  

no LookAtPlayerGoal class , my forge version : 1.16.5-36.2.2,only LookAtGoal

Edited by Spring
Posted
  On 12/15/2021 at 1:48 PM, Luis_ST said:

sorry was 1.18 class name, in 1.16.5 it's LookAtGoal

Expand  

 

What I do now is to extend LootAtGoal, then add my custom LootAtGoal to the monster, and then override the tick method, but the parent method is still called. So i don't understand, what to do

 

I'm using translation, if you understand, I'm sorry

 

my custom LookAtGoal class:

public class MonsterLookAtGoal extends LookAtGoal {

    public MonsterLookAtGoal(MobEntity entity, Class<? extends LivingEntity> livingEntity, float lookDistance) {
        super(entity, livingEntity, lookDistance);
    }

    @Override
    public void tick() {

    }
}

caller:

        @SubscribeEvent
        public static void onEntityJoinWorldEvent(final EntityJoinWorldEvent event) {
            Entity entity = event.getEntity();
            if (entity.isAlive() && entity instanceof MobEntity) {
                MobEntity mobEntity = (MobEntity) entity;
                GoalSelector targetSelector = mobEntity.targetSelector;
                targetSelector.addGoal(1, new MonsterAttackGoal(mobEntity, false));
                targetSelector.addGoal(1, new MonsterLookAtGoal(mobEntity, PlayerEntity.class, 8F));
            }
        }

 

Posted (edited)
  On 12/17/2021 at 5:39 AM, Luis_ST said:

Did you remove the existing Goal?

Expand  

this ? 

this.goalSelector.addGoal(8, new LookAtGoal(this, PlayerEntity.class, 8.0F));

How can I delete it? I have tried a lot, but there is no way to delete it. I use the removeGoal method

 

This is the source code of removeGoal. When EntityJoinWorldEvent is called, isRunning is false, so I don’t understand how to use it now

   public void removeGoal(Goal p_85156_1_) {
      this.availableGoals.stream().filter((p_220882_1_) -> {
         return p_220882_1_.getGoal() == p_85156_1_;
      }).filter(PrioritizedGoal::isRunning).forEach(PrioritizedGoal::stop);
      this.availableGoals.removeIf((p_220884_1_) -> {
         return p_220884_1_.getGoal() == p_85156_1_;
      });
   }

 

I use it like this:

                targetSelector.removeGoal(new LookAtGoal(mob, PlayerEntity.class, 8.0F));

 

Edited by Spring

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.