playerMap = new HashMap<>();
+ private final Object playerLock = new Object();
+
+ /**
+ * Remove a player from the player map
+ *
+ * @param plotPlayer Player to remove
+ */
+ public void removePlayer(@NotNull final PlotPlayer plotPlayer) {
+ synchronized (playerLock) {
+ this.playerMap.remove(plotPlayer.getUUID());
+ }
+ }
+
+ /**
+ * Get the player from its UUID if it is stored in the player map.
+ *
+ * @param uuid Player UUID
+ * @return Player, or null
+ */
+ @Nullable public PlotPlayer getPlayerIfExists(@Nullable final UUID uuid) {
+ if (uuid == null) {
+ return null;
+ }
+ return this.playerMap.get(uuid);
+ }
+
+ @Nullable public PlotPlayer getPlayerIfExists(@Nullable final String name) {
+ for (final PlotPlayer plotPlayer : this.playerMap.values()) {
+ if (plotPlayer.getName().equalsIgnoreCase(name)) {
+ return plotPlayer;
+ }
+ }
+ return null;
+ }
+
+ /**
+ * Get a plot player from a UUID. This method requires
+ * that the caller actually knows that the player exists.
+ *
+ * The method will throw an exception if there is no such
+ * player online.
+ *
+ * @param uuid Player UUID
+ * @return Player object
+ */
+ @NotNull public PlotPlayer getPlayer(@NotNull final UUID uuid) {
+ synchronized (playerLock) {
+ PlotPlayer player = this.playerMap.get(uuid);
+ if (player == null) {
+ player = createPlayer(uuid);
+ this.playerMap.put(uuid, player);
+ }
+ return player;
+ }
+ }
+
+ @NotNull protected abstract PlotPlayer createPlayer(@NotNull final UUID uuid);
+
+ /**
+ * Get all online players
+ *
+ * @return Unmodifiable collection of players
+ */
+ public Collection getPlayers() {
+ return Collections.unmodifiableCollection(this.playerMap.values());
+ }
+
+
+ public static final class NoSuchPlayerException extends IllegalArgumentException {
+
+ public NoSuchPlayerException(@NotNull final UUID uuid) {
+ super(String.format("There is no online player with UUID '%s'", uuid.toString()));
+ }
+
+ }
+
+}
diff --git a/Core/src/main/java/com/plotsquared/core/util/SchematicHandler.java b/Core/src/main/java/com/plotsquared/core/util/SchematicHandler.java
index 3adb8d19e..aa9e7a04c 100644
--- a/Core/src/main/java/com/plotsquared/core/util/SchematicHandler.java
+++ b/Core/src/main/java/com/plotsquared/core/util/SchematicHandler.java
@@ -104,7 +104,7 @@ public abstract class SchematicHandler {
Iterator i = plots.iterator();
final Plot plot = i.next();
i.remove();
- String owner = UUIDHandler.getName(plot.guessOwner());
+ String owner = UUIDHandler.getName(plot.getOwnerAbs());
if (owner == null) {
owner = "unknown";
}
diff --git a/Core/src/main/java/com/plotsquared/core/util/TabCompletions.java b/Core/src/main/java/com/plotsquared/core/util/TabCompletions.java
new file mode 100644
index 000000000..a7c7c2a8f
--- /dev/null
+++ b/Core/src/main/java/com/plotsquared/core/util/TabCompletions.java
@@ -0,0 +1,82 @@
+/*
+ * _____ _ _ _____ _
+ * | __ \| | | | / ____| | |
+ * | |__) | | ___ | |_| (___ __ _ _ _ __ _ _ __ ___ __| |
+ * | ___/| |/ _ \| __|\___ \ / _` | | | |/ _` | '__/ _ \/ _` |
+ * | | | | (_) | |_ ____) | (_| | |_| | (_| | | | __/ (_| |
+ * |_| |_|\___/ \__|_____/ \__, |\__,_|\__,_|_| \___|\__,_|
+ * | |
+ * |_|
+ * PlotSquared plot management system for Minecraft
+ * Copyright (C) 2020 IntellectualSites
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see .
+ */
+package com.plotsquared.core.util;
+
+import com.google.common.cache.Cache;
+import com.google.common.cache.CacheBuilder;
+import com.plotsquared.core.PlotSquared;
+import com.plotsquared.core.command.Command;
+import com.plotsquared.core.command.CommandCategory;
+import com.plotsquared.core.command.RequiredType;
+import com.plotsquared.core.uuid.UUIDMapping;
+import lombok.experimental.UtilityClass;
+import org.jetbrains.annotations.NotNull;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.List;
+import java.util.Locale;
+import java.util.concurrent.TimeUnit;
+import java.util.stream.Collectors;
+
+/**
+ * Tab completion utilities
+ */
+@UtilityClass public class TabCompletions {
+
+ private final Cache> cachedCompletionValues =
+ CacheBuilder.newBuilder().expireAfterWrite(1, TimeUnit.MINUTES).build();
+
+ /**
+ * Get a list of tab completions corresponding to player names. This uses the UUID pipeline
+ * cache, so it will complete will all names known to PlotSquared
+ *
+ * @param input Command input
+ * @param existing Players that should not be included in completions
+ * @return List of completions
+ */
+ @NotNull public List completePlayers(@NotNull final String input,
+ @NotNull final List existing) {
+ List players = cachedCompletionValues.getIfPresent("players");
+ if (players == null) {
+ final Collection mappings =
+ PlotSquared.get().getImpromptuUUIDPipeline().getAllImmediately();
+ players = new ArrayList<>(mappings.size());
+ for (final UUIDMapping mapping : mappings) {
+ players.add(mapping.getUsername());
+ }
+ cachedCompletionValues.put("players", players);
+ }
+ final String processedInput = input.toLowerCase(Locale.ENGLISH);
+ return players.stream()
+ .filter(player -> player.toLowerCase(Locale.ENGLISH).startsWith(processedInput))
+ .filter(player -> !existing.contains(player)).map(
+ player -> new Command(null, false, player, "", RequiredType.NONE,
+ CommandCategory.INFO) {
+ }).collect(Collectors.toList());
+ }
+
+}
diff --git a/Core/src/main/java/com/plotsquared/core/util/uuid/UUIDHandler.java b/Core/src/main/java/com/plotsquared/core/util/uuid/UUIDHandler.java
deleted file mode 100644
index 6dd8b1c47..000000000
--- a/Core/src/main/java/com/plotsquared/core/util/uuid/UUIDHandler.java
+++ /dev/null
@@ -1,198 +0,0 @@
-/*
- * _____ _ _ _____ _
- * | __ \| | | | / ____| | |
- * | |__) | | ___ | |_| (___ __ _ _ _ __ _ _ __ ___ __| |
- * | ___/| |/ _ \| __|\___ \ / _` | | | |/ _` | '__/ _ \/ _` |
- * | | | | (_) | |_ ____) | (_| | |_| | (_| | | | __/ (_| |
- * |_| |_|\___/ \__|_____/ \__, |\__,_|\__,_|_| \___|\__,_|
- * | |
- * |_|
- * PlotSquared plot management system for Minecraft
- * Copyright (C) 2020 IntellectualSites
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see .
- */
-package com.plotsquared.core.util.uuid;
-
-import com.google.common.collect.BiMap;
-import com.plotsquared.core.PlotSquared;
-import com.plotsquared.core.configuration.Captions;
-import com.plotsquared.core.database.DBFunc;
-import com.plotsquared.core.player.OfflinePlotPlayer;
-import com.plotsquared.core.player.PlotPlayer;
-import com.plotsquared.core.util.StringWrapper;
-import com.plotsquared.core.util.task.RunnableVal;
-import org.jetbrains.annotations.NotNull;
-import org.jetbrains.annotations.Nullable;
-
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.Map;
-import java.util.UUID;
-
-public class UUIDHandler {
-
- public static UUIDHandlerImplementation implementation;
-
- public static void add(StringWrapper name, UUID uuid) {
- implementation.add(name, uuid);
- }
-
- /**
- * Get the map containing all names/uuids.
- *
- * @return map with names + uuids
- * @see BiMap
- */
- public static BiMap getUuidMap() {
- return implementation.getUUIDMap();
- }
-
- /**
- * Check if a uuid is cached
- *
- * @param uuid to check
- * @return true of the uuid is cached
- * @see BiMap#containsValue(Object)
- */
- public static boolean uuidExists(UUID uuid) {
- return implementation.uuidExists(uuid);
- }
-
- /**
- * Check if a name is cached
- *
- * @param name to check
- * @return true of the name is cached
- * @see BiMap#containsKey(Object)
- */
- public static boolean nameExists(StringWrapper name) {
- return implementation.nameExists(name);
- }
-
- public static HashSet getAllUUIDS() {
- final HashSet uuids = new HashSet<>();
- PlotSquared.get().forEachPlotRaw(plot -> {
- if (plot.hasOwner()) {
- uuids.add(plot.getOwnerAbs());
- uuids.addAll(plot.getTrusted());
- uuids.addAll(plot.getMembers());
- uuids.addAll(plot.getDenied());
- }
- });
- return uuids;
- }
-
- public static UUIDWrapper getUUIDWrapper() {
- return implementation.getUUIDWrapper();
- }
-
- public static void setUUIDWrapper(UUIDWrapper wrapper) {
- implementation.setUUIDWrapper(wrapper);
- }
-
- public static void startCaching(Runnable whenDone) {
- implementation.startCaching(whenDone);
- }
-
- public static void cache(BiMap toAdd) {
- implementation.add(toAdd);
- }
-
- @NotNull public static UUID getUUID(PlotPlayer player) {
- return implementation.getUUID(player);
- }
-
- public static UUID getUUID(OfflinePlotPlayer player) {
- if (implementation == null) {
- return null;
- }
- return implementation.getUUID(player);
- }
-
- @Nullable public static String getName(UUID uuid) {
- if (implementation == null) {
- return null;
- }
- if (uuid != null && uuid.equals(DBFunc.SERVER)) {
- return Captions.SERVER.getTranslated();
- }
- return implementation.getName(uuid);
- }
-
- @Nullable public static PlotPlayer getPlayer(@Nullable final UUID uuid) {
- if (implementation == null || uuid == null) {
- return null;
- }
- return check(implementation.getPlayer(uuid));
- }
-
- public static PlotPlayer getPlayer(String name) {
- if (implementation == null) {
- return null;
- }
- return check(implementation.getPlayer(name));
- }
-
- private static PlotPlayer check(@Nullable PlotPlayer player) {
- if (player != null && !player.isOnline()) {
- UUIDHandler.getPlayers().remove(player.getName());
- PlotSquared.get().IMP.unregister(player);
- player = null;
- }
- return player;
- }
-
- public static UUID getUUIDFromString(String nameOrUUIDString) {
- if (implementation == null) {
- return null;
- }
- if (nameOrUUIDString.length() > 16) {
- try {
- return UUID.fromString(nameOrUUIDString);
- } catch (IllegalArgumentException e) {
- return null;
- }
- }
- return UUIDHandler.getUUID(nameOrUUIDString, null);
- }
-
- public static UUID getUUID(String name, RunnableVal ifFetch) {
- if (implementation == null) {
- return null;
- }
- return implementation.getUUID(name, ifFetch);
- }
-
- public static UUID getCachedUUID(String name, RunnableVal ifFetch) {
- if (implementation == null) {
- return null;
- }
- return implementation.getUUIDMap().get(new StringWrapper(name));
- }
-
- public static Map getPlayers() {
- if (implementation == null) {
- return new HashMap<>();
- }
- return implementation.getPlayers();
- }
-
- public static void handleShutdown() {
- if (implementation == null) {
- return;
- }
- implementation.handleShutdown();
- }
-}
diff --git a/Core/src/main/java/com/plotsquared/core/util/uuid/UUIDHandlerImplementation.java b/Core/src/main/java/com/plotsquared/core/util/uuid/UUIDHandlerImplementation.java
deleted file mode 100644
index 687af58e0..000000000
--- a/Core/src/main/java/com/plotsquared/core/util/uuid/UUIDHandlerImplementation.java
+++ /dev/null
@@ -1,324 +0,0 @@
-/*
- * _____ _ _ _____ _
- * | __ \| | | | / ____| | |
- * | |__) | | ___ | |_| (___ __ _ _ _ __ _ _ __ ___ __| |
- * | ___/| |/ _ \| __|\___ \ / _` | | | |/ _` | '__/ _ \/ _` |
- * | | | | (_) | |_ ____) | (_| | |_| | (_| | | | __/ (_| |
- * |_| |_|\___/ \__|_____/ \__, |\__,_|\__,_|_| \___|\__,_|
- * | |
- * |_|
- * PlotSquared plot management system for Minecraft
- * Copyright (C) 2020 IntellectualSites
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see .
- */
-package com.plotsquared.core.util.uuid;
-
-import com.google.common.base.Charsets;
-import com.google.common.collect.BiMap;
-import com.google.common.collect.HashBiMap;
-import com.plotsquared.core.PlotSquared;
-import com.plotsquared.core.configuration.Captions;
-import com.plotsquared.core.configuration.Settings;
-import com.plotsquared.core.database.DBFunc;
-import com.plotsquared.core.player.OfflinePlotPlayer;
-import com.plotsquared.core.player.PlotPlayer;
-import com.plotsquared.core.plot.Plot;
-import com.plotsquared.core.util.StringMan;
-import com.plotsquared.core.util.StringWrapper;
-import com.plotsquared.core.util.task.RunnableVal;
-import com.plotsquared.core.util.task.TaskManager;
-import org.jetbrains.annotations.NotNull;
-import org.jetbrains.annotations.Nullable;
-
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.Map;
-import java.util.Set;
-import java.util.UUID;
-import java.util.concurrent.ConcurrentHashMap;
-
-public abstract class UUIDHandlerImplementation {
-
- public final HashSet unknown = new HashSet<>();
- private final ConcurrentHashMap players;
- protected UUIDWrapper uuidWrapper;
- private boolean cached = false;
- private BiMap uuidMap = HashBiMap.create(new HashMap<>());
-
- public UUIDHandlerImplementation(UUIDWrapper wrapper) {
- this.uuidWrapper = wrapper;
- this.players = new ConcurrentHashMap<>();
- }
-
- /**
- * If the UUID is not found, some commands can request to fetch the UUID when possible.
- *
- * @param name
- * @param ifFetch
- */
- public abstract void fetchUUID(String name, RunnableVal ifFetch);
-
- /**
- * Start UUID caching (this should be an async task)
- * Recommended to override this is you want to cache offline players
- */
- public boolean startCaching(Runnable whenDone) {
- if (this.cached) {
- return false;
- }
- return this.cached = true;
- }
-
- public UUIDWrapper getUUIDWrapper() {
- return this.uuidWrapper;
- }
-
- public void setUUIDWrapper(UUIDWrapper wrapper) {
- this.uuidWrapper = wrapper;
- }
-
- public void rename(UUID uuid, StringWrapper name) {
- this.uuidMap.inverse().remove(uuid);
- this.uuidMap.put(name, uuid);
- }
-
- public void add(BiMap toAdd) {
- if (this.uuidMap.isEmpty()) {
- this.uuidMap = toAdd;
- }
- for (Map.Entry entry : toAdd.entrySet()) {
- UUID uuid = entry.getValue();
- StringWrapper name = entry.getKey();
- if (uuid == null || name == null) {
- continue;
- }
- StringWrapper oldName = this.uuidMap.inverse().get(uuid);
- if (oldName != null) {
- if (this.uuidMap.containsKey(name)) {
- continue;
- }
- if (getPlayer(uuid) == null) {
- rename(uuid, name);
- }
- continue;
- }
- this.uuidMap.put(name, uuid);
- }
- PlotSquared
- .debug(Captions.PREFIX + "&6Cached a total of: " + this.uuidMap.size() + " UUIDs");
- }
-
- public boolean add(final StringWrapper name, final UUID uuid) {
- if (uuid == null) {
- PlotSquared.debug("UUID cannot be null!");
- return false;
- }
- if (name == null) {
- try {
- this.unknown.add(uuid);
- } catch (Exception e) {
- PlotSquared.log("&c(minor) Invalid UUID mapping: " + uuid);
- e.printStackTrace();
- }
- return false;
- }
-
- /*
- * lazy UUID conversion:
- * - Useful if the person misconfigured the database, or settings before
- * PlotMe conversion
- */
- if (!Settings.UUID.OFFLINE && !this.unknown.isEmpty()) {
- TaskManager.runTaskAsync(() -> {
- UUID offline = UUID.nameUUIDFromBytes(
- ("OfflinePlayer:" + name.value).getBytes(Charsets.UTF_8));
- if (!UUIDHandlerImplementation.this.unknown.contains(offline) && !name.value
- .equals(name.value.toLowerCase())) {
- offline = UUID.nameUUIDFromBytes(
- ("OfflinePlayer:" + name.value.toLowerCase()).getBytes(Charsets.UTF_8));
- if (!UUIDHandlerImplementation.this.unknown.contains(offline)) {
- offline = null;
- }
- }
- if (offline != null && !offline.equals(uuid)) {
- UUIDHandlerImplementation.this.unknown.remove(offline);
- Set plots = PlotSquared.get().getPlotsAbs(offline);
- if (!plots.isEmpty()) {
- for (final Plot plot : plots) {
- plot.setOwnerAbs(uuid);
- }
- DBFunc.replaceUUID(offline, uuid);
- PlotSquared.debug("&cDetected invalid UUID stored for: " + name.value);
- PlotSquared.debug(
- "&7 - Did you recently switch to online-mode storage without running `uuidconvert`?");
- PlotSquared.debug("&6" + PlotSquared.imp().getPluginName()
- + " will update incorrect entries when the user logs in, or you can reconstruct your database.");
- }
- }
- });
- } else if (Settings.UUID.FORCE_LOWERCASE && !this.unknown.isEmpty() && !name.value
- .equals(name.value.toLowerCase())) {
- TaskManager.runTaskAsync(() -> {
- UUID offlineUpper = UUID.nameUUIDFromBytes(
- ("OfflinePlayer:" + name.value).getBytes(Charsets.UTF_8));
- if (UUIDHandlerImplementation.this.unknown.contains(offlineUpper) && !offlineUpper
- .equals(uuid)) {
- UUIDHandlerImplementation.this.unknown.remove(offlineUpper);
- Set plots = PlotSquared.get().getPlotsAbs(offlineUpper);
- if (!plots.isEmpty()) {
- for (final Plot plot : plots) {
- plot.setOwnerAbs(uuid);
- }
- replace(offlineUpper, uuid, name.value);
- }
- }
- });
- }
- try {
- UUID existing = this.uuidMap.put(name, uuid);
- if (existing != null) {
- if (!existing.equals(uuid)) {
- Set plots = PlotSquared.get().getPlots(existing);
- if (!plots.isEmpty()) {
- for (final Plot plot : plots) {
- plot.setOwnerAbs(uuid);
- }
- replace(existing, uuid, name.value);
- }
- return true;
- } else {
- StringWrapper oName = this.uuidMap.inverse().get(existing);
- if (!oName.equals(name)) {
- this.uuidMap.remove(name);
- this.uuidMap.put(name, uuid);
- }
- }
- return false;
- }
- } catch (Exception ignored) {
- BiMap inverse = this.uuidMap.inverse();
- StringWrapper oldName = inverse.get(uuid);
- if (oldName != null) {
- if (this.uuidMap.containsKey(name)) {
- return false;
- }
- PlotPlayer player = getPlayer(uuid);
- if (player == null || player.getName().equalsIgnoreCase(name.value)) {
- rename(uuid, name);
- return false;
- }
- StringWrapper newName = new StringWrapper(player.getName());
- UUID newUUID = player.getUUID();
- if (newUUID.equals(uuid) && !newName.equals(oldName)) {
- inverse.remove(uuid);
- this.uuidMap.put(newName, newUUID);
- }
- return false;
- }
- this.uuidMap.put(name, uuid);
- }
- return true;
- }
-
- private void replace(UUID from, UUID to, String name) {
- DBFunc.replaceUUID(from, to);
- PlotSquared.debug("&cDetected invalid UUID stored for: " + name);
- PlotSquared.debug(
- "&7 - Did you recently switch to online-mode storage without running `uuidconvert`?");
- PlotSquared.debug("&6" + PlotSquared.imp().getPluginName()
- + " will update incorrect entries when the user logs in, or you can reconstruct your database.");
- }
-
- public boolean uuidExists(UUID uuid) {
- return this.uuidMap.containsValue(uuid);
- }
-
- public BiMap getUUIDMap() {
- return this.uuidMap;
- }
-
- public boolean nameExists(StringWrapper wrapper) {
- return this.uuidMap.containsKey(wrapper);
- }
-
- public void handleShutdown() {
- this.players.clear();
- this.uuidMap.clear();
- }
-
- @Nullable public String getName(UUID uuid) {
- if (uuid == null) {
- return null;
- }
- StringWrapper name = this.uuidMap.inverse().get(uuid);
- if (name != null) {
- return name.value;
- }
- return null;
- }
-
- @Nullable public UUID getUUID(String name, RunnableVal ifFetch) {
- if (name.isEmpty()) {
- return null;
- }
- // check online
- PlotPlayer player = getPlayer(name);
- if (player != null) {
- return player.getUUID();
- }
- // check cache
- StringWrapper wrap = new StringWrapper(name);
- UUID uuid = this.uuidMap.get(wrap);
- if (uuid != null) {
- return uuid;
- }
- // Read from disk OR convert directly to offline UUID
- if (Settings.UUID.OFFLINE && !StringMan.contains(name, ';')) {
- uuid = this.uuidWrapper.getUUID(name);
- add(new StringWrapper(name), uuid);
- return uuid;
- }
- if ((ifFetch != null)) {
- fetchUUID(name, ifFetch);
- return null;
- }
- return null;
- }
-
- @NotNull public UUID getUUID(PlotPlayer player) {
- return this.uuidWrapper.getUUID(player);
- }
-
- public UUID getUUID(OfflinePlotPlayer player) {
- return this.uuidWrapper.getUUID(player);
- }
-
- @Nullable public PlotPlayer getPlayer(UUID uuid) {
- String name = getName(uuid);
- if (name != null) {
- return getPlayer(name);
- }
- return null;
- }
-
- public PlotPlayer getPlayer(String name) {
- return this.players.get(name);
- }
-
- public Map getPlayers() {
- return this.players;
- }
-
-}
diff --git a/Core/src/main/java/com/plotsquared/core/uuid/CacheUUIDService.java b/Core/src/main/java/com/plotsquared/core/uuid/CacheUUIDService.java
index 81a42bacd..32bbbb6bc 100644
--- a/Core/src/main/java/com/plotsquared/core/uuid/CacheUUIDService.java
+++ b/Core/src/main/java/com/plotsquared/core/uuid/CacheUUIDService.java
@@ -30,6 +30,7 @@ import com.google.common.cache.CacheBuilder;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
+import java.util.Collection;
import java.util.List;
import java.util.UUID;
import java.util.function.Consumer;
@@ -73,4 +74,8 @@ public class CacheUUIDService implements UUIDService, Consumer
}
}
+ @Override @NotNull public Collection getImmediately() {
+ return this.usernameCache.asMap().values();
+ }
+
}
diff --git a/Core/src/main/java/com/plotsquared/core/uuid/UUIDMapping.java b/Core/src/main/java/com/plotsquared/core/uuid/UUIDMapping.java
index 798d586ff..35a4c6c2c 100644
--- a/Core/src/main/java/com/plotsquared/core/uuid/UUIDMapping.java
+++ b/Core/src/main/java/com/plotsquared/core/uuid/UUIDMapping.java
@@ -25,6 +25,7 @@
*/
package com.plotsquared.core.uuid;
+import lombok.EqualsAndHashCode;
import org.jetbrains.annotations.NotNull;
import java.util.UUID;
@@ -32,7 +33,7 @@ import java.util.UUID;
/**
* A pair consisting of a UUID and a username
*/
-public class UUIDMapping {
+@EqualsAndHashCode public class UUIDMapping {
private final UUID uuid;
private final String username;
diff --git a/Core/src/main/java/com/plotsquared/core/uuid/UUIDPipeline.java b/Core/src/main/java/com/plotsquared/core/uuid/UUIDPipeline.java
index 7add9ef8a..6d85db51f 100644
--- a/Core/src/main/java/com/plotsquared/core/uuid/UUIDPipeline.java
+++ b/Core/src/main/java/com/plotsquared/core/uuid/UUIDPipeline.java
@@ -35,13 +35,16 @@ import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
+import java.util.LinkedHashSet;
import java.util.List;
+import java.util.Set;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
+import java.util.function.BiConsumer;
import java.util.function.Consumer;
/**
@@ -181,10 +184,16 @@ public class UUIDPipeline {
* @param username Username
* @param uuid UUID consumer
*/
- public void getSingle(@NotNull final String username, @NotNull final Consumer uuid) {
- this.getUUIDs(Collections.singletonList(username)).thenAccept(uuids -> {
- if (!uuids.isEmpty()) {
- uuid.accept(uuids.get(0).getUuid());
+ public void getSingle(@NotNull final String username, @NotNull final BiConsumer uuid) {
+ this.getUUIDs(Collections.singletonList(username)).whenComplete((uuids, throwable) -> {
+ if (throwable != null) {
+ uuid.accept(null, throwable);
+ } else {
+ if (!uuids.isEmpty()) {
+ uuid.accept(uuids.get(0).getUuid(), null);
+ } else {
+ uuid.accept(null, null);
+ }
}
});
}
@@ -195,10 +204,16 @@ public class UUIDPipeline {
* @param uuid UUID
* @param username Username consumer
*/
- public void getSingle(@NotNull final UUID uuid, @NotNull final Consumer username) {
- this.getNames(Collections.singletonList(uuid)).thenAccept(uuids -> {
- if (!uuids.isEmpty()) {
- username.accept(uuids.get(0).getUsername());
+ public void getSingle(@NotNull final UUID uuid, @NotNull final BiConsumer username) {
+ this.getNames(Collections.singletonList(uuid)).whenComplete((uuids, throwable) -> {
+ if (throwable != null) {
+ username.accept(null, throwable);
+ } else {
+ if (!uuids.isEmpty()) {
+ username.accept(uuids.get(0).getUsername(), null);
+ } else {
+ username.accept(null, null);
+ }
}
});
}
@@ -274,4 +289,18 @@ public class UUIDPipeline {
}, this.executor);
}
+ /**
+ * Get as many UUID mappings as possible under the condition
+ * that the operation cannot be blocking (for an extended amount of time)
+ *
+ * @return All mappings that could be provided immediately
+ */
+ @NotNull public final Collection getAllImmediately() {
+ final Set mappings = new LinkedHashSet<>();
+ for (final UUIDService service : this.getServiceListInstance()) {
+ mappings.addAll(service.getImmediately());
+ }
+ return mappings;
+ }
+
}
diff --git a/Core/src/main/java/com/plotsquared/core/uuid/UUIDService.java b/Core/src/main/java/com/plotsquared/core/uuid/UUIDService.java
index 468de09b2..25588305e 100644
--- a/Core/src/main/java/com/plotsquared/core/uuid/UUIDService.java
+++ b/Core/src/main/java/com/plotsquared/core/uuid/UUIDService.java
@@ -27,6 +27,8 @@ package com.plotsquared.core.uuid;
import org.jetbrains.annotations.NotNull;
+import java.util.Collection;
+import java.util.Collections;
import java.util.List;
import java.util.UUID;
@@ -53,4 +55,14 @@ public interface UUIDService {
*/
@NotNull List getUUIDs(@NotNull final List usernames);
+ /**
+ * Get as many UUID mappings as possible under the condition
+ * that the operation cannot be blocking (for an extended amount of time)
+ *
+ * @return All mappings that could be provided immediately
+ */
+ default @NotNull Collection getImmediately() {
+ return Collections.emptyList();
+ }
+
}
diff --git a/Core/src/test/java/com/plotsquared/core/plot/util/UUIDHandlerImplementationTest.java b/Core/src/test/java/com/plotsquared/core/plot/util/UUIDHandlerImplementationTest.java
index 291461478..9bcad05bd 100644
--- a/Core/src/test/java/com/plotsquared/core/plot/util/UUIDHandlerImplementationTest.java
+++ b/Core/src/test/java/com/plotsquared/core/plot/util/UUIDHandlerImplementationTest.java
@@ -28,15 +28,14 @@ package com.plotsquared.core.plot.util;
import com.plotsquared.core.database.AbstractDBTest;
import com.plotsquared.core.database.DBFunc;
import com.plotsquared.core.util.task.RunnableVal;
-import com.plotsquared.core.util.uuid.UUIDHandlerImplementation;
-import com.plotsquared.core.util.uuid.UUIDWrapper;
+import com.plotsquared.core.util.uuid.OfflinePlayerService;
import org.junit.Before;
import java.util.UUID;
public class UUIDHandlerImplementationTest extends UUIDHandlerImplementation {
- public UUIDHandlerImplementationTest(UUIDWrapper wrapper) {
+ public UUIDHandlerImplementationTest(OfflinePlayerService wrapper) {
super(wrapper);
}