-
-
Notifications
You must be signed in to change notification settings - Fork 103
/
HealthCommand.java
161 lines (139 loc) · 6.12 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package com.denizenscript.denizen.scripts.commands.entity;
import com.denizenscript.denizen.utilities.Utilities;
import com.denizenscript.denizen.utilities.debugging.dB;
import com.denizenscript.denizen.npc.traits.HealthTrait;
import com.denizenscript.denizen.objects.dEntity;
import com.denizenscript.denizencore.exceptions.InvalidArgumentsException;
import com.denizenscript.denizencore.objects.Element;
import com.denizenscript.denizencore.objects.aH;
import com.denizenscript.denizencore.objects.dList;
import com.denizenscript.denizencore.scripts.ScriptEntry;
import com.denizenscript.denizencore.scripts.commands.AbstractCommand;
import java.util.Arrays;
import java.util.List;
public class HealthCommand extends AbstractCommand {
// <--[command]
// @Name Health
// @Syntax health ({npc}/<entity>|...) [<#>] (state:{true}/false/toggle)
// @Required 1
// @Short Changes the target's maximum health.
// @Group entity
//
// @Description
// Use this command to modify an entity's maximum health. If the target is an NPC,
// you can use the 'state' argument to enable, disable, or toggle the Health trait
// (which is used to track the NPC's health, and handle actions such as 'on death')
// the Health trait will be enabled by default.
// By default, this command will target the linked NPC but can be set to target any
// other living entity, such as a player or mob.
// Additionally, you may input a list of entities, each one will calculate the effects
// explained above.
//
// @Tags
// <e@entity.health>
// <n@npc.has_trait[health]>
//
// @Usage
// Use to set the NPC's maximum health to 50.
// - health 50
//
// @Usage
// Use to disable tracking of health value on the NPC.
// - health state:false
//
// @Usage
// Use to change a player's health limit to 50.
// - health <player> 50
//
// @Usage
// Use to change a list of entities' health limits all to 50.
// - health <player.location.find.living_entities.within[10]> 50
// -->
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
// Interpret arguments
for (aH.Argument arg : aH.interpretArguments(scriptEntry.aHArgs)) {
if (!scriptEntry.hasObject("target")
&& arg.matches("player")) {
if (!Utilities.entryHasPlayer(scriptEntry)) {
throw new InvalidArgumentsException("No player attached!");
}
scriptEntry.addObject("target", Arrays.asList(Utilities.getEntryPlayer(scriptEntry).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 (!Utilities.entryHasNPC(scriptEntry)) {
throw new InvalidArgumentsException("Missing NPC!");
}
scriptEntry.addObject("target", Arrays.asList(Utilities.getEntryNPC(scriptEntry).getDenizenEntity()));
}
}
@Override
public void execute(ScriptEntry scriptEntry) {
Element qty = scriptEntry.getElement("qty");
Element action = scriptEntry.getElement("action");
List<dEntity> targets = (List<dEntity>) scriptEntry.getObject("target");
if (scriptEntry.dbCallShouldDebug()) {
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 = new 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!");
}
}
}
}
}