/
RotateCommand.java
182 lines (174 loc) · 8.12 KB
/
RotateCommand.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package com.denizenscript.denizen.scripts.commands.entity;
import com.denizenscript.denizen.Denizen;
import com.denizenscript.denizen.nms.interfaces.EntityHelper;
import com.denizenscript.denizen.utilities.Utilities;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizen.nms.NMSHandler;
import com.denizenscript.denizen.objects.EntityTag;
import com.denizenscript.denizencore.exceptions.InvalidArgumentsException;
import com.denizenscript.denizencore.objects.*;
import com.denizenscript.denizencore.objects.core.DurationTag;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.core.ListTag;
import com.denizenscript.denizencore.scripts.ScriptEntry;
import com.denizenscript.denizencore.scripts.commands.AbstractCommand;
import com.denizenscript.denizencore.scripts.commands.Holdable;
import org.bukkit.scheduler.BukkitRunnable;
import java.util.*;
public class RotateCommand extends AbstractCommand implements Holdable {
public RotateCommand() {
setName("rotate");
setSyntax("rotate (cancel) (<entity>|...) (yaw:<#.#>) (pitch:<#.#>) (infinite/duration:<duration>) (frequency:<duration>)");
setRequiredArguments(1, 6);
isProcedural = false;
}
// <--[command]
// @Name Rotate
// @Syntax rotate (cancel) (<entity>|...) (yaw:<#.#>) (pitch:<#.#>) (infinite/duration:<duration>) (frequency:<duration>)
// @Required 1
// @Maximum 6
// @Short Rotates a list of entities.
// @Synonyms Spin
// @Group entity
//
// @Description
// Induces incremental rotation on a list of entities over a period of time.
//
// The yaw and pitch arguments specify how much the entity will rotate each step. Default to 10 and 0 respectively.
//
// The frequency argument specifies how long it takes between each rotation step. Defaults to 1t.
//
// The duration argument specifies how long the whole rotation process will last. Defaults to 1s.
// Alternatively, use "infinite" if you want the entity to spin forever.
//
// You can use "cancel" to prematurely stop the ongoing rotation (useful when set to infinite)
//
// The rotate command is ~waitable. Refer to <@link language ~waitable>.
//
// @Tags
// <EntityTag.location>
//
// @Usage
// Use to rotate the player's yaw by 10 every tick for 3 seconds total
// - rotate <player> duration:3s
//
// @Usage
// Use to rotate the player's pitch by 20 every 5 ticks for a second total
// - rotate <player> yaw:0.0 pitch:20.0 frequency:5t
//
// @Usage
// Use to prematurely stop the player's rotation
// - rotate cancel <player>
// -->
public static Set<UUID> rotatingEntities = new HashSet<>();
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (Argument arg : scriptEntry) {
if (!scriptEntry.hasObject("cancel")
&& (arg.matches("cancel") || arg.matches("stop"))) {
scriptEntry.addObject("cancel", new ElementTag("true"));
}
else if (!scriptEntry.hasObject("infinite")
&& arg.matches("infinite")) {
scriptEntry.addObject("infinite", new ElementTag("true"));
}
else if (!scriptEntry.hasObject("duration")
&& arg.matchesArgumentType(DurationTag.class)
&& arg.matchesPrefix("duration", "d")) {
scriptEntry.addObject("duration", arg.asType(DurationTag.class));
}
else if (!scriptEntry.hasObject("frequency")
&& arg.matchesArgumentType(DurationTag.class)
&& arg.matchesPrefix("frequency", "f")) {
scriptEntry.addObject("frequency", arg.asType(DurationTag.class));
}
else if (!scriptEntry.hasObject("yaw")
&& arg.matchesPrefix("yaw", "y", "rotation", "r")
&& arg.matchesFloat()) {
scriptEntry.addObject("yaw", arg.asElement());
}
else if (!scriptEntry.hasObject("pitch")
&& arg.matchesPrefix("pitch", "p", "tilt", "t")
&& arg.matchesFloat()) {
scriptEntry.addObject("pitch", arg.asElement());
}
else if (!scriptEntry.hasObject("entities")
&& arg.matchesArgumentList(EntityTag.class)) {
scriptEntry.addObject("entities", arg.asType(ListTag.class).filter(EntityTag.class, scriptEntry));
}
else {
arg.reportUnhandled();
}
}
scriptEntry.defaultObject("entities", Utilities.entryDefaultEntityList(scriptEntry, true));
scriptEntry.defaultObject("yaw", new ElementTag(10));
scriptEntry.defaultObject("pitch", new ElementTag(0));
scriptEntry.defaultObject("duration", new DurationTag(20));
scriptEntry.defaultObject("frequency", new DurationTag(1L));
if (!scriptEntry.hasObject("entities")) {
throw new InvalidArgumentsException("Must specify entity/entities!");
}
}
@Override
public void execute(final ScriptEntry scriptEntry) {
final List<EntityTag> entities = new ArrayList<>((List<EntityTag>) scriptEntry.getObject("entities"));
final DurationTag duration = scriptEntry.getObjectTag("duration");
final DurationTag frequency = scriptEntry.getObjectTag("frequency");
final ElementTag yaw = scriptEntry.getElement("yaw");
final ElementTag pitch = scriptEntry.getElement("pitch");
boolean cancel = scriptEntry.hasObject("cancel");
final boolean infinite = scriptEntry.hasObject("infinite");
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), (cancel ? db("cancel", true) : ""), db("entities", entities),
(infinite ? db("duration", "infinite") : duration), frequency, yaw, pitch);
}
for (EntityTag entity : entities) {
if (cancel) {
rotatingEntities.remove(entity.getUUID());
}
else {
rotatingEntities.add(entity.getUUID());
}
}
if (cancel) {
return;
}
BukkitRunnable task = new BukkitRunnable() {
int ticks = 0;
int maxTicks = duration.getTicksAsInt();
ArrayList<EntityTag> unusedEntities = new ArrayList<>();
@Override
public void run() {
if (entities.isEmpty()) {
scriptEntry.setFinished(true);
this.cancel();
}
else if (infinite || ticks < maxTicks) {
for (EntityTag entity : entities) {
if (entity.isSpawned() && rotatingEntities.contains(entity.getUUID())) {
NMSHandler.getEntityHelper().rotate(entity.getBukkitEntity(),
EntityHelper.normalizeYaw(entity.getLocation().getYaw() + yaw.asFloat()),
entity.getLocation().getPitch() + pitch.asFloat());
}
else {
rotatingEntities.remove(entity.getUUID());
unusedEntities.add(entity);
}
}
if (!unusedEntities.isEmpty()) {
for (EntityTag unusedEntity : unusedEntities) {
entities.remove(unusedEntity);
}
unusedEntities.clear();
}
ticks = (int) (ticks + frequency.getTicks());
}
else {
scriptEntry.setFinished(true);
this.cancel();
}
}
};
task.runTaskTimer(Denizen.getInstance(), 0, frequency.getTicks());
}
}