-
-
Notifications
You must be signed in to change notification settings - Fork 302
/
SitTrait.java
117 lines (100 loc) · 3.54 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
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
package net.citizensnpcs.trait;
import org.bukkit.Location;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Sittable;
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.api.util.SpigotUtil;
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 (SUPPORT_SITTABLE && npc.getEntity() instanceof Sittable) {
((Sittable) npc.getEntity()).setSitting(false);
return;
}
if (chair != null) {
if (chair.getEntity() != null) {
chair.getEntity().eject();
Location npcLoc = npc.getEntity().getLocation().clone();
if (requiresPassengerOffsetCorrection()) {
npcLoc = npcLoc.add(0, 0.3, 0);
}
npc.getEntity().teleport(npcLoc);
}
chair.destroy();
chair = null;
}
}
@Override
public void onRemove() {
onDespawn();
}
private boolean requiresPassengerOffsetCorrection() {
return SpigotUtil.getVersion() != null && SpigotUtil.getVersion().length >= 2
&& SpigotUtil.getVersion()[1] <= 19;
}
@Override
public void run() {
if (!npc.isSpawned() || !isSitting())
return;
if (SUPPORT_SITTABLE && npc.getEntity() instanceof Sittable) {
((Sittable) npc.getEntity()).setSitting(true);
if (npc.getEntity().getLocation().distance(sittingAt) >= 0.03) {
npc.teleport(sittingAt, TeleportCause.PLUGIN);
}
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.clone())) {
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.03) {
chair.teleport(sittingAt.clone(), TeleportCause.PLUGIN);
}
}
public void setSitting(Location at) {
this.sittingAt = at != null ? at.clone() : null;
if (requiresPassengerOffsetCorrection()) {
sittingAt = sittingAt.add(0, 0.3, 0);
}
if (at == null) {
onDespawn();
}
}
private static boolean SUPPORT_SITTABLE = true;
static {
try {
Class.forName("org.bukkit.entity.Sittable");
} catch (ClassNotFoundException e) {
SUPPORT_SITTABLE = false;
}
}
}