/
SleepingTrait.java
167 lines (153 loc) · 4.76 KB
/
SleepingTrait.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
package com.denizenscript.denizen.npc.traits;
import com.denizenscript.denizen.Denizen;
import com.denizenscript.denizen.nms.NMSHandler;
import com.denizenscript.denizen.utilities.Utilities;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import net.citizensnpcs.api.persistence.Persist;
import net.citizensnpcs.api.trait.Trait;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.block.data.type.Bed;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.entity.Pose;
import org.bukkit.entity.Villager;
import org.bukkit.event.EventHandler;
import org.bukkit.event.block.BlockBreakEvent;
public class SleepingTrait extends Trait {
@Persist("sleeping")
private boolean sleeping = false;
@Persist("bed location")
private Location bedLocation = null;
@Override
public void run() {
if (npc == null || bedLocation == null || !npc.isSpawned()) {
return;
}
if (!Utilities.checkLocation((LivingEntity) npc.getEntity(), bedLocation, 2)) {
wakeUp();
Bukkit.getScheduler().scheduleSyncDelayedTask(Denizen.getInstance(), () -> {
if (npc.hasTrait(SleepingTrait.class) && !npc.getOrAddTrait(SleepingTrait.class).isSleeping()) {
npc.removeTrait(SleepingTrait.class);
}
});
}
}
@Override
public void onSpawn() {
if (sleeping) {
internalSleepNow();
}
}
public void internalSleepNow() {
if (npc.getEntity() instanceof Villager) {
if (!((Villager) npc.getEntity()).sleep(bedLocation.clone())) {
return;
}
}
else if (npc.getEntity() instanceof Player) {
if (bedLocation.getBlock().getBlockData() instanceof Bed) {
((Player) npc.getEntity()).sleep(bedLocation.clone(), true);
}
else {
NMSHandler.entityHelper.setPose(npc.getEntity(), Pose.SLEEPING);
}
}
else {
return;
}
sleeping = true;
}
/**
* Makes the NPC sleep
*/
public void toSleep() {
if (sleeping) {
return;
}
if (!npc.isSpawned()) {
Debug.echoError("NPC " + npc.getId() + " cannot sleep: not spawned.");
return;
}
bedLocation = npc.getStoredLocation().clone();
if (!bedLocation.isWorldLoaded()) {
Debug.echoError("NPC " + npc.getId() + " cannot sleep: invalid bed location.");
return;
}
internalSleepNow();
}
/**
* Makes the NPC sleep at the specified location
*
* @param location where to sleep at
*/
public void toSleep(Location location) {
if (sleeping) {
return;
}
if (!npc.isSpawned()) {
Debug.echoError("NPC " + npc.getId() + " cannot sleep: not spawned.");
return;
}
if (!location.isWorldLoaded()) {
Debug.echoError("NPC " + npc.getId() + " cannot sleep: invalid bed location.");
return;
}
//TODO Adjust the .add()
npc.getEntity().teleport(location.clone().add(0.5, 0, 0.5));
bedLocation = location.clone();
internalSleepNow();
}
/**
* Makes the NPC wake up
*/
public void wakeUp() {
if (!sleeping) {
return;
}
sleeping = false;
if (npc.getEntity() instanceof Villager) {
((Villager) npc.getEntity()).wakeup();
}
else {
if (((Player) npc.getEntity()).isSleeping()) {
((Player) npc.getEntity()).wakeup(false);
}
NMSHandler.entityHelper.setPose(npc.getEntity(), Pose.STANDING);
}
bedLocation = null;
}
/**
* Checks if the NPC is currently sleeping
*
* @return boolean
*/
public boolean isSleeping() {
return sleeping;
}
/**
* Gets the bed the NPC is sleeping on
* Returns null if the NPC isnt sleeping
*
* @return Location
*/
public Location getBed() {
return bedLocation;
}
/**
* If someone tries to break the poor
* NPC's bed, we need to stop them!
*/
@EventHandler(ignoreCancelled = true)
public void onBlockBreak(BlockBreakEvent event) {
if (bedLocation == null) {
return;
}
if (event.getBlock().getLocation().equals(bedLocation)) {
event.setCancelled(true);
}
}
public SleepingTrait() {
super("sleeping");
}
}