PlotSquared/Core/src/main/java/com/intellectualcrafters/plot/util/UUIDHandlerImplementation.java

257 lines
8.9 KiB
Java
Raw Normal View History

2015-07-27 09:26:50 +02:00
package com.intellectualcrafters.plot.util;
2015-09-05 12:07:59 +02:00
import com.google.common.base.Charsets;
2015-07-27 09:26:50 +02:00
import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
import com.intellectualcrafters.plot.PS;
import com.intellectualcrafters.plot.config.C;
import com.intellectualcrafters.plot.config.Settings;
2015-09-05 12:07:59 +02:00
import com.intellectualcrafters.plot.database.DBFunc;
import com.intellectualcrafters.plot.object.ConsolePlayer;
import com.intellectualcrafters.plot.object.OfflinePlotPlayer;
import com.intellectualcrafters.plot.object.Plot;
import com.intellectualcrafters.plot.object.PlotPlayer;
import com.intellectualcrafters.plot.object.RunnableVal;
import com.intellectualcrafters.plot.object.StringWrapper;
2015-07-27 09:26:50 +02:00
import com.intellectualcrafters.plot.uuid.UUIDWrapper;
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;
2015-07-27 09:26:50 +02:00
2015-09-13 06:04:31 +02:00
public abstract class UUIDHandlerImplementation {
public final ConcurrentHashMap<String, PlotPlayer> players;
2015-07-27 09:26:50 +02:00
public boolean CACHED = false;
public UUIDWrapper uuidWrapper = null;
public HashSet<UUID> unknown = new HashSet<>();
private BiMap<StringWrapper, UUID> uuidMap = HashBiMap.create(new HashMap<StringWrapper, UUID>());
2015-09-13 06:04:31 +02:00
public UUIDHandlerImplementation(final UUIDWrapper wrapper) {
2015-09-11 12:09:22 +02:00
uuidWrapper = wrapper;
2015-10-19 08:27:51 +02:00
players = new ConcurrentHashMap<>();
2015-07-27 09:26:50 +02:00
}
2015-09-13 06:04:31 +02:00
2015-07-27 09:26:50 +02:00
/**
* If the UUID is not found, some commands can request to fetch the UUID when possible
* @param name
2015-07-27 09:26:50 +02:00
* @param ifFetch
*/
2015-09-11 12:09:22 +02:00
public abstract void fetchUUID(final String name, final RunnableVal<UUID> ifFetch);
2015-09-13 06:04:31 +02:00
2015-07-27 09:26:50 +02:00
/**
* Start UUID caching (this should be an async task)
* Recommended to override this is you want to cache offline players
*/
2015-09-13 06:04:31 +02:00
public boolean startCaching(final Runnable whenDone) {
if (CACHED) {
return false;
}
2015-09-11 12:09:22 +02:00
return CACHED = true;
2015-07-27 09:26:50 +02:00
}
2015-09-13 06:04:31 +02:00
public UUIDWrapper getUUIDWrapper() {
2015-09-11 12:09:22 +02:00
return uuidWrapper;
2015-07-27 09:26:50 +02:00
}
2015-09-13 06:04:31 +02:00
public void setUUIDWrapper(final UUIDWrapper wrapper) {
2015-09-11 12:09:22 +02:00
uuidWrapper = wrapper;
2015-07-27 09:26:50 +02:00
}
2015-09-13 06:04:31 +02:00
public void rename(final UUID uuid, final StringWrapper name) {
2015-07-27 15:10:14 +02:00
uuidMap.inverse().remove(uuid);
uuidMap.put(name, uuid);
}
2015-09-13 06:04:31 +02:00
public void add(final BiMap<StringWrapper, UUID> toAdd) {
if (uuidMap.isEmpty()) {
2015-07-27 09:26:50 +02:00
uuidMap = toAdd;
}
2015-09-13 06:04:31 +02:00
for (final Map.Entry<StringWrapper, UUID> entry : toAdd.entrySet()) {
2015-09-11 12:09:22 +02:00
final UUID uuid = entry.getValue();
final StringWrapper name = entry.getKey();
2015-09-13 06:04:31 +02:00
if ((uuid == null) || (name == null)) {
2015-09-05 12:07:59 +02:00
continue;
}
2015-09-11 12:09:22 +02:00
final BiMap<UUID, StringWrapper> inverse = uuidMap.inverse();
2015-09-13 06:04:31 +02:00
if (inverse.containsKey(uuid)) {
if (uuidMap.containsKey(name)) {
2015-09-05 12:07:59 +02:00
continue;
2015-07-27 09:26:50 +02:00
}
2015-09-05 12:07:59 +02:00
rename(uuid, name);
continue;
2015-07-27 09:26:50 +02:00
}
2015-09-05 12:07:59 +02:00
uuidMap.put(name, uuid);
}
PS.debug(C.PREFIX.s() + "&6Cached a total of: " + uuidMap.size() + " UUIDs");
2015-07-27 09:26:50 +02:00
}
2015-09-13 06:04:31 +02:00
public boolean add(final StringWrapper name, final UUID uuid) {
if ((uuid == null)) {
return false;
}
if (name == null) {
try {
2015-09-05 12:07:59 +02:00
unknown.add(uuid);
2015-09-13 06:04:31 +02:00
} catch (final Exception e) {
2015-10-19 08:27:51 +02:00
ConsolePlayer.getConsole().sendMessage("&c(minor) Invalid UUID mapping: " + uuid);
2015-09-05 12:07:59 +02:00
e.printStackTrace();
}
2015-07-27 09:26:50 +02:00
return false;
}
2015-09-13 06:04:31 +02:00
2015-09-05 12:07:59 +02:00
/*
* lazy UUID conversion:
* - Useful if the person misconfigured the database, or settings before PlotMe conversion
*/
if (!Settings.OFFLINE_MODE && !unknown.isEmpty()) {
2015-12-04 10:00:30 +01:00
TaskManager.runTaskAsync(new Runnable() {
@Override
public void run() {
UUID offline = UUID.nameUUIDFromBytes(("OfflinePlayer:" + name.value).getBytes(Charsets.UTF_8));
if (!unknown.contains(offline) && !name.value.equals(name.value.toLowerCase())) {
offline = UUID.nameUUIDFromBytes(("OfflinePlayer:" + name.value.toLowerCase()).getBytes(Charsets.UTF_8));
if (!unknown.contains(offline)) {
offline = null;
}
}
if (offline != null && !offline.equals(uuid)) {
unknown.remove(offline);
2016-02-10 19:59:51 +01:00
final Set<Plot> plots = PS.get().getPlotsAbs(offline);
if (!plots.isEmpty()) {
2015-12-04 10:00:30 +01:00
for (final Plot plot : plots) {
plot.owner = uuid;
}
DBFunc.replaceUUID(offline, uuid);
PS.debug("&cDetected invalid UUID stored for: " + name.value);
PS.debug("&7 - Did you recently switch to online-mode storage without running `uuidconvert`?");
PS.debug("&6PlotSquared will update incorrect entries when the user logs in, or you can reconstruct your database.");
}
2015-09-05 12:07:59 +02:00
}
}
2015-12-04 10:00:30 +01:00
});
2015-09-05 12:07:59 +02:00
}
2015-09-13 06:04:31 +02:00
try {
2015-09-11 12:09:22 +02:00
final UUID offline = uuidMap.put(name, uuid);
2015-11-28 12:46:10 +01:00
if (offline != null) {
if (!offline.equals(uuid)) {
final Set<Plot> plots = PS.get().getPlots(offline);
if (!plots.isEmpty()) {
2015-11-29 09:21:32 +01:00
for (final Plot plot : plots) {
2015-11-28 12:46:10 +01:00
plot.owner = uuid;
}
DBFunc.replaceUUID(offline, uuid);
PS.debug("&cDetected invalid UUID stored for (1): " + name.value);
PS.debug("&7 - Did you recently switch to online-mode storage without running `uuidconvert`?");
PS.debug("&6PlotSquared will update incorrect entries when the user logs in, or you can reconstruct your database.");
2015-09-05 12:07:59 +02:00
}
2015-11-28 12:46:10 +01:00
return true;
2015-09-05 12:07:59 +02:00
}
2015-11-28 12:46:10 +01:00
return false;
2015-09-05 12:07:59 +02:00
}
2015-09-13 06:04:31 +02:00
} catch (final Exception e) {
2015-09-11 12:09:22 +02:00
final BiMap<UUID, StringWrapper> inverse = uuidMap.inverse();
2015-09-13 06:04:31 +02:00
if (inverse.containsKey(uuid)) {
if (uuidMap.containsKey(name)) {
return false;
}
2015-08-26 06:21:48 +02:00
rename(uuid, name);
2015-07-27 09:26:50 +02:00
return false;
}
2015-08-26 06:21:48 +02:00
uuidMap.put(name, uuid);
2015-07-27 09:26:50 +02:00
}
return true;
}
2015-09-13 06:04:31 +02:00
public boolean uuidExists(final UUID uuid) {
2015-07-27 09:26:50 +02:00
return uuidMap.containsValue(uuid);
}
2015-09-13 06:04:31 +02:00
public BiMap<StringWrapper, UUID> getUUIDMap() {
2015-07-27 09:26:50 +02:00
return uuidMap;
}
2015-09-13 06:04:31 +02:00
public boolean nameExists(final StringWrapper wrapper) {
2015-07-27 09:26:50 +02:00
return uuidMap.containsKey(wrapper);
}
2015-09-13 06:04:31 +02:00
public void handleShutdown() {
2015-07-27 09:26:50 +02:00
players.clear();
uuidMap.clear();
uuidWrapper = null;
}
2015-09-13 06:04:31 +02:00
public String getName(final UUID uuid) {
if (uuid == null) {
return null;
}
2015-10-19 08:27:51 +02:00
// // check online
// final PlotPlayer player = getPlayer(uuid);
// if (player != null) {
// return player.getName();
// }
2015-07-27 09:26:50 +02:00
// check cache
final StringWrapper name = uuidMap.inverse().get(uuid);
2015-09-13 06:04:31 +02:00
if (name != null) {
return name.value;
}
2015-07-27 09:26:50 +02:00
return null;
}
2015-09-13 06:04:31 +02:00
public UUID getUUID(final String name, final RunnableVal<UUID> ifFetch) {
if ((name == null) || (name.isEmpty())) {
2015-09-13 06:04:31 +02:00
return null;
}
2015-07-27 09:26:50 +02:00
// check online
final PlotPlayer player = getPlayer(name);
2015-09-13 06:04:31 +02:00
if (player != null) {
return player.getUUID();
}
2015-07-27 09:26:50 +02:00
// check cache
final StringWrapper wrap = new StringWrapper(name);
UUID uuid = uuidMap.get(wrap);
2015-09-13 06:04:31 +02:00
if (uuid != null) {
return uuid;
}
2015-07-27 09:26:50 +02:00
// Read from disk OR convert directly to offline UUID
2015-09-13 06:04:31 +02:00
if (Settings.OFFLINE_MODE) {
2015-07-27 09:26:50 +02:00
uuid = uuidWrapper.getUUID(name);
add(new StringWrapper(name), uuid);
return uuid;
}
2015-09-13 06:04:31 +02:00
if (Settings.UUID_FROM_DISK && (ifFetch != null)) {
2015-07-27 09:26:50 +02:00
fetchUUID(name, ifFetch);
return null;
}
return null;
}
2015-09-13 06:04:31 +02:00
public UUID getUUID(final PlotPlayer player) {
2015-07-27 09:26:50 +02:00
return uuidWrapper.getUUID(player);
}
2015-09-13 06:04:31 +02:00
public UUID getUUID(final OfflinePlotPlayer player) {
2015-07-27 09:26:50 +02:00
return uuidWrapper.getUUID(player);
}
2015-09-13 06:04:31 +02:00
public PlotPlayer getPlayer(final UUID uuid) {
2015-10-19 08:27:51 +02:00
String name = getName(uuid);
if (name != null) {
return getPlayer(name);
2015-07-27 09:26:50 +02:00
}
return null;
}
2015-09-13 06:04:31 +02:00
public PlotPlayer getPlayer(final String name) {
2015-07-27 09:26:50 +02:00
return players.get(name);
}
2015-09-13 06:04:31 +02:00
public Map<String, PlotPlayer> getPlayers() {
2015-07-27 09:26:50 +02:00
return players;
}
2015-09-13 06:04:31 +02:00
2015-07-27 09:26:50 +02:00
}