/
TimeCommand.java
91 lines (77 loc) · 3.73 KB
/
TimeCommand.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
package net.aufdemrand.denizen.scripts.commands.world;
import net.aufdemrand.denizen.BukkitScriptEntryData;
import net.aufdemrand.denizen.objects.dWorld;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizencore.exceptions.CommandExecutionException;
import net.aufdemrand.denizencore.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizencore.objects.Duration;
import net.aufdemrand.denizencore.objects.Element;
import net.aufdemrand.denizencore.objects.aH;
import net.aufdemrand.denizencore.scripts.ScriptEntry;
import net.aufdemrand.denizencore.scripts.commands.AbstractCommand;
public class TimeCommand extends AbstractCommand {
private enum Type {GLOBAL, PLAYER}
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (aH.Argument arg : aH.interpret(scriptEntry.getArguments())) {
if (!scriptEntry.hasObject("type")
&& arg.matchesEnum(Type.values())) {
scriptEntry.addObject("type", arg.asElement());
}
else if (!scriptEntry.hasObject("value")
&& arg.matchesArgumentType(Duration.class)) {
scriptEntry.addObject("value", arg.asType(Duration.class));
}
else if (!scriptEntry.hasObject("world")
&& arg.matchesArgumentType(dWorld.class)) {
scriptEntry.addObject("world", arg.asType(dWorld.class));
}
else {
arg.reportUnhandled();
}
}
// Check to make sure required arguments have been filled
if ((!scriptEntry.hasObject("value"))) {
throw new InvalidArgumentsException("Must specify a value!");
}
// If the world has not been specified, try to use the NPC's or player's
// world, or default to "world" if necessary
if (!scriptEntry.hasObject("world")) {
scriptEntry.addObject("world",
((BukkitScriptEntryData) scriptEntry.entryData).hasNPC() ?
new dWorld(((BukkitScriptEntryData) scriptEntry.entryData).getNPC().getWorld()) :
(((BukkitScriptEntryData) scriptEntry.entryData).hasPlayer() ?
new dWorld(((BukkitScriptEntryData) scriptEntry.entryData).getPlayer().getWorld()) : null));
}
scriptEntry.defaultObject("type", new Element("GLOBAL"));
if (!scriptEntry.hasObject("world")) {
throw new InvalidArgumentsException("Must specify a valid world!");
}
}
@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
// Fetch objects
Duration value = (Duration) scriptEntry.getObject("value");
dWorld world = (dWorld) scriptEntry.getObject("world");
Element type_element = scriptEntry.getElement("type");
Type type = Type.valueOf(type_element.asString().toUpperCase());
// Report to dB
if (scriptEntry.dbCallShouldDebug()) {
dB.report(scriptEntry, getName(), type_element.debug()
+ value.debug()
+ world.debug());
}
if (type.equals(Type.GLOBAL)) {
world.getWorld().setTime(value.getTicks());
}
else {
if (!((BukkitScriptEntryData) scriptEntry.entryData).hasPlayer()) {
dB.echoError("Must have a valid player link!");
}
else {
((BukkitScriptEntryData) scriptEntry.entryData).getPlayer()
.getPlayerEntity().setPlayerTime(value.getTicks(), true);
}
}
}
}