/
SleepingTrait.java
122 lines (100 loc) · 2.86 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
package net.aufdemrand.denizen.npc.traits;
import net.aufdemrand.denizen.utilities.Utilities;
import net.citizensnpcs.api.persistence.Persist;
import net.citizensnpcs.api.trait.Trait;
import net.citizensnpcs.util.PlayerAnimation;
import org.bukkit.Location;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
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) {
return;
}
//if (npc.getEntity().getPassenger() == null && sitting) eh.mount(eh);
if (!Utilities.checkLocation((LivingEntity) npc.getEntity(), bedLocation, 1)) {
wakeUp();
}
}
/**
* Makes the NPC sleep
*/
public void toSleep() {
if (sleeping) {
return;
}
PlayerAnimation.SLEEP.play((Player) npc.getEntity());
sleeping = true;
bedLocation = npc.getEntity().getLocation();
}
/**
* Makes the NPC sleep at the specified location
*
* @param location where to sleep at
*/
public void toSleep(Location location) {
if (sleeping) {
return;
}
/*
* Teleport NPC to the location before
* playing sleep animation.
*/
//TODO Adjust the .add()
npc.getEntity().teleport(location.add(0.5, 0, 0.5));
PlayerAnimation.SLEEP.play((Player) npc.getEntity());
sleeping = true;
bedLocation = location;
}
/**
* Makes the NPC wake up
*/
public void wakeUp() {
if (!sleeping) {
return;
}
PlayerAnimation.STOP_SLEEPING.play((Player) npc.getEntity());
bedLocation = null;
sleeping = false;
}
/**
* 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");
}
}