-
-
Notifications
You must be signed in to change notification settings - Fork 307
/
PlayerlistTracker.java
188 lines (169 loc) · 7.55 KB
/
PlayerlistTracker.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
package net.citizensnpcs.nms.v1_15_R1.util;
import java.lang.invoke.MethodHandle;
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_15_R1.entity.EntityHumanNPC;
import net.citizensnpcs.npc.ai.NPCHolder;
import net.citizensnpcs.util.NMS;
import net.minecraft.server.v1_15_R1.Entity;
import net.minecraft.server.v1_15_R1.EntityPlayer;
import net.minecraft.server.v1_15_R1.EntityTrackerEntry;
import net.minecraft.server.v1_15_R1.PacketPlayOutAnimation;
import net.minecraft.server.v1_15_R1.PlayerChunkMap;
import net.minecraft.server.v1_15_R1.PlayerChunkMap.EntityTracker;
public class PlayerlistTracker extends PlayerChunkMap.EntityTracker {
private final Entity tracker;
private Map<EntityPlayer, Boolean> trackingMap;
public PlayerlistTracker(PlayerChunkMap map, Entity entity, int i, int j, boolean flag) {
map.super(entity, i, j, flag);
this.tracker = entity;
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 PlayerlistTracker(PlayerChunkMap map, EntityTracker entry) {
this(map, getTracker(entry), getTrackingDistance(entry), getD(entry), getE(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());
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 getD(EntityTracker entry) {
try {
return (int) D.invoke(TRACKER_ENTRY.invoke(entry));
} catch (Throwable e) {
e.printStackTrace();
}
return 0;
}
private static boolean getE(EntityTracker entry) {
try {
return (boolean) E.invoke(TRACKER_ENTRY.invoke(entry));
} catch (Throwable e) {
e.printStackTrace();
}
return false;
}
private static Entity getTracker(EntityTracker entry) {
try {
return (Entity) TRACKER.invoke(entry);
} catch (Throwable e) {
e.printStackTrace();
}
return null;
}
private static int getTrackingDistance(EntityTracker entry) {
try {
return (Integer) TRACKING_RANGE.invoke(entry);
} catch (Throwable e) {
e.printStackTrace();
}
return 0;
}
private static final MethodHandle D = NMS.getGetter(EntityTrackerEntry.class, "d");
private static final MethodHandle E = NMS.getGetter(EntityTrackerEntry.class, "e");
private static final MethodHandle TRACKER = NMS.getGetter(EntityTracker.class, "tracker");
private static final MethodHandle TRACKER_ENTRY = NMS.getGetter(EntityTracker.class, "trackerEntry");
private static MethodHandle TRACKING_MAP_GETTER;
private static MethodHandle TRACKING_MAP_SETTER;
private static final MethodHandle TRACKING_RANGE = NMS.getGetter(EntityTracker.class, "trackingDistance");
private static final MethodHandle TRACKING_RANGE_SETTER = NMS.getFirstFinalSetter(EntityTracker.class, int.class);
private static final MethodHandle TRACKING_SET_SETTER = NMS.getFirstFinalSetter(EntityTracker.class, Set.class);
static {
try {
// Old paper versions override the tracked player set to be a map
if (EntityTracker.class.getField("trackedPlayerMap") != null) {
TRACKING_MAP_SETTER = NMS.getFirstSetter(EntityTracker.class, Map.class);
TRACKING_MAP_GETTER = NMS.getFirstGetter(EntityTracker.class, Map.class);
}
} catch (Exception e) {
}
}
}