Jump to content

[1.15.2] Problem with Trying to Remove Potion Effect


Recommended Posts

Posted

I'm new to modding, but I'm trying to make a mod that replaces the hunger effect with weakness. It should check if the player has received the hunger effect, then add a weakness effect of equal strength/duration, and then remove the hunger effect. However, for some reason it won't remove the hunger effect. I'm really hoping someone can explain why it doesn't work and give criticism if I have any bad practices. Below is the event handler class.

 

Thanks in advance!

 

@Mod.EventBusSubscriber(modid = StahTweaks.MOD_ID, bus = Mod.EventBusSubscriber.Bus.FORGE, value = Dist.CLIENT)
public class ModClientEvents {

    @SubscribeEvent
    /** Theoretically checks if the effect gained is hunger and then replaces
     * it with a weakness effect of equal strength
     */
    public static void onPlayerHungerEffect(PotionEvent.PotionAddedEvent event) {

        LivingEntity player = event.getEntityLiving();
        EffectInstance effect = event.getPotionEffect();

        if(effect.getPotion().equals(Effects.HUNGER)) {
            
            player.addPotionEffect(new EffectInstance(Effects.WEAKNESS, effect.getDuration(), effect.getAmplifier()));
            player.removePotionEffect(Effects.HUNGER);

        }

    }

}

 

Posted

Because when the event is triggered the potion has not yet being applied to the entity.

You can use PotionApplicableEvent instead, in there you can set the event result to DENY (see it's javadoc) and apply your own one.

  • Like 1
Posted
13 minutes ago, poopoodice said:

Because when the event is triggered the potion has not yet being applied to the entity.

You can use PotionApplicableEvent instead, in there you can set the event result to DENY (see it's javadoc) and apply your own one.

Oh that makes a lot of sense, thank you!

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.