Jump to content

[1.18.1] Prevent mob (living entity) from attacking / moving


Adil Yilan

Recommended Posts

Hi,

I am trying to implement "stun" mob effect which would prevent living entity to move or attack.

For prevention of attack I have used LivingAttackEvent like this:
 

    @SubscribeEvent
    public static void onAttacked(final LivingAttackEvent event) {

        // Get source of the damage.
        DamageSource damageSource = event.getSource();
        
        // Get reference to entity that caused the damage.
        Entity entity = damageSource.getEntity();

        // IF: Entity is not alive.
        if (!(entity instanceof LivingEntity)) {
            return;
        }

        // Cast entity to mob.
        LivingEntity mob = (LivingEntity)entity;
        
        // Get reference to level where entity is living.
        Level level = entity.getLevel();

        // IF: Code is executing on the client side.
        if (level.isClientSide()) {
            return;
        }

        // IF: Mob does not have stunned effect applied.
        if(!mob.hasEffect(PlayerMobEffects.STUNNED.get())){
            return;
        }
        
        // Cancel the event - prevent mob from doing anything.
        event.setCanceled(true);
    }

However this does not work as expected as mob still makes the attack but damage is not done so it does not really look like a stun - it's more like its damage is reduced to 0.

I have managed to totally block mob by canceling LivingUpdateEvent - but that has other side effects as living entity is not updated with anything - can't be damaged, can't be hit/burn by sun - basically it becomes immortal. :D

I have also tried with LivingSetAttackTargetEvent but this event can't be canceled. :(

Is there a way to just stop entity from making an attack?

Same question regarding the movement - is there a way to stop living entity from moving?

Thanks!

Link to comment
Share on other sites

15 hours ago, Adil Yilan said:

Same question regarding the movement - is there a way to stop living entity from moving?

 

Hi!

You can just set the x and z delta movement to zero. But don't change the y delta movement, otherwise the entity wouldn't fall on the ground if the block under it is removed.

  • Like 1

Sorry if my Posts are weird sometimes, I just try to help and learn as much as I can :D

Also: PLEASE use SPOILERS for logs!

Link to comment
Share on other sites

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.