/
TimeCommand.java
166 lines (157 loc) · 7.08 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
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
package com.denizenscript.denizen.scripts.commands.world;
import com.denizenscript.denizen.Denizen;
import com.denizenscript.denizen.utilities.Utilities;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizen.objects.WorldTag;
import com.denizenscript.denizencore.exceptions.InvalidArgumentsException;
import com.denizenscript.denizencore.objects.Argument;
import com.denizenscript.denizencore.objects.core.DurationTag;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.scripts.ScriptEntry;
import com.denizenscript.denizencore.scripts.commands.AbstractCommand;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import java.util.HashMap;
import java.util.UUID;
public class TimeCommand extends AbstractCommand {
public TimeCommand() {
setName("time");
setSyntax("time ({global}/player) [<time-duration>/reset] (<world>) (reset:<duration>) (freeze)");
setRequiredArguments(1, 5);
isProcedural = false;
}
// <--[command]
// @Name Time
// @Syntax time ({global}/player) [<time-duration>/reset] (<world>) (reset:<duration>) (freeze)
// @Required 1
// @Maximum 5
// @Short Changes the current time in the minecraft world.
// @Group world
//
// @Description
// Changes the current time in a world or the time that a player sees the world in.
// If no world is specified, defaults to the NPCs world. If no NPC is available,
// defaults to the player's world. If no player is available, an error will be thrown.
//
// If 'player' is specified, this will change their personal time.
// This is separate from the global time, and does not affect other players.
// When that player logs off, their time will be reset to the global time.
// Additionally, you may instead specify 'reset' to return the player's time back to global time.
// If you specify a custom time, optionally specify 'reset:<duration>'
// to set a time after which the player's time will reset (if not manually changed again before then).
// Optionally specify 'freeze' to lock a player's time in at the specified time value.
//
// @Tags
// <WorldTag.time>
// <WorldTag.time.period>
//
// @Usage
// Use to set the time in the NPC or Player's world.
// - time 500t
//
// @Usage
// Use to make the player see a different time than everyone else.
// - time player 500t
//
// @Usage
// Use to make the player see a different time than everyone else, with the sun no longer moving.
// - time player 500t freeze
//
// @Usage
// Use to make the player see a different time than everyone else for the next 5 minutes.
// - time player 500t reset:5m
//
// @Usage
// Use to make the player see the global time again.
// - time player reset
//
// @Usage
// Use to set the time in a specific world.
// - time 500t myworld
//
// -->
private enum Type {GLOBAL, PLAYER}
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (Argument arg : scriptEntry) {
if (!scriptEntry.hasObject("type")
&& arg.matchesEnum(Type.values())) {
scriptEntry.addObject("type", arg.asElement());
}
else if (!scriptEntry.hasObject("value")
&& !scriptEntry.hasObject("reset")
&& !arg.matchesPrefix("reset")
&& arg.matchesArgumentType(DurationTag.class)) {
scriptEntry.addObject("value", arg.asType(DurationTag.class));
}
else if (!scriptEntry.hasObject("value")
&& !scriptEntry.hasObject("reset")
&& arg.matches("reset")) {
scriptEntry.addObject("reset", new ElementTag(true));
}
else if (!scriptEntry.hasObject("freeze")
&& arg.matches("freeze")) {
scriptEntry.addObject("freeze", new ElementTag(true));
}
else if (!scriptEntry.hasObject("reset_after")
&& arg.matchesPrefix("reset")
&& arg.matchesArgumentType(DurationTag.class)) {
scriptEntry.addObject("reset_after", arg.asType(DurationTag.class));
}
else if (!scriptEntry.hasObject("world")
&& arg.matchesArgumentType(WorldTag.class)) {
scriptEntry.addObject("world", arg.asType(WorldTag.class));
}
else {
arg.reportUnhandled();
}
}
if (!scriptEntry.hasObject("value") && !scriptEntry.hasObject("reset")) {
throw new InvalidArgumentsException("Must specify a value!");
}
if (!scriptEntry.hasObject("world")) {
scriptEntry.addObject("world", Utilities.entryDefaultWorld(scriptEntry, false));
}
scriptEntry.defaultObject("type", new ElementTag("GLOBAL"));
}
public HashMap<UUID, Integer> resetTasks = new HashMap<>();
@Override
public void execute(ScriptEntry scriptEntry) {
DurationTag value = scriptEntry.getObjectTag("value");
DurationTag resetAfter = scriptEntry.getObjectTag("reset_after");
WorldTag world = scriptEntry.getObjectTag("world");
ElementTag type_element = scriptEntry.getElement("type");
ElementTag reset = scriptEntry.getElement("reset");
ElementTag freeze = scriptEntry.getElement("freeze");
Type type = Type.valueOf(type_element.asString().toUpperCase());
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), type_element, reset, value, freeze, resetAfter, world);
}
if (type.equals(Type.GLOBAL)) {
world.getWorld().setTime(value.getTicks());
}
else {
if (!Utilities.entryHasPlayer(scriptEntry)) {
Debug.echoError("Must have a valid player link!");
}
else {
Player player = Utilities.getEntryPlayer(scriptEntry).getPlayerEntity();
if (reset != null && reset.asBoolean()) {
player.resetPlayerTime();
}
else {
Integer existingTask = resetTasks.get(player.getUniqueId());
if (existingTask != null) {
Bukkit.getScheduler().cancelTask(existingTask);
resetTasks.remove(player.getUniqueId());
}
player.setPlayerTime(value.getTicks(), freeze == null || !freeze.asBoolean());
if (resetAfter != null) {
int newTask = Bukkit.getScheduler().scheduleSyncDelayedTask(Denizen.getInstance(), player::resetPlayerTime, resetAfter.getTicks());
resetTasks.put(player.getUniqueId(), newTask);
}
}
}
}
}
}