/
ResetCommand.java
124 lines (101 loc) · 4.98 KB
/
ResetCommand.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
package net.aufdemrand.denizen.scripts.commands.core;
import net.aufdemrand.denizen.BukkitScriptEntryData;
import net.aufdemrand.denizen.objects.dPlayer;
import net.aufdemrand.denizen.utilities.DenizenAPI;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizencore.exceptions.CommandExecutionException;
import net.aufdemrand.denizencore.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizencore.objects.*;
import net.aufdemrand.denizencore.scripts.ScriptEntry;
import net.aufdemrand.denizencore.scripts.commands.AbstractCommand;
public class ResetCommand extends AbstractCommand {
private enum Type {FINISH, FAIL, PLAYER_COOLDOWN, GLOBAL_COOLDOWN, SAVES}
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (aH.Argument arg : aH.interpret(scriptEntry.getArguments())) {
if (arg.matches("finishes", "finished", "finish")
&& !scriptEntry.hasObject("type")) {
scriptEntry.addObject("type", Type.FINISH);
}
else if (arg.matches("fails", "failed", "fail")
&& !scriptEntry.hasObject("type")) {
scriptEntry.addObject("type", Type.FAIL);
}
else if (arg.matches("cooldown")
&& !scriptEntry.hasObject("type")) {
scriptEntry.addObject("type", Type.PLAYER_COOLDOWN);
}
else if (arg.matches("global_cooldown")
&& !scriptEntry.hasObject("type")) {
scriptEntry.addObject("type", Type.GLOBAL_COOLDOWN);
}
else if (arg.matches("saves") && !scriptEntry.hasObject("type")) {
scriptEntry.addObject("type", Type.SAVES);
}
else if (arg.matchesArgumentType(dScript.class)) {
scriptEntry.addObject("script", arg.asType(dScript.class));
}
else if (arg.matchesArgumentList(dPlayer.class)) {
scriptEntry.addObject("players", arg.asType(dList.class));
}
// TODO: Reset NPCs option too!
else {
arg.reportUnhandled();
}
}
// Use attached player if none is specified, and we're not resetting GLOBAL_COOLDOWN
if (!scriptEntry.getObject("type").equals(Type.GLOBAL_COOLDOWN)) {
scriptEntry.defaultObject("players", ((BukkitScriptEntryData) scriptEntry.entryData).getPlayer());
}
// Must specify a script unless resetting SAVES
if (!scriptEntry.hasObject("script") && !scriptEntry.getObject("type").equals(Type.SAVES)) {
throw new InvalidArgumentsException("Must specify a script!");
}
}
@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
// We allow players to be a single player or multiple players
dObject player = scriptEntry.getdObject("players");
dList players;
if (player instanceof dPlayer) {
players = new dList(player.identify());
}
else {
players = scriptEntry.getdObject("players");
}
Type type = (Type) scriptEntry.getObject("type");
dScript script = scriptEntry.getdObject("script");
if (scriptEntry.dbCallShouldDebug()) {
dB.report(scriptEntry, getName(),
(players != null ? players.debug() : "")
+ aH.debugObj("type", type)
+ (script != null ? script.debug() : ""));
}
// Deal with GLOBAL_COOLDOWN reset first, since there's no player/players involved
if (type == Type.GLOBAL_COOLDOWN) {
CooldownCommand.setCooldown(null, Duration.ZERO, script.getName(), true);
return;
}
// Now deal with the rest
for (String object : players) {
dPlayer resettable = dPlayer.valueOf(object);
if (resettable.isValid()) {
switch (type) {
case FAIL:
FailCommand.class.toString(); // TODO: Remove references to FAIL command.
dB.echoError("The 'FAIL' command is deprecated. Use flags instead!");
return;
case FINISH:
FinishCommand.class.toString(); // TODO: Remove references to FINISH command.
dB.echoError("The 'FINISH' command is deprecated. Use flags instead!");
return;
case PLAYER_COOLDOWN:
CooldownCommand.setCooldown(resettable, Duration.ZERO, script.getName(), false);
return;
case SAVES:
DenizenAPI.getCurrentInstance().getSaves().set("Players." + resettable.getSaveName(), null);
}
}
}
}
}