-
-
Notifications
You must be signed in to change notification settings - Fork 295
/
SitTrait.java
67 lines (56 loc) · 1.89 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
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 holder;
@Persist
private Location sittingAt;
public SitTrait() {
super("sittrait");
}
public boolean isSitting() {
return sittingAt != null;
}
@Override
public void onDespawn() {
if (holder != null) {
holder.destroy();
holder = null;
}
}
@Override
public void run() {
if (!npc.isSpawned() || !isSitting()) {
return;
}
if (holder == null) {
NPCRegistry registry = CitizensAPI.getNamedNPCRegistry("PlayerAnimationImpl");
if (registry == null) {
registry = CitizensAPI.createNamedNPCRegistry("PlayerAnimationImpl", new MemoryNPCDataStore());
}
holder = registry.createNPC(EntityType.ARMOR_STAND, "");
holder.getOrAddTrait(ArmorStandTrait.class).setAsHelperEntity(npc);
holder.spawn(sittingAt);
}
if (holder.getEntity() != null && !NMS.getPassengers(holder.getEntity()).contains(npc.getEntity())) {
NMS.mount(holder.getEntity(), npc.getEntity());
}
holder.teleport(sittingAt, TeleportCause.PLUGIN);
}
public void setSitting(Location at) {
this.sittingAt = at != null ? at.clone() : null;
if (!isSitting()) {
onDespawn();
}
}
}