2014-04-20 05:12:28 +00:00
|
|
|
/**
|
|
|
|
* Copyright © 2013 tuxed <write@imaginarycode.com>
|
|
|
|
* This work is free. You can redistribute it and/or modify it under the
|
|
|
|
* terms of the Do What The Fuck You Want To Public License, Version 2,
|
|
|
|
* as published by Sam Hocevar. See http://www.wtfpl.net/ for more details.
|
|
|
|
*/
|
|
|
|
package com.imaginarycode.minecraft.redisbungee.util;
|
|
|
|
|
|
|
|
import com.google.common.base.Charsets;
|
|
|
|
import com.imaginarycode.minecraft.redisbungee.RedisBungee;
|
2014-07-30 17:46:39 +00:00
|
|
|
import lombok.Getter;
|
2014-05-28 06:06:17 +00:00
|
|
|
import lombok.NonNull;
|
2014-04-20 05:12:28 +00:00
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
|
import net.md_5.bungee.api.ProxyServer;
|
|
|
|
import redis.clients.jedis.Jedis;
|
2014-07-30 17:46:39 +00:00
|
|
|
import redis.clients.jedis.Pipeline;
|
2014-05-28 22:29:44 +00:00
|
|
|
import redis.clients.jedis.exceptions.JedisException;
|
2014-04-20 05:12:28 +00:00
|
|
|
|
2014-07-30 17:46:39 +00:00
|
|
|
import java.util.*;
|
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
2014-04-20 05:12:28 +00:00
|
|
|
import java.util.logging.Level;
|
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
|
|
|
@RequiredArgsConstructor
|
2014-07-30 17:46:39 +00:00
|
|
|
public final class UUIDTranslator {
|
2014-04-20 05:12:28 +00:00
|
|
|
private final RedisBungee plugin;
|
2014-07-30 17:46:39 +00:00
|
|
|
private final Map<String, CachedUUIDEntry> nameToUuidMap = new ConcurrentHashMap<>(128, 0.5f, 4);
|
|
|
|
private final Map<UUID, CachedUUIDEntry> uuidToNameMap = new ConcurrentHashMap<>(128, 0.5f, 4);
|
2014-05-31 03:57:42 +00:00
|
|
|
private static final Pattern UUID_PATTERN = Pattern.compile("[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}");
|
|
|
|
private static final Pattern MOJANGIAN_UUID_PATTERN = Pattern.compile("[a-fA-F0-9]{32}");
|
2014-04-20 05:12:28 +00:00
|
|
|
|
2014-07-30 17:46:39 +00:00
|
|
|
private void addToMaps(String name, UUID uuid) {
|
|
|
|
// This is why I like LocalDate...
|
|
|
|
|
|
|
|
// Cache the entry for three days.
|
|
|
|
Calendar calendar = Calendar.getInstance();
|
|
|
|
calendar.add(Calendar.DAY_OF_MONTH, 3);
|
|
|
|
|
|
|
|
// Create the entry and populate the local maps
|
2014-07-30 22:03:07 +00:00
|
|
|
CachedUUIDEntry entry = new CachedUUIDEntry(name, uuid, calendar);
|
2014-07-30 17:46:39 +00:00
|
|
|
nameToUuidMap.put(name.toLowerCase(), entry);
|
|
|
|
uuidToNameMap.put(uuid, entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
public final UUID getTranslatedUuid(@NonNull String player, boolean expensiveLookups) {
|
|
|
|
// If the player is online, give them their UUID.
|
|
|
|
// Remember, local data > remote data.
|
2014-04-20 05:12:28 +00:00
|
|
|
if (ProxyServer.getInstance().getPlayer(player) != null)
|
|
|
|
return ProxyServer.getInstance().getPlayer(player).getUniqueId();
|
|
|
|
|
2014-05-31 03:22:31 +00:00
|
|
|
// Check if it exists in the map
|
2014-07-30 17:46:39 +00:00
|
|
|
CachedUUIDEntry cachedUUIDEntry = nameToUuidMap.get(player.toLowerCase());
|
|
|
|
if (cachedUUIDEntry != null) {
|
|
|
|
if (!cachedUUIDEntry.expired())
|
|
|
|
return cachedUUIDEntry.getUuid();
|
|
|
|
else
|
|
|
|
nameToUuidMap.remove(player);
|
2014-05-31 03:22:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check if we can exit early
|
2014-05-23 14:50:05 +00:00
|
|
|
if (UUID_PATTERN.matcher(player).find()) {
|
|
|
|
return UUID.fromString(player);
|
|
|
|
}
|
|
|
|
|
2014-05-23 14:53:38 +00:00
|
|
|
if (MOJANGIAN_UUID_PATTERN.matcher(player).find()) {
|
|
|
|
// Reconstruct the UUID
|
|
|
|
return UUIDFetcher.getUUID(player);
|
|
|
|
}
|
|
|
|
|
2014-07-30 17:46:39 +00:00
|
|
|
// This is a bit of a special case.
|
|
|
|
// If we are in offline mode, UUID generation is simple.
|
|
|
|
// We don't even have to cache the UUID, unless we need to reduce GC pressure.
|
2014-04-20 05:12:28 +00:00
|
|
|
if (!plugin.getProxy().getConfig().isOnlineMode()) {
|
2014-07-30 17:46:39 +00:00
|
|
|
return UUID.nameUUIDFromBytes(("OfflinePlayer:" + player).getBytes(Charsets.UTF_8));
|
2014-05-31 03:22:31 +00:00
|
|
|
}
|
|
|
|
|
2014-07-30 17:46:39 +00:00
|
|
|
// Let's try Redis.
|
2014-04-20 05:12:28 +00:00
|
|
|
Jedis jedis = plugin.getPool().getResource();
|
|
|
|
try {
|
2014-05-28 22:29:44 +00:00
|
|
|
try {
|
2014-07-30 17:46:39 +00:00
|
|
|
String stored = jedis.hget("uuid-cache", player.toLowerCase());
|
|
|
|
if (stored != null) {
|
|
|
|
// Found an entry value. Deserialize it.
|
|
|
|
CachedUUIDEntry entry = RedisBungee.getGson().fromJson(stored, CachedUUIDEntry.class);
|
|
|
|
|
|
|
|
// Check for expiry:
|
|
|
|
if (entry.expired()) {
|
|
|
|
jedis.hdel("uuid-cache", player.toLowerCase());
|
|
|
|
} else {
|
|
|
|
nameToUuidMap.put(player.toLowerCase(), entry);
|
|
|
|
uuidToNameMap.put(entry.getUuid(), entry);
|
|
|
|
return entry.getUuid();
|
2014-05-31 03:22:31 +00:00
|
|
|
}
|
2014-05-28 22:29:44 +00:00
|
|
|
}
|
2014-04-20 05:12:28 +00:00
|
|
|
|
2014-05-28 22:29:44 +00:00
|
|
|
// That didn't work. Let's ask Mojang.
|
2014-05-31 03:22:31 +00:00
|
|
|
if (!expensiveLookups)
|
|
|
|
return null;
|
|
|
|
|
2014-05-28 22:29:44 +00:00
|
|
|
Map<String, UUID> uuidMap1;
|
|
|
|
try {
|
|
|
|
uuidMap1 = new UUIDFetcher(Collections.singletonList(player)).call();
|
|
|
|
} catch (Exception e) {
|
|
|
|
plugin.getLogger().log(Level.SEVERE, "Unable to fetch UUID from Mojang for " + player, e);
|
|
|
|
return null;
|
2014-04-26 23:43:40 +00:00
|
|
|
}
|
2014-05-28 22:29:44 +00:00
|
|
|
for (Map.Entry<String, UUID> entry : uuidMap1.entrySet()) {
|
|
|
|
if (entry.getKey().equalsIgnoreCase(player)) {
|
2014-07-30 17:46:39 +00:00
|
|
|
persistInfo(entry.getKey(), entry.getValue(), jedis);
|
2014-05-28 22:29:44 +00:00
|
|
|
return entry.getValue();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (JedisException e) {
|
|
|
|
plugin.getLogger().log(Level.SEVERE, "Unable to fetch UUID for " + player, e);
|
|
|
|
// Go ahead and give them what we have.
|
2014-07-30 17:46:39 +00:00
|
|
|
return null;
|
2014-04-20 05:12:28 +00:00
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
plugin.getPool().returnResource(jedis);
|
|
|
|
}
|
2014-05-28 22:29:44 +00:00
|
|
|
|
|
|
|
return null; // Nope, game over!
|
2014-04-20 05:12:28 +00:00
|
|
|
}
|
|
|
|
|
2014-07-30 17:46:39 +00:00
|
|
|
public final String getNameFromUuid(@NonNull UUID player, boolean expensiveLookups) {
|
|
|
|
// If the player is online, give them their UUID.
|
|
|
|
// Remember, local data > remote data.
|
2014-04-20 05:12:28 +00:00
|
|
|
if (ProxyServer.getInstance().getPlayer(player) != null)
|
|
|
|
return ProxyServer.getInstance().getPlayer(player).getName();
|
|
|
|
|
2014-07-30 17:46:39 +00:00
|
|
|
// Check if it exists in the map
|
|
|
|
CachedUUIDEntry cachedUUIDEntry = uuidToNameMap.get(player);
|
|
|
|
if (cachedUUIDEntry != null) {
|
|
|
|
if (!cachedUUIDEntry.expired())
|
|
|
|
return cachedUUIDEntry.getName();
|
|
|
|
else
|
|
|
|
uuidToNameMap.remove(player);
|
2014-05-31 03:22:31 +00:00
|
|
|
}
|
2014-04-20 05:12:28 +00:00
|
|
|
|
|
|
|
// Okay, it wasn't locally cached. Let's try Redis.
|
|
|
|
Jedis jedis = plugin.getPool().getResource();
|
|
|
|
try {
|
2014-07-30 17:46:39 +00:00
|
|
|
String stored = jedis.hget("uuid-cache", player.toString());
|
2014-04-20 05:12:28 +00:00
|
|
|
if (stored != null) {
|
2014-07-30 17:46:39 +00:00
|
|
|
// Found an entry value. Deserialize it.
|
|
|
|
CachedUUIDEntry entry = RedisBungee.getGson().fromJson(stored, CachedUUIDEntry.class);
|
|
|
|
|
|
|
|
// Check for expiry:
|
|
|
|
if (entry.expired()) {
|
|
|
|
jedis.hdel("uuid-cache", player.toString());
|
|
|
|
} else {
|
|
|
|
nameToUuidMap.put(entry.getName().toLowerCase(), entry);
|
|
|
|
uuidToNameMap.put(player, entry);
|
|
|
|
return entry.getName();
|
2014-05-31 03:22:31 +00:00
|
|
|
}
|
2014-04-20 05:12:28 +00:00
|
|
|
}
|
|
|
|
|
2014-05-31 03:22:31 +00:00
|
|
|
if (!expensiveLookups)
|
|
|
|
return null;
|
|
|
|
|
2014-07-30 17:46:39 +00:00
|
|
|
// That didn't work. Let's ask Mojang. This call may fail, because Mojang is insane.
|
|
|
|
String name;
|
2014-05-28 22:29:44 +00:00
|
|
|
try {
|
|
|
|
name = new NameFetcher(Collections.singletonList(player)).call().get(player);
|
|
|
|
} catch (Exception e) {
|
|
|
|
plugin.getLogger().log(Level.SEVERE, "Unable to fetch name from Mojang for " + player, e);
|
|
|
|
return null;
|
|
|
|
}
|
2014-04-20 05:12:28 +00:00
|
|
|
|
|
|
|
if (name != null) {
|
2014-07-30 17:46:39 +00:00
|
|
|
persistInfo(name, player, jedis);
|
2014-04-20 05:12:28 +00:00
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
2014-05-28 22:29:44 +00:00
|
|
|
} catch (JedisException e) {
|
2014-04-20 05:12:28 +00:00
|
|
|
plugin.getLogger().log(Level.SEVERE, "Unable to fetch name for " + player, e);
|
2014-07-30 17:46:39 +00:00
|
|
|
return null;
|
2014-04-20 05:12:28 +00:00
|
|
|
} finally {
|
|
|
|
plugin.getPool().returnResource(jedis);
|
|
|
|
}
|
|
|
|
}
|
2014-04-23 22:05:42 +00:00
|
|
|
|
2014-09-11 20:38:40 +00:00
|
|
|
public final void persistInfo(String name, UUID uuid, Jedis jedis) {
|
2014-07-30 17:46:39 +00:00
|
|
|
addToMaps(name, uuid);
|
|
|
|
jedis.hset("uuid-cache", name.toLowerCase(), RedisBungee.getGson().toJson(uuidToNameMap.get(uuid)));
|
|
|
|
jedis.hset("uuid-cache", uuid.toString(), RedisBungee.getGson().toJson(uuidToNameMap.get(uuid)));
|
|
|
|
}
|
|
|
|
|
|
|
|
@RequiredArgsConstructor
|
|
|
|
@Getter
|
|
|
|
private class CachedUUIDEntry {
|
|
|
|
private final String name;
|
|
|
|
private final UUID uuid;
|
2014-07-30 22:03:07 +00:00
|
|
|
private final Calendar expiry;
|
2014-07-30 17:46:39 +00:00
|
|
|
|
|
|
|
public boolean expired() {
|
2014-07-30 22:03:07 +00:00
|
|
|
return Calendar.getInstance().after(expiry);
|
2014-07-30 17:46:39 +00:00
|
|
|
}
|
2014-04-23 22:05:42 +00:00
|
|
|
}
|
2014-04-20 05:12:28 +00:00
|
|
|
}
|