-
-
Notifications
You must be signed in to change notification settings - Fork 104
/
SleepingTrait.java
149 lines (132 loc) · 3.82 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
package com.denizenscript.denizen.npc.traits;
import com.denizenscript.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.block.data.type.Bed;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
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) {
return;
}
//if (npc.getEntity().getPassenger() == null && sitting) eh.mount(eh);
if (!Utilities.checkLocation((LivingEntity) npc.getEntity(), bedLocation, 1)) {
wakeUp();
}
}
@Override
public void onSpawn() {
if (sleeping) {
internalSleepNow();
}
}
public void internalSleepNow() {
if (npc.getEntity() instanceof Villager) {
if (!((Villager) npc.getEntity()).sleep(bedLocation.clone())) {
return;
}
}
else { // Player
if (bedLocation.getBlock().getBlockData() instanceof Bed) {
((Player) npc.getEntity()).sleep(bedLocation.clone(), true);
}
else {
PlayerAnimation.SLEEP.play((Player) npc.getEntity());
}
}
sleeping = true;
}
/**
* Makes the NPC sleep
*/
public void toSleep() {
if (sleeping) {
return;
}
if (bedLocation == null) {
bedLocation = npc.getEntity().getLocation().clone();
}
internalSleepNow();
}
/**
* 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.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);
}
PlayerAnimation.STOP_SLEEPING.play((Player) npc.getEntity());
}
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");
}
}