/
MountCommand.java
156 lines (120 loc) · 5.32 KB
/
MountCommand.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.utilities.arguments.dEntity;
import net.aufdemrand.denizen.utilities.arguments.aH;
import net.aufdemrand.denizen.utilities.arguments.dLocation;
import net.aufdemrand.denizen.utilities.arguments.aH.ArgumentType;
import net.aufdemrand.denizen.utilities.debugging.dB;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import java.util.ArrayList;
import java.util.List;
/**
* Mounts a player on the NPC if no targets are specified.
* If targets are specified, mount them on each other in order.
*
* @author David Cernat
*/
public class MountCommand extends AbstractCommand {
@Override
public void parseArgs(ScriptEntry scriptEntry)
throws InvalidArgumentsException {
//
// List of entities to be mounted.
//
List<Entity> entities = new ArrayList<Entity> ();
//
// Process the arguments.
//
Boolean dismount = false;
dLocation location = null;
for (String arg : scriptEntry.getArguments()) {
if (aH.matchesLocation(arg)) {
location = aH.getLocationFrom(arg);
dB.echoDebug("...location set to '%s'.", arg);
}
else if (aH.matchesArg("cancel", arg)) {
dismount = true;
dB.echoDebug("...will dismount.");
}
else if (aH.matchesValueArg("TARGETS, TARGET", arg, ArgumentType.Custom)) {
Entity entity = null;
for (String target : aH.getListFrom(arg)) {
// Get entity
if (aH.matchesEntityType(target)) {
dLocation entityLocation = null;
// Cannot spawn an entity without a location, so go through possible locations
if (location != null)
entityLocation = location;
else if (scriptEntry.getPlayer() != null)
entityLocation = new dLocation(scriptEntry.getPlayer().getLocation());
else if (scriptEntry.getNPC() != null)
entityLocation = new dLocation(scriptEntry.getNPC().getLocation());
if (entityLocation != null) {
EntityType entityType = aH.getEntityFrom(target);
entity = entityLocation.getWorld().spawnEntity(entityLocation, entityType);
}
}
else {
entity = dEntity.valueOf(target).getBukkitEntity();
}
if (entity != null) {
entities.add(entity);
}
else {
dB.echoError("Invalid target '%s'!", target);
}
}
}
else throw new InvalidArgumentsException(dB.Messages.ERROR_UNKNOWN_ARGUMENT, arg);
}
// If there are no targets, default to the player mounting this NPC
if (entities.size() == 0) {
entities.add(scriptEntry.getPlayer());
entities.add(scriptEntry.getNPC().getEntity());
}
// If there is only one target entity, there will be no one to mount
// it, so make this player mount it by adding him/her to the start
// of the list
if (entities.size() == 1) {
entities.add(0, scriptEntry.getPlayer());
}
// Store objects in ScriptEntry for use in execute()
scriptEntry.addObject("entities", entities);
scriptEntry.addObject("location", location);
scriptEntry.addObject("dismount", dismount);
}
@SuppressWarnings("unchecked")
@Override
public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
List<Entity> entities = (List<Entity>) scriptEntry.getObject("entities");
final dLocation location = (dLocation) scriptEntry.getObject("location");
Boolean dismount = (Boolean) scriptEntry.getObject("dismount");
// Debug output
dB.echoApproval("<G>Executing '<Y>" + getName() + "<G>': "
+ "Targets=<Y>'" + entities.toString() + "<G>'");
Entity lastEntity = null;
for (Entity entity : entities) {
if (dismount) {
entity.leaveVehicle();
}
else {
if (lastEntity != null) {
// Because setPassenger() is a toggle, only use it if the new passenger
// is not already the current passenger
if (entity.getPassenger() != lastEntity) {
lastEntity.teleport(entity.getLocation());
entity.setPassenger(lastEntity);
}
}
lastEntity = entity;
}
}
if (location != null) {
lastEntity.teleport(location);
}
}
}