RedisBungee/RedisBungee-API/src/main/java/com/imaginarycode/minecraft/redisbungee/api/util/uuid/UUIDTranslator.java

213 lines
8.6 KiB
Java
Raw Normal View History

2022-12-31 03:23:35 +00:00
/*
* Copyright (c) 2013-present RedisBungee contributors
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
*
* http://www.eclipse.org/legal/epl-v10.html
*/
2022-07-16 12:50:09 +00:00
package com.imaginarycode.minecraft.redisbungee.api.util.uuid;
import com.google.common.base.Charsets;
import com.google.common.collect.ImmutableMap;
2015-02-04 14:55:45 +00:00
import com.google.common.collect.Iterables;
import com.google.gson.Gson;
2022-07-16 12:50:09 +00:00
import com.imaginarycode.minecraft.redisbungee.api.RedisBungeePlugin;
import com.imaginarycode.minecraft.redisbungee.api.tasks.RedisTask;
import org.checkerframework.checker.nullness.qual.NonNull;
import redis.clients.jedis.UnifiedJedis;
2014-05-28 22:29:44 +00:00
import redis.clients.jedis.exceptions.JedisException;
2015-02-04 14:55:45 +00:00
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Pattern;
public final class UUIDTranslator {
2014-09-21 17:56:46 +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}");
private final RedisBungeePlugin<?> plugin;
private final Map<String, CachedUUIDEntry> nameToUuidMap = new ConcurrentHashMap<>(128, 0.5f, 4);
private final Map<UUID, CachedUUIDEntry> uuidToNameMap = new ConcurrentHashMap<>(128, 0.5f, 4);
private static final Gson gson = new Gson();
public UUIDTranslator(RedisBungeePlugin<?> plugin) {
this.plugin = plugin;
}
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
CachedUUIDEntry entry = new CachedUUIDEntry(name, uuid, calendar);
nameToUuidMap.put(name.toLowerCase(), entry);
uuidToNameMap.put(uuid, entry);
}
2022-07-16 05:18:33 +00:00
public UUID getTranslatedUuid(@NonNull String player, boolean expensiveLookups) {
// If the player is online, give them their UUID.
// Remember, local data > remote data.
if (plugin.getPlayer(player) != null)
return plugin.getPlayerUUID(player);
// Check if it exists in the map
CachedUUIDEntry cachedUUIDEntry = nameToUuidMap.get(player.toLowerCase());
if (cachedUUIDEntry != null) {
if (!cachedUUIDEntry.expired())
return cachedUUIDEntry.getUuid();
else
nameToUuidMap.remove(player);
}
// 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);
}
// If we are in offline mode, UUID generation is simple.
// We don't even have to cache the UUID, since this is easy to recalculate.
if (!plugin.isOnlineMode()) {
return UUID.nameUUIDFromBytes(("OfflinePlayer:" + player).getBytes(Charsets.UTF_8));
}
RedisTask<UUID> redisTask = new RedisTask<UUID>(plugin) {
2022-07-16 05:18:33 +00:00
@Override
public UUID unifiedJedisTask(UnifiedJedis unifiedJedis) {
String stored = unifiedJedis.hget("uuid-cache", player.toLowerCase());
2022-07-16 05:18:33 +00:00
if (stored != null) {
// Found an entry value. Deserialize it.
CachedUUIDEntry entry = gson.fromJson(stored, CachedUUIDEntry.class);
// Check for expiry:
if (entry.expired()) {
unifiedJedis.hdel("uuid-cache", player.toLowerCase());
2022-07-16 05:18:33 +00:00
// Doesn't hurt to also remove the UUID entry as well.
unifiedJedis.hdel("uuid-cache", entry.getUuid().toString());
2022-07-16 05:18:33 +00:00
} else {
nameToUuidMap.put(player.toLowerCase(), entry);
uuidToNameMap.put(entry.getUuid(), entry);
return entry.getUuid();
}
2014-05-28 22:29:44 +00:00
}
2022-07-16 05:18:33 +00:00
// That didn't work. Let's ask Mojang.
if (!expensiveLookups || !plugin.isOnlineMode())
return null;
2022-07-16 05:18:33 +00:00
Map<String, UUID> uuidMap1;
try {
uuidMap1 = new UUIDFetcher(Collections.singletonList(player)).call();
} catch (Exception e) {
plugin.logFatal("Unable to fetch UUID from Mojang for " + player);
return null;
}
for (Map.Entry<String, UUID> entry : uuidMap1.entrySet()) {
if (entry.getKey().equalsIgnoreCase(player)) {
persistInfo(entry.getKey(), entry.getValue(), unifiedJedis);
2022-07-16 05:18:33 +00:00
return entry.getValue();
}
}
return null;
}
2022-07-16 05:18:33 +00:00
};
// Let's try Redis.
try {
return redisTask.execute();
} catch (JedisException e) {
plugin.logFatal("Unable to fetch UUID for " + player);
}
2014-05-28 22:29:44 +00:00
return null; // Nope, game over!
}
2022-07-16 05:18:33 +00:00
public String getNameFromUuid(@NonNull UUID player, boolean expensiveLookups) {
// If the player is online, give them their UUID.
// Remember, local data > remote data.
if (plugin.getPlayer(player) != null)
return plugin.getPlayerName(player);
// 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);
}
RedisTask<String> redisTask = new RedisTask<String>(plugin) {
2022-07-16 05:18:33 +00:00
@Override
public String unifiedJedisTask(UnifiedJedis unifiedJedis) {
String stored = unifiedJedis.hget("uuid-cache", player.toString());
2022-07-16 05:18:33 +00:00
if (stored != null) {
// Found an entry value. Deserialize it.
CachedUUIDEntry entry = gson.fromJson(stored, CachedUUIDEntry.class);
// Check for expiry:
if (entry.expired()) {
unifiedJedis.hdel("uuid-cache", player.toString());
2022-07-16 05:18:33 +00:00
// Doesn't hurt to also remove the named entry as well.
// TODO: Since UUIDs are fixed, we could look up the name and see if the UUID matches.
unifiedJedis.hdel("uuid-cache", entry.getName());
2022-07-16 05:18:33 +00:00
} else {
nameToUuidMap.put(entry.getName().toLowerCase(), entry);
uuidToNameMap.put(player, entry);
return entry.getName();
}
}
if (!expensiveLookups || !plugin.isOnlineMode())
return null;
// That didn't work. Let's ask Mojang. This call may fail, because Mojang is insane.
2022-12-31 03:23:35 +00:00
//
// UPDATE: Mojang has removed the API somewhere in september/2022 due privacy issues
// this is expected to fail now, so we will keep logging it until we figure out something or remove name fetching completely
// Name fetching class was deprecated as result
2022-07-16 05:18:33 +00:00
String name;
try {
2022-12-31 03:23:35 +00:00
plugin.logFatal("Due Mojang removing the naming API, we were unable to fetch player names.");
name = Iterables.getLast(NameFetcher.nameHistoryFromUuid(player));
2022-07-16 05:18:33 +00:00
} catch (Exception e) {
plugin.logFatal("Unable to fetch name from Mojang for " + player);
return null;
}
if (name != null) {
persistInfo(name, player, unifiedJedis);
2022-07-16 05:18:33 +00:00
return name;
}
2014-05-28 22:29:44 +00:00
return null;
}
2022-07-16 05:18:33 +00:00
};
2022-07-16 05:18:33 +00:00
// Okay, it wasn't locally cached. Let's try Redis.
try {
return redisTask.execute();
2014-05-28 22:29:44 +00:00
} catch (JedisException e) {
plugin.logFatal("Unable to fetch name for " + player);
return null;
}
}
2014-04-23 22:05:42 +00:00
public void persistInfo(String name, UUID uuid, UnifiedJedis unifiedJedis) {
2015-06-21 22:09:46 +00:00
addToMaps(name, uuid);
String json = gson.toJson(uuidToNameMap.get(uuid));
unifiedJedis.hset("uuid-cache", ImmutableMap.of(name.toLowerCase(), json, uuid.toString(), json));
2015-06-21 22:09:46 +00:00
}
}