Jump to content

How can I make my mob completely invincibile to all types of damage [1.17]


Recommended Posts

Posted
public class Electricity extends Zombie implements IAnimatable
{

    public static final EntityDataAccessor<Integer> STATE = SynchedEntityData.defineId(Electricity.class, EntityDataSerializers.INT);
    private boolean invulnerable;

    private int lifeTicks = 42;

    private final AnimationFactory factory = new AnimationFactory(this);

    private <E extends IAnimatable> PlayState predicate(AnimationEvent<E> event)
    {
            event.getController().setAnimation(new AnimationBuilder().addAnimation("lightning", true));
            return PlayState.CONTINUE;
    }
    @Override
    public void registerControllers(AnimationData data)
    {
        data.addAnimationController(new AnimationController<Electricity>
                (this, "controller", 0, this::predicate));
    }

    @Override
    public AnimationFactory getFactory()
    {
        return this.factory;
    }

    public Electricity(EntityType<? extends Zombie> p_i48549_1_, Level p_i48549_2_)
    {
        super(p_i48549_1_, p_i48549_2_);
    }

    public static AttributeSupplier.Builder createAttributes()
    {
        return Mob.createMobAttributes()
                .add(Attributes.MAX_HEALTH, 1000.0D)
                .add(Attributes.MOVEMENT_SPEED, 0.0D)
                .add(Attributes.ATTACK_DAMAGE, 25.0D)
                .add(Attributes.FOLLOW_RANGE, 45.0D)
                .add(Attributes.SPAWN_REINFORCEMENTS_CHANCE);
    }


    @Override
    public boolean doHurtTarget(Entity entityIn)
    {
        if (!super.doHurtTarget(entityIn))
        {
            return false;
        }
        else
        {
            if (entityIn instanceof LivingEntity)
            {
                ((LivingEntity)entityIn).addEffect(new MobEffectInstance(MobEffects.CONFUSION, 140));
            }
            return true;
        }
    }

    @Override
    protected void defineSynchedData()
    {
        super.defineSynchedData();
        this.entityData.define(STATE, 0);
    }

    @Override
    protected void registerGoals()
    {
        super.registerGoals();
        this.targetSelector.addGoal(2, new NearestAttackableTargetGoal<>(this, Player.class, true));
        this.targetSelector.addGoal(3, new NearestAttackableTargetGoal<>(this, Monster.class, false));
        this.targetSelector.addGoal(3, new NearestAttackableTargetGoal<>(this, FlyingMob.class, false));
        this.targetSelector.addGoal(3, new NearestAttackableTargetGoal<>(this, Animal.class, false));
    }


    public boolean isInvulnerableTo(DamageSource p_20122_) {
        return this.isRemoved() || this.invulnerable && p_20122_ != DamageSource.OUT_OF_WORLD && !p_20122_.isCreativePlayer();
    }

    public boolean isInvulnerable() {
        return this.invulnerable;
    }

    public void setInvulnerable(boolean p_20332_) {
        this.invulnerable = p_20332_;
    }
    @Nullable
    @Override
    protected SoundEvent getHurtSound(@Nonnull DamageSource damageSourceIn)
    {
        this.playSound(SoundEvents.GHAST_HURT, 0.0F, 0.0F);
        return null;
    }

    @Nullable
    @Override
    protected SoundEvent getDeathSound()
    {
        this.playSound(SoundEvents.LIGHTNING_BOLT_IMPACT, 7.0F, 1.0F);
        this.playSound(SoundEvents.LIGHTNING_BOLT_IMPACT, 7.0F, 1.0F);
        this.playSound(SoundEvents.LIGHTNING_BOLT_IMPACT, 7.0F, 1.0F);
        this.playSound(SoundEvents.LIGHTNING_BOLT_IMPACT, 7.0F, 1.0F);
        return null;
    }

    @Nullable
    @Override
    protected SoundEvent getAmbientSound()
    {
        this.playSound(SoundEvents.LIGHTNING_BOLT_THUNDER, 7.0F, 1.0F);
        this.playSound(SoundEvents.LIGHTNING_BOLT_THUNDER, 7.0F, 1.0F);
        this.playSound(SoundEvents.LIGHTNING_BOLT_THUNDER, 7.0F, 1.0F);
        this.playSound(SoundEvents.LIGHTNING_BOLT_THUNDER, 7.0F, 1.0F);
        this.playSound(SoundEvents.LIGHTNING_BOLT_THUNDER, 7.0F, 1.0F);
        return null;
    }

    public void tick()
    {
        super.tick();
            if (--this.lifeTicks < 0)
            {
                this.remove(RemovalReason.DISCARDED);
            }
    }

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

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

    @Override
    protected void pushEntities()
    {
    }
}

There it is

Posted
5 minutes ago, diesieben07 said:

This is not how you use these methods. Do not play sounds in these methods.

 

Why did you copy-paste this from the Entity class? Doing this makes zero sense and achieves nothing. If you want your entity to actually be invulnerable, you need to either call setInvulnerable(true) from the constructor or override isInvulnerable and isInvulnerableTo to always return true. The difference is that the first options allows users to still override it using e.g. the data command.

Oh my goodness I actually forgot I could do that I apologize but thank you again!

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.