/
ResetCommand.java
111 lines (83 loc) · 4.47 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
package net.aufdemrand.denizen.scripts.commands.core;
import net.aufdemrand.denizen.exceptions.CommandExecutionException;
import net.aufdemrand.denizen.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizen.objects.*;
import net.aufdemrand.denizen.scripts.ScriptEntry;
import net.aufdemrand.denizen.scripts.commands.AbstractCommand;
import net.aufdemrand.denizen.utilities.DenizenAPI;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.npc.NPC;
import org.bukkit.entity.EntityType;
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));
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", scriptEntry.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 = (dList) scriptEntry.getdObject("players");
Element type = (Element) scriptEntry.getdObject("type");
dScript script = (dScript) scriptEntry.getObject("script");
dB.report(scriptEntry, getName(),
(players != null ? players.debug() : "")
+ type.debug()
+ (script != null ? script.debug() : ""));
// Deal with GLOBAL_COOLDOWN reset first, since there's no player/players involved
if (Type.valueOf(type.asString()) == 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.valueOf(type.asString())) {
case FAIL:
FailCommand.resetFails(resettable.getName(), script.getName());
return;
case FINISH:
FinishCommand.resetFinishes(resettable.getName(), script.getName());
return;
case PLAYER_COOLDOWN:
CooldownCommand.setCooldown(resettable.getName(), Duration.ZERO, script.getName(), false);
return;
case SAVES:
DenizenAPI.getCurrentInstance().getSaves().set("Players." + resettable.getName(), null);
}
}
}
}
}