PotionEffect仅在第一次应用

问题描述 投票:1回答:2

我正在创建一个微型mod,在其中我创建了一个具有4种效果的苹果。这是我的代码:

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;
    }
}

[效果是在我第一次吃苹果时应用的,但不适用于我再吃一个苹果的情况。我在屏幕的右上角看到了1个滴答声的效果,所以我猜这些效果是用duration值错误应用的,或者是在应用后立即清除的。我该如何解决?

java minecraft minecraft-forge
2个回答
1
投票

我找到了原因。我并不是每次吃苹果时都创建一个新的PotionEffect,所以PotionEffect的滴答计数等于0;


1
投票

当前,您为每个被食用的苹果使用相同的PotionEffect对象。每次玩家吃苹果时,都需要创建新的PotionEffect对象。也许将您的班级改为这样:

© www.soinside.com 2019 - 2024. All rights reserved.