From e5cbf19c0c7c2f112431a46b0b03165060d797dc Mon Sep 17 00:00:00 2001 From: mcmonkey4eva Date: Tue, 1 Oct 2013 19:15:40 -0700 Subject: [PATCH] Update Feed Command (Hunger trait doesn't work?) --- .../scripts/commands/core/InjectCommand.java | 2 +- .../commands/core/_templateCommand.java | 53 ++++--- .../scripts/commands/entity/FeedCommand.java | 141 +++++++++--------- 3 files changed, 95 insertions(+), 101 deletions(-) diff --git a/src/main/java/net/aufdemrand/denizen/scripts/commands/core/InjectCommand.java b/src/main/java/net/aufdemrand/denizen/scripts/commands/core/InjectCommand.java index 751b788c5e..b1f4589689 100644 --- a/src/main/java/net/aufdemrand/denizen/scripts/commands/core/InjectCommand.java +++ b/src/main/java/net/aufdemrand/denizen/scripts/commands/core/InjectCommand.java @@ -29,7 +29,7 @@ public class InjectCommand extends AbstractCommand { // @Code // # +----------------------------------- // # | Script Injection tutorial - // + // # | // # | Script injection is an alternative way to run task scripts. Pluses include the ability // # | to use and modify established definitions and context contained in the script. Check out // # | this example on some simple ways to use script injection. diff --git a/src/main/java/net/aufdemrand/denizen/scripts/commands/core/_templateCommand.java b/src/main/java/net/aufdemrand/denizen/scripts/commands/core/_templateCommand.java index 7af888eafe..1dd99dadf6 100644 --- a/src/main/java/net/aufdemrand/denizen/scripts/commands/core/_templateCommand.java +++ b/src/main/java/net/aufdemrand/denizen/scripts/commands/core/_templateCommand.java @@ -21,34 +21,34 @@ */ public class _templateCommand extends AbstractCommand /* implements dExternal */ { -// @Override -// public void load() { -// activate().as("MyCommand").withOptions("mycommand [#] [l@location]", 2); -// } + // @Override + // public void load() { + // activate().as("MyCommand").withOptions("mycommand [#] [l@location]", 2); + // } @Override public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException { -// Interpret arguments + // Interpret arguments -for (aH.Argument arg : aH.interpret(scriptEntry.getArguments())) { + for (aH.Argument arg : aH.interpret(scriptEntry.getArguments())) { -// if (!scriptEntry.hasObject("required_integer") -// && arg.matchesPrimitive(aH.PrimitiveType.Integer)) -// scriptEntry.addObject("required_integer", arg.asElement()); + // if (!scriptEntry.hasObject("required_integer") + // && arg.matchesPrimitive(aH.PrimitiveType.Integer)) + // scriptEntry.addObject("required_integer", arg.asElement()); -// if (!scriptEntry.hasObject("required_location") -// && arg.matchesArgumentType(dLocation.class)) -// scriptEntry.addObject("required_location", arg.asType(dLocation.class)); + // if (!scriptEntry.hasObject("required_location") + // && arg.matchesArgumentType(dLocation.class)) + // scriptEntry.addObject("required_location", arg.asType(dLocation.class)); -} + } -// Check for required information + // Check for required information -// if (!scriptEntry.hasObject("required_object")) -// throw new InvalidArgumentsException("Must have required object!"); + // if (!scriptEntry.hasObject("required_object")) + // throw new InvalidArgumentsException("Must have required object!"); } @@ -56,24 +56,23 @@ public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException @Override public void execute(ScriptEntry scriptEntry) throws CommandExecutionException { -// Fetch required objects + // Fetch required objects -// Element required_integer = (Element) scriptEntry.getObject("required_integer"); -// dLocation required_location = (dLocation) scriptEntry.getObject("required_location"); + // Element required_integer = (Element) scriptEntry.getObject("required_integer"); + // dLocation required_location = (dLocation) scriptEntry.getObject("required_location"); -// Debug the execution + // Debug the execution -// dB.report(getName(), required_integer.debug() + required_location.debug()); + // dB.report(getName(), required_integer.debug() + required_location.debug()); -// Do the execution + // Do the execution -// INSERT -// YOUR -// CODE -// HERE :) + // INSERT + // YOUR + // CODE + // HERE :) } - } diff --git a/src/main/java/net/aufdemrand/denizen/scripts/commands/entity/FeedCommand.java b/src/main/java/net/aufdemrand/denizen/scripts/commands/entity/FeedCommand.java index 666578fc94..1c1e3c9604 100644 --- a/src/main/java/net/aufdemrand/denizen/scripts/commands/entity/FeedCommand.java +++ b/src/main/java/net/aufdemrand/denizen/scripts/commands/entity/FeedCommand.java @@ -1,5 +1,9 @@ package net.aufdemrand.denizen.scripts.commands.entity; +import net.aufdemrand.denizen.objects.Element; +import net.aufdemrand.denizen.objects.dNPC; +import net.aufdemrand.denizen.objects.dPlayer; +import net.citizensnpcs.api.ai.TargetType; import org.bukkit.entity.LivingEntity; import org.bukkit.entity.Player; @@ -23,90 +27,81 @@ public class FeedCommand extends AbstractCommand { - @Override - public void onEnable() { - // nothing to do here - } - - /* FEED (AMT:#) (TARGET:NPC|PLAYER) */ - - /* - * Arguments: [] - Required, () - Optional - * (AMT:#) 1-20, usually. - * (TARGET:NPC|PLAYER) Specifies which object is the target of the feeding effects. - * Default: Player, unless not available - * - * Example Usage: - * FEED AMT:20 TARGET:NPC - * FEED AMT:5 - * FEED - * - */ - - private enum TargetType { NPC, PLAYER } - - private int amount; - private LivingEntity target; - private TargetType targetType; - @Override public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException { - // Must reset ALL private variables, else information left over from last time - // might be used. - targetType = TargetType.PLAYER; - amount = Integer.MAX_VALUE; - // Set target to Player by default, if available - if (scriptEntry.getPlayer() != null) target = scriptEntry.getPlayer().getPlayerEntity(); - else target = null; - - for (String arg : scriptEntry.getArguments()) { - - if (aH.matchesQuantity(arg) || aH.matchesValueArg("amt", arg, ArgumentType.Integer)) { - amount = aH.getIntegerFrom(arg); - dB.echoDebug(Messages.DEBUG_SET_QUANTITY, String.valueOf(amount)); - - } else if (aH.matchesValueArg("target", arg, ArgumentType.String)) { - try { - targetType = TargetType.valueOf(aH.getStringFrom(arg)); - dB.echoDebug("TARGET to FEED: " + targetType.name()); - } catch (Exception e) { - dB.echoError("Invalid TARGET! Valid: NPC, PLAYER"); - } - - } else throw new InvalidArgumentsException(Messages.ERROR_UNKNOWN_ARGUMENT, arg); + for (aH.Argument arg : aH.interpret(scriptEntry.getArguments())) { + if (arg.matchesPrimitive(aH.PrimitiveType.Integer) + && !scriptEntry.hasObject("amount")) + scriptEntry.addObject("amount", arg.asElement()); + + else if (arg.matchesArgumentType(dPlayer.class) + && !scriptEntry.hasObject("targetplayer") + && !scriptEntry.hasObject("targetnpc")) + scriptEntry.addObject("targetplayer", arg.asType(dPlayer.class)); + + else if (arg.matchesArgumentType(dNPC.class) + && !scriptEntry.hasObject("targetplayer") + && !scriptEntry.hasObject("targetnpc")) + scriptEntry.addObject("targetnpc", arg.asType(dNPC.class)); + + // Backwards compatibility + else if (arg.matches("NPC") + && !scriptEntry.hasObject("targetplayer") + && !scriptEntry.hasObject("targetnpc") + && scriptEntry.hasNPC()) + scriptEntry.addObject("targetnpc", scriptEntry.getNPC()); + + else if (arg.matches("PLAYER") + && !scriptEntry.hasObject("targetplayer") + && !scriptEntry.hasObject("targetnpc") + && scriptEntry.hasPlayer()) + scriptEntry.addObject("targetplayer", scriptEntry.getPlayer()); + + else + dB.echoError(Messages.ERROR_UNKNOWN_ARGUMENT, arg.raw_value); } - // If TARGET is NPC/PLAYER and no NPC/PLAYER available, throw exception. - if (targetType == TargetType.PLAYER && scriptEntry.getPlayer() == null) throw new InvalidArgumentsException(Messages.ERROR_NO_PLAYER); - else if (targetType == TargetType.NPC && scriptEntry.getNPC() == null) throw new InvalidArgumentsException(Messages.ERROR_NO_NPCID); - // If TARGET is NPC, set entity. - else if (targetType == TargetType.NPC) target = scriptEntry.getNPC().getEntity(); + if (!scriptEntry.hasObject("targetplayer") && + !scriptEntry.hasObject("targetnpc")) { + if (scriptEntry.hasPlayer()) + scriptEntry.addObject("targetplayer", scriptEntry.getPlayer()); + else if (scriptEntry.hasNPC()) + scriptEntry.addObject("targetnpc", scriptEntry.getNPC()); + else + throw new InvalidArgumentsException(Messages.ERROR_NO_PLAYER); + } + if (!scriptEntry.hasObject("amount")) + scriptEntry.addObject("amount", new Element(9999)); } - @Override public void execute(ScriptEntry scriptEntry) throws CommandExecutionException { - // Target is a NPC - if (CitizensAPI.getNPCRegistry().isNPC(target)) { - NPC npc = CitizensAPI.getNPCRegistry().getNPC(target); - if (!npc.hasTrait(HungerTrait.class)) throw new CommandExecutionException("This NPC does not have the HungerTrait enabled! Use /trait hunger"); - // Set hunger level to zero - if (amount == Integer.MAX_VALUE) npc.getTrait(HungerTrait.class).setHunger(0.00); - // else, feed NPC - else npc.getTrait(HungerTrait.class).feed(amount); - - // Target is a Player - } else { - // Set to max food level - if (amount == Integer.MAX_VALUE) ((Player) target).setFoodLevel(20); - // else, increase food levels - else ((Player) target).setFoodLevel(((Player) target).getFoodLevel() + amount); + dPlayer player = (dPlayer) scriptEntry.getObject("targetplayer"); + dNPC npc = (dNPC) scriptEntry.getObject("targetnpc"); + Element amount = scriptEntry.getElement("amount"); + + dB.report(getName(), + (player == null?"": player.debug()) + +(npc == null?"":npc.debug()) + +amount.debug()); + + if (npc != null) { + if (!npc.getCitizen().hasTrait(HungerTrait.class)) { + dB.echoError("This NPC does not have the HungerTrait enabled! Use /trait hunger"); + return; + } + npc.getCitizen().getTrait(HungerTrait.class).feed(amount.asInt()); + } + else if (player != null) { + if (95999 - player.getPlayerEntity().getFoodLevel() < amount.asInt()) // Setting hunger too high = error + amount = new Element(95999 - player.getPlayerEntity().getFoodLevel()); + player.getPlayerEntity().setFoodLevel(player.getPlayerEntity().getFoodLevel() + amount.asInt()); + } + else { + dB.echoError("No target?"); // Mostly just here to quiet code analyzers. } - } - - }