All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.nukkit.item.food.FoodEffective Maven / Gradle / Ivy

There is a newer version: 1.20.40-r1
Show newest version
package cn.nukkit.item.food;

import cn.nukkit.Player;
import cn.nukkit.potion.Effect;

import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * @author Snake1999
 * @since 2016/1/13
 */
public class FoodEffective extends Food {

    protected final Map effects = new LinkedHashMap<>();

    public FoodEffective(int restoreFood, float restoreSaturation) {
        this.setRestoreFood(restoreFood);
        this.setRestoreSaturation(restoreSaturation);
    }

    public FoodEffective addEffect(Effect effect) {
        return addChanceEffect(1F, effect);
    }

    public FoodEffective addChanceEffect(float chance, Effect effect) {
        if (chance > 1f) chance = 1f;
        if (chance < 0f) chance = 0f;
        effects.put(effect, chance);
        return this;
    }

    @Override
    protected boolean onEatenBy(Player player) {
        super.onEatenBy(player);
        List toApply = new LinkedList<>();
        effects.forEach((effect, chance) -> {
            if (chance >= Math.random()) toApply.add(effect.clone());
        });
        toApply.forEach(player::addEffect);
        return true;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy