From af8d7885dea0db3b5bbbc1286a69463228ea40d9 Mon Sep 17 00:00:00 2001 From: MorphanOne Date: Thu, 25 Jul 2013 07:56:06 -0400 Subject: [PATCH] Change Heal command into 0.9 syntax --- .../scripts/commands/CommandRegistry.java | 2 +- .../scripts/commands/entity/HealCommand.java | 117 ++++++------------ 2 files changed, 42 insertions(+), 77 deletions(-) diff --git a/src/main/java/net/aufdemrand/denizen/scripts/commands/CommandRegistry.java b/src/main/java/net/aufdemrand/denizen/scripts/commands/CommandRegistry.java index 5647dc9813..6c8440c828 100644 --- a/src/main/java/net/aufdemrand/denizen/scripts/commands/CommandRegistry.java +++ b/src/main/java/net/aufdemrand/denizen/scripts/commands/CommandRegistry.java @@ -167,7 +167,7 @@ public void registerCoreMembers() { "HEAD", "head (player) [skin:]", 0); registerCoreMember(HealCommand.class, - "HEAL", "heal (qty:<#.#>) (target:)", 0); + "HEAL", "heal (<#.#>) (entities:|...)", 0); registerCoreMember(HealthCommand.class, "HEALTH", "health (state:true/false/toggle) (set_max:<#>)", 1); diff --git a/src/main/java/net/aufdemrand/denizen/scripts/commands/entity/HealCommand.java b/src/main/java/net/aufdemrand/denizen/scripts/commands/entity/HealCommand.java index 5de8f98256..0b631855a7 100644 --- a/src/main/java/net/aufdemrand/denizen/scripts/commands/entity/HealCommand.java +++ b/src/main/java/net/aufdemrand/denizen/scripts/commands/entity/HealCommand.java @@ -1,106 +1,71 @@ package net.aufdemrand.denizen.scripts.commands.entity; -import org.bukkit.craftbukkit.v1_6_R2.entity.CraftLivingEntity; -import org.bukkit.entity.Player; +import java.util.List; import net.aufdemrand.denizen.exceptions.CommandExecutionException; import net.aufdemrand.denizen.exceptions.InvalidArgumentsException; +import net.aufdemrand.denizen.objects.Element; import net.aufdemrand.denizen.objects.aH; -import net.aufdemrand.denizen.objects.aH.ArgumentType; +import net.aufdemrand.denizen.objects.dEntity; +import net.aufdemrand.denizen.objects.dList; import net.aufdemrand.denizen.scripts.ScriptEntry; import net.aufdemrand.denizen.scripts.commands.AbstractCommand; -import net.aufdemrand.denizen.utilities.debugging.dB; -import net.aufdemrand.denizen.utilities.debugging.dB.Messages; -import net.citizensnpcs.api.npc.NPC; /** * Heals a Player or NPC. * - * @author Jeremy Schroeder, Mason Adkins + * @author Jeremy Schroeder, Mason Adkins, Morphan1 */ public class HealCommand extends AbstractCommand { - /* HEAL (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: - * HEAL AMT:20 TARGET:NPC - * HEAL AMT:5 - * HEAL - * - */ - - private enum TargetType { NPC, PLAYER } - @Override public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException { - TargetType targetType = TargetType.PLAYER; - Integer amount = Integer.MAX_VALUE; - - for (String arg : scriptEntry.getArguments()) { + for (aH.Argument arg : aH.interpret(scriptEntry.getArguments())) { - if (aH.matchesQuantity(arg) || aH.matchesDouble(arg) - || aH.matchesValueArg("amt", arg, ArgumentType.Double)) - amount = aH.getIntegerFrom(arg); + if (!scriptEntry.hasObject("amount") + && (arg.matchesPrimitive(aH.PrimitiveType.Double) + || arg.matchesPrimitive(aH.PrimitiveType.Integer))) + scriptEntry.addObject("amount", arg.asElement()); - else if (aH.matchesValueArg("target", arg, ArgumentType.String)) { - try { - targetType = TargetType.valueOf(aH.getStringFrom(arg).toUpperCase()); - } catch (Exception e) { dB.echoError("Invalid TARGET! Valid: NPC, PLAYER"); } - - } else throw new InvalidArgumentsException(Messages.ERROR_UNKNOWN_ARGUMENT, arg); + else if (!scriptEntry.hasObject("entities") + && arg.matchesPrefix("entity, entities, e, target, targets")) { + scriptEntry.addObject("entities", ((dList) arg.asType(dList.class)).filter(dEntity.class)); + } } - // 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); - - scriptEntry.addObject("target", targetType) - .addObject("amount", amount); + if (!scriptEntry.hasObject("entities")) + scriptEntry.addObject("entities", scriptEntry.getPlayer()); + + if (!scriptEntry.hasObject("amount")) + scriptEntry.addObject("amount", Integer.MAX_VALUE); } - @Override + @SuppressWarnings("unchecked") + @Override public void execute(ScriptEntry scriptEntry) throws CommandExecutionException { - TargetType target = (TargetType) scriptEntry.getObject("target"); - Double amount = (Double) scriptEntry.getObject("amount"); - - dB.report(getName(), - aH.debugObj("Target", (target == TargetType.PLAYER ? scriptEntry.getPlayer().getName() - : scriptEntry.getNPC().getName())) - + aH.debugObj("Amount", (amount == Double.MAX_VALUE ? "Full" - : String.valueOf(amount)))); - - switch (target) { - - case NPC: - NPC npc = scriptEntry.getNPC().getCitizen(); - - // Set health to max - if (amount == Integer.MAX_VALUE) - npc.getBukkitEntity().setHealth(npc.getBukkitEntity().getMaxHealth()); - // else, set Health - else npc.getBukkitEntity().setHealth(npc.getBukkitEntity().getHealth() + amount); - return; - - case PLAYER: - Player player = scriptEntry.getPlayer().getPlayerEntity(); - // Set to max health - if (amount == Integer.MAX_VALUE) player.setHealth(player.getMaxHealth()); - // else, increase health - else ((CraftLivingEntity) player).getHandle().setHealth((float) (player.getHealth() + amount)); - } - + if (scriptEntry.getObject("amount").equals(Integer.MAX_VALUE)) { + if (scriptEntry.getObject("entities").equals(scriptEntry.getPlayer())) + scriptEntry.getPlayer().getDenizenEntity().getLivingEntity().setHealth(scriptEntry.getPlayer().getDenizenEntity().getLivingEntity().getMaxHealth()); + else { + List entities = (List) scriptEntry.getObject("entities"); + for (dEntity entity : entities) + entity.getLivingEntity().setHealth(entity.getLivingEntity().getMaxHealth()); + } + } + else { + Double amount = ((Element) scriptEntry.getObject("amount")).asDouble(); + if (scriptEntry.getObject("entities").equals(scriptEntry.getPlayer())) + scriptEntry.getPlayer().getDenizenEntity().getLivingEntity().setHealth(scriptEntry.getPlayer().getDenizenEntity().getLivingEntity().getHealth() + amount); + else { + List entities = (List) scriptEntry.getObject("entities"); + for (dEntity entity : entities) + entity.getLivingEntity().setHealth(entity.getLivingEntity().getHealth() + amount); + } + + } } }