-
-
Notifications
You must be signed in to change notification settings - Fork 294
/
CitizensNPC.java
560 lines (496 loc) · 21.4 KB
/
CitizensNPC.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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
package net.citizensnpcs.npc;
import java.util.ArrayList;
import java.util.Collection;
import java.util.UUID;
import java.util.function.Consumer;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.block.Block;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.entity.CreatureSpawnEvent;
import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;
import org.bukkit.metadata.FixedMetadataValue;
import org.bukkit.scheduler.BukkitRunnable;
import com.google.common.base.Preconditions;
import com.google.common.base.Throwables;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Iterables;
import com.google.common.collect.SetMultimap;
import net.citizensnpcs.NPCNeedsRespawnEvent;
import net.citizensnpcs.Settings.Setting;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.ai.Navigator;
import net.citizensnpcs.api.astar.pathfinder.SwimmingExaminer;
import net.citizensnpcs.api.event.DespawnReason;
import net.citizensnpcs.api.event.NPCDespawnEvent;
import net.citizensnpcs.api.event.NPCSpawnEvent;
import net.citizensnpcs.api.event.SpawnReason;
import net.citizensnpcs.api.npc.AbstractNPC;
import net.citizensnpcs.api.npc.BlockBreaker;
import net.citizensnpcs.api.npc.BlockBreaker.BlockBreakerConfiguration;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.npc.NPCRegistry;
import net.citizensnpcs.api.trait.Trait;
import net.citizensnpcs.api.trait.trait.MobType;
import net.citizensnpcs.api.trait.trait.Spawned;
import net.citizensnpcs.api.util.DataKey;
import net.citizensnpcs.api.util.Messaging;
import net.citizensnpcs.npc.ai.CitizensNavigator;
import net.citizensnpcs.npc.skin.SkinnableEntity;
import net.citizensnpcs.trait.CurrentLocation;
import net.citizensnpcs.trait.Gravity;
import net.citizensnpcs.trait.HologramTrait;
import net.citizensnpcs.trait.ScoreboardTrait;
import net.citizensnpcs.trait.SneakTrait;
import net.citizensnpcs.util.ChunkCoord;
import net.citizensnpcs.util.Messages;
import net.citizensnpcs.util.NMS;
import net.citizensnpcs.util.PlayerAnimation;
import net.citizensnpcs.util.PlayerUpdateTask;
import net.citizensnpcs.util.Util;
public class CitizensNPC extends AbstractNPC {
private ChunkCoord cachedCoord;
private EntityController entityController;
private final CitizensNavigator navigator = new CitizensNavigator(this);
private int updateCounter = 0;
public CitizensNPC(UUID uuid, int id, String name, EntityController entityController, NPCRegistry registry) {
super(uuid, id, name, registry);
Preconditions.checkNotNull(entityController);
this.entityController = entityController;
}
@Override
public boolean despawn(DespawnReason reason) {
if (getEntity() == null && reason != DespawnReason.DEATH) {
Messaging.debug("Tried to despawn", this, "while already despawned, DespawnReason." + reason);
if (reason == DespawnReason.RELOAD) {
unloadEvents();
}
return true;
}
NPCDespawnEvent event = new NPCDespawnEvent(this, reason);
if (reason == DespawnReason.CHUNK_UNLOAD) {
event.setCancelled(data().get(NPC.KEEP_CHUNK_LOADED_METADATA, Setting.KEEP_CHUNKS_LOADED.asBoolean()));
}
Bukkit.getPluginManager().callEvent(event);
if (event.isCancelled() && reason != DespawnReason.DEATH) {
Messaging.debug("Couldn't despawn", this, "due to despawn event cancellation. Will load chunk.",
getEntity().isValid(), ", DespawnReason." + reason);
return false;
}
boolean keepSelected = getOrAddTrait(Spawned.class).shouldSpawn();
if (!keepSelected) {
data().remove("selectors");
}
if (getEntity() instanceof Player) {
PlayerUpdateTask.deregisterPlayer(getEntity());
}
navigator.onDespawn();
if (reason == DespawnReason.RELOAD) {
unloadEvents();
}
for (Trait trait : new ArrayList<Trait>(traits.values())) {
trait.onDespawn();
}
Messaging.debug("Despawned", this, "DespawnReason." + reason);
if (reason == DespawnReason.DEATH) {
entityController.setEntity(null);
} else {
entityController.remove();
}
return true;
}
@Override
public void destroy() {
super.destroy();
resetCachedCoord();
}
@Override
public void faceLocation(Location location) {
if (!isSpawned())
return;
Util.faceLocation(getEntity(), location);
}
@Override
public BlockBreaker getBlockBreaker(Block targetBlock, BlockBreakerConfiguration config) {
return NMS.getBlockBreaker(getEntity(), targetBlock, config);
}
@Override
public Entity getEntity() {
return entityController == null ? null : entityController.getBukkitEntity();
}
@Override
public Navigator getNavigator() {
return navigator;
}
@Override
public Location getStoredLocation() {
return isSpawned() ? getEntity().getLocation() : getOrAddTrait(CurrentLocation.class).getLocation();
}
@Override
public boolean isFlyable() {
updateFlyableState();
return super.isFlyable();
}
@Override
public boolean isSpawned() {
return getEntity() != null && NMS.isValid(getEntity());
}
@Override
public void load(final DataKey root) {
super.load(root);
// Spawn the NPC
CurrentLocation spawnLocation = getOrAddTrait(CurrentLocation.class);
if (getOrAddTrait(Spawned.class).shouldSpawn() && spawnLocation.getLocation() != null) {
if (spawnLocation.getLocation() != null) {
spawn(spawnLocation.getLocation(), SpawnReason.RESPAWN);
} else {
Messaging.debug("Tried to spawn", this, "on load but world was null");
}
}
navigator.load(root.getRelative("navigator"));
}
@Override
public boolean requiresNameHologram() {
return super.requiresNameHologram()
|| (getEntityType() != EntityType.ARMOR_STAND && Setting.ALWAYS_USE_NAME_HOLOGRAM.asBoolean());
}
private void resetCachedCoord() {
if (cachedCoord == null)
return;
CHUNK_LOADERS.remove(NPC_METADATA_MARKER, CHUNK_LOADERS);
CHUNK_LOADERS.remove(cachedCoord, this);
if (CHUNK_LOADERS.get(cachedCoord).size() == 0) {
cachedCoord.setForceLoaded(false);
}
cachedCoord = null;
}
@Override
public void save(DataKey root) {
super.save(root);
if (!data().get(NPC.SHOULD_SAVE_METADATA, true))
return;
navigator.save(root.getRelative("navigator"));
}
@Override
public void setBukkitEntityType(EntityType type) {
EntityController controller = EntityControllers.createForType(type);
if (controller == null)
throw new IllegalArgumentException("Unsupported entity type " + type);
setEntityController(controller);
}
public void setEntityController(EntityController newController) {
Preconditions.checkNotNull(newController);
boolean wasSpawned = isSpawned();
Location prev = null;
if (wasSpawned) {
prev = getEntity().getLocation(CACHE_LOCATION);
despawn(DespawnReason.PENDING_RESPAWN);
}
entityController = newController;
if (wasSpawned) {
spawn(prev, SpawnReason.RESPAWN);
}
}
@Override
public void setFlyable(boolean flyable) {
super.setFlyable(flyable);
updateFlyableState();
}
@Override
public void setMoveDestination(Location destination) {
if (!isSpawned())
return;
if (destination == null) {
NMS.cancelMoveDestination(getEntity());
} else {
NMS.setDestination(getEntity(), destination.getX(), destination.getY(), destination.getZ(), 1);
}
}
@Override
public void setName(String name) {
super.setName(name);
if (requiresNameHologram() && !hasTrait(HologramTrait.class)) {
addTrait(HologramTrait.class);
}
}
@Override
public boolean spawn(Location at) {
return spawn(at, SpawnReason.PLUGIN);
}
@Override
public boolean spawn(Location at, SpawnReason reason) {
Preconditions.checkNotNull(at, "location cannot be null");
Preconditions.checkNotNull(reason, "reason cannot be null");
if (getEntity() != null) {
Messaging.debug("Tried to spawn", this, "while already spawned. SpawnReason." + reason);
return false;
}
if (at.getWorld() == null) {
Messaging.debug("Tried to spawn", this, "but the world was null. SpawnReason." + reason);
return false;
}
at = at.clone();
if (reason == SpawnReason.CHUNK_LOAD || reason == SpawnReason.COMMAND) {
at.getChunk().load();
}
getOrAddTrait(CurrentLocation.class).setLocation(at);
entityController.spawn(at.clone(), this);
getEntity().setMetadata(NPC_METADATA_MARKER, new FixedMetadataValue(CitizensAPI.getPlugin(), true));
Collection<Trait> onPreSpawn = traits.values();
for (Trait trait : onPreSpawn.toArray(new Trait[onPreSpawn.size()])) {
try {
trait.onPreSpawn();
} catch (Throwable ex) {
Messaging.severeTr(Messages.TRAIT_ONSPAWN_FAILED, trait.getName(), getId());
ex.printStackTrace();
}
}
boolean loaded = Util.isLoaded(at);
boolean couldSpawn = !loaded ? false : NMS.addEntityToWorld(getEntity(), CreatureSpawnEvent.SpawnReason.CUSTOM);
if (!couldSpawn) {
if (Messaging.isDebugging()) {
Messaging.debug("Retrying spawn of", this, "later, SpawnReason." + reason + ". Was loaded", loaded,
"is loaded", Util.isLoaded(at));
}
// we need to wait before trying to spawn
entityController.remove();
Bukkit.getPluginManager().callEvent(new NPCNeedsRespawnEvent(this, at));
return false;
}
// send skin packets, if applicable, before other NMS packets are sent
SkinnableEntity skinnable = getEntity() instanceof SkinnableEntity ? ((SkinnableEntity) getEntity()) : null;
if (skinnable != null) {
skinnable.getSkinTracker().onSpawnNPC();
}
getEntity().teleport(at);
NMS.setHeadYaw(getEntity(), at.getYaw());
NMS.setBodyYaw(getEntity(), at.getYaw());
final Location to = at;
Consumer<Runnable> postSpawn = new Consumer<Runnable>() {
private int timer;
@Override
public void accept(Runnable cancel) {
if (getEntity() == null || !getEntity().isValid()) {
if (timer++ > Setting.ENTITY_SPAWN_WAIT_TICKS.asInt()) {
Messaging.debug("Couldn't spawn ", CitizensNPC.this, "waited", timer,
"ticks but entity not added to world");
entityController.remove();
cancel.run();
Bukkit.getPluginManager().callEvent(new NPCNeedsRespawnEvent(CitizensNPC.this, to));
}
return;
}
// Set the spawned state
getOrAddTrait(CurrentLocation.class).setLocation(to);
getOrAddTrait(Spawned.class).setSpawned(true);
NPCSpawnEvent spawnEvent = new NPCSpawnEvent(CitizensNPC.this, to, reason);
Bukkit.getPluginManager().callEvent(spawnEvent);
if (spawnEvent.isCancelled()) {
Messaging.debug("Couldn't spawn", CitizensNPC.this, "SpawnReason." + reason,
"due to event cancellation.");
entityController.remove();
cancel.run();
return;
}
navigator.onSpawn();
for (Trait trait : Iterables.toArray(traits.values(), Trait.class)) {
try {
trait.onSpawn();
} catch (Throwable ex) {
Messaging.severeTr(Messages.TRAIT_ONSPAWN_FAILED, trait.getName(), getId());
ex.printStackTrace();
}
}
EntityType type = getEntity().getType();
if (type.isAlive()) {
LivingEntity entity = (LivingEntity) getEntity();
entity.setRemoveWhenFarAway(false);
if (NMS.getStepHeight(entity) < 1) {
NMS.setStepHeight(entity, 1);
}
if (type == EntityType.PLAYER) {
NMS.replaceTrackerEntry((Player) getEntity());
PlayerUpdateTask.registerPlayer(getEntity());
}
if (SUPPORT_NODAMAGE_TICKS && (Setting.DEFAULT_SPAWN_NODAMAGE_TICKS.asInt() != 20
|| data().has(NPC.Metadata.SPAWN_NODAMAGE_TICKS))) {
try {
entity.setNoDamageTicks(data().get(NPC.Metadata.SPAWN_NODAMAGE_TICKS,
Setting.DEFAULT_SPAWN_NODAMAGE_TICKS.asInt()));
} catch (NoSuchMethodError err) {
SUPPORT_NODAMAGE_TICKS = false;
}
}
}
if (requiresNameHologram() && !hasTrait(HologramTrait.class)) {
addTrait(HologramTrait.class);
}
updateFlyableState();
updateCustomNameVisibility();
updateCustomName();
Messaging.debug("Spawned", CitizensNPC.this, "SpawnReason." + reason);
cancel.run();
}
};
if (getEntity() != null && getEntity().isValid()) {
postSpawn.accept(() -> {
});
} else {
new BukkitRunnable() {
@Override
public void run() {
postSpawn.accept(() -> cancel());
}
}.runTaskTimer(CitizensAPI.getPlugin(), 0, 1);
}
return true;
}
@Override
public void teleport(Location location, TeleportCause reason) {
super.teleport(location, reason);
if (!isSpawned())
return;
Location npcLoc = getEntity().getLocation(CACHE_LOCATION);
if (isSpawned() && npcLoc.getWorld() == location.getWorld() && npcLoc.distanceSquared(location) < 1) {
NMS.setHeadYaw(getEntity(), location.getYaw());
}
}
@Override
public String toString() {
EntityType mobType = hasTrait(MobType.class) ? getTraitNullable(MobType.class).getType() : null;
return getId() + "{" + getName() + ", " + mobType + "}";
}
@Override
public void update() {
try {
super.update();
if (!isSpawned()) {
resetCachedCoord();
return;
}
if (navigator.isNavigating()) {
if (data().get(NPC.Metadata.SWIMMING, true)) {
Location currentDest = navigator.getPathStrategy().getCurrentDestination();
if (currentDest == null || currentDest.getY() > getStoredLocation().getY()) {
NMS.trySwim(getEntity(), SwimmingExaminer.isWaterMob(getEntity()) ? 0.02F : 0.04F);
}
}
} else if (data().<Boolean> get(NPC.Metadata.SWIMMING, !SwimmingExaminer.isWaterMob(getEntity()))) {
Gravity trait = getTraitNullable(Gravity.class);
if (trait == null || trait.hasGravity()) {
NMS.trySwim(getEntity());
}
}
navigator.run();
if (SUPPORT_GLOWING) {
try {
getEntity().setGlowing(data().get(NPC.Metadata.GLOWING, false));
} catch (NoSuchMethodError e) {
SUPPORT_GLOWING = false;
}
}
boolean isLiving = getEntity() instanceof LivingEntity;
int packetUpdateDelay = data().get(NPC.Metadata.PACKET_UPDATE_DELAY, Setting.PACKET_UPDATE_DELAY.asInt());
if (updateCounter++ > packetUpdateDelay) {
if (Setting.KEEP_CHUNKS_LOADED.asBoolean()) {
ChunkCoord currentCoord = new ChunkCoord(getStoredLocation());
if (!currentCoord.equals(cachedCoord)) {
resetCachedCoord();
currentCoord.setForceLoaded(true);
CHUNK_LOADERS.put(currentCoord, this);
cachedCoord = currentCoord;
}
}
if (isLiving) {
updateCustomName();
}
updateCounter = 0;
}
updateCustomNameVisibility();
if (isLiving) {
NMS.setKnockbackResistance((LivingEntity) getEntity(), isProtected() ? 1D : 0D);
if (SUPPORT_PICKUP_ITEMS) {
try {
((LivingEntity) getEntity())
.setCanPickupItems(data().get(NPC.Metadata.PICKUP_ITEMS, !isProtected()));
} catch (Throwable t) {
SUPPORT_PICKUP_ITEMS = false;
}
}
}
if (isLiving && getEntity() instanceof Player) {
updateUsingItemState((Player) getEntity());
if (data().has(NPC.Metadata.SNEAKING) && !hasTrait(SneakTrait.class)) {
addTrait(SneakTrait.class);
}
}
if (SUPPORT_SILENT && data().has(NPC.SILENT_METADATA)) {
try {
getEntity().setSilent(Boolean.parseBoolean(data().get(NPC.Metadata.SILENT).toString()));
} catch (NoSuchMethodError e) {
SUPPORT_SILENT = false;
}
}
} catch (Exception ex) {
Throwable error = Throwables.getRootCause(ex);
Messaging.logTr(Messages.EXCEPTION_UPDATING_NPC, getId(), error.getMessage());
error.printStackTrace();
}
}
private void updateCustomName() {
if (data().has(NPC.Metadata.SCOREBOARD_FAKE_TEAM_NAME)) {
getOrAddTrait(ScoreboardTrait.class).update();
}
}
private void updateCustomNameVisibility() {
String nameplateVisible = data().<Object> get(NPC.Metadata.NAMEPLATE_VISIBLE, true).toString();
if (requiresNameHologram()) {
nameplateVisible = "false";
}
if (nameplateVisible.equals("true") || nameplateVisible.equals("hover")) {
getEntity().setCustomName(getFullName());
}
getEntity().setCustomNameVisible(Boolean.parseBoolean(nameplateVisible));
}
private void updateFlyableState() {
EntityType type = isSpawned() ? getEntity().getType() : getOrAddTrait(MobType.class).getType();
if (type == null)
return;
if (!Util.isAlwaysFlyable(type))
return;
if (!data().has(NPC.FLYABLE_METADATA)) {
data().setPersistent(NPC.FLYABLE_METADATA, true);
}
if (!hasTrait(Gravity.class)) {
getOrAddTrait(Gravity.class).setEnabled(true);
}
}
private void updateUsingItemState(Player player) {
boolean useItem = data().get(NPC.Metadata.USING_HELD_ITEM, false),
offhand = data().get(NPC.Metadata.USING_OFFHAND_ITEM, false);
if (!SUPPORT_USE_ITEM)
return;
try {
if (useItem) {
NMS.playAnimation(PlayerAnimation.STOP_USE_ITEM, player, 64);
NMS.playAnimation(PlayerAnimation.START_USE_MAINHAND_ITEM, player, 64);
} else if (offhand) {
NMS.playAnimation(PlayerAnimation.STOP_USE_ITEM, player, 64);
NMS.playAnimation(PlayerAnimation.START_USE_OFFHAND_ITEM, player, 64);
}
} catch (UnsupportedOperationException ex) {
SUPPORT_USE_ITEM = false;
}
}
private static final Location CACHE_LOCATION = new Location(null, 0, 0, 0);
private static final SetMultimap<ChunkCoord, NPC> CHUNK_LOADERS = HashMultimap.create();
private static final String NPC_METADATA_MARKER = "NPC";
private static boolean SUPPORT_GLOWING = true;
private static boolean SUPPORT_NODAMAGE_TICKS = true;
private static boolean SUPPORT_PICKUP_ITEMS = true;
private static boolean SUPPORT_SILENT = true;
private static boolean SUPPORT_USE_ITEM = true;
}