-
-
Notifications
You must be signed in to change notification settings - Fork 104
/
HealthCommand.java
172 lines (165 loc) · 7.41 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
162
163
164
165
166
167
168
169
170
171
172
package com.denizenscript.denizen.scripts.commands.entity;
import com.denizenscript.denizen.utilities.Utilities;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizen.npc.traits.HealthTrait;
import com.denizenscript.denizen.objects.EntityTag;
import com.denizenscript.denizencore.exceptions.InvalidArgumentsException;
import com.denizenscript.denizencore.objects.Argument;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.ArgumentHelper;
import com.denizenscript.denizencore.objects.core.ListTag;
import com.denizenscript.denizencore.scripts.ScriptEntry;
import com.denizenscript.denizencore.scripts.commands.AbstractCommand;
import java.util.Collections;
import java.util.List;
public class HealthCommand extends AbstractCommand {
public HealthCommand() {
setName("health");
setSyntax("health ({npc}/<entity>|...) [<#>] (state:{true}/false/toggle) (heal)");
setRequiredArguments(1, 4);
isProcedural = false;
}
// <--[command]
// @Name Health
// @Syntax health ({npc}/<entity>|...) [<#>] (state:{true}/false/toggle) (heal)
// @Required 1
// @Maximum 4
// @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.
//
// Optionally specify the 'heal' argument to automatically heal the entity to the new health value.
// If not specified, the entity's health will remain wherever it was
// (so for example a change from 20 max to 50 max will leave an entity with 20 health out of 50 max).
//
// Additionally, you may input a list of entities, each one will calculate the effects explained above.
//
// @Tags
// <EntityTag.health>
// <EntityTag.health_max>
// <NPCTag.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 and current health both to 50.
// - health <player> 50 heal
//
// @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 {
for (Argument arg : scriptEntry) {
if (!scriptEntry.hasObject("target")
&& arg.matches("player")) {
if (!Utilities.entryHasPlayer(scriptEntry)) {
throw new InvalidArgumentsException("No player attached!");
}
scriptEntry.addObject("target", Collections.singletonList(Utilities.getEntryPlayer(scriptEntry).getDenizenEntity()));
}
else if (!scriptEntry.hasObject("quantity")
&& arg.matchesFloat()) {
scriptEntry.addObject("quantity", arg.asElement());
}
else if (!scriptEntry.hasObject("target")
&& arg.matchesArgumentList(EntityTag.class)) {
scriptEntry.addObject("target", arg.asType(ListTag.class).filter(EntityTag.class, scriptEntry));
}
else if (!scriptEntry.hasObject("action")
&& arg.matchesPrefix("state")) {
scriptEntry.addObject("action", arg.asElement());
}
else if (!scriptEntry.hasObject("heal")
&& arg.matches("heal")) {
scriptEntry.addObject("heal", new ElementTag(true));
}
else {
arg.reportUnhandled();
}
}
if (!scriptEntry.hasObject("quantity") && !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", Collections.singletonList(Utilities.getEntryNPC(scriptEntry).getDenizenEntity()));
}
scriptEntry.defaultObject("heal", new ElementTag(false));
}
@Override
public void execute(ScriptEntry scriptEntry) {
ElementTag quantity = scriptEntry.getElement("quantity");
ElementTag action = scriptEntry.getElement("action");
ElementTag heal = scriptEntry.getElement("heal");
List<EntityTag> targets = (List<EntityTag>) scriptEntry.getObject("target");
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), (quantity != null ? quantity.debug() : "") +
(action != null ? action.debug() : "") +
heal.debug() +
db("target", targets.toString()));
}
if (quantity == null && action == null) {
Debug.echoError(scriptEntry.getResidingQueue(), "Null quantity!");
}
if (action == null) {
action = new ElementTag(true);
}
for (EntityTag 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 (quantity != null) {
if (target.isCitizensNPC()) {
if (target.getDenizenNPC().getCitizen().hasTrait(HealthTrait.class)) {
HealthTrait trait = target.getDenizenNPC().getCitizen().getOrAddTrait(HealthTrait.class);
trait.setMaxhealth(quantity.asInt());
if (heal.asBoolean()) {
trait.setHealth(quantity.asDouble());
}
}
else {
Debug.echoError(scriptEntry.getResidingQueue(), "NPC doesn't have health trait!");
}
}
else if (target.isLivingEntity()) {
target.getLivingEntity().setMaxHealth(quantity.asDouble());
if (heal.asBoolean()) {
target.getLivingEntity().setHealth(quantity.asDouble());
}
}
else {
Debug.echoError(scriptEntry.getResidingQueue(), "Entity '" + target.identify() + "'is not alive!");
}
}
}
}
}