/
HealCommand.java
137 lines (120 loc) · 4.84 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
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
package com.denizenscript.denizen.scripts.commands.entity;
import com.denizenscript.denizen.utilities.Utilities;
import com.denizenscript.denizen.utilities.debugging.Debug;
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.ArrayList;
import java.util.Arrays;
import java.util.List;
public class HealCommand extends AbstractCommand {
public HealCommand() {
setName("heal");
setSyntax("heal (<#.#>) ({player}/<entity>|...)");
setRequiredArguments(0, 2);
isProcedural = false;
}
// <--[command]
// @Name Heal
// @Syntax heal (<#.#>) ({player}/<entity>|...)
// @Required 0
// @Maximum 2
// @Short Heals the player or list of entities.
// @Group entity
//
// @Description
// This command heals a player, list of players, entity or list of entities. If no amount is specified it will
// heal the specified player(s)/entity(s) fully.
//
// @Tags
// <EntityTag.health>
//
// @Usage
// Use to fully heal a player.
// - heal
//
// @Usage
// Use to heal a player 5 hearts.
// - heal 10
//
// @Usage
// Use to heal a player by the name of steve fully.
// - heal <[someplayer]>
// -->
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
boolean specified_targets = false;
for (Argument arg : scriptEntry.getProcessedArgs()) {
if (!scriptEntry.hasObject("amount")
&& arg.matchesFloat()) {
scriptEntry.addObject("amount", arg.asElement());
}
else if (!scriptEntry.hasObject("entities")
&& arg.matchesArgumentType(ListTag.class)) {
// Entity arg
scriptEntry.addObject("entities", arg.asType(ListTag.class).filter(EntityTag.class, scriptEntry));
specified_targets = true;
}
else if (!scriptEntry.hasObject("entities")
&& arg.matchesArgumentType(EntityTag.class)) {
// Entity arg
scriptEntry.addObject("entities", Arrays.asList(arg.asType(EntityTag.class)));
specified_targets = true;
}
else {
arg.reportUnhandled();
}
}
if (!scriptEntry.hasObject("amount")) {
scriptEntry.addObject("amount", new ElementTag(-1));
}
if (!specified_targets) {
List<EntityTag> entities = new ArrayList<>();
if (Utilities.getEntryPlayer(scriptEntry) != null) {
entities.add(Utilities.getEntryPlayer(scriptEntry).getDenizenEntity());
}
else if (Utilities.getEntryNPC(scriptEntry) != null) {
entities.add(Utilities.getEntryNPC(scriptEntry).getDenizenEntity());
}
else {
throw new InvalidArgumentsException("No valid target entities found.");
}
scriptEntry.addObject("entities", entities);
}
}
@SuppressWarnings("unchecked")
@Override
public void execute(ScriptEntry scriptEntry) {
List<EntityTag> entities = (List<EntityTag>) scriptEntry.getObject("entities");
if (entities == null) {
return;
}
ElementTag amountelement = scriptEntry.getElement("amount");
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), amountelement.debug() + ArgumentHelper.debugObj("entities", entities));
}
if (amountelement.asDouble() == -1) {
for (EntityTag entity : entities) {
if (entity.isLivingEntity()) {
entity.getLivingEntity().setHealth(entity.getLivingEntity().getMaxHealth());
}
}
}
else {
double amount = amountelement.asDouble();
for (EntityTag entity : entities) {
if (entity.getLivingEntity().getHealth() + amount < entity.getLivingEntity().getMaxHealth()) {
entity.getLivingEntity().setHealth(entity.getLivingEntity().getHealth() + amount);
}
else {
entity.getLivingEntity().setHealth(entity.getLivingEntity().getMaxHealth());
}
}
}
}
}