Skip to content

Commit d835afc

Browse files
committed
Implement centralized UUID/name lookup with caching in UuidLookup class
1 parent 3dc1c5d commit d835afc

File tree

13 files changed

+842
-466
lines changed

13 files changed

+842
-466
lines changed

AdvancedCore/src/main/java/com/bencodez/advancedcore/AdvancedCorePlugin.java

Lines changed: 60 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import java.util.Map.Entry;
1313
import java.util.Queue;
1414
import java.util.UUID;
15-
import java.util.concurrent.ConcurrentHashMap;
1615
import java.util.concurrent.Executors;
1716
import java.util.concurrent.ScheduledExecutorService;
1817
import java.util.concurrent.TimeUnit;
@@ -38,6 +37,7 @@
3837
import com.bencodez.advancedcore.api.misc.effects.FireworkHandler;
3938
import com.bencodez.advancedcore.api.permissions.LuckPermsHandle;
4039
import com.bencodez.advancedcore.api.permissions.PermissionHandler;
40+
import com.bencodez.advancedcore.api.player.UuidLookup;
4141
import com.bencodez.advancedcore.api.rewards.Reward;
4242
import com.bencodez.advancedcore.api.rewards.RewardHandler;
4343
import com.bencodez.advancedcore.api.time.TimeChecker;
@@ -181,9 +181,6 @@ public static void setInstance(AdvancedCorePlugin plugin) {
181181

182182
private ArrayList<UserStartup> userStartup = new ArrayList<>();
183183

184-
@Getter
185-
private ConcurrentHashMap<String, String> uuidNameCache;
186-
187184
@Getter
188185
private String advancedCoreVersion = "";
189186

@@ -579,11 +576,40 @@ public void loadTabComplete() {
579576
@Override
580577
public void reload() {
581578
ArrayList<String> players = new ArrayList<>();
582-
for (String name : getUuidNameCache().values()) {
583-
if (!players.contains(name)) {
579+
580+
// Prefer UUIDs from storage; names come from UuidLookup cache (seeded at
581+
// startup)
582+
for (String uuid : getUserManager().getAllUUIDs()) {
583+
String name = UuidLookup.getInstance().getCachedName(uuid);
584+
if (name == null || name.isEmpty()) {
585+
// Fallback: try to read from user data without DB lookup
586+
try {
587+
AdvancedCoreUser user = getUserManager().getUser(UUID.fromString(uuid), false);
588+
if (user != null) {
589+
String stored = user.getData().getString("PlayerName", UserDataFetchMode.NO_DB_LOOKUP);
590+
if (stored != null && !stored.isEmpty()
591+
&& !stored.equalsIgnoreCase("Error getting name")
592+
&& !stored.equalsIgnoreCase("null")) {
593+
UuidLookup.getInstance().cacheMapping(uuid, stored);
594+
name = stored;
595+
}
596+
}
597+
} catch (Exception ignored) {
598+
}
599+
}
600+
601+
if (name != null && !name.isEmpty() && !players.contains(name)) {
584602
players.add(name);
585603
}
586604
}
605+
606+
// Always include currently online players
607+
for (Player p : Bukkit.getOnlinePlayers()) {
608+
if (!players.contains(p.getName())) {
609+
players.add(p.getName());
610+
}
611+
}
612+
587613
setReplace(players);
588614
}
589615

@@ -594,7 +620,6 @@ public void updateReplacements() {
594620
getReplace().add(player.getName());
595621
}
596622
}
597-
598623
}
599624
});
600625

