-
-
Notifications
You must be signed in to change notification settings - Fork 307
/
PlayerlistTrackerEntry.java
214 lines (189 loc) · 8.03 KB
/
PlayerlistTrackerEntry.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
package net.citizensnpcs.nms.v1_13_R2.util;
import java.lang.invoke.MethodHandle;
import java.lang.reflect.Field;
import java.util.Map;
import java.util.Set;
import org.bukkit.Bukkit;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import com.google.common.collect.ForwardingMap;
import com.google.common.collect.ForwardingSet;
import net.citizensnpcs.Settings.Setting;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.event.NPCSeenByPlayerEvent;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.nms.v1_13_R2.entity.EntityHumanNPC;
import net.citizensnpcs.npc.ai.NPCHolder;
import net.citizensnpcs.util.NMS;
import net.minecraft.server.v1_13_R2.Entity;
import net.minecraft.server.v1_13_R2.EntityPlayer;
import net.minecraft.server.v1_13_R2.EntityTrackerEntry;
import net.minecraft.server.v1_13_R2.PacketPlayOutAnimation;
public class PlayerlistTrackerEntry extends EntityTrackerEntry {
private final Entity tracker;
private Map<EntityPlayer, Boolean> trackingMap;
public PlayerlistTrackerEntry(Entity entity, int i, int j, int k, boolean flag) {
super(entity, i, j, k, flag);
tracker = getTracker(this);
if (TRACKING_MAP_SETTER != null) {
try {
Map<EntityPlayer, Boolean> delegate = (Map<EntityPlayer, Boolean>) TRACKING_MAP_GETTER.invoke(this);
trackingMap = delegate;
TRACKING_MAP_SETTER.invoke(this, new ForwardingMap<EntityPlayer, Boolean>() {
@Override
protected Map<EntityPlayer, Boolean> delegate() {
return delegate;
}
@Override
public Boolean put(EntityPlayer player, Boolean value) {
Boolean res = super.put(player, value);
if (res == null) {
updateLastPlayer(player);
}
return res;
}
});
} catch (Throwable e) {
e.printStackTrace();
}
} else {
try {
Set<EntityPlayer> delegate = super.trackedPlayers;
TRACKING_SET_SETTER.invoke(this, new ForwardingSet<EntityPlayer>() {
@Override
public boolean add(EntityPlayer player) {
boolean res = super.add(player);
if (res) {
updateLastPlayer(player);
}
return res;
}
@Override
protected Set<EntityPlayer> delegate() {
return delegate;
}
});
} catch (Throwable e) {
e.printStackTrace();
}
}
}
public PlayerlistTrackerEntry(EntityTrackerEntry entry) {
this(getTracker(entry), getE(entry), getF(entry), getG(entry), getU(entry));
}
private boolean isTracked(EntityPlayer player) {
return trackingMap != null ? trackingMap.containsKey(player) : trackedPlayers.contains(player);
}
public void updateLastPlayer(EntityPlayer lastUpdatedPlayer) {
if (tracker.dead || lastUpdatedPlayer == null || tracker.getBukkitEntity().getType() != EntityType.PLAYER)
return;
final EntityPlayer entityplayer = lastUpdatedPlayer;
NMS.sendTabListAdd(entityplayer.getBukkitEntity(), (Player) tracker.getBukkitEntity());
lastUpdatedPlayer = null;
NPC npc = ((NPCHolder) tracker).getNPC();
if (npc.data().get(NPC.Metadata.RESET_YAW_ON_SPAWN, Setting.RESET_YAW_ON_SPAWN.asBoolean())) {
Bukkit.getScheduler().scheduleSyncDelayedTask(CitizensAPI.getPlugin(),
() -> NMSImpl.sendPacket(entityplayer.getBukkitEntity(), new PacketPlayOutAnimation(tracker, 0)),
1);
}
if (!Setting.DISABLE_TABLIST.asBoolean())
return;
Bukkit.getScheduler().scheduleSyncDelayedTask(CitizensAPI.getPlugin(),
() -> NMS.sendTabListRemove(entityplayer.getBukkitEntity(), (Player) tracker.getBukkitEntity()),
Setting.TABLIST_REMOVE_PACKET_DELAY.asTicks());
}
@Override
public void updatePlayer(final EntityPlayer entityplayer) {
if (!tracker.dead && !isTracked(entityplayer) && tracker instanceof NPCHolder) {
NPC npc = ((NPCHolder) tracker).getNPC();
NPCSeenByPlayerEvent event = new NPCSeenByPlayerEvent(npc, entityplayer.getBukkitEntity());
Bukkit.getPluginManager().callEvent(event);
if (event.isCancelled())
return;
Integer trackingRange = npc.data().<Integer> get(NPC.Metadata.TRACKING_RANGE);
if (TRACKING_RANGE_SETTER != null && trackingRange != null
&& npc.data().get("last-tracking-range", -1) != trackingRange.intValue()) {
try {
TRACKING_RANGE_SETTER.invoke(this, trackingRange);
npc.data().set("last-tracking-range", trackingRange);
} catch (Throwable e) {
e.printStackTrace();
}
}
}
if (entityplayer instanceof EntityHumanNPC)
return;
super.updatePlayer(entityplayer);
}
private static int getE(EntityTrackerEntry entry) {
try {
return (Integer) E.get(entry);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return 0;
}
private static int getF(EntityTrackerEntry entry) {
try {
return (Integer) F.get(entry);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return 0;
}
private static int getG(EntityTrackerEntry entry) {
try {
return (Integer) G.get(entry);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return 0;
}
private static Entity getTracker(EntityTrackerEntry entry) {
try {
return (Entity) TRACKER.get(entry);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return null;
}
private static boolean getU(EntityTrackerEntry entry) {
try {
return (Boolean) U.get(entry);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return false;
}
private static Field E = NMS.getField(EntityTrackerEntry.class, "e");
private static Field F = NMS.getField(EntityTrackerEntry.class, "f");
private static Field G = NMS.getField(EntityTrackerEntry.class, "g");
private static Field TRACKER = NMS.getField(EntityTrackerEntry.class, "tracker");
private static MethodHandle TRACKING_MAP_GETTER;
private static MethodHandle TRACKING_MAP_SETTER;
private static final MethodHandle TRACKING_RANGE_SETTER = NMS.getFirstFinalSetter(EntityTrackerEntry.class,
int.class);
private static final MethodHandle TRACKING_SET_SETTER = NMS.getFirstFinalSetter(EntityTrackerEntry.class,
Set.class);
private static Field U = NMS.getField(EntityTrackerEntry.class, "u");
static {
try {
// Old paper versions override the tracked player set to be a map
if (EntityTrackerEntry.class.getField("trackedPlayerMap") != null) {
TRACKING_MAP_SETTER = NMS.getFirstSetter(EntityTrackerEntry.class, Map.class);
TRACKING_MAP_GETTER = NMS.getFirstGetter(EntityTrackerEntry.class, Map.class);
}
} catch (Exception e) {
}
}
}