Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
42 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
117 changes: 41 additions & 76 deletions
117
src/main/java/net/aufdemrand/denizen/scripts/commands/entity/HealCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
|
||
} | ||
} | ||
} |