@@ -646,19 +671,35 @@ public void updateReplacements() {
646671
@Override
647672
public void reload() {
648673
ArrayList<String> uuids = new ArrayList<>();
649-
for (String name : getUuidNameCache().keySet()) {
650-
if (!uuids.contains(name)) {
651-
uuids.add(name);
674+
675+
// UUIDs from storage
676+
for (String uuid : getUserManager().getAllUUIDs()) {
677+
if (uuid != null && !uuid.isEmpty() && !uuids.contains(uuid)) {
678+
uuids.add(uuid);
652679
}
653680
}
681+
682+
// Also include online players UUIDs depending on mode
683+
for (Player player : Bukkit.getOnlinePlayers()) {
684+
String uuid = getOptions().isOnlineMode() ? player.getUniqueId().toString()
685+
: UuidLookup.getInstance().getUUID(player.getName()); // name-derived in offline-mode
686+
687+
if (uuid != null && !uuid.isEmpty() && !uuids.contains(uuid)) {
688+
uuids.add(uuid);
689+
}
690+
}
691+
654692
setReplace(uuids);
655693
}
656694

657695
@Override
658696
public void updateReplacements() {
659697
for (Player player : Bukkit.getOnlinePlayers()) {
660-
if (!getReplace().contains(player.getUniqueId().toString())) {
661-
getReplace().add(player.getUniqueId().toString());
698+
String uuid = getOptions().isOnlineMode() ? player.getUniqueId().toString()
699+
: UuidLookup.getInstance().getUUID(player.getName()); // name-derived in offline-mode
700+
701+
if (uuid != null && !uuid.isEmpty() && !getReplace().contains(uuid)) {
702+
getReplace().add(uuid);
662703
}
663704
}
664705
}
@@ -757,8 +798,6 @@ public void loadUserAPI(UserStorage storageType) {
757798

758799
private void loadUUIDs() {
759800

760-
uuidNameCache = new ConcurrentHashMap<>();
761-
762801
addUserStartup(new UserStartup() {
763802

764803
@Override
@@ -776,26 +815,22 @@ public void onStart() {
776815
public void onStartUp(AdvancedCoreUser user) {
777816
String uuid = user.getUUID();
778817
String name = user.getData().getString("PlayerName", UserDataFetchMode.NO_DB_LOOKUP);
818+
779819
boolean add = true;
780-
if (uuidNameCache.containsKey(uuid)) {
781-
debug("Duplicate uuid? " + uuid + "/" + name);
782-
}
783820

784-
if (name == null || name.equals("") || name.equals("Error getting name") || name.equals("null")) {
785-
// extraDebug("Invalid player name: " + uuid);
821+
if (name == null || name.isEmpty() || name.equalsIgnoreCase("Error getting name")
822+
|| name.equalsIgnoreCase("null")) {
786823
add = false;
787-
} else {
788-
if (uuidNameCache.containsValue(name)) {
789-
debug("Duplicate player name?" + uuid + "/" + name);
790-
}
791824
}
792-
if (uuid == null || uuid.equals("")) {
825+
826+
if (uuid == null || uuid.isEmpty()) {
793827
debug("Invalid uuid: " + uuid);
794828
add = false;
795829
}
796830

797831
if (add) {
798-
uuidNameCache.put(uuid, name);
832+
// Seed the centralized cache
833+
UuidLookup.getInstance().cacheMapping(uuid, name);
799834
}
800835
}
801836
});

AdvancedCore/src/main/java/com/bencodez/advancedcore/api/misc/PlayerManager.java

Lines changed: 16 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package com.bencodez.advancedcore.api.misc;
22

3-
import java.nio.charset.StandardCharsets;
4-
import java.util.Map.Entry;
53
import java.util.UUID;
6-
import java.util.concurrent.ConcurrentHashMap;
74
import java.util.concurrent.ThreadLocalRandom;
85

96
import org.bukkit.Bukkit;
@@ -16,9 +13,8 @@
1613
import org.bukkit.inventory.meta.ItemMeta;
1714

1815
import com.bencodez.advancedcore.AdvancedCorePlugin;
16+
import com.bencodez.advancedcore.api.player.UuidLookup;
1917
import com.bencodez.advancedcore.api.user.AdvancedCoreUser;
20-
import com.bencodez.advancedcore.api.user.UserDataFetchMode;
21-
import com.bencodez.advancedcore.api.user.UserStorage;
2218

2319
public class PlayerManager {
2420

@@ -67,45 +63,21 @@ public boolean damageItemInHand(Player player, int damage) {
6763
return false;
6864
}
6965

66+
/**
67+
* @deprecated Use {@link UuidLookup#getPlayerName(AdvancedCoreUser, String)}.
68+
*/
69+
@Deprecated
7070
public String getPlayerName(AdvancedCoreUser user, String uuid) {
71-
return getPlayerName(user, uuid, true);
71+
return UuidLookup.getInstance().getPlayerName(user, uuid);
7272
}
7373

74+
/**
75+
* @deprecated Use
76+
* {@link UuidLookup#getPlayerName(AdvancedCoreUser, String, boolean)}.
77+
*/
78+
@Deprecated
7479
public String getPlayerName(AdvancedCoreUser user, String uuid, boolean useCache) {
75-
if ((uuid == null) || uuid.equalsIgnoreCase("null") || uuid.isEmpty()) {
76-
plugin.debug("Null UUID");
77-
return "";
78-
}
79-
80-
if (plugin.getUuidNameCache().containsKey(uuid)) {
81-
String n = plugin.getUuidNameCache().get(uuid);
82-
if (n != null && !n.isEmpty() && !n.equalsIgnoreCase("Error getting name")) {
83-
return n;
84-
}
85-
}
86-
87-
String name = "";
88-
89-
if (uuid.length() <= 5) {
90-
return "Error getting name";
91-
}
92-
java.util.UUID u = java.util.UUID.fromString(uuid);
93-
Player player = Bukkit.getPlayer(u);
94-
95-
String storedName = user.getData().getString("PlayerName", UserDataFetchMode.fromBooleans(useCache, true));
96-
// String storedName = "";
97-
if (player != null) {
98-
name = player.getName();
99-
100-
if (storedName == null || name != storedName || storedName.isEmpty()
101-
|| storedName.equalsIgnoreCase("Error getting name")) {
102-
if (user.getUserData().hasData()) {
103-
user.getData().setString("PlayerName", name);
104-
}
105-
}
106-
return name;
107-
}
108-
return storedName;
80+
return UuidLookup.getInstance().getPlayerName(user, uuid, useCache);
10981
}
11082

11183
public ItemStack getPlayerSkull(UUID uuid, String name) {
@@ -114,95 +86,19 @@ public ItemStack getPlayerSkull(UUID uuid, String name) {
11486

11587
public ItemStack getPlayerSkull(UUID uuid, String name, boolean force) {
11688
return plugin.getSkullCacheHandler().getSkull(uuid, name);
117-
/*
118-
* String skullMaterial = "PLAYER_HEAD"; if
119-
* (NMSManager.getInstance().isVersion("1.12")) { skullMaterial = "PAPER"; } if
120-
* (AdvancedCorePlugin.getInstance().getOptions().isLoadSkulls()) { if
121-
* (SkullHandler.getInstance().hasSkull(playerName)) { try { return
122-
* SkullHandler.getInstance().getItemStack(playerName); } catch (Exception e) {
123-
* e.printStackTrace(); } } else {
124-
* SkullHandler.getInstance().loadSkull(playerName); if (force) { return new
125-
* ItemBuilder(Material.valueOf(skullMaterial),
126-
* 1).setSkullOwner(playerName).toItemStack(); } else { return new
127-
* ItemBuilder(Material.valueOf(skullMaterial), 1).toItemStack(); } } } return
128-
* new ItemBuilder(Material.valueOf(skullMaterial),
129-
* 1).setSkullOwner(playerName).toItemStack();
130-
*/
131-
13289
}
13390

13491
/**
13592
* Gets the uuid.
13693
*
13794
* @param playerName the player name
13895
* @return the uuid
96+
*
97+
* @deprecated Use {@link UuidLookup#getUUID(String)}.
13998
*/
99+
@Deprecated
140100
public String getUUID(String playerName) {
141-
if (playerName == null || playerName.isEmpty()) {
142-
return null;
143-
}
144-
145-
if (!plugin.getOptions().isOnlineMode()) {
146-
return UUID.nameUUIDFromBytes(("OfflinePlayer:" + playerName).getBytes(StandardCharsets.UTF_8)).toString();
147-
}
148-
149-
Player player = Bukkit.getPlayerExact(playerName);
150-
if (player != null) {
151-
return player.getUniqueId().toString();
152-
}
153-
154-
String uuid = getUUIDLookup(playerName);
155-
156-
if (!uuid.equals("")) {
157-
return uuid;
158-
}
159-
160-
try {
161-
@SuppressWarnings("deprecation")
162-
OfflinePlayer p = Bukkit.getOfflinePlayer(playerName);
163-
return p.getUniqueId().toString();
164-
} catch (Exception e) {
165-
plugin.getLogger().info("Unable to get UUID for: " + playerName);
166-
plugin.debug(e);
167-
return "";
168-
}
169-
}
170-
171-
private String getUUIDLookup(String playerName) {
172-
if (playerName == null) {
173-
return "";
174-
}
175-
ConcurrentHashMap<String, String> uuids = plugin.getUuidNameCache();
176-
if (uuids != null) {
177-
for (Entry<String, String> entry : uuids.entrySet()) {
178-
if (entry.getValue().equalsIgnoreCase(playerName)) {
179-
return entry.getKey();
180-
}
181-
}
182-
}
183-
184-
if (plugin.getStorageType().equals(UserStorage.MYSQL)) {
185-
String name = plugin.getMysql().getUUID(playerName);
186-
if (name != null) {
187-
return name;
188-
}
189-
} else if (plugin.getStorageType().equals(UserStorage.SQLITE)) {
190-
String name = plugin.getSQLiteUserTable().getUUID(playerName);
191-
if (name != null) {
192-
return name;
193-
}
194-
} else {
195-
for (String uuid : plugin.getUserManager().getAllUUIDs()) {
196-
AdvancedCoreUser user = plugin.getUserManager().getUser(UUID.fromString(uuid));
197-
user.userDataFetechMode(UserDataFetchMode.NO_CACHE);
198-
String name = user.getData().getString("PlayerName", UserDataFetchMode.NO_CACHE);
199-
if (name != null && name.equals(playerName)) {
200-
plugin.getUuidNameCache().put(uuid, playerName);
201-
return uuid;
202-
}
203-
}
204-
}
205-
return "";
101+
return UuidLookup.getInstance().getUUID(playerName);
206102
}
207103

208104
public boolean hasEitherPermission(CommandSender sender, String perm) {
@@ -321,9 +217,7 @@ public boolean hasServerPermission(UUID playerUUID, String playername, String pe
321217
}
322218

323219
if (plugin.getLuckPermsHandle() != null && plugin.getLuckPermsHandle().luckpermsApiLoaded()) {
324-
// plugin.devDebug("Attempting to use luckperms");
325220
if (plugin.getLuckPermsHandle().hasPermission(playerUUID, perm)) {
326-
// plugin.devDebug("does have permission: " + perm);
327221
return true;
328222
}
329223
}
@@ -348,7 +242,6 @@ public boolean isValidUser(String name, boolean checkServer) {
348242
return true;
349243
}
350244

351-
// plugin.extraDebug("Checking if user exists in database: " + name);
352245
boolean userExist = plugin.getUserManager().userExist(name);
353246
if (userExist) {
354247
return userExist;
@@ -364,16 +257,13 @@ public boolean isValidUser(String name, boolean checkServer) {
364257
}
365258

366259
if (checkServer && !name.startsWith(plugin.getOptions().getBedrockPlayerPrefix())) {
367-
// plugin.extraDebug("Checking offline player: " + name);
368260
OfflinePlayer p = Bukkit.getOfflinePlayer(name);
369261
if (p.hasPlayedBefore() || p.isOnline() || p.getLastPlayed() != 0) {
370-
// plugin.extraDebug(name + " has joined before");
371262
return true;
372263
}
373264
}
374265

375266
plugin.extraDebug("Player " + name + " does not exist");
376267
return false;
377268
}
378-
379269
}

0 commit comments

Comments
 (0)