package com.elmakers.mine.bukkit.action.builtin;

import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;

import com.elmakers.mine.bukkit.action.BaseSpellAction;
import com.elmakers.mine.bukkit.api.action.CastContext;
import com.elmakers.mine.bukkit.api.spell.SpellResult;

public class FeedAction extends BaseSpellAction
{
    private static int MAX_FOOD_LEVEL = 20;

    private Integer foodLevel;
    private int feedAmount;
    private float saturationAmount;
    private boolean clearExhaustion;

    @Override
    public void prepare(CastContext context, ConfigurationSection parameters)
    {
        super.prepare(context, parameters);
        int defaultFeed = 20;
        float defaultSaturation = 20;
        if (parameters.contains("food_level")) {
            defaultFeed = 0;
            defaultSaturation = 0;
            foodLevel = parameters.getInt("food_level");
        } else {
            foodLevel = null;
        }
        feedAmount = parameters.getInt("feed", defaultFeed);
        saturationAmount = (float)parameters.getDouble("saturation", defaultSaturation);
        clearExhaustion = parameters.getBoolean("exhaustion", true);
    }

    @Override
    public SpellResult perform(CastContext context)
    {
        Entity targetEntity = context.getTargetEntity();
        if (!(targetEntity instanceof Player))
        {
            return SpellResult.NO_TARGET;
        }

        Player player = (Player)targetEntity;
        if (feedAmount > 0 && player.getFoodLevel() >= MAX_FOOD_LEVEL)
        {
            return SpellResult.NO_TARGET;
        }
        if (feedAmount < 0 && player.getFoodLevel() == 0)
        {
            return SpellResult.NO_TARGET;
        }
        if (clearExhaustion)
        {
            player.setExhaustion(0);
        }
        if (saturationAmount != 0)
        {
            player.setSaturation(saturationAmount);
        }
        if (foodLevel != null) {
            player.setFoodLevel(foodLevel);
        }
        if (feedAmount != 0)
        {
            player.setFoodLevel(Math.min(MAX_FOOD_LEVEL, Math.max(0, player.getFoodLevel() + feedAmount)));
        }

        return SpellResult.CAST;
    }

    @Override
    public boolean requiresTargetEntity()
    {
        return true;
    }
}

@@ -43,16 +43,71 @@
 iltin;%0A%0A
+import java.util.Arrays;%0Aimport java.util.Collection;%0A%0A
 import o
@@ -378,22 +378,120 @@
 ll.Spell
-Result
+;%0Aimport com.elmakers.mine.bukkit.api.spell.SpellResult;%0Aimport com.elmakers.mine.bukkit.spell.BaseSpell
 ;%0A%0Apubli
@@ -2480,11 +2480,869 @@
 e;%0A    %7D
+%0A%0A    @Override%0A    public void getParameterNames(Spell spell, Collection%3CString%3E parameters) %7B%0A        super.getParameterNames(spell, parameters);%0A        parameters.add(%22food_level%22);%0A        parameters.add(%22feed%22);%0A        parameters.add(%22saturation%22);%0A        parameters.add(%22exhaustion%22);%0A    %7D%0A%0A    @Override%0A    public void getParameterOptions(Spell spell, String parameterKey, Collection%3CString%3E examples) %7B%0A        if (parameterKey.equals(%22food_level%22) %7C%7C parameterKey.equals(%22feed%22) %7C%7C  parameterKey.equals(%22saturation%22)) %7B%0A            examples.add(%220%22);%0A            examples.add(%221%22);%0A            examples.add(%2210%22);%0A        %7D else if (parameterKey.equals(%22exhaustion%22)) %7B%0A            examples.addAll(Arrays.asList(BaseSpell.EXAMPLE_BOOLEANS));%0A        %7D else %7B%0A            super.getParameterOptions(spell, parameterKey, examples);%0A        %7D%0A    %7D
 %0A%7D%0A

Add some tab-completion to Feed action