-
-
Notifications
You must be signed in to change notification settings - Fork 302
/
Controllable.java
463 lines (409 loc) · 16.1 KB
/
Controllable.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
package net.citizensnpcs.trait;
import java.lang.reflect.Constructor;
import java.util.List;
import java.util.Map;
import org.bukkit.Location;
import org.bukkit.entity.EnderDragon;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.entity.Vehicle;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.util.Vector;
import com.google.common.collect.Maps;
import net.citizensnpcs.Settings.Setting;
import net.citizensnpcs.api.command.CommandConfigurable;
import net.citizensnpcs.api.command.CommandContext;
import net.citizensnpcs.api.event.NPCRightClickEvent;
import net.citizensnpcs.api.exception.NPCLoadException;
import net.citizensnpcs.api.persistence.Persist;
import net.citizensnpcs.api.trait.Trait;
import net.citizensnpcs.api.trait.TraitName;
import net.citizensnpcs.api.trait.trait.Owner;
import net.citizensnpcs.api.util.DataKey;
import net.citizensnpcs.util.NMS;
import net.citizensnpcs.util.Util;
/**
* Persists the controllable status for /npc controllable
*
* A controllable {@link NPC} can be mounted by a {@link Player} using right click or /npc mount and moved around using
* e.g. arrow keys.
*/
@TraitName("controllable")
public class Controllable extends Trait implements Toggleable, CommandConfigurable {
private MovementController controller = new GroundController();
@Persist
private boolean enabled = true;
private EntityType explicitType;
@Persist("owner_required")
private boolean ownerRequired;
public Controllable() {
super("controllable");
}
public Controllable(boolean enabled) {
this();
this.enabled = enabled;
}
/**
* Configures the explicit type parameter.
*
* @see #setExplicitType(EntityType)
*/
@Override
public void configure(CommandContext args) {
if (args.hasFlag('f')) {
explicitType = EntityType.BLAZE;
} else if (args.hasFlag('g')) {
explicitType = EntityType.OCELOT;
} else if (args.hasFlag('o')) {
explicitType = EntityType.UNKNOWN;
} else if (args.hasFlag('r')) {
explicitType = null;
} else if (args.hasValueFlag("explicittype")) {
explicitType = Util.matchEntityType(args.getFlag("explicittype"));
}
if (npc.isSpawned()) {
loadController();
}
}
private void enterOrLeaveVehicle(Player player) {
List<Entity> passengers = NMS.getPassengers(player);
if (passengers.size() > 0) {
if (passengers.contains(player)) {
player.leaveVehicle();
}
return;
}
if (ownerRequired && !npc.getOrAddTrait(Owner.class).isOwnedBy(player)) {
return;
}
NMS.mount(npc.getEntity(), player);
}
public boolean isEnabled() {
return enabled;
}
@Override
public void load(DataKey key) throws NPCLoadException {
if (key.keyExists("explicittype")) {
explicitType = Util.matchEntityType(key.getString("explicittype"));
}
}
private void loadController() {
EntityType type = npc.getEntity().getType();
if (explicitType != null) {
type = explicitType;
}
if (!(npc.getEntity() instanceof LivingEntity) && !(npc.getEntity() instanceof Vehicle) && (explicitType == null
|| explicitType == EntityType.UNKNOWN || npc.getEntity().getType() == explicitType)) {
controller = new LookAirController();
return;
}
Constructor<? extends MovementController> innerConstructor = CONTROLLER_TYPES.get(type);
if (innerConstructor == null) {
controller = new GroundController();
return;
}
try {
if (innerConstructor.getParameterCount() == 0) {
controller = innerConstructor.newInstance();
} else {
controller = innerConstructor.newInstance(this);
}
} catch (Exception e) {
e.printStackTrace();
controller = new GroundController();
}
}
/**
* Attempts to mount the {@link Player} onto the {@link NPC}.
*
* @param toMount
* the player to mount
* @return whether the mount was successful
*/
public boolean mount(Player toMount) {
List<Entity> passengers = NMS.getPassengers(npc.getEntity());
if (passengers.size() != 0) {
return false;
}
boolean found = false;
for (Entity passenger : passengers) {
if (passenger != null && passenger == toMount) {
found = true;
break;
}
}
if (found) {
return false;
}
enterOrLeaveVehicle(toMount);
return true;
}
@EventHandler(ignoreCancelled = true, priority = EventPriority.MONITOR)
private void onPlayerInteract(PlayerInteractEvent event) {
if (!npc.isSpawned() || !enabled)
return;
Action performed = event.getAction();
if (NMS.getPassengers(npc.getEntity()).contains(npc.getEntity()))
return;
switch (performed) {
case RIGHT_CLICK_BLOCK:
case RIGHT_CLICK_AIR:
controller.rightClick(event);
break;
case LEFT_CLICK_BLOCK:
case LEFT_CLICK_AIR:
controller.leftClick(event);
break;
default:
break;
}
}
@EventHandler
private void onRightClick(NPCRightClickEvent event) {
if (!enabled || !npc.isSpawned() || !event.getNPC().equals(npc))
return;
controller.rightClickEntity(event);
}
@Override
public void onSpawn() {
loadController();
}
@Override
public void run() {
if (!enabled || !npc.isSpawned())
return;
List<Entity> passengers = NMS.getPassengers(npc.getEntity());
if (passengers.size() == 0 || !(passengers.get(0) instanceof Player) || npc.getNavigator().isNavigating())
return;
controller.run((Player) passengers.get(0));
}
@Override
public void save(DataKey key) {
if (explicitType == null) {
key.removeKey("explicittype");
} else {
key.setString("explicittype", explicitType.name());
}
}
public boolean setEnabled(boolean enabled) {
this.enabled = enabled;
return enabled;
}
/**
* Configures the explicit typei.e. whether the NPC should be controlled as if it was a certain {@link EntityType}.
*
* @param type
* the explicit type
*/
public void setExplicitType(EntityType type) {
this.explicitType = type;
}
private void setMountedYaw(Entity entity) {
if (entity instanceof EnderDragon || !Setting.USE_BOAT_CONTROLS.asBoolean())
return; // EnderDragon handles this separately
Location loc = entity.getLocation();
Vector vel = entity.getVelocity();
if (vel.lengthSquared() == 0) {
return;
}
double tX = loc.getX() + vel.getX();
double tZ = loc.getZ() + vel.getZ();
if (loc.getZ() > tZ) {
loc.setYaw((float) -Math.toDegrees(Math.atan((loc.getX() - tX) / (loc.getZ() - tZ))) + 180F);
} else if (loc.getZ() < tZ) {
loc.setYaw((float) -Math.toDegrees(Math.atan((loc.getX() - tX) / (loc.getZ() - tZ))));
}
NMS.look(entity, loc.getYaw(), loc.getPitch());
}
/**
* Sets whether the {@link Player} attempting to mount the {@link NPC} must actually own the {@link NPC} to mount
* it.
*
* @see Owner#isOwnedBy(org.bukkit.command.CommandSender)
*/
public void setOwnerRequired(boolean ownerRequired) {
this.ownerRequired = ownerRequired;
}
@Override
public boolean toggle() {
enabled = !enabled;
if (!enabled && NMS.getPassengers(npc.getEntity()).size() > 0) {
NMS.getPassengers(npc.getEntity()).get(0).leaveVehicle();
}
return enabled;
}
private double updateHorizontalSpeed(Entity handle, Entity passenger, double speed, float speedMod) {
double maxSpeed = Setting.MAX_CONTROLLABLE_GROUND_SPEED.asDouble();
Vector vel = handle.getVelocity();
double oldSpeed = Math.sqrt(vel.getX() * vel.getX() + vel.getZ() * vel.getZ());
double horizontal = NMS.getHorizontalMovement(passenger);
double yaw = passenger.getLocation().getYaw();
if (horizontal > 0.0D) {
double dXcos = -Math.sin(yaw * Math.PI / 180.0F);
double dXsin = Math.cos(yaw * Math.PI / 180.0F);
vel = vel.setX(dXcos * speed * speedMod).setZ(dXsin * speed * speedMod);
}
vel = vel.add(new Vector(
passenger.getVelocity().getX() * speedMod * Setting.CONTROLLABLE_GROUND_DIRECTION_MODIFIER.asDouble(),
0D,
passenger.getVelocity().getZ() * speedMod * Setting.CONTROLLABLE_GROUND_DIRECTION_MODIFIER.asDouble()))
.multiply(0.98);
double newSpeed = Math.sqrt(vel.getX() * vel.getX() + vel.getZ() * vel.getZ());
if (newSpeed > maxSpeed) {
vel = vel.multiply(new Vector(maxSpeed / newSpeed, 1, maxSpeed / newSpeed));
newSpeed = maxSpeed;
}
handle.setVelocity(vel);
if (newSpeed > oldSpeed && speed < maxSpeed) {
return (float) Math.min(maxSpeed, (speed + ((maxSpeed - speed) / 50.0D)));
} else {
return (float) Math.max(0, (speed - ((speed) / 50.0D)));
}
}
public class GroundController implements MovementController {
private int jumpTicks = 0;
private double speed = 0.07D;
@Override
public void leftClick(PlayerInteractEvent event) {
}
@Override
public void rightClick(PlayerInteractEvent event) {
}
@Override
public void rightClickEntity(NPCRightClickEvent event) {
enterOrLeaveVehicle(event.getClicker());
}
@Override
public void run(Player rider) {
boolean onGround = NMS.isOnGround(npc.getEntity());
float speedMod = npc.getNavigator().getDefaultParameters()
.modifiedSpeed((onGround ? GROUND_SPEED : AIR_SPEED));
if (!Util.isHorse(npc.getEntity().getType()) || npc.getEntity().getType().name().equals("LLAMA")
|| npc.getEntity().getType().name().equals("TRADER_LLAMA")) {
// just use minecraft horse physics
speed = updateHorizontalSpeed(npc.getEntity(), rider, speed, speedMod);
}
boolean shouldJump = NMS.shouldJump(rider);
if (shouldJump) {
if (onGround && jumpTicks == 0) {
npc.getEntity().setVelocity(npc.getEntity().getVelocity().setY(JUMP_VELOCITY));
jumpTicks = 10;
}
} else {
jumpTicks = 0;
}
jumpTicks = Math.max(0, jumpTicks - 1);
setMountedYaw(npc.getEntity());
}
private static final float AIR_SPEED = 0.5F;
private static final float GROUND_SPEED = 0.5F;
private static final float JUMP_VELOCITY = 0.5F;
}
public class LookAirController implements MovementController {
private boolean paused = false;
@Override
public void leftClick(PlayerInteractEvent event) {
paused = !paused;
}
@Override
public void rightClick(PlayerInteractEvent event) {
paused = !paused;
}
@Override
public void rightClickEntity(NPCRightClickEvent event) {
enterOrLeaveVehicle(event.getClicker());
}
@Override
public void run(Player rider) {
if (paused) {
npc.getEntity().setVelocity(npc.getEntity().getVelocity().setY(0.001));
return;
}
Vector dir = rider.getEyeLocation().getDirection();
dir.multiply(npc.getNavigator().getDefaultParameters().speedModifier());
npc.getEntity().setVelocity(dir);
setMountedYaw(npc.getEntity());
}
}
public static interface MovementController {
void leftClick(PlayerInteractEvent event);
void rightClick(PlayerInteractEvent event);
void rightClickEntity(NPCRightClickEvent event);
void run(Player rider);
}
public class PlayerInputAirController implements MovementController {
private boolean paused = false;
private double speed;
@Override
public void leftClick(PlayerInteractEvent event) {
paused = !paused;
}
@Override
public void rightClick(PlayerInteractEvent event) {
npc.getEntity().setVelocity(npc.getEntity().getVelocity().setY(-0.3F));
}
@Override
public void rightClickEntity(NPCRightClickEvent event) {
enterOrLeaveVehicle(event.getClicker());
}
@Override
public void run(Player rider) {
if (paused) {
npc.getEntity().setVelocity(npc.getEntity().getVelocity().setY(0.001F));
return;
}
speed = updateHorizontalSpeed(npc.getEntity(), rider, speed, 1F);
boolean shouldJump = NMS.shouldJump(rider);
if (shouldJump) {
npc.getEntity().setVelocity(npc.getEntity().getVelocity().setY(0.3F));
}
npc.getEntity().setVelocity(npc.getEntity().getVelocity().multiply(new Vector(1, 0.98, 1)));
}
}
/**
* Register a movement controller for a certain {@link EntityType} to be used for {@link NPC}s with that type.
*
* Default controllers are registered for BAT, BLAZE, ENDER_DRAGON, GHAST, WITHER and PARROT using
* {@link PlayerInputAirController}.
*
* @param type
* the entity type
* @param clazz
* the controller class
*/
public static void registerControllerType(EntityType type, Class<? extends MovementController> clazz) {
try {
Constructor<? extends MovementController> constructor = clazz.getConstructor(Controllable.class);
constructor.setAccessible(true);
CONTROLLER_TYPES.put(type, constructor);
return;
} catch (Exception e) {
try {
Constructor<? extends MovementController> constructor = clazz.getConstructor();
constructor.setAccessible(true);
CONTROLLER_TYPES.put(type, constructor);
} catch (Exception e2) {
throw new RuntimeException(e2);
}
}
}
private static final Map<EntityType, Constructor<? extends MovementController>> CONTROLLER_TYPES = Maps
.newEnumMap(EntityType.class);
static {
registerControllerType(EntityType.BAT, PlayerInputAirController.class);
registerControllerType(EntityType.BLAZE, PlayerInputAirController.class);
registerControllerType(EntityType.ENDER_DRAGON, PlayerInputAirController.class);
registerControllerType(EntityType.GHAST, PlayerInputAirController.class);
registerControllerType(EntityType.WITHER, PlayerInputAirController.class);
try {
registerControllerType(EntityType.valueOf("PARROT"), PlayerInputAirController.class);
} catch (IllegalArgumentException ex) {
}
registerControllerType(EntityType.UNKNOWN, LookAirController.class);
}
}