Repository navigation
Keep canonical linked-player names in Bedrock resolution #386
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
f08019b
1e6a2aa
0188867
6fe805a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ | |
| import java.lang.reflect.Method; | ||
| import java.util.Locale; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
| import java.util.UUID; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
| import java.util.function.Consumer; | ||
|
|
@@ -27,7 +28,12 @@ public final class BedrockNameResolver { | |
| private final String bedrockPrefix; | ||
| private final AdvancedCorePlugin plugin; | ||
|
|
||
| private static final String CANONICAL_NAME_KEY = "BedrockCanonicalName"; | ||
| private final Map<String, Boolean> cache = new ConcurrentHashMap<>(); | ||
| // An observed server name is separate from Bedrock client/transport detection. | ||
| private final Map<String, String> canonicalNames = new ConcurrentHashMap<>(); | ||
| private record CachedIdentity(String canonicalName, boolean bedrock) { } | ||
| private final Map<String, CachedIdentity> observedIdentities = new ConcurrentHashMap<>(); | ||
| private final Map<String, String> ciIndex = new ConcurrentHashMap<>(); | ||
|
|
||
| /** | ||
|
|
@@ -127,30 +133,89 @@ public void learn(AdvancedCoreUser user) { | |
| String name = user.getPlayerName(); | ||
| if (name == null || name.isEmpty()) | ||
| return; | ||
| putLearned(name, user.isBedrockUser()); | ||
| boolean bedrock = user.isBedrockUser(); | ||
| putLearned(name, bedrock); | ||
| String canonical = bedrock ? storedCanonicalName(user, name) : null; | ||
| if (canonical != null) canonicalNames.put(name.toLowerCase(Locale.ROOT), canonical); | ||
|
Comment on lines
+137
to
+139
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When an offline linked player's persisted record is learned concurrently with a lookup, AGENTS.md reference: AGENTS.md:L55-L55 Useful? React with 👍 / 👎. |
||
| } | ||
|
|
||
| /** | ||
| * Learns whether a player is a Bedrock player. | ||
| * | ||
| * Observations are immediately available in this resolver. Persistence is queued | ||
| * on the ordered user-storage worker and follows ordinary user-cache durability; | ||
| * return does not acknowledge a durable write. Call from the platform owner. | ||
| * Legacy inline notifications return through that player's owner scheduler; | ||
| * the shared-SQL notification lane remains worker-owned. | ||
| * | ||
| * @param player the player to learn from | ||
| */ | ||
| public void learn(Player player) { | ||
| if (player == null) | ||
| return; | ||
| if (player == null) return; | ||
| // Capture storage identity now. The Player is only a platform callback target. | ||
| final UUID uuid = player.getUniqueId(); | ||
| final boolean isBedrock = bedrockDetect.isBedrock(uuid); | ||
| final String originalName = player.getName(); | ||
| if (isBedrock) { | ||
| plugin.debug("Learned Bedrock player: " + originalName + " (" + uuid + ")"); | ||
| cache.put(originalName, true); | ||
| ciIndex.put(originalName.toLowerCase(Locale.ROOT), originalName); | ||
| AdvancedCoreUser user = userManager.getUser(player); | ||
| if (user != null) | ||
| user.setBedrockUser(true); | ||
| final String name = player.getName(); | ||
| if (uuid == null || name == null || name.isEmpty() || !bedrockDetect.isBedrock(uuid)) return; | ||
| // Publish the flag and exact spelling in one immutable per-name observation. | ||
| observedIdentities.put(name.toLowerCase(Locale.ROOT), new CachedIdentity(name, true)); | ||
| var manager = userManager.getDataManager(); | ||
| if (manager == null || manager.getTimer() == null) { | ||
| reportLearningFailure(); | ||
| return; | ||
| } | ||
| try { | ||
| manager.getTimer().execute(() -> { | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
| try { | ||
| Runnable mutation = () -> { | ||
| AdvancedCoreUser user = userManager.getUser(uuid, false); | ||
| if (user == null) { reportLearningFailure(); return; } | ||
| user.setPlayerName(name); | ||
| // Evidence on this UUID record, never a name/UUID remap. | ||
| user.getData().setString(CANONICAL_NAME_KEY, name); | ||
| user.setBedrockUser(true); | ||
| }; | ||
| // Shared SQL queues its notifications for later, outside this inline scope. | ||
| userManager.withUserDataChangeDispatcher(notification -> { | ||
| try { | ||
| plugin.getBukkitScheduler().runTask(plugin, notification, player); | ||
| } catch (RuntimeException rejected) { | ||
| plugin.getLogger().warning("Unable to deliver observed Bedrock identity notification"); | ||
| } | ||
| }, mutation); | ||
| } catch (RuntimeException failure) { | ||
| reportLearningFailure(); | ||
| } | ||
| }); | ||
| } catch (RuntimeException rejected) { | ||
| reportLearningFailure(); | ||
| } | ||
| } | ||
|
|
||
| private void reportLearningFailure() { | ||
| plugin.getLogger().warning("Unable to persist observed Bedrock identity; observation remains session-local"); | ||
| } | ||
|
|
||
| private String storedCanonicalName(AdvancedCoreUser user, String name) { | ||
| try { | ||
| if (user.getData() == null) return null; | ||
| String observed = user.getData().getString(CANONICAL_NAME_KEY, user.getUserDataFetchMode()); | ||
| // Never let metadata redirect a query to another name/account. | ||
| return observed != null && observed.equalsIgnoreCase(name) ? observed : null; | ||
| } catch (RuntimeException unreadable) { | ||
| plugin.getLogger().warning("Unable to read observed Bedrock identity; retaining legacy resolution"); | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| private String exactName(String name, CachedIdentity identity) { | ||
| return identity.canonicalName() == null ? addPrefixIfNeeded(name, identity.bedrock()) : identity.canonicalName(); | ||
| } | ||
|
|
||
| private String storedExactName(String name, boolean bedrock, AdvancedCoreUser user) { | ||
| String observed = bedrock ? storedCanonicalName(user, name) : null; | ||
| return observed == null ? addPrefixIfNeeded(name, bedrock) : observed; | ||
| } | ||
|
|
||
| /** | ||
| * Detect whether a name corresponds to a Bedrock player. | ||
| * | ||
|
|
@@ -189,18 +254,18 @@ public Result resolve(String incomingName) { | |
| // A known exact identity must win before an online prefixed variant. This | ||
| // prevents an online Bedrock account such as ".Name" from taking a vote for | ||
| // an offline Java account named "Name". | ||
| Boolean cached = getCachedCaseInsensitive(incomingName); | ||
| CachedIdentity cached = getCachedCaseInsensitive(incomingName); | ||
| if (cached != null) { | ||
| boolean bedrock = cached; | ||
| String finalName = addPrefixIfNeeded(incomingName, bedrock); | ||
| boolean bedrock = cached.bedrock(); | ||
| String finalName = exactName(incomingName, cached); | ||
| return new Result(finalName, bedrock, "cache-" + (bedrock ? "bedrock" : "java")); | ||
| } | ||
|
|
||
| try { | ||
| if (userManager.userExistStored(incomingName)) { | ||
| AdvancedCoreUser user = userManager.getUser(incomingName); | ||
| boolean bedrock = user.isBedrockUser(); | ||
| String finalName = addPrefixIfNeeded(incomingName, bedrock); | ||
| String finalName = storedExactName(incomingName, bedrock, user); | ||
| return new Result(finalName, bedrock, "db-" + (bedrock ? "bedrock" : "java")); | ||
| } | ||
| } catch (Throwable ignored) { | ||
|
|
@@ -216,11 +281,11 @@ public Result resolve(String incomingName) { | |
| // Cache on prefixed variant | ||
| String prefixed = buildPrefixedVariant(incomingName); | ||
| if (prefixed != null) { | ||
| Boolean cachedPrefixed = getCachedCaseInsensitive(prefixed); | ||
| CachedIdentity cachedPrefixed = getCachedCaseInsensitive(prefixed); | ||
| if (cachedPrefixed != null) { | ||
| boolean bedrock = cachedPrefixed; | ||
| boolean bedrock = cachedPrefixed.bedrock(); | ||
| // if the prefixed variant is known bedrock, credit the prefixed name | ||
| String finalName = bedrock ? prefixed : incomingName; | ||
| String finalName = bedrock ? exactName(prefixed, cachedPrefixed) : incomingName; | ||
| return new Result(finalName, bedrock, "cache-" + (bedrock ? "bedrock" : "java") + "-prefixed-variant"); | ||
| } | ||
| } | ||
|
|
@@ -321,7 +386,7 @@ private Result resolvePersisted(String incomingName, Result fallback) { | |
| AdvancedCoreUser user = getPersistedUser(incomingName); | ||
| if (user != null) { | ||
| boolean bedrock = user.isBedrockUser(); | ||
| return new Result(addPrefixIfNeeded(incomingName, bedrock), bedrock, | ||
| return new Result(storedExactName(incomingName, bedrock, user), bedrock, | ||
| "db-" + (bedrock ? "bedrock" : "java")); | ||
| } | ||
|
|
||
|
|
@@ -366,10 +431,10 @@ public Result resolveWithoutDb(String incomingName) { | |
| // Cache on prefixed variant | ||
| String prefixed = buildPrefixedVariant(incomingName); | ||
| if (prefixed != null) { | ||
| Boolean cachedPrefixed = getCachedCaseInsensitive(prefixed); | ||
| CachedIdentity cachedPrefixed = getCachedCaseInsensitive(prefixed); | ||
| if (cachedPrefixed != null) { | ||
| boolean bedrock = cachedPrefixed; | ||
| String finalName = bedrock ? prefixed : incomingName; | ||
| boolean bedrock = cachedPrefixed.bedrock(); | ||
| String finalName = bedrock ? exactName(prefixed, cachedPrefixed) : incomingName; | ||
| return new Result(finalName, bedrock, "cache-" + (bedrock ? "bedrock" : "java") + "-prefixed-variant"); | ||
| } | ||
| } | ||
|
|
@@ -385,28 +450,30 @@ public Result resolveWithoutDb(String incomingName) { | |
| private Result resolveExactWithoutDb(String incomingName) { | ||
| Player match = findOnlineExact(incomingName); | ||
| if (match != null) return resultFromOnlineMatch(incomingName, match); | ||
| Boolean cached = getCachedCaseInsensitive(incomingName); | ||
| CachedIdentity cached = getCachedCaseInsensitive(incomingName); | ||
| if (cached == null) return null; | ||
| return new Result(addPrefixIfNeeded(incomingName, cached), cached, | ||
| "cache-" + (cached ? "bedrock" : "java")); | ||
| return new Result(exactName(incomingName, cached), cached.bedrock(), | ||
| "cache-" + (cached.bedrock() ? "bedrock" : "java")); | ||
| } | ||
|
|
||
| /** | ||
| * Get the prefixed name if the player is a Bedrock player. | ||
| * Get the canonical name, preserving observed linked Java and unlinked Bedrock identities. | ||
| * | ||
| * @param name the player name | ||
| * @return the prefixed name if Bedrock, otherwise the original name | ||
| * @return the canonical name selected by the resolver | ||
| */ | ||
| public String getPrefixedIfBedrock(String name) { | ||
| return addPrefixIfNeeded(name, isBedrockName(name)); | ||
| return resolve(name).finalName; | ||
| } | ||
|
|
||
| /** | ||
| * Clear the cache and case-insensitive index. | ||
| */ | ||
| public void clearCache() { | ||
| observedIdentities.clear(); | ||
| cache.clear(); | ||
| ciIndex.clear(); | ||
| canonicalNames.clear(); | ||
| } | ||
|
|
||
| private void putLearned(String originalCaseName, boolean isBedrock) { | ||
|
|
@@ -416,12 +483,18 @@ private void putLearned(String originalCaseName, boolean isBedrock) { | |
| ciIndex.put(originalCaseName.toLowerCase(Locale.ROOT), originalCaseName); | ||
| } | ||
|
|
||
| private Boolean getCachedCaseInsensitive(String name) { | ||
| Boolean exact = cache.get(name); | ||
| if (exact != null) | ||
| return exact; | ||
| String canonical = ciIndex.get(name.toLowerCase(Locale.ROOT)); | ||
| return (canonical != null) ? cache.get(canonical) : null; | ||
| private CachedIdentity getCachedCaseInsensitive(String name) { | ||
| Boolean flag = cache.get(name); | ||
| String lower = name.toLowerCase(Locale.ROOT); | ||
| if (flag == null) { | ||
| String canonical = ciIndex.get(lower); | ||
| flag = canonical == null ? null : cache.get(canonical); | ||
| } | ||
| String storedCanonical = canonicalNames.get(lower); | ||
| // A single read captures both pieces of the live observation. Read it | ||
| // after legacy evidence so concurrent stored learning cannot mask it. | ||
| CachedIdentity observed = observedIdentities.get(lower); | ||
| return observed != null ? observed : flag == null ? null : new CachedIdentity(storedCanonical, flag); | ||
| } | ||
|
|
||
| private String addPrefixIfNeeded(String name, boolean bedrock) { | ||
|
|
@@ -462,7 +535,8 @@ private String stripPrefixIfPresent(String n) { | |
|
|
||
| private Result resultFromOnlineMatch(String incomingName, Player match) { | ||
| boolean bedrock = bedrockDetect.isBedrock(match.getUniqueId()); | ||
| String finalName = bedrock ? addPrefixIfNeeded(match.getName(), true) : incomingName; | ||
| // Bukkit already exposes the canonical linked Java or unlinked Bedrock name. | ||
| String finalName = bedrock ? match.getName() : incomingName; | ||
| return new Result(finalName, bedrock, bedrock ? "online-uuid-bedrock" : "online-uuid-java"); | ||
| } | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.