/
SleepTrait.java
88 lines (78 loc) · 2.45 KB
/
SleepTrait.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
package net.citizensnpcs.trait;
import org.bukkit.Location;
import org.bukkit.block.Bed;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.entity.Villager;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.persistence.Persist;
import net.citizensnpcs.api.trait.Trait;
import net.citizensnpcs.api.trait.TraitName;
import net.citizensnpcs.util.NMS;
@TraitName("sleeptrait")
public class SleepTrait extends Trait {
@Persist
private Location at;
private boolean sleeping;
public SleepTrait() {
super("sleeptrait");
}
@Override
public void onDespawn() {
sleeping = false;
}
@Override
public void run() {
if (!npc.isSpawned())
return;
if (at == null) {
if (sleeping) {
wakeup();
}
return;
}
if (npc.getEntity() instanceof Player) {
Player player = (Player) npc.getEntity();
npc.data().set(NPC.Metadata.ENTITY_POSE, "SLEEPING");
if (SUPPORT_BLOCKDATA) {
try {
if (at.getBlock().getBlockData() instanceof Bed || at.getBlock().getState() instanceof Bed) {
player.sleep(at, true);
} else {
NMS.sleep(player, true);
}
} catch (Throwable t) {
SUPPORT_BLOCKDATA = false;
NMS.sleep(player, true);
}
} else {
NMS.sleep(player, true);
}
sleeping = true;
} else if (npc.getEntity() instanceof Villager) {
sleeping = ((Villager) npc.getEntity()).sleep(at);
}
}
public void setSleeping(Location at) {
this.at = at != null ? at.clone() : null;
wakeup();
}
private void wakeup() {
npc.data().remove(NPC.Metadata.ENTITY_POSE);
if (npc.getEntity() instanceof Player) {
NMS.sleep((Player) npc.getEntity(), false);
} else if (npc.getEntity() instanceof Villager) {
((Villager) npc.getEntity()).wakeup();
}
sleeping = false;
}
private static Boolean SUPPORT_BLOCKDATA = null;
static {
try {
Block.class.getMethod("getBlockData");
SUPPORT_BLOCKDATA = true;
} catch (NoSuchMethodException | SecurityException e) {
SUPPORT_BLOCKDATA = false;
}
}
}