forked from GrimAnticheat/Grim
-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathPlayerDataManager.java
More file actions
133 lines (109 loc) · 5.49 KB
/
Copy pathPlayerDataManager.java
File metadata and controls
133 lines (109 loc) · 5.49 KB
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
package ac.grim.grimac.utils.anticheat;
import ac.grim.grimac.GrimAPI;
import ac.grim.grimac.api.event.events.GrimJoinEvent;
import ac.grim.grimac.api.event.events.GrimQuitEvent;
import ac.grim.grimac.player.GrimPlayer;
import ac.grim.grimac.utils.reflection.GeyserUtil;
import com.github.retrooper.packetevents.PacketEvents;
import com.github.retrooper.packetevents.netty.channel.ChannelHelper;
import com.github.retrooper.packetevents.protocol.player.User;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Collection;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
public class PlayerDataManager {
// Holder — PlayerDataManager is constructed inside GrimAPI's ctor, so a
// plain static-final would see a null GrimAPI.INSTANCE. Holder init runs
// on first fire, after GrimAPI is fully built.
private static final class Channels {
static final GrimJoinEvent.Channel JOIN = GrimAPI.INSTANCE.getEventBus().get(GrimJoinEvent.class);
static final GrimQuitEvent.Channel QUIT = GrimAPI.INSTANCE.getEventBus().get(GrimQuitEvent.class);
}
public final Collection<User> exemptUsers = ConcurrentHashMap.newKeySet();
private final ConcurrentHashMap<User, GrimPlayer> playerDataMap = new ConcurrentHashMap<>();
@Nullable
public GrimPlayer getPlayer(final @NotNull UUID uuid) {
// Is it safe to interact with this, or is this internal PacketEvents code?
Object channel = PacketEvents.getAPI().getProtocolManager().getChannel(uuid);
User user = PacketEvents.getAPI().getProtocolManager().getUser(channel);
return getPlayer(user);
}
@Nullable
public GrimPlayer getPlayer(final @NotNull User user) {
@Nullable GrimPlayer player = playerDataMap.get(user);
if (player != null && player.platformPlayer != null && player.platformPlayer.isExternalPlayer())
return null;
return player;
}
public boolean shouldCheck(@NotNull User user) {
if (exemptUsers.contains(user)) return false;
if (!ChannelHelper.isOpen(user.getChannel())) return false;
if (user.getUUID() != null) {
// Bedrock players don't have Java movement
if (GeyserUtil.isBedrockPlayer(user.getUUID())) {
exemptUsers.add(user);
return false;
}
// Has exempt permission
GrimPlayer grimPlayer = GrimAPI.INSTANCE.getPlayerDataManager().getPlayer(user);
if (grimPlayer != null && grimPlayer.hasPermission("grim.exempt")) {
exemptUsers.add(user);
return false;
}
// Geyser formatted player string
// This will never happen for Java players, as the first character in the 3rd group is always 4 (xxxxxxxx-xxxx-4xxx-xxxx-xxxxxxxxxxxx)
if (user.getUUID().toString().startsWith("00000000-0000-0000-0009")) {
exemptUsers.add(user);
return false;
}
}
return true;
}
public void addUser(final @NotNull User user) {
if (shouldCheck(user)) {
GrimPlayer player = new GrimPlayer(user);
playerDataMap.put(user, player);
Channels.JOIN.fire(player);
}
}
public void forcePut(@NotNull User user, @NotNull GrimPlayer player) {
playerDataMap.put(user, player);
}
public GrimPlayer remove(final @NotNull User user) {
return playerDataMap.remove(user);
}
public void onDisconnect(User user) {
GrimPlayer grimPlayer = remove(user);
if (grimPlayer != null) Channels.QUIT.fire(grimPlayer);
exemptUsers.remove(user);
UUID uuid = user.getProfile().getUUID();
// All cleanup paths should call onDisconnect; routing the session-close + toggle
// eviction here means a stuck PE event (or a JVM-level channel
// close that doesn't surface as UserDisconnectEvent) doesn't leak an open session.
// hooks/toggles are NOOP when the datastore is disabled or its init failed
// AND go NOOP mid-session if an operator runs /grim reload after flipping database.enabled to false
// a player who joined under the prior (enabled) config and disconnects post-reload has no live writer to fire onQuit, so their session stays open (row closed_at IS NULL).
// The next datastore-enabled boot's crash sweep stamps closed_at = last_activity for still-open rows; permanently-disabled-after-the-fact leaves the row untouched until DB is enabled again.
GrimAPI.INSTANCE.getDataStoreLifecycle().liveWriteHooks()
.onQuitFromUserDisconnect(user, grimPlayer, System.currentTimeMillis());
if (uuid != null) {
GrimAPI.INSTANCE.getDataStoreLifecycle().playerToggleStore().evict(uuid);
}
// Check if calling async is safe
if (uuid == null)
return; // folia doesn't like null getPlayer()
GrimAPI.INSTANCE.getAlertManager().handlePlayerQuit(
GrimAPI.INSTANCE.getPlatformPlayerFactory().getFromUUID(uuid)
);
GrimAPI.INSTANCE.getSpectateManager().onQuit(uuid);
// TODO (Cross-platform) confirm this is 100% correct and will always remove players from cache when necessary
GrimAPI.INSTANCE.getPlatformPlayerFactory().invalidatePlayer(uuid);
}
public Collection<GrimPlayer> getEntries() {
return playerDataMap.values();
}
public int size() {
return playerDataMap.size();
}
}