Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,17 @@ private Connection getConnection() {
@Override
public @NonNull List<UUIDMapping> getUUIDs(@NonNull List<String> usernames) {
final List<UUIDMapping> mappings = new ArrayList<>(usernames.size());
try (final PreparedStatement statement = getConnection()
.prepareStatement("SELECT `uuid` FROM `usercache` WHERE `username` = ?")) {
final String query = UUIDService.usernamesCaseSensitive()
? "SELECT `uuid`, `username` FROM `usercache` WHERE `username` = ?"
: "SELECT `uuid`, `username` FROM `usercache` WHERE `username` = ? COLLATE NOCASE";
try (final PreparedStatement statement = getConnection().prepareStatement(query)) {
for (final String username : usernames) {
statement.setString(1, username);
try (final ResultSet resultSet = statement.executeQuery()) {
if (resultSet.next()) {
mappings.add(new UUIDMapping(
UUID.fromString(resultSet.getString("uuid")),
username
resultSet.getString("username")
));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.UUID;
import java.util.function.Consumer;

Expand Down Expand Up @@ -60,16 +61,28 @@ public CacheUUIDService(final int size) {
@Override
public @NonNull List<UUIDMapping> getUUIDs(final @NonNull List<@NonNull String> usernames) {
final List<UUIDMapping> mappings = new ArrayList<>(usernames.size());
mappings.addAll(this.usernameCache.getAllPresent(usernames).values());
for (final String username : usernames) {
final UUIDMapping mapping = this.usernameCache.getIfPresent(usernameKey(username));
if (mapping != null) {
mappings.add(mapping);
}
}
return mappings;
}

@Override
public void accept(final @NonNull List<@NonNull UUIDMapping> uuidMappings) {
for (final UUIDMapping mapping : uuidMappings) {
this.uuidCache.put(mapping.uuid(), mapping);
this.usernameCache.put(mapping.username(), mapping);
this.usernameCache.put(usernameKey(mapping.username()), mapping);
}
}

private static @NonNull String usernameKey(final @NonNull String username) {
if (UUIDService.usernamesCaseSensitive()) {
return username;
}
return username.toLowerCase(Locale.ENGLISH);
}

@Override
Expand Down
21 changes: 17 additions & 4 deletions Core/src/main/java/com/plotsquared/core/uuid/UUIDPipeline.java
Original file line number Diff line number Diff line change
Expand Up @@ -357,15 +357,21 @@ public void getSingle(final @NonNull UUID uuid, final @NonNull BiConsumer<@Nulla

final List<UUIDService> serviceList = this.getServiceListInstance();
final List<UUIDMapping> mappings = new ArrayList<>(requests.size());
final List<String> remainingRequests = new ArrayList<>(requests);
final List<String> remainingRequests = new ArrayList<>(requests.size());
for (final String request : requests) {
if (remainingRequests.stream().noneMatch(existing -> usernamesEqual(existing, request))) {
remainingRequests.add(request);
}
}
final int totalRequests = remainingRequests.size();
Comment on lines +360 to +366

for (final UUIDService service : serviceList) {
// We can chain multiple synchronous
// ones in a row
if (service.canBeSynchronous()) {
final List<UUIDMapping> completedRequests = service.getUUIDs(remainingRequests);
for (final UUIDMapping mapping : completedRequests) {
remainingRequests.remove(mapping.username());
remainingRequests.removeIf(remaining -> usernamesEqual(remaining, mapping.username()));
}
mappings.addAll(completedRequests);
} else {
Expand All @@ -380,15 +386,15 @@ public void getSingle(final @NonNull UUID uuid, final @NonNull BiConsumer<@Nulla
for (final UUIDService service : serviceList) {
final List<UUIDMapping> completedRequests = service.getUUIDs(remainingRequests);
for (final UUIDMapping mapping : completedRequests) {
remainingRequests.remove(mapping.username());
remainingRequests.removeIf(remaining -> usernamesEqual(remaining, mapping.username()));
}
mappings.addAll(completedRequests);
if (remainingRequests.isEmpty()) {
break;
}
}

if (mappings.size() == requests.size()) {
if (mappings.size() == totalRequests) {
this.consume(mappings);
return mappings;
} else if (Settings.DEBUG) {
Expand Down Expand Up @@ -431,4 +437,11 @@ final UUIDMapping getImmediately(final @NonNull Object object) {
return null;
}

private static boolean usernamesEqual(final @NonNull String a, final @NonNull String b) {
if (UUIDService.usernamesCaseSensitive()) {
return a.equals(b);
}
return a.equalsIgnoreCase(b);
}

}
13 changes: 13 additions & 0 deletions Core/src/main/java/com/plotsquared/core/uuid/UUIDService.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package com.plotsquared.core.uuid;

import com.plotsquared.core.configuration.Settings;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;

Expand All @@ -31,6 +32,18 @@
*/
public interface UUIDService {

/**
* Check whether usernames must be treated as case-sensitive. This is
* only the case in offline mode without forced lowercase names, where
* UUIDs are derived from the exact casing of the username, meaning that
* two usernames differing only in case belong to different players.
*
* @return {@code true} if usernames must be matched case-sensitively
*/
static boolean usernamesCaseSensitive() {
return Settings.UUID.OFFLINE && !Settings.UUID.FORCE_LOWERCASE;
}

/**
* Attempt to complete the given requests. Returns the mappings
* that could be created by this server
Expand Down
Loading