-
-
Notifications
You must be signed in to change notification settings - Fork 102
/
TeleportCommand.java
117 lines (109 loc) · 4.74 KB
/
TeleportCommand.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
package com.denizenscript.denizen.scripts.commands.entity;
import com.denizenscript.denizen.nms.NMSHandler;
import com.denizenscript.denizen.utilities.Utilities;
import com.denizenscript.denizen.utilities.debugging.Debug;
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.ArgumentHelper;
import com.denizenscript.denizencore.objects.core.ListTag;
import com.denizenscript.denizencore.objects.notable.Notable;
import com.denizenscript.denizencore.objects.notable.NoteManager;
import com.denizenscript.denizencore.scripts.ScriptEntry;
import com.denizenscript.denizencore.scripts.commands.AbstractCommand;
import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;
public class TeleportCommand extends AbstractCommand {
public TeleportCommand() {
setName("teleport");
setSyntax("teleport (<entity>|...) [<location>]");
setRequiredArguments(1, 2);
isProcedural = false;
}
// <--[command]
// @Name Teleport
// @Syntax teleport (<entity>|...) [<location>]
// @Required 1
// @Maximum 2
// @Short Teleports the entity(s) to a new location.
// @Synonyms tp
// @Group entity
//
// @Description
// Teleports the entity or entities to the new location.
// Entities can be teleported between worlds using this command.
//
// @Tags
// <EntityTag.location>
//
// @Usage
// Use to teleport a player to the location their cursor is pointing at.
// - teleport <player> <player.cursor_on>
//
// @Usage
// Use to teleport a player high above.
// - teleport <player> <player.location.add[0,200,0]>
//
// @Usage
// Use to teleport to a random online player.
// - teleport <player> <server.online_players.random.location>
//
// @Usage
// Use to teleport all players to your location.
// - teleport <server.online_players> <player.location>
//
// @Usage
// Use to teleport the NPC to a location that was noted wih the <@link command note> command.
// - teleport <npc> my_prenoted_location
// -->
@Override
public void addCustomTabCompletions(String arg, Consumer<String> addOne) {
for (Notable note : NoteManager.notesByType.get(LocationTag.class)) {
addOne.accept(NoteManager.getSavedId(note));
}
}
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {
for (Argument arg : scriptEntry) {
if (!scriptEntry.hasObject("location")
&& arg.matchesArgumentType(LocationTag.class)) {
scriptEntry.addObject("location", arg.asType(LocationTag.class));
}
else if (!scriptEntry.hasObject("entities")
&& arg.matchesArgumentList(EntityTag.class)) {
scriptEntry.addObject("entities", arg.asType(ListTag.class).filter(EntityTag.class, scriptEntry));
}
// NPC arg for compatibility with old scripts
else if (arg.matches("npc") && Utilities.entryHasNPC(scriptEntry)) {
scriptEntry.addObject("entities", Collections.singletonList(Utilities.getEntryNPC(scriptEntry).getDenizenEntity()));
}
else {
arg.reportUnhandled();
}
}
if (!scriptEntry.hasObject("location")) {
throw new InvalidArgumentsException("Must specify a location!");
}
if (!scriptEntry.hasObject("entities")) {
scriptEntry.defaultObject("entities", Utilities.entryDefaultEntityList(scriptEntry, true));
}
}
@Override
public void execute(final ScriptEntry scriptEntry) {
LocationTag location = scriptEntry.getObjectTag("location");
List<EntityTag> entities = (List<EntityTag>) scriptEntry.getObject("entities");
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), ArgumentHelper.debugObj("location", location), ArgumentHelper.debugObj("entities", entities.toString()));
}
for (EntityTag entity : entities) {
if (entity.isFake && entity.getWorld().equals(location.getWorld())) {
NMSHandler.getEntityHelper().snapPositionTo(entity.getBukkitEntity(), location.toVector());
NMSHandler.getEntityHelper().look(entity.getBukkitEntity(), location.getYaw(), location.getPitch());
return;
}
entity.spawnAt(location);
}
}
}