Merge branch '1.12' into 1.13

This commit is contained in:
Phoenix616 2018-08-29 21:29:52 +01:00
commit 5f0bbfff0c
3 changed files with 47 additions and 18 deletions

View File

@ -40,7 +40,7 @@ public class SimpleCache<K, V> {
} }
} }
private boolean contains(K key) { public boolean contains(K key) {
return map.containsKey(key); return map.containsKey(key);
} }
} }

View File

@ -17,6 +17,10 @@ public class PlayerConnect implements Listener {
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public static void onPlayerConnect(final PlayerJoinEvent event) { public static void onPlayerConnect(final PlayerJoinEvent event) {
if (NameManager.getUuidVersion() < 0) {
NameManager.setUuidVersion(event.getPlayer().getUniqueId().version());
}
final PlayerDTO playerDTO = new PlayerDTO(event.getPlayer()); final PlayerDTO playerDTO = new PlayerDTO(event.getPlayer());
Bukkit.getScheduler().runTaskAsynchronously(ChestShop.getPlugin(), new Runnable() { Bukkit.getScheduler().runTaskAsynchronously(ChestShop.getPlugin(), new Runnable() {

View File

@ -34,9 +34,11 @@ public class NameManager {
private static SimpleCache<String, Account> usernameToAccount = new SimpleCache<>(Properties.CACHE_SIZE); private static SimpleCache<String, Account> usernameToAccount = new SimpleCache<>(Properties.CACHE_SIZE);
private static SimpleCache<UUID, Account> uuidToAccount = new SimpleCache<>(Properties.CACHE_SIZE); private static SimpleCache<UUID, Account> uuidToAccount = new SimpleCache<>(Properties.CACHE_SIZE);
private static SimpleCache<String, Account> shortToAccount = new SimpleCache<>(Properties.CACHE_SIZE); private static SimpleCache<String, Account> shortToAccount = new SimpleCache<>(Properties.CACHE_SIZE);
private static SimpleCache<String, Boolean> invalidPlayers = new SimpleCache<>(Properties.CACHE_SIZE);
private static Account adminAccount; private static Account adminAccount;
private static Account serverEconomyAccount; private static Account serverEconomyAccount;
private static int uuidVersion = -1;
/** /**
* Get account info from a UUID * Get account info from a UUID
@ -102,26 +104,38 @@ public class NameManager {
*/ */
public static Account getAccountFromShortName(String shortName) { public static Account getAccountFromShortName(String shortName) {
Validate.notEmpty(shortName, "shortName cannot be null or empty!"); Validate.notEmpty(shortName, "shortName cannot be null or empty!");
Account account = null;
if (shortName.length() > 15) { if (shortName.length() > 15) {
return getAccount(shortName); account = getAccount(shortName);
} else {
try {
account = shortToAccount.get(shortName, () -> {
try {
Account a = accounts.queryBuilder().where().eq("shortName", shortName).queryForFirst();
if (a != null) {
a.setShortName(shortName); // HOW IS IT EVEN POSSIBLE THAT THE NAME IS NOT SET EVEN IF WE HAVE FOUND THE PLAYER?!
return a;
}
} catch (SQLException e) {
ChestShop.getBukkitLogger().log(Level.WARNING, "Error while getting account for " + shortName + ":", e);
}
throw new Exception("Could not find account for " + shortName);
});
} catch (ExecutionException ignored) {}
} }
try { if (account == null && !invalidPlayers.contains(shortName.toLowerCase())) {
return shortToAccount.get(shortName, () -> { // no account with that shortname was found, try to get an offline player with that name
try { OfflinePlayer offlinePlayer = ChestShop.getBukkitServer().getOfflinePlayer(shortName);
Account account = accounts.queryBuilder().where().eq("shortName", shortName).queryForFirst(); if (offlinePlayer != null && offlinePlayer.getName() != null && offlinePlayer.getUniqueId() != null
if (account != null) { && offlinePlayer.getUniqueId().version() == uuidVersion) {
account.setShortName(shortName); // HOW IS IT EVEN POSSIBLE THAT THE NAME IS NOT SET EVEN IF WE HAVE FOUND THE PLAYER?! account = storeUsername(new PlayerDTO(offlinePlayer.getUniqueId(), offlinePlayer.getName()));
return account; } else {
} invalidPlayers.put(shortName.toLowerCase(), true);
} catch (SQLException e) { }
ChestShop.getBukkitLogger().log(Level.WARNING, "Error while getting account for " + shortName + ":", e);
}
throw new Exception("Could not find account for " + shortName);
});
} catch (ExecutionException ignored) {
return null;
} }
return account;
} }
/** /**
@ -222,8 +236,9 @@ public class NameManager {
* Store the username of a player into the database and the username-uuid cache * Store the username of a player into the database and the username-uuid cache
* *
* @param player The data transfer object of the player to store * @param player The data transfer object of the player to store
* @return The stored/updated account. <tt>null</tt> if there was an error updating it
*/ */
public static void storeUsername(final PlayerDTO player) { public static Account storeUsername(final PlayerDTO player) {
final UUID uuid = player.getUniqueId(); final UUID uuid = player.getUniqueId();
Account latestAccount = null; Account latestAccount = null;
@ -243,11 +258,14 @@ public class NameManager {
accounts.createOrUpdate(latestAccount); accounts.createOrUpdate(latestAccount);
} catch (SQLException e) { } catch (SQLException e) {
ChestShop.getBukkitLogger().log(Level.WARNING, "Error while updating account " + latestAccount + ":", e); ChestShop.getBukkitLogger().log(Level.WARNING, "Error while updating account " + latestAccount + ":", e);
return null;
} }
usernameToAccount.put(latestAccount.getName(), latestAccount); usernameToAccount.put(latestAccount.getName(), latestAccount);
uuidToAccount.put(uuid, latestAccount); uuidToAccount.put(uuid, latestAccount);
shortToAccount.put(latestAccount.getShortName(), latestAccount); shortToAccount.put(latestAccount.getShortName(), latestAccount);
return latestAccount;
} }
/** /**
@ -317,4 +335,11 @@ public class NameManager {
return serverEconomyAccount; return serverEconomyAccount;
} }
public static void setUuidVersion(int uuidVersion) {
NameManager.uuidVersion = uuidVersion;
}
public static int getUuidVersion() {
return uuidVersion;
}
} }