/
SittingTrait.java
215 lines (188 loc) · 5.58 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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package com.denizenscript.denizen.npc.traits;
import com.denizenscript.denizen.Denizen;
import com.denizenscript.denizen.objects.NPCTag;
import com.denizenscript.denizen.utilities.Utilities;
import com.denizenscript.denizen.utilities.entity.DenizenEntityType;
import com.denizenscript.denizen.nms.interfaces.FakeArrow;
import net.citizensnpcs.api.persistence.Persist;
import net.citizensnpcs.api.trait.Trait;
import net.citizensnpcs.util.PlayerAnimation;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.player.PlayerTeleportEvent;
import org.bukkit.event.vehicle.VehicleExitEvent;
import java.util.ArrayList;
public class SittingTrait extends Trait implements Listener {
@Persist("sitting")
private boolean sitting = false;
@Persist("chair location")
private Location chairLocation = null;
@Override
public void run() {
if (!npc.isSpawned() || chairLocation == null) {
return;
}
if (!Utilities.checkLocation((LivingEntity) npc.getEntity(), chairLocation, 1)) {
stand();
}
}
@Override
public void onSpawn() {
if (sitting) {
if (chairLocation == null) {
sit();
}
else {
sit(chairLocation);
}
}
}
@Override
public void onDespawn() {
if (npc == null || npc.getEntity() == null) {
// Wat.
return;
}
if (npc.getEntity().getVehicle() != null) {
npc.getEntity().getVehicle().eject();
}
}
// <--[action]
// @Actions
// sit
//
// @Triggers when the NPC sits down.
//
// @Context
// None
//
// -->
/**
* Makes the NPC sit
*/
public void sit() {
if (!npc.isSpawned()) {
return;
}
new NPCTag(npc).action("sit", null);
sit(npc.getEntity().getLocation());
}
private void sitInternal() {
if (npc.getEntity() instanceof Player) {
PlayerAnimation.SIT.play((Player) npc.getEntity());
}
else {
DenizenEntityType.getByName("FAKE_ARROW").spawnNewEntity(npc.getEntity().getLocation(),
new ArrayList<>(), null).setPassenger(npc.getEntity());
}
sitting = true;
}
private void standInternal() {
if (npc.getEntity() instanceof Player) {
PlayerAnimation.STOP_SITTING.play((Player) npc.getEntity());
if (chairLocation != null) {
npc.teleport(chairLocation.clone().add(0, 0.5, 0), PlayerTeleportEvent.TeleportCause.PLUGIN);
}
}
else {
Entity vehicle = npc.getEntity().getVehicle();
npc.despawn();
npc.spawn(npc.getStoredLocation().clone().add(0, 0.5, 0));
if (vehicle != null && vehicle.isValid()) {
vehicle.eject();
vehicle.remove();
}
}
sitting = false;
}
public void sitInternal(Location location) {
new NPCTag(npc).action("sit", null);
/*
* Teleport NPC to the location before
* sending the sit packet to the clients.
*/
npc.teleport(location, PlayerTeleportEvent.TeleportCause.PLUGIN);
sitInternal();
}
/**
* Makes the NPC sit at the specified location
*
* @param location where to sit
*/
public void sit(Location location) {
sitInternal(location.clone());
chairLocation = location.clone();
}
// <--[action]
// @Actions
// stand
//
// @Triggers when the NPC stands up.
//
// @Context
// None
//
// -->
/**
* Makes the NPC stand
*/
public void stand() {
new NPCTag(npc).action("stand", null);
standInternal();
standInternal();
chairLocation = null;
}
/**
* 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);
}
}
@EventHandler
public void arrowDismount(final VehicleExitEvent event) {
// TODO: Move elsewhere so not multi-firing?
if (event.getVehicle() instanceof FakeArrow) {
Bukkit.getScheduler().runTaskLater(Denizen.getInstance(), new Runnable() {
@Override
public void run() {
if (event.getVehicle().isValid()) {
event.getVehicle().remove();
}
}
}, 1);
}
}
public SittingTrait() {
super("sitting");
}
}