Edouard Courty
Edouard Courty

Reputation: 82

PotionEffect only applied the first time

I'm creating a tiny mod in which one I created an apple that gives 4 effects. Here is my code:

public class AmethystApple extends FoodBase
{
    List<PotionEffect> effects = new ArrayList<PotionEffect>();

    public AmethystApple(String name, int amount, float saturation, boolean isWolfFood) {
        super(name, amount, saturation, isWolfFood);
        setAlwaysEdible();
        this.registerAppleEffects();
    }

    private void registerAppleEffects()
    {
        PotionEffect speedEffect = new PotionEffect(MobEffects.SPEED, 400, 2);
        PotionEffect resistanceEfect = new PotionEffect(MobEffects.RESISTANCE, 400, 9);
        PotionEffect nauseaEffect = new PotionEffect(MobEffects.NAUSEA, 400, 0);
        PotionEffect hungerEffect = new PotionEffect(MobEffects.HUNGER, 500, 1);
        effects.add(speedEffect); // Speed 3 - 20 sec
        effects.add(resistanceEfect); // Resistance 10 - 20 sec
        effects.add(nauseaEffect); // Nausea 1 - 20 sec
        effects.add(hungerEffect); // Hunger 2 - 25 sec
    }

    @Override
    protected void onFoodEaten(ItemStack stack, World worldIn, EntityPlayer player)
    {
        if (!worldIn.isRemote) {
            for (PotionEffect effect : effects) {
                player.addPotionEffect(effect);
            }
            System.out.println("POTION EFFECTS APPLIED");
        }
    }

    @SideOnly(Side.CLIENT)
    public boolean hasEffect (ItemStack stack)
    {
        return true;
    }
}

The effects are applied the first time I eat the apple, but not when I eat another one. I see the effects for 1 tick in the top right corner of the screen so I guess the effects are applied with a bad duration value or cleared just after they are applied. How can I fix this ?

Upvotes: 1

Views: 57

Answers (2)

Gesit
Gesit

Reputation: 539

Currently, your are using the same PotionEffect objects for every eaten apple. You need to create new PotionEffect objects every time a player does eat an apple. Maybe change your class to something like this:

public class AmethystApple extends FoodBase
{


    public AmethystApple(String name, int amount, float saturation, boolean isWolfFood) {
        super(name, amount, saturation, isWolfFood);
        setAlwaysEdible();
    }

    private List<PotionEffect> registerAppleEffects()
    {
        List<PotionEffect> effects = new ArrayList<PotionEffect>
        PotionEffect speedEffect = new PotionEffect(MobEffects.SPEED, 400, 2);
        PotionEffect resistanceEfect = new PotionEffect(MobEffects.RESISTANCE, 400, 9);
        PotionEffect nauseaEffect = new PotionEffect(MobEffects.NAUSEA, 400, 0);
        PotionEffect hungerEffect = new PotionEffect(MobEffects.HUNGER, 500, 1);
        effects.add(speedEffect); // Speed 3 - 20 sec
        effects.add(resistanceEfect); // Resistance 10 - 20 sec
        effects.add(nauseaEffect); // Nausea 1 - 20 sec
        effects.add(hungerEffect); // Hunger 2 - 25 sec
        return effects
    }

    @Override
    protected void onFoodEaten(ItemStack stack, World worldIn, EntityPlayer player)
    {
        if (!worldIn.isRemote) {
            List<PotionEffect> effects = this.registerAppleEffects();
            for (PotionEffect effect : effects) {
                player.addPotionEffect(effect);
            }
            System.out.println("POTION EFFECTS APPLIED");
        }
    }

    @SideOnly(Side.CLIENT)
    public boolean hasEffect (ItemStack stack)
    {
        return true;
    }
}

Upvotes: 1

Edouard Courty
Edouard Courty

Reputation: 82

I found the reason. I was not creating a new PotionEffect every time I eat the apple so the PotionEffect tick count was equal to 0;

The tickcount of an effect is stored in the PotionEffect itself and not related to the EntityPlayer !

I fixed this by rewriting the content of my for loop to:

for (PotionEffect effect : effects) {
    player.addPotionEffect(new PotionEffect(effect));
}

Upvotes: 1

Related Questions