-
-
Notifications
You must be signed in to change notification settings - Fork 306
/
PlayerlistTracker.java
106 lines (93 loc) · 3.88 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
package net.citizensnpcs.nms.v1_17_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_17_R1.entity.EntityHumanNPC;
import net.citizensnpcs.npc.ai.NPCHolder;
import net.citizensnpcs.util.NMS;
import net.minecraft.network.protocol.game.ClientboundAnimatePacket;
import net.minecraft.server.level.ChunkMap;
import net.minecraft.server.level.ChunkMap.TrackedEntity;
import net.minecraft.server.level.ServerEntity;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.Entity;
public class PlayerlistTracker extends ChunkMap.TrackedEntity {
private ServerPlayer lastUpdatedPlayer;
private final Entity tracker;
public PlayerlistTracker(ChunkMap map, Entity entity, int i, int j, boolean flag) {
map.super(entity, i, j, flag);
this.tracker = entity;
}
public PlayerlistTracker(ChunkMap map, TrackedEntity entry) {
this(map, getTracker(entry), getTrackingDistance(entry), getE(entry), getF(entry));
}
public void updateLastPlayer() {
if (tracker.isRemoved())
return;
final ServerPlayer entityplayer = lastUpdatedPlayer;
if (entityplayer == null)
return;
NMS.sendTabListAdd(entityplayer.getBukkitEntity(), (Player) tracker.getBukkitEntity());
if (!Setting.DISABLE_TABLIST.asBoolean())
return;
Bukkit.getScheduler().scheduleSyncDelayedTask(CitizensAPI.getPlugin(), new Runnable() {
@Override
public void run() {
NMSImpl.sendPacket(entityplayer.getBukkitEntity(), new ClientboundAnimatePacket(tracker, 0));
NMS.sendTabListRemove(entityplayer.getBukkitEntity(), (Player) tracker.getBukkitEntity());
}
}, Setting.TABLIST_REMOVE_PACKET_DELAY.asInt());
}
@Override
public void updatePlayer(final ServerPlayer entityplayer) {
if (entityplayer instanceof EntityHumanNPC) // prevent updates to NPC "viewers"
return;
this.lastUpdatedPlayer = entityplayer;
super.updatePlayer(entityplayer);
}
private static int getE(TrackedEntity entry) {
try {
return (int) E.invoke(TRACKER_ENTRY.invoke(entry));
} catch (Throwable e) {
e.printStackTrace();
}
return 0;
}
private static boolean getF(TrackedEntity entry) {
try {
return (boolean) F.invoke(TRACKER_ENTRY.invoke(entry));
} catch (Throwable e) {
e.printStackTrace();
}
return false;
}
private static Entity getTracker(TrackedEntity entry) {
try {
return (Entity) TRACKER.invoke(entry);
} catch (Throwable e) {
e.printStackTrace();
}
return null;
}
private static int getTrackingDistance(TrackedEntity 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 E = NMS.getGetter(ServerEntity.class, "e");
private static final MethodHandle F = NMS.getGetter(ServerEntity.class, "f");
private static final MethodHandle TRACKER = NMS.getFirstGetter(TrackedEntity.class, Entity.class);
private static final MethodHandle TRACKER_ENTRY = NMS.getFirstGetter(TrackedEntity.class, ServerEntity.class);
private static final MethodHandle TRACKING_RANGE = NMS.getFirstGetter(TrackedEntity.class, int.class);
}