/
SkinPacketTracker.java
254 lines (211 loc) · 7.34 KB
/
SkinPacketTracker.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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package net.citizensnpcs.npc.skin;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.scheduler.BukkitTask;
import com.google.common.base.Preconditions;
import net.citizensnpcs.Settings;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.util.NMS;
/**
* Handles and synchronizes add and remove packets for Player type NPC's in order to properly apply the NPC skin.
*
* <p>
* Used as one instance per NPC entity.
* </p>
*/
public class SkinPacketTracker {
private final SkinnableEntity entity;
private final Map<UUID, PlayerEntry> inProgress = new HashMap<UUID, PlayerEntry>(Bukkit.getMaxPlayers() / 2);
private boolean isRemoved;
private Skin skin;
/**
* Constructor.
*
* @param entity
* The skinnable entity the instance belongs to.
*/
public SkinPacketTracker(SkinnableEntity entity) {
Preconditions.checkNotNull(entity);
this.entity = entity;
this.skin = Skin.get(entity);
if (LISTENER == null) {
LISTENER = new PlayerListener();
Bukkit.getPluginManager().registerEvents(LISTENER, CitizensAPI.getPlugin());
}
}
/**
* Get the NPC skin.
*/
public Skin getSkin() {
return skin;
}
/**
* Notify the tracker that a remove packet has been sent to the specified player.
*
* @param playerId
* The ID of the player.
*/
void notifyRemovePacketCancelled(UUID playerId) {
inProgress.remove(playerId);
}
/**
* Notify the tracker that a remove packet has been sent to the specified player.
*
* @param playerId
* The ID of the player.
*/
void notifyRemovePacketSent(UUID playerId) {
PlayerEntry entry = inProgress.get(playerId);
if (entry == null)
return;
if (entry.removeCount == 0)
return;
entry.removeCount -= 1;
if (entry.removeCount == 0) {
inProgress.remove(playerId);
} else {
scheduleRemovePacket(entry);
}
}
/**
* Notify that the NPC skin has been changed.
*/
public void notifySkinChange(boolean forceUpdate) {
this.skin = Skin.get(entity, forceUpdate);
skin.applyAndRespawn(entity);
}
/**
* Invoke when the NPC entity is removed.
*
* <p>
* Sends remove packets to all players.
* </p>
*/
public void onRemoveNPC() {
isRemoved = true;
Collection<? extends Player> players = Bukkit.getOnlinePlayers();
for (Player player : players) {
if (player.hasMetadata("NPC"))
continue;
// send packet now and later to ensure removal from player list
NMS.sendTabListRemove(player, entity.getBukkitEntity());
TAB_LIST_REMOVER.sendPacket(player, entity);
}
}
/**
* Invoke when the NPC entity is spawned.
*/
public void onSpawnNPC() {
isRemoved = false;
new BukkitRunnable() {
@Override
public void run() {
if (!entity.getNPC().isSpawned())
return;
double viewDistance = Settings.Setting.NPC_SKIN_VIEW_DISTANCE.asDouble();
updateNearbyViewers(viewDistance);
}
}.runTaskLater(CitizensAPI.getPlugin(), 20);
}
private void scheduleRemovePacket(final PlayerEntry entry) {
if (isRemoved || !CitizensAPI.hasImplementation() || !CitizensAPI.getPlugin().isEnabled())
return;
entry.removeTask = Bukkit.getScheduler().runTaskLater(CitizensAPI.getPlugin(), new Runnable() {
@Override
public void run() {
if (shouldRemoveFromTabList()) {
TAB_LIST_REMOVER.sendPacket(entry.player, entity);
}
}
}, PACKET_DELAY_REMOVE);
}
private void scheduleRemovePacket(PlayerEntry entry, int count) {
if (!shouldRemoveFromTabList())
return;
entry.removeCount = count;
scheduleRemovePacket(entry);
}
private boolean shouldRemoveFromTabList() {
return entity.getNPC().data().get("removefromtablist", Settings.Setting.DISABLE_TABLIST.asBoolean());
}
/**
* Send skin related packets to all nearby players within the specified block radius.
*
* @param radius
* The radius.
*/
public void updateNearbyViewers(double radius) {
radius *= radius;
org.bukkit.World world = entity.getBukkitEntity().getWorld();
Player from = entity.getBukkitEntity();
Location location = from.getLocation();
for (Player player : world.getPlayers()) {
if (player == null || player.hasMetadata("NPC"))
continue;
player.getLocation(CACHE_LOCATION);
if (!player.canSee(from) || !location.getWorld().equals(CACHE_LOCATION.getWorld()))
continue;
if (location.distanceSquared(CACHE_LOCATION) > radius)
continue;
updateViewer(player);
}
}
/**
* Send skin related packets to a player.
*
* @param player
* The player.
*/
public void updateViewer(final Player player) {
Preconditions.checkNotNull(player);
if (isRemoved || player.hasMetadata("NPC"))
return;
PlayerEntry entry = inProgress.get(player.getUniqueId());
if (entry != null) {
entry.cancel();
} else {
entry = new PlayerEntry(player);
}
TAB_LIST_REMOVER.cancelPackets(player, entity);
inProgress.put(player.getUniqueId(), entry);
skin.apply(entity);
NMS.sendTabListAdd(player, entity.getBukkitEntity());
scheduleRemovePacket(entry, 2);
}
private class PlayerEntry {
Player player;
int removeCount;
BukkitTask removeTask;
PlayerEntry(Player player) {
this.player = player;
}
// cancel previous packet tasks so they do not interfere with
// new tasks
void cancel() {
if (removeTask != null)
removeTask.cancel();
removeCount = 0;
}
}
private static class PlayerListener implements Listener {
@EventHandler
private void onPlayerQuit(PlayerQuitEvent event) {
// this also causes any entries in the "inProgress" field to
// be removed.
TAB_LIST_REMOVER.cancelPackets(event.getPlayer());
}
}
private static final Location CACHE_LOCATION = new Location(null, 0, 0, 0);
private static PlayerListener LISTENER;
private static final int PACKET_DELAY_REMOVE = 1;
private static final TabListRemover TAB_LIST_REMOVER = new TabListRemover();
}