Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

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

  • Author
3 minutes ago, Luis_ST said:

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

the best way would be to replace the target Goals of the Mobs in EntityJoinWorldEvent, with your own TargetGoals
 

  • Author
2 minutes ago, Luis_ST said:

the best way would be to replace the target Goals of the Mobs in EntityJoinWorldEvent, with your own TargetGoals
 

i dont have TargetGoals ...

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

  • Author
11 minutes ago, 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

How to replace the target, first removeGoal(), then addGoal()?

  • Author
23 hours ago, Luis_ST said:

if you want to replace all TargetGoals, yes

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

23 hours ago, 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

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

  • Author
1 hour ago, Luis_ST said:

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

I used a custom goal, he is no longer attacking me, but he still looks at me

  • Author
7 minutes ago, Luis_ST said:

show your code

 @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 ....
                }
            }
        }
    }
}

 

On 12/12/2021 at 12:35 PM, Luis_ST said:

overwrite the tick method and call super if you want to run the vanilla logic

 

  • Author
25 minutes ago, Luis_ST said:

 

This is useful, but not complete. I stand in front of the monster and sometimes look at me.

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

 

2 hours ago, Spring said:

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

which Monster and of which Monsters do you want to change the TargetGoals, all?

  • Author
9 hours ago, Luis_ST said:

你想改变目标目标,哪些怪物和哪些怪物,所有?

yes 

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

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

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 2:59 PM, Spring said:

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

then you also need to replace the LookAtPlayerGoal

  • Author
18 hours ago, 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

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

Edited by Spring

3 hours ago, Spring said:

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

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

  • Author
15 hours ago, Luis_ST said:

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

 

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));
            }
        }

 

On 12/16/2021 at 5:55 AM, Spring said:

then add my custom LootAtGoal to the monster

Did you remove the existing Goal?

  • Author
28 minutes ago, Luis_ST said:

Did you remove the existing Goal?

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.