-
-
Notifications
You must be signed in to change notification settings - Fork 295
/
SitTrait.java
79 lines (67 loc) · 2.2 KB
/
SitTrait.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
package net.citizensnpcs.trait;
import org.bukkit.Location;
import org.bukkit.entity.EntityType;
import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.npc.MemoryNPCDataStore;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.npc.NPCRegistry;
import net.citizensnpcs.api.persistence.Persist;
import net.citizensnpcs.api.trait.Trait;
import net.citizensnpcs.api.trait.TraitName;
import net.citizensnpcs.util.NMS;
@TraitName("sittrait")
public class SitTrait extends Trait {
private NPC chair;
@Persist
private Location sittingAt;
public SitTrait() {
super("sittrait");
}
public boolean isSitting() {
return sittingAt != null;
}
@Override
public void onDespawn() {
if (chair != null) {
if (chair.getEntity() != null) {
chair.getEntity().eject();
}
chair.destroy();
chair = null;
}
}
@Override
public void onRemove() {
onDespawn();
}
@Override
public void run() {
if (!npc.isSpawned() || !isSitting())
return;
if (chair == null) {
NPCRegistry registry = CitizensAPI.getNamedNPCRegistry("SitRegistry");
if (registry == null) {
registry = CitizensAPI.createNamedNPCRegistry("SitRegistry", new MemoryNPCDataStore());
}
chair = registry.createNPC(EntityType.ARMOR_STAND, "");
chair.getOrAddTrait(ArmorStandTrait.class).setAsHelperEntity(npc);
if (!chair.spawn(sittingAt)) {
chair = null;
return;
}
}
if (chair.isSpawned() && !NMS.getPassengers(chair.getEntity()).contains(npc.getEntity())) {
NMS.mount(chair.getEntity(), npc.getEntity());
}
if (chair.getStoredLocation() != null && chair.getStoredLocation().distance(sittingAt) > 0.05) {
chair.teleport(sittingAt, TeleportCause.PLUGIN);
}
}
public void setSitting(Location at) {
this.sittingAt = at != null ? at.clone() : null;
if (!isSitting()) {
onDespawn();
}
}
}