Jump to content

[Version 1.18.2, SOLVED] Custom status effect that changes the player's (or any entity's) hitbox size


Recommended Posts

Posted (edited)

Hello,

I am having a little trouble implementing an efficient method of changing the player's size whenever that player gains a certain custom status effect. The player's size would revert back to normal when the effect ends.

This is the class for my custom status effect that makes the player bigger (currently I am focusing on the player's hitbox size rather than its rendered size):

public class BigEffect extends MobEffect {
    public BigEffect(MobEffectCategory mobEffectCategory, int color) {
        super(mobEffectCategory, color);
    }

    @Override
    public void applyEffectTick(LivingEntity pLivingEntity, int pAmplifier) {
        if (!pLivingEntity.level.isClientSide() && pLivingEntity instanceof Player player) {
             // forcibly trigger an EntityEvent.Size event
             player.setForcedPose(Pose.CROUCHING);
        }
        super.applyEffectTick(pLivingEntity, pAmplifier);
    }

    @Override
    public boolean isDurationEffectTick(int pDuration, int pAmplifier) {
        return true;
    }
}

This is the event handler that takes in a EntityEvent.Size event:

@SubscribeEvent
public static void big(EntityEvent.Size event) {
    if (!event.getEntity().level.isClientSide() && event.getEntity() instanceof Player player) {
        if (player.getActiveEffectsMap() != null && player.hasEffect(ModEffects.BIG.get())) {
            event.setNewSize(new EntityDimensions(5.0F, 3.6F, false), true);
        }
        else {
            event.setNewSize(new EntityDimensions(0.6F, 1.8F, false), true);
        }
    }
}

 

It technically works, but I have a feeling that my current method is too crude, and I don't want that. Plus, the player's size does not go back to normal when the potion effect wears off (I even tried crouching or swimming to trigger the EntityEvent.Size event, but to no avail).

 

Even when the hitbox is enlarged, the rendered bounding box (which shows when I press F3 + B) does not get bigger. Oh, and another thing, when the player touches a block with its enlarged hitbox, it starts shaking and becomes unable to jump unless it moves away.

 

Are EntityEvent.Size events the only way to resize the player's hitbox? Is it even the right thing to use? Should I still use capabilities, despite the fact that I can easily check a player's potion effects with hasEffect()/getEffect()?

Edited by LeeCrafts
Posted (edited)

---------------------------------------

CLEANER, MORE EFFICIENT IMPLEMENTATION FOR ALL LIVING ENTITIES, NOT JUST PLAYERS:

https://forums.minecraftforge.net/topic/111159-version-1182-solved-custom-status-effect-that-changes-the-players-or-any-entitys-size-part-ii/

(If you just want to increase the hitbox size) https://forums.minecraftforge.net/topic/111362-version-1182-solved-increased-hitbox-size-only-works-for-entity-collision-but-not-for-block-collision/

---------------------------------------

 

Solved. Turns out that I should not be using EntityEvent.Size. Instead, I have to use the AABB class and call setBoundingBox(). Here is the code in a nutshell if any of you were wondering:

public class BigEffect extends MobEffect {
    public BigEffect(MobEffectCategory mobEffectCategory, int color) {
        super(mobEffectCategory, color);
    }

    @Override
    public void applyEffectTick(LivingEntity pLivingEntity, int pAmplifier) {
        // (This if-statement makes it not work)
        // if (!pLivingEntity.level.isClientSide()) {
            pLivingEntity.setBoundingBox(new AABB(pLivingEntity.getX()+2.5, pLivingEntity.getY()+3.6, pLivingEntity.getZ()+2.5, pLivingEntity.getX()-2.5, pLivingEntity.getY(), pLivingEntity.getZ()-2.5));
        // }
        super.applyEffectTick(pLivingEntity, pAmplifier);
    }

    @Override
    public boolean isDurationEffectTick(int pDuration, int pAmplifier) {
        return true;
    }
}

 

As you can see, no capabilities are needed. And there is no need to check if the player does NOT have the custom status effect because setBoundingBox() is temporary for only 1 tick (or so it seems).

 

As mentioned in the comment in my code above, it does not work if I make sure that the entity is not in the client side. I guess that's just the nature of AABBs.

 

(Side note, but I just don't know how to use setNewSize() from EntityEvent.Size. I don't really need it anymore, but what kinda confuses me is that I see other people using it and that it works for them. Not a big deal though.)

Edited by LeeCrafts
update solution
  • LeeCrafts changed the title to [Version 1.18.2, SOLVED] Custom status effect that changes the player's (or any entity's) hitbox size

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.