/
HealthCommand.java
109 lines (85 loc) · 4.61 KB
/
HealthCommand.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package net.aufdemrand.denizen.scripts.commands.entity;
import net.aufdemrand.denizen.BukkitScriptEntryData;
import net.aufdemrand.denizen.npc.traits.HealthTrait;
import net.aufdemrand.denizen.objects.dEntity;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizencore.exceptions.CommandExecutionException;
import net.aufdemrand.denizencore.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizencore.objects.Element;
import net.aufdemrand.denizencore.objects.aH;
import net.aufdemrand.denizencore.objects.dList;
import net.aufdemrand.denizencore.scripts.ScriptEntry;
import net.aufdemrand.denizencore.scripts.commands.AbstractCommand;
import java.util.Arrays;
import java.util.List;
public class HealthCommand extends AbstractCommand {
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
// Interpret arguments
for (aH.Argument arg : aH.interpret(scriptEntry.getArguments())) {
if (!scriptEntry.hasObject("target")
&& arg.matches("player")) {
if (!((BukkitScriptEntryData) scriptEntry.entryData).hasPlayer())
throw new InvalidArgumentsException("No player attached!");
scriptEntry.addObject("target", Arrays.asList(((BukkitScriptEntryData) scriptEntry.entryData).getPlayer().getDenizenEntity()));
}
else if (!scriptEntry.hasObject("qty")
&& arg.matchesPrimitive(aH.PrimitiveType.Double))
scriptEntry.addObject("qty", arg.asElement());
else if (!scriptEntry.hasObject("target")
&& arg.matchesArgumentList(dEntity.class))
scriptEntry.addObject("target", arg.asType(dList.class).filter(dEntity.class, scriptEntry));
else if (!scriptEntry.hasObject("action")
&& arg.matchesPrefix("state"))
scriptEntry.addObject("action", arg.asElement());
else arg.reportUnhandled();
}
// Check for required information
if (!scriptEntry.hasObject("qty") && !scriptEntry.hasObject("action"))
throw new InvalidArgumentsException("Must specify a quantity!");
if (!scriptEntry.hasObject("target")) {
if (!((BukkitScriptEntryData) scriptEntry.entryData).hasNPC())
throw new InvalidArgumentsException("Missing NPC!");
scriptEntry.addObject("target", Arrays.asList(((BukkitScriptEntryData) scriptEntry.entryData).getNPC().getDenizenEntity()));
}
}
@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
Element qty = scriptEntry.getElement("qty");
Element action = scriptEntry.getElement("action");
List<dEntity> targets = (List<dEntity>) scriptEntry.getObject("target");
dB.report(scriptEntry, getName(), (qty != null ? qty.debug() : "") +
(action != null ? action.debug() : "") +
aH.debugObj("target", targets.toString()));
if (qty == null && action == null)
dB.echoError(scriptEntry.getResidingQueue(), "Null quantity!");
if (action == null)
action = Element.TRUE;
for (dEntity target : targets) {
if (target.isCitizensNPC()) {
if (action.asString().equalsIgnoreCase("true"))
target.getDenizenNPC().getCitizen().addTrait(HealthTrait.class);
else if (action.asString().equalsIgnoreCase("false"))
target.getDenizenNPC().getCitizen().removeTrait(HealthTrait.class);
else if (target.getDenizenNPC().getCitizen().hasTrait(HealthTrait.class))
target.getDenizenNPC().getCitizen().removeTrait(HealthTrait.class);
else
target.getDenizenNPC().getCitizen().addTrait(HealthTrait.class);
}
if (qty != null) {
if (target.isCitizensNPC()) {
if (target.getDenizenNPC().getCitizen().hasTrait(HealthTrait.class))
target.getDenizenNPC().getCitizen().getTrait(HealthTrait.class).setMaxhealth((int) qty.asFloat());
else
dB.echoError(scriptEntry.getResidingQueue(), "NPC doesn't have health trait!");
}
else if (target.isLivingEntity()) {
target.getLivingEntity().setMaxHealth(qty.asDouble());
}
else {
dB.echoError(scriptEntry.getResidingQueue(), "Entity '" + target.identify() + "'is not alive!");
}
}
}
}
}