/
SittingTrait.java
132 lines (105 loc) · 3.11 KB
/
SittingTrait.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
package net.aufdemrand.denizen.npc.traits;
import net.aufdemrand.denizen.utilities.DenizenAPI;
import net.aufdemrand.denizen.utilities.Utilities;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.citizensnpcs.api.persistence.Persist;
import net.citizensnpcs.api.trait.Trait;
import net.minecraft.server.v1_5_R2.EntityHuman;
import net.minecraft.server.v1_5_R2.EntityPlayer;
import org.bukkit.Location;
import org.bukkit.craftbukkit.v1_5_R2.entity.CraftPlayer;
import org.bukkit.entity.EntityType;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
public class SittingTrait extends Trait implements Listener {
@Persist("sitting")
private boolean sitting = false;
@Persist("chair location")
private Location chairLocation = null;
EntityHuman eh = null;
@Override
public void run() {
if (eh == null || chairLocation == null) return;
if (!Utilities.checkLocation(npc.getBukkitEntity(), chairLocation, 1)) stand();
}
@Override
public void onSpawn() {
eh = ((CraftPlayer) npc.getBukkitEntity()).getHandle();
if (sitting) sit();
}
/**
* Makes the NPC sit
*/
public void sit() {
DenizenAPI.getDenizenNPC(npc).action("sit", null);
if (npc.getBukkitEntity().getType() != EntityType.PLAYER) {
return;
}
((EntityPlayer) eh).getDataWatcher().watch(0, Byte.valueOf((byte) 0x04));
sitting = true;
chairLocation = npc.getBukkitEntity().getLocation();
return;
}
/**
* Makes the NPC sit a the specified location
*
* @param location
*/
public void sit(Location location) {
DenizenAPI.getDenizenNPC(npc).action("sit", null);
if (npc.getBukkitEntity().getType() != EntityType.PLAYER) {
return;
}
/*
* Teleport NPC to the location before
* sending the sit packet to the clients.
*/
eh.getBukkitEntity().teleport(location.add(0.5, 0, 0.5));
dB.echoDebug("...NPC moved to chair");
((EntityPlayer) eh).getDataWatcher().watch(0, Byte.valueOf((byte) 0x04));
sitting = true;
chairLocation = location;
}
/**
* Makes the NPC stand
*/
public void stand() {
DenizenAPI.getDenizenNPC(npc).action("stand", null);
((EntityPlayer) eh).getDataWatcher().watch(0, Byte.valueOf((byte) 0x00));
chairLocation = null;
sitting = false;
}
/**
* Checks if the NPC is currently sitting
*
* @return boolean
*/
public boolean isSitting() {
return sitting;
}
/**
* Gets the chair the NPC is sitting on
* Returns null if the NPC isnt sitting
*
* @return Location
*/
public Location getChair() {
return chairLocation;
}
/**
* If someone tries to break the poor
* NPC's chair, we need to stop them!
*
*/
@EventHandler(ignoreCancelled = true)
public void onBlockBreak(BlockBreakEvent event) {
if (chairLocation == null) return;
if (event.getBlock().getLocation().equals(chairLocation)) {
event.setCancelled(true);
}
}
public SittingTrait() {
super("sitting");
}
}