From 4673a3f00c0fa875b60ff5b6fd2ad6dbfed4dfd2 Mon Sep 17 00:00:00 2001 From: Phoenix616 Date: Sat, 2 May 2020 19:25:44 +0100 Subject: [PATCH] Use AccountQueryEvent in NameManager#canUseName utility method --- .../ChestShop/Events/AccountQueryEvent.java | 17 +++++++++++++++ .../Acrobot/ChestShop/UUIDs/NameManager.java | 21 ++++++++++++++++--- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/Acrobot/ChestShop/Events/AccountQueryEvent.java b/src/main/java/com/Acrobot/ChestShop/Events/AccountQueryEvent.java index 429da3a..8e5932e 100644 --- a/src/main/java/com/Acrobot/ChestShop/Events/AccountQueryEvent.java +++ b/src/main/java/com/Acrobot/ChestShop/Events/AccountQueryEvent.java @@ -11,6 +11,7 @@ public class AccountQueryEvent extends Event { private static final HandlerList handlers = new HandlerList(); private final String name; private Account account = null; + private boolean searchOfflinePlayers = true; public AccountQueryEvent(String name) { this.name = name; @@ -28,6 +29,22 @@ public class AccountQueryEvent extends Event { this.account = account; } + /** + * Get whether or not offline player data should be searched (too) + * @return Whether or not offline player data should be searched (too) + */ + public boolean searchOfflinePlayers() { + return searchOfflinePlayers; + } + + /** + * Set whether or not offline player data should be searched (too) + * @param searchOfflinePlayers Whether or not offline player data should be searched (too) + */ + public void searchOfflinePlayers(boolean searchOfflinePlayers) { + this.searchOfflinePlayers = searchOfflinePlayers; + } + @Override public HandlerList getHandlers() { return handlers; diff --git a/src/main/java/com/Acrobot/ChestShop/UUIDs/NameManager.java b/src/main/java/com/Acrobot/ChestShop/UUIDs/NameManager.java index 111e0d7..1f3aff4 100644 --- a/src/main/java/com/Acrobot/ChestShop/UUIDs/NameManager.java +++ b/src/main/java/com/Acrobot/ChestShop/UUIDs/NameManager.java @@ -137,7 +137,7 @@ public class NameManager implements Listener { @EventHandler public static void onAccountQuery(AccountQueryEvent event) { if (event.getAccount() == null) { - event.setAccount(getLastAccountFromShortName(event.getName())); + event.setAccount(getLastAccountFromShortName(event.getName(), event.searchOfflinePlayers())); } } @@ -201,7 +201,19 @@ public class NameManager implements Listener { */ @Deprecated public static Account getLastAccountFromShortName(String shortName) { - Account account = getAccountFromShortName(shortName); // first get the account associated with the short name + return getLastAccountFromShortName(shortName, true); + } + + /** + * Get the information from the last time a player logged in that previously used the shortened name + * + * @param shortName The name of the player to get the last account for + * @param searchOfflinePlayer Whether or not to search the offline players too + * @return The last account or null if none was found + * @throws IllegalArgumentException if the username is empty + */ + private static Account getLastAccountFromShortName(String shortName, boolean searchOfflinePlayer) { + Account account = getAccountFromShortName(shortName, searchOfflinePlayer); // first get the account associated with the short name if (account != null) { return getAccount(account.getUuid()); // then get the last account that was online with that UUID } @@ -363,7 +375,10 @@ public class NameManager implements Listener { return true; } - Account account = getAccountFromShortName(name, false); + AccountQueryEvent queryEvent = new AccountQueryEvent(name); + queryEvent.searchOfflinePlayers(false); + ChestShop.callEvent(queryEvent); + Account account = queryEvent.getAccount(); if (account == null) { return false; }