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

248 lines
8.3 KiB
Java
Raw Normal View History

2015-07-27 09:26:50 +02:00
package com.intellectualcrafters.plot.util;
2015-07-30 16:25:16 +02:00
import java.util.HashMap;
2015-09-05 12:07:59 +02:00
import java.util.HashSet;
2015-07-30 16:25:16 +02:00
import java.util.Map;
2015-09-05 12:07:59 +02:00
import java.util.Set;
2015-07-30 16:25:16 +02:00
import java.util.UUID;
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;
2015-07-27 09:26:50 +02:00
import com.intellectualcrafters.plot.object.OfflinePlotPlayer;
2015-09-05 12:07:59 +02:00
import com.intellectualcrafters.plot.object.Plot;
2015-07-27 09:26:50 +02:00
import com.intellectualcrafters.plot.object.PlotPlayer;
import com.intellectualcrafters.plot.object.RunnableVal;
import com.intellectualcrafters.plot.object.StringWrapper;
import com.intellectualcrafters.plot.uuid.UUIDWrapper;
2015-09-13 06:04:31 +02:00
public abstract class UUIDHandlerImplementation {
2015-07-27 09:26:50 +02:00
private BiMap<StringWrapper, UUID> uuidMap = HashBiMap.create(new HashMap<StringWrapper, UUID>());
public boolean CACHED = false;
public UUIDWrapper uuidWrapper = null;
public final HashMap<String, PlotPlayer> players;
2015-09-13 06:04:31 +02:00
public UUIDHandlerImplementation(final UUIDWrapper wrapper) {
2015-09-11 12:09:22 +02:00
uuidWrapper = wrapper;
players = new HashMap<>();
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 player
* @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.size() == 0) {
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
2015-09-05 12:07:59 +02:00
public HashSet<UUID> unknown = new HashSet<>();
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-09-05 12:07:59 +02:00
PS.log("&c(minor) Invalid UUID mapping: " + uuid);
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
*/
2015-09-13 06:04:31 +02:00
if (!Settings.OFFLINE_MODE) {
2015-09-05 12:07:59 +02:00
UUID offline = UUID.nameUUIDFromBytes(("OfflinePlayer:" + name.value).getBytes(Charsets.UTF_8));
2015-09-13 06:04:31 +02:00
if (!unknown.contains(offline) && !name.value.equals(name.value.toLowerCase())) {
2015-09-05 12:07:59 +02:00
offline = UUID.nameUUIDFromBytes(("OfflinePlayer:" + name.value).getBytes(Charsets.UTF_8));
2015-09-13 06:04:31 +02:00
if (!unknown.contains(offline)) {
2015-09-05 12:07:59 +02:00
offline = null;
}
}
2015-09-13 06:04:31 +02:00
if (offline != null) {
2015-09-05 12:07:59 +02:00
unknown.remove(offline);
2015-09-11 12:09:22 +02:00
final Set<Plot> plots = PS.get().getPlots(offline);
2015-09-13 06:04:31 +02:00
if (plots.size() > 0) {
for (final Plot plot : PS.get().getPlots(offline)) {
2015-09-05 12:07:59 +02:00
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-13 06:04:31 +02:00
try {
2015-09-11 12:09:22 +02:00
final UUID offline = uuidMap.put(name, uuid);
2015-09-13 06:04:31 +02:00
if ((offline != null) && !offline.equals(uuid)) {
2015-09-11 12:09:22 +02:00
final Set<Plot> plots = PS.get().getPlots(offline);
2015-09-13 06:04:31 +02:00
if (plots.size() > 0) {
for (final Plot plot : PS.get().getPlots(offline)) {
2015-09-05 12:07:59 +02: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-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-07-27 09:26:50 +02:00
// check online
final PlotPlayer player = getPlayer(uuid);
2015-09-13 06:04:31 +02:00
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.length() == 0)) {
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) {
for (final PlotPlayer player : players.values()) {
if (player.getUUID().equals(uuid)) {
return player;
}
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
}