-
-
Notifications
You must be signed in to change notification settings - Fork 106
/
HealCommand.java
70 lines (56 loc) · 2.64 KB
/
HealCommand.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package net.aufdemrand.denizen.scripts.commands.entity;
import java.util.ArrayList;
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.dEntity;
import net.aufdemrand.denizen.objects.dList;
import net.aufdemrand.denizen.scripts.ScriptEntry;
import net.aufdemrand.denizen.scripts.commands.AbstractCommand;
/**
* Heals an entity.
*
* @author Jeremy Schroeder, Mason Adkins, Morphan1
*/
public class HealCommand extends AbstractCommand {
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (aH.Argument arg : aH.interpret(scriptEntry.getArguments())) {
if (!scriptEntry.hasObject("amount")
&& (arg.matchesPrimitive(aH.PrimitiveType.Double)
|| arg.matchesPrimitive(aH.PrimitiveType.Integer)))
scriptEntry.addObject("amount", arg.asElement());
else if (!scriptEntry.hasObject("entities")
&& arg.matchesArgumentList(dEntity.class)) {
scriptEntry.addObject("entities", ((dList) arg.asType(dList.class)).filter(dEntity.class));
}
}
if (!scriptEntry.hasObject("amount"))
scriptEntry.addObject("amount", Integer.MAX_VALUE);
if (!scriptEntry.hasObject("entities")) {
List<dEntity> entities = new ArrayList<dEntity>();
if (scriptEntry.getPlayer() != null)
entities.add(scriptEntry.getPlayer().getDenizenEntity());
else if (scriptEntry.getNPC() != null)
entities.add(scriptEntry.getNPC().getDenizenEntity());
else
throw new InvalidArgumentsException("No valid target entities found.");
scriptEntry.addObject("entities", entities);
}
}
@SuppressWarnings("unchecked")
@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
List<dEntity> entities = (List<dEntity>) scriptEntry.getObject("entities");
if (scriptEntry.getObject("amount").equals(Integer.MAX_VALUE))
for (dEntity entity : entities)
entity.getLivingEntity().setHealth(entity.getLivingEntity().getMaxHealth());
else {
Double amount = ((Element) scriptEntry.getObject("amount")).asDouble();
for (dEntity entity : entities)
entity.getLivingEntity().setHealth(entity.getLivingEntity().getHealth() + amount);
}
}
}