/
LookCommand.java
153 lines (144 loc) · 6.46 KB
/
LookCommand.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
package com.denizenscript.denizen.scripts.commands.entity;
import com.denizenscript.denizen.utilities.DenizenAPI;
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.denizen.objects.LocationTag;
import com.denizenscript.denizencore.exceptions.InvalidArgumentsException;
import com.denizenscript.denizencore.objects.Argument;
import com.denizenscript.denizencore.objects.core.DurationTag;
import com.denizenscript.denizencore.objects.ArgumentHelper;
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 org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scheduler.BukkitTask;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;
public class LookCommand extends AbstractCommand {
public LookCommand() {
setName("look");
setSyntax("look (<entity>|...) [<location>/cancel] (duration:<duration>)");
setRequiredArguments(1, 3);
isProcedural = false;
}
// <--[command]
// @Name Look
// @Syntax look (<entity>|...) [<location>/cancel] (duration:<duration>)
// @Required 1
// @Maximum 3
// @Short Causes the NPC or other entity to look at a target location.
// @Group entity
//
// @Description
// Makes the entity look towards the location.
//
// Can be used on players.
//
// If a duration is set, the entity cannot look away from the location until the duration has expired.
// Use the cancel argument to end the duration earlier.
//
// @Tags
// <LocationTag.yaw>
// <LocationTag.pitch>
//
// @Usage
// Use to point an npc towards a spot.
// - look <npc> <player.location>
//
// @Usage
// Use to force a player to stare at a spot for some time.
// - look <player> <npc.location> duration:10s
// -->
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (Argument arg : scriptEntry.getProcessedArgs()) {
if (!scriptEntry.hasObject("location")
&& !scriptEntry.hasObject("cancel")
&& arg.matchesArgumentType(LocationTag.class)) {
scriptEntry.addObject("location", arg.asType(LocationTag.class));
}
else if (!scriptEntry.hasObject("cancel")
&& !scriptEntry.hasObject("location")
&& arg.matches("cancel")) {
scriptEntry.addObject("cancel", 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("entities")
&& arg.matchesArgumentList(EntityTag.class)) {
scriptEntry.addObject("entities", arg.asType(ListTag.class).filter(EntityTag.class, scriptEntry));
}
else {
arg.reportUnhandled();
}
}
if (!scriptEntry.hasObject("entities")) {
scriptEntry.defaultObject("entities",
Utilities.entryHasNPC(scriptEntry) && Utilities.getEntryNPC(scriptEntry).isSpawned() ? Arrays.asList(Utilities.getEntryNPC(scriptEntry).getDenizenEntity()) : null,
Utilities.entryHasPlayer(scriptEntry) && Utilities.getEntryPlayer(scriptEntry).isOnline() ? Arrays.asList(Utilities.getEntryPlayer(scriptEntry).getDenizenEntity()) : null);
}
if (!scriptEntry.hasObject("location") && !scriptEntry.hasObject("cancel")) {
throw new InvalidArgumentsException("Must specify a location or 'cancel'!");
}
if (!scriptEntry.hasObject("entities")) {
throw new InvalidArgumentsException("Must specify an entity!");
}
}
public static HashMap<UUID, BukkitTask> lookTasks = new HashMap<>();
@SuppressWarnings("unchecked")
@Override
public void execute(ScriptEntry scriptEntry) {
final LocationTag loc = scriptEntry.getObjectTag("location");
List<EntityTag> entities = (List<EntityTag>) scriptEntry.getObject("entities");
final DurationTag duration = scriptEntry.getObjectTag("duration");
ElementTag cancel = scriptEntry.getElement("cancel");
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), (cancel != null ? cancel.debug() : loc.debug()) +
ArgumentHelper.debugObj("entities", entities.toString()));
}
for (EntityTag entity : entities) {
if (entity.isSpawned()) {
BukkitTask task = lookTasks.remove(entity.getUUID());
if (task != null) {
task.cancel();
}
}
}
if (cancel != null && cancel.asBoolean()) {
return;
}
for (EntityTag entity : entities) {
if (entity.isSpawned()) {
NMSHandler.getEntityHelper().faceLocation(entity.getBukkitEntity(), loc);
}
}
if (duration != null && duration.getTicks() > 2) {
for (EntityTag entity : entities) {
BukkitRunnable task = new BukkitRunnable() {
long bounces = 0;
public void run() {
bounces += 2;
if (bounces > duration.getTicks()) {
this.cancel();
lookTasks.remove(entity.getUUID());
return;
}
if (entity.isSpawned()) {
NMSHandler.getEntityHelper().faceLocation(entity.getBukkitEntity(), loc);
}
}
};
BukkitTask newTask = task.runTaskTimer(DenizenAPI.getCurrentInstance(), 0, 2);
lookTasks.put(entity.getUUID(), newTask);
}
}
}
}