-
-
Notifications
You must be signed in to change notification settings - Fork 104
/
StatisticCommand.java
206 lines (198 loc) · 9.08 KB
/
StatisticCommand.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package com.denizenscript.denizen.scripts.commands.player;
import com.denizenscript.denizen.utilities.Utilities;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import com.denizenscript.denizen.objects.EntityTag;
import com.denizenscript.denizen.objects.MaterialTag;
import com.denizenscript.denizen.objects.PlayerTag;
import com.denizenscript.denizencore.exceptions.InvalidArgumentsException;
import com.denizenscript.denizencore.objects.Argument;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.core.ListTag;
import com.denizenscript.denizencore.scripts.ScriptEntry;
import com.denizenscript.denizencore.scripts.commands.AbstractCommand;
import org.bukkit.Material;
import org.bukkit.Statistic;
import org.bukkit.entity.EntityType;
public class StatisticCommand extends AbstractCommand {
public StatisticCommand() {
setName("statistic");
setSyntax("statistic [<statistic>] [add/take/set] (<#>) (qualifier:<material>/<entity>)");
setRequiredArguments(2, 4);
isProcedural = false;
}
// <--[command]
// @Name Statistic
// @Syntax statistic [<statistic>] [add/take/set] (<#>) (qualifier:<material>/<entity>)
// @Required 2
// @Maximum 4
// @Short Changes the specified statistic value for a player.
// @Group player
//
// @Description
// Changes the specified statistic for the player.
// For more info on statistics, see <@link url https://minecraft.wiki/w/Statistics>
// For statistic names, see <@link url https://hub.spigotmc.org/javadocs/spigot/org/bukkit/Statistic.html>
//
// You can add, take, or set a numeric value to the statistic for the linked player.
// Works with offline players.
//
// Some statistics are unique per a material or entity - for those, use the "qualifier" argument.
//
// @Tags
// <PlayerTag.statistic[<statistic>]>
// <PlayerTag.statistic[<statistic>].qualifier[<material>/<entity>]>
//
// @Usage
// Use to hide the evidence of all the animal breeding you've done.
// - statistic animals_bred set 0
//
// @Usage
// Use to pretend you just ran a 5k.
// - statistic walk_one_cm add 500000
//
// @Usage
// Use to make it look like that challenge course wasn't even hard for you at all.
// - statistic deaths take 200
//
// -->
private enum Action {ADD, TAKE, SET}
@Override
public void addCustomTabCompletions(TabCompletionsBuilder tab) {
tab.add(Statistic.values());
}
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
boolean specified_players = false;
for (Argument arg : scriptEntry) {
if (!scriptEntry.hasObject("action")
&& arg.matchesEnum(Action.class)) {
scriptEntry.addObject("action", arg.asElement());
}
else if (arg.matchesPrefix("players")
&& !scriptEntry.hasObject("players")
&& arg.matchesArgumentList(PlayerTag.class)) {
scriptEntry.addObject("players", arg.asType(ListTag.class));
specified_players = true;
}
else if (!scriptEntry.hasObject("statistic")
&& arg.matchesEnum(Statistic.class)) {
scriptEntry.addObject("statistic", arg.asElement());
}
else if (!scriptEntry.hasObject("amount")
&& arg.matchesInteger()) {
scriptEntry.addObject("amount", arg.asElement());
}
else if (arg.matchesPrefix("qualifier", "q")
&& !scriptEntry.hasObject("material")
&& !scriptEntry.hasObject("entity")) {
if (arg.matchesArgumentType(MaterialTag.class)) {
scriptEntry.addObject("material", arg.asType(MaterialTag.class));
}
else if (arg.matchesArgumentType(EntityTag.class)) {
scriptEntry.addObject("entity", arg.asType(EntityTag.class));
}
}
}
if (!scriptEntry.hasObject("action")) {
throw new InvalidArgumentsException("Must specify a valid action!");
}
if (!scriptEntry.hasObject("statistic")) {
throw new InvalidArgumentsException("Must specify a valid Statistic!");
}
if (!scriptEntry.hasObject("amount")) {
scriptEntry.addObject("amount", new ElementTag(1));
}
Statistic.Type type = Statistic.valueOf(scriptEntry.getElement("statistic").asString().toUpperCase()).getType();
if (type != Statistic.Type.UNTYPED) {
if ((type == Statistic.Type.BLOCK || type == Statistic.Type.ITEM) && !scriptEntry.hasObject("material")) {
throw new InvalidArgumentsException("Must specify a valid " + type.name() + " MATERIAL!");
}
else if (type == Statistic.Type.ENTITY && !scriptEntry.hasObject("entity")) {
throw new InvalidArgumentsException("Must specify a valid ENTITY!");
}
}
if (!scriptEntry.hasObject("players") && Utilities.entryHasPlayer(scriptEntry) && !specified_players) {
scriptEntry.addObject("players", new ListTag(Utilities.getEntryPlayer(scriptEntry)));
}
if (!scriptEntry.hasObject("players")) {
throw new InvalidArgumentsException("Must specify valid players!");
}
}
@Override
public void execute(ScriptEntry scriptEntry) {
ElementTag action = scriptEntry.getElement("action");
ListTag players = scriptEntry.getObjectTag("players");
ElementTag statistic = scriptEntry.getElement("statistic");
ElementTag amount = scriptEntry.getElement("amount");
MaterialTag material = scriptEntry.getObjectTag("material");
EntityTag entity = scriptEntry.getObjectTag("entity");
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), action, statistic, amount, players, material, entity);
}
Action act = Action.valueOf(action.asString().toUpperCase());
Statistic stat = Statistic.valueOf(statistic.asString().toUpperCase());
int amt = amount.asInt();
switch (stat.getType()) {
case BLOCK:
case ITEM:
Material mat = material.getMaterial();
switch (act) {
case ADD:
for (PlayerTag player : players.filter(PlayerTag.class, scriptEntry)) {
player.incrementStatistic(stat, mat, amt);
}
break;
case TAKE:
for (PlayerTag player : players.filter(PlayerTag.class, scriptEntry)) {
player.decrementStatistic(stat, mat, amt);
}
break;
case SET:
for (PlayerTag player : players.filter(PlayerTag.class, scriptEntry)) {
player.setStatistic(stat, mat, amt);
}
break;
}
break;
case ENTITY:
EntityType ent = entity.getBukkitEntityType();
switch (act) {
case ADD:
for (PlayerTag player : players.filter(PlayerTag.class, scriptEntry)) {
player.incrementStatistic(stat, ent, amt);
}
break;
case TAKE:
for (PlayerTag player : players.filter(PlayerTag.class, scriptEntry)) {
player.decrementStatistic(stat, ent, amt);
}
break;
case SET:
for (PlayerTag player : players.filter(PlayerTag.class, scriptEntry)) {
player.setStatistic(stat, ent, amt);
}
break;
}
break;
case UNTYPED:
switch (act) {
case ADD:
for (PlayerTag player : players.filter(PlayerTag.class, scriptEntry)) {
player.incrementStatistic(stat, amt);
}
break;
case TAKE:
for (PlayerTag player : players.filter(PlayerTag.class, scriptEntry)) {
player.decrementStatistic(stat, amt);
}
break;
case SET:
for (PlayerTag player : players.filter(PlayerTag.class, scriptEntry)) {
player.setStatistic(stat, amt);
}
break;
}
break;
}
}
}