-
-
Notifications
You must be signed in to change notification settings - Fork 306
/
PlayerlistTracker.java
103 lines (90 loc) · 3.71 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
package net.citizensnpcs.nms.v1_14_R1.util;
import java.lang.invoke.MethodHandle;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import net.citizensnpcs.Settings.Setting;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.nms.v1_14_R1.entity.EntityHumanNPC;
import net.citizensnpcs.npc.ai.NPCHolder;
import net.citizensnpcs.util.NMS;
import net.minecraft.server.v1_14_R1.Entity;
import net.minecraft.server.v1_14_R1.EntityPlayer;
import net.minecraft.server.v1_14_R1.EntityTrackerEntry;
import net.minecraft.server.v1_14_R1.PlayerChunkMap;
import net.minecraft.server.v1_14_R1.PlayerChunkMap.EntityTracker;
public class PlayerlistTracker extends PlayerChunkMap.EntityTracker {
private EntityPlayer lastUpdatedPlayer;
private final Entity tracker;
public PlayerlistTracker(PlayerChunkMap map, Entity entity, int i, int j, boolean flag) {
map.super(entity, i, j, flag);
this.tracker = entity;
}
public PlayerlistTracker(PlayerChunkMap map, EntityTracker entry) {
this(map, getTracker(entry), getTrackingDistance(entry), getD(entry), getE(entry));
}
public void updateLastPlayer() {
if (tracker.dead)
return;
final EntityPlayer entityplayer = lastUpdatedPlayer;
NMS.sendTabListAdd(entityplayer.getBukkitEntity(), (Player) tracker.getBukkitEntity());
if (!Setting.DISABLE_TABLIST.asBoolean())
return;
Bukkit.getScheduler().scheduleSyncDelayedTask(CitizensAPI.getPlugin(), new Runnable() {
@Override
public void run() {
NMS.sendTabListRemove(entityplayer.getBukkitEntity(), (Player) tracker.getBukkitEntity());
}
}, Setting.TABLIST_REMOVE_PACKET_DELAY.asInt());
}
@Override
public void updatePlayer(final EntityPlayer entityplayer) {
if (!(entityplayer instanceof EntityHumanNPC)) {
// prevent updates to NPC "viewers"
lastUpdatedPlayer = entityplayer;
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 {
Entity entity = getTracker(entry);
if (entity instanceof NPCHolder) {
return ((NPCHolder) entity).getNPC().data().get(NPC.Metadata.TRACKING_RANGE,
(Integer) TRACKING_RANGE.invoke(entry));
}
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 final MethodHandle TRACKING_RANGE = NMS.getGetter(EntityTracker.class, "trackingDistance");
}