Skip to content

Commit

Permalink
Change Heal command into 0.9 syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
Morphan1 committed Jul 25, 2013
1 parent 5037d76 commit af8d788
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 77 deletions.
Expand Up @@ -167,7 +167,7 @@ public void registerCoreMembers() {
"HEAD", "head (player) [skin:<name>]", 0);

registerCoreMember(HealCommand.class,
"HEAL", "heal (qty:<#.#>) (target:<entity>)", 0);
"HEAL", "heal (<#.#>) (entities:<entity>|...)", 0);

registerCoreMember(HealthCommand.class,
"HEALTH", "health (state:true/false/toggle) (set_max:<#>)", 1);
Expand Down
@@ -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<dEntity> entities = (List<dEntity>) 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<dEntity> entities = (List<dEntity>) scriptEntry.getObject("entities");
for (dEntity entity : entities)
entity.getLivingEntity().setHealth(entity.getLivingEntity().getHealth() + amount);
}

}
}
}

0 comments on commit af8d788

Please sign in to comment.