Search in sources :

Example 1 with HungerTrait

use of com.denizenscript.denizen.npc.traits.HungerTrait in project Denizen-For-Bukkit by DenizenScript.

the class FeedCommand method execute.

@Override
public void execute(ScriptEntry scriptEntry) {
    PlayerTag player = scriptEntry.getObjectTag("targetplayer");
    NPCTag npc = scriptEntry.getObjectTag("targetnpc");
    ElementTag amount = scriptEntry.getElement("amount");
    ElementTag saturation = scriptEntry.getElement("saturation");
    if (scriptEntry.dbCallShouldDebug()) {
        Debug.report(scriptEntry, getName(), player, npc, amount, saturation);
    }
    if (npc != null) {
        if (!npc.getCitizen().hasTrait(HungerTrait.class)) {
            Debug.echoError(scriptEntry, "This NPC does not have the HungerTrait enabled! Use /trait hunger");
            return;
        }
        npc.getCitizen().getOrAddTrait(HungerTrait.class).feed(amount.asInt());
    } else {
        int result = Math.max(0, Math.min(20, player.getPlayerEntity().getFoodLevel() + amount.asInt()));
        player.getPlayerEntity().setFoodLevel(result);
        float satResult = Math.max(0, Math.min(20, player.getPlayerEntity().getSaturation() + saturation.asFloat()));
        player.getPlayerEntity().setSaturation(satResult);
        Debug.echoDebug(scriptEntry, "Player food level updated to " + result + " food and " + satResult + " saturation.");
    }
}
Also used : PlayerTag(com.denizenscript.denizen.objects.PlayerTag) NPCTag(com.denizenscript.denizen.objects.NPCTag) ElementTag(com.denizenscript.denizencore.objects.core.ElementTag) HungerTrait(com.denizenscript.denizen.npc.traits.HungerTrait)

Aggregations

HungerTrait (com.denizenscript.denizen.npc.traits.HungerTrait)1 NPCTag (com.denizenscript.denizen.objects.NPCTag)1 PlayerTag (com.denizenscript.denizen.objects.PlayerTag)1 ElementTag (com.denizenscript.denizencore.objects.core.ElementTag)1