/
TimeCommand.java
84 lines (68 loc) · 3.17 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
package net.aufdemrand.denizen.scripts.commands.world;
import net.aufdemrand.denizen.exceptions.CommandExecutionException;
import net.aufdemrand.denizen.exceptions.InvalidArgumentsException;
import net.aufdemrand.denizen.scripts.ScriptEntry;
import net.aufdemrand.denizen.scripts.commands.AbstractCommand;
import net.aufdemrand.denizen.objects.Duration;
import net.aufdemrand.denizen.objects.aH;
import net.aufdemrand.denizen.objects.dWorld;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizen.utilities.debugging.dB.Messages;
/**
*
* Set the time in the world to a number of ticks.
*
*/
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())) {
// add type
scriptEntry.addObject("type", Type.valueOf(arg.getValue().toUpperCase()));
}
else if (!scriptEntry.hasObject("value")
&& arg.matchesArgumentType(Duration.class)) {
// add value
scriptEntry.addObject("value", arg.asType(Duration.class));
}
else if (!scriptEntry.hasObject("world")
&& arg.matchesArgumentType(dWorld.class)) {
// add world
scriptEntry.addObject("world", arg.asType(dWorld.class));
}
}
// Check to make sure required arguments have been filled
if ((!scriptEntry.hasObject("value")))
throw new InvalidArgumentsException(Messages.ERROR_MISSING_OTHER, "VALUE");
// If the world has not been specified, try to use the NPC's or player's
// world, or default to "world" if necessary
scriptEntry.defaultObject("world",
new dWorld(scriptEntry.getNPC().getWorld()),
new dWorld(scriptEntry.getPlayer().getWorld()),
dWorld.valueOf("world"));
}
@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
// Fetch objects
Duration value = (Duration) scriptEntry.getObject("value");
dWorld world = (dWorld) scriptEntry.getObject("world");
Type type = scriptEntry.hasObject("type") ?
(Type) scriptEntry.getObject("type") : Type.GLOBAL;
// Report to dB
dB.report(getName(), aH.debugObj("type", type.name()) +
(type.name().equalsIgnoreCase("player") ?
aH.debugObj("player", scriptEntry.getPlayer()) : "") +
(type.name().equalsIgnoreCase("global") ?
aH.debugObj("world", world) : "") +
aH.debugObj("value", value));
if (type.equals(Type.GLOBAL)) {
world.getWorld().setTime(value.getTicks());
}
else {
scriptEntry.getPlayer().getPlayerEntity().setPlayerTime(value.getTicks(), true);
}
}
}