/
TeleportCommand.java
156 lines (136 loc) · 5.81 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
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
package net.aufdemrand.denizen.scripts.commands.core;
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.dEntity;
import net.aufdemrand.denizen.objects.dLocation;
import net.aufdemrand.denizen.objects.aH;
import net.aufdemrand.denizen.objects.aH.ArgumentType;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.npc.NPC;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerTeleportEvent;
import java.util.ArrayList;
import java.util.List;
/**
* This command will teleport an entity to a new location.
* <br/>
* <pre>Usage: TELEPORT (NPC) [LOCATION:x,y,z,world] (TARGETS:[[NPCID|PlayerName](,)+])</pre>
* <br/>
* Examples:
* <br/><br/>
* 1) NPC wants to teleport the player to 100,100,100 in world:<br/>
* <pre>- teleport location:100,100,100,world</pre>
* <br/>
* 2) NPC wants to teleport herself to 50,50,50 in world:<br/>
* <pre>- teleport npc location:50,50,50,world</pre>
* <br/>
* 3) NPC wants to teleport NPC #456 and the player named "Dave" to 25,25,25
* in world:<br/>
* <pre>- teleport location:25,25,25,world targets:456,Dave</pre>
* <br/>
*/
public class TeleportCommand extends AbstractCommand {
public static final String NPC_ARG = "NPC";
/**
* This method will parse the arguments needed to execute the Teleport
* command from the given script entry. It verifies that the format of the
* command is accurate. If not, it will throw an InvalidArgumentException
* with the error message.
*
* @param scriptEntry The script entry processing this command.
*
* @throws InvalidArgumentsException
*/
@Override
public void parseArgs(ScriptEntry scriptEntry)
throws InvalidArgumentsException {
//
// List of entities to be teleported.
//
List<LivingEntity> teleportEntities = new ArrayList<LivingEntity> ();
//
// List of NPCs to be teleported.
//
List<NPC> teleportNPCs = new ArrayList<NPC> ();
//
// This is the location that the entity/entities are being teleported to.
//
dLocation teleportLocation = null;
//
// Process the arguments.
//
Boolean teleportPlayer = true;
for (String arg : scriptEntry.getArguments()) {
//
// Is this script attempting to teleport the NPC?
//
if (arg.equalsIgnoreCase(TeleportCommand.NPC_ARG)) {
teleportNPCs.add (scriptEntry.getNPC().getCitizen());
teleportPlayer = false;
}
// If argument is a location?
else if (aH.matchesLocation(arg))
teleportLocation = aH.getLocationFrom(arg);
else if (aH.matchesValueArg("TARGETS, TARGET", arg, ArgumentType.Custom)) {
teleportPlayer = false;
for (String target : aH.getListFrom(arg)) {
// Get entity
LivingEntity entity = dEntity.valueOf(target).getBukkitEntity();
if (entity != null) {
if (CitizensAPI.getNPCRegistry().getNPC(entity) != null) {
teleportNPCs.add(CitizensAPI.getNPCRegistry().getNPC(entity));
continue;
} else if (entity instanceof Player) {
teleportEntities.add(aH.getPlayerFrom(target));
continue;
}
}
dB.echoError("Invalid TARGET '%s'!", target);
}
}
else throw new InvalidArgumentsException(dB.Messages.ERROR_UNKNOWN_ARGUMENT, arg);
}
//
// If we're teleporting the player, add them as a recipient.
//
if (teleportPlayer == true) {
teleportEntities.add (scriptEntry.getPlayer());
}
// Check some required arguments, make sure they are valid
if (teleportLocation == null)
throw new InvalidArgumentsException("Missing LOCATION argument. No teleport location.");
if (teleportEntities.isEmpty() && teleportNPCs.isEmpty())
throw new InvalidArgumentsException("Missing TARGETS argument. Nothing to teleport.");
// Store objects in ScriptEntry for use in execute()
scriptEntry.addObject("location", teleportLocation)
.addObject("entities", teleportEntities)
.addObject("npcs", teleportNPCs);
}
/**
* Executes the Teleport command.
*
* @param scriptEntry the ScriptEntry
*/
@SuppressWarnings("unchecked")
@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
dLocation teleportLocation = (dLocation) scriptEntry.getObject("location");
List<LivingEntity> teleportEntities = (List<LivingEntity>) scriptEntry.getObject("entities");
List<NPC> teleportNPCs = (List<NPC>) scriptEntry.getObject("npcs");
// Debug output
dB.echoApproval("<G>Executing '<Y>" + getName() + "<G>': "
+ teleportLocation.debug() + ", "
+ "Targets=<Y>'" + teleportEntities.toString() + "/" + teleportNPCs.toString() + "<G>'");
for (LivingEntity entity : teleportEntities) {
entity.teleport(teleportLocation);
}
for (NPC npc : teleportNPCs) {
npc.spawn(teleportLocation);
npc.getBukkitEntity().teleport(teleportLocation, PlayerTeleportEvent.TeleportCause.COMMAND);
}
}
}