From b300798a62595cceb3ffe1e8b51e6df8b5b3e596 Mon Sep 17 00:00:00 2001 From: Phoenix616 Date: Mon, 6 May 2019 23:49:16 +0100 Subject: [PATCH] Make querying of accounts by (short) name an event too --- .../java/com/Acrobot/ChestShop/ChestShop.java | 2 + .../Acrobot/ChestShop/Database/Account.java | 6 ++- .../ChestShop/Events/AccountQueryEvent.java | 39 +++++++++++++++++++ .../Listeners/Player/PlayerInteract.java | 6 ++- .../PreShopCreation/NameChecker.java | 6 ++- .../ShopRemoval/ShopRefundListener.java | 6 ++- .../java/com/Acrobot/ChestShop/Security.java | 7 +++- .../ChestShop/Signs/ChestShopSign.java | 6 ++- .../Acrobot/ChestShop/UUIDs/NameManager.java | 24 +++++++++--- 9 files changed, 89 insertions(+), 13 deletions(-) create mode 100644 src/main/java/com/Acrobot/ChestShop/Events/AccountQueryEvent.java diff --git a/src/main/java/com/Acrobot/ChestShop/ChestShop.java b/src/main/java/com/Acrobot/ChestShop/ChestShop.java index d7248b4..ad47e9d 100644 --- a/src/main/java/com/Acrobot/ChestShop/ChestShop.java +++ b/src/main/java/com/Acrobot/ChestShop/ChestShop.java @@ -276,6 +276,8 @@ public class ChestShop extends JavaPlugin { private void registerEvents() { registerEvent(new com.Acrobot.ChestShop.Plugins.ChestShop()); //Chest protection + registerEvent(new NameManager()); + registerPreShopCreationEvents(); registerPreTransactionEvents(); registerPostShopCreationEvents(); diff --git a/src/main/java/com/Acrobot/ChestShop/Database/Account.java b/src/main/java/com/Acrobot/ChestShop/Database/Account.java index bbc6afb..5af9c82 100644 --- a/src/main/java/com/Acrobot/ChestShop/Database/Account.java +++ b/src/main/java/com/Acrobot/ChestShop/Database/Account.java @@ -33,8 +33,12 @@ public class Account { } public Account(String name, UUID uuid) { + this(name, NameUtil.stripUsername(name), uuid); + } + + public Account(String name, String shortName, UUID uuid) { this.name = name; - this.shortName = NameUtil.stripUsername(name); + this.shortName = shortName; this.uuid = uuid; } diff --git a/src/main/java/com/Acrobot/ChestShop/Events/AccountQueryEvent.java b/src/main/java/com/Acrobot/ChestShop/Events/AccountQueryEvent.java new file mode 100644 index 0000000..429da3a --- /dev/null +++ b/src/main/java/com/Acrobot/ChestShop/Events/AccountQueryEvent.java @@ -0,0 +1,39 @@ +package com.Acrobot.ChestShop.Events; + +import com.Acrobot.ChestShop.Database.Account; +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; + +/** + * Represents a query for an account by using the name (e.g. from the shop sign) + */ +public class AccountQueryEvent extends Event { + private static final HandlerList handlers = new HandlerList(); + private final String name; + private Account account = null; + + public AccountQueryEvent(String name) { + this.name = name; + } + + public String getName() { + return name; + } + + public Account getAccount() { + return account; + } + + public void setAccount(Account account) { + this.account = account; + } + + @Override + public HandlerList getHandlers() { + return handlers; + } + + public static HandlerList getHandlerList() { + return handlers; + } +} diff --git a/src/main/java/com/Acrobot/ChestShop/Listeners/Player/PlayerInteract.java b/src/main/java/com/Acrobot/ChestShop/Listeners/Player/PlayerInteract.java index cc17930..d2172ea 100644 --- a/src/main/java/com/Acrobot/ChestShop/Listeners/Player/PlayerInteract.java +++ b/src/main/java/com/Acrobot/ChestShop/Listeners/Player/PlayerInteract.java @@ -5,6 +5,7 @@ import com.Acrobot.ChestShop.Configuration.Messages; import com.Acrobot.ChestShop.Configuration.Properties; import com.Acrobot.ChestShop.Containers.AdminInventory; import com.Acrobot.ChestShop.Database.Account; +import com.Acrobot.ChestShop.Events.AccountQueryEvent; import com.Acrobot.ChestShop.Events.Economy.AccountCheckEvent; import com.Acrobot.ChestShop.Events.ItemParseEvent; import com.Acrobot.ChestShop.Events.PreTransactionEvent; @@ -13,7 +14,6 @@ import com.Acrobot.ChestShop.Permission; import com.Acrobot.ChestShop.Plugins.ChestShop; import com.Acrobot.ChestShop.Security; import com.Acrobot.ChestShop.Signs.ChestShopSign; -import com.Acrobot.ChestShop.UUIDs.NameManager; import com.Acrobot.ChestShop.Utils.uBlock; import org.bukkit.Bukkit; import org.bukkit.ChatColor; @@ -155,7 +155,9 @@ public class PlayerInteract implements Listener { String prices = sign.getLine(PRICE_LINE); String material = sign.getLine(ITEM_LINE); - Account account = NameManager.getLastAccountFromShortName(name); + AccountQueryEvent accountQueryEvent = new AccountQueryEvent(name); + Bukkit.getPluginManager().callEvent(accountQueryEvent); + Account account = accountQueryEvent.getAccount(); if (account == null) { player.sendMessage(Messages.prefix(Messages.PLAYER_NOT_FOUND)); return null; diff --git a/src/main/java/com/Acrobot/ChestShop/Listeners/PreShopCreation/NameChecker.java b/src/main/java/com/Acrobot/ChestShop/Listeners/PreShopCreation/NameChecker.java index d8bcf41..2eee766 100644 --- a/src/main/java/com/Acrobot/ChestShop/Listeners/PreShopCreation/NameChecker.java +++ b/src/main/java/com/Acrobot/ChestShop/Listeners/PreShopCreation/NameChecker.java @@ -1,8 +1,10 @@ package com.Acrobot.ChestShop.Listeners.PreShopCreation; import com.Acrobot.ChestShop.Database.Account; +import com.Acrobot.ChestShop.Events.AccountQueryEvent; import com.Acrobot.ChestShop.Events.PreShopCreationEvent; import com.Acrobot.ChestShop.UUIDs.NameManager; +import org.bukkit.Bukkit; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; @@ -30,7 +32,9 @@ public class NameChecker implements Listener { event.setOutcome(UNKNOWN_PLAYER); } } else { - Account account = NameManager.getAccountFromShortName(name); + AccountQueryEvent accountQueryEvent = new AccountQueryEvent(name); + Bukkit.getPluginManager().callEvent(accountQueryEvent); + Account account = accountQueryEvent.getAccount(); if (account == null) { event.setOutcome(UNKNOWN_PLAYER); } diff --git a/src/main/java/com/Acrobot/ChestShop/Listeners/ShopRemoval/ShopRefundListener.java b/src/main/java/com/Acrobot/ChestShop/Listeners/ShopRemoval/ShopRefundListener.java index 6d5ba6f..78e5761 100644 --- a/src/main/java/com/Acrobot/ChestShop/Listeners/ShopRemoval/ShopRefundListener.java +++ b/src/main/java/com/Acrobot/ChestShop/Listeners/ShopRemoval/ShopRefundListener.java @@ -5,11 +5,13 @@ import com.Acrobot.ChestShop.Configuration.Messages; import com.Acrobot.ChestShop.Configuration.Properties; import com.Acrobot.ChestShop.Database.Account; import com.Acrobot.ChestShop.Economy.Economy; +import com.Acrobot.ChestShop.Events.AccountQueryEvent; import com.Acrobot.ChestShop.Events.Economy.CurrencyAddEvent; import com.Acrobot.ChestShop.Events.Economy.CurrencySubtractEvent; import com.Acrobot.ChestShop.Events.ShopDestroyedEvent; import com.Acrobot.ChestShop.Permission; import com.Acrobot.ChestShop.UUIDs.NameManager; +import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; @@ -39,7 +41,9 @@ public class ShopRefundListener implements Listener { return; } - Account account = NameManager.getAccountFromShortName(event.getSign().getLine(NAME_LINE)); + AccountQueryEvent accountQueryEvent = new AccountQueryEvent(event.getSign().getLine(NAME_LINE)); + Bukkit.getPluginManager().callEvent(accountQueryEvent); + Account account = accountQueryEvent.getAccount(); if (account == null) { return; } diff --git a/src/main/java/com/Acrobot/ChestShop/Security.java b/src/main/java/com/Acrobot/ChestShop/Security.java index 6b7300c..c288a8a 100644 --- a/src/main/java/com/Acrobot/ChestShop/Security.java +++ b/src/main/java/com/Acrobot/ChestShop/Security.java @@ -3,11 +3,12 @@ package com.Acrobot.ChestShop; import com.Acrobot.Breeze.Utils.BlockUtil; import com.Acrobot.ChestShop.Configuration.Properties; import com.Acrobot.ChestShop.Database.Account; +import com.Acrobot.ChestShop.Events.AccountQueryEvent; import com.Acrobot.ChestShop.Events.Protection.ProtectBlockEvent; import com.Acrobot.ChestShop.Events.Protection.ProtectionCheckEvent; import com.Acrobot.ChestShop.Signs.ChestShopSign; -import com.Acrobot.ChestShop.UUIDs.NameManager; import com.Acrobot.ChestShop.Utils.uBlock; +import org.bukkit.Bukkit; import org.bukkit.block.Block; import org.bukkit.block.BlockFace; import org.bukkit.block.Sign; @@ -78,7 +79,9 @@ public class Security { continue; } - Account account = NameManager.getAccountFromShortName(sign.getLine(ChestShopSign.NAME_LINE)); + AccountQueryEvent accountQueryEvent = new AccountQueryEvent(sign.getLine(ChestShopSign.NAME_LINE)); + Bukkit.getPluginManager().callEvent(accountQueryEvent); + Account account = accountQueryEvent.getAccount(); if (account != null && !account.getUuid().equals(player.getUniqueId())) { return true; } diff --git a/src/main/java/com/Acrobot/ChestShop/Signs/ChestShopSign.java b/src/main/java/com/Acrobot/ChestShop/Signs/ChestShopSign.java index 2a578c2..1661346 100644 --- a/src/main/java/com/Acrobot/ChestShop/Signs/ChestShopSign.java +++ b/src/main/java/com/Acrobot/ChestShop/Signs/ChestShopSign.java @@ -5,9 +5,11 @@ import com.Acrobot.Breeze.Utils.StringUtil; import com.Acrobot.ChestShop.Configuration.Properties; import com.Acrobot.ChestShop.Containers.AdminInventory; import com.Acrobot.ChestShop.Database.Account; +import com.Acrobot.ChestShop.Events.AccountQueryEvent; import com.Acrobot.ChestShop.Permission; import com.Acrobot.ChestShop.UUIDs.NameManager; import com.Acrobot.ChestShop.Utils.uBlock; +import org.bukkit.Bukkit; import org.bukkit.block.Block; import org.bukkit.block.BlockState; import org.bukkit.block.Chest; @@ -123,7 +125,9 @@ public class ChestShopSign { String name = sign.getLine(NAME_LINE); if (name == null || name.isEmpty()) return false; - Account account = NameManager.getAccountFromShortName(name); + AccountQueryEvent accountQueryEvent = new AccountQueryEvent(name); + Bukkit.getPluginManager().callEvent(accountQueryEvent); + Account account = accountQueryEvent.getAccount(); if (account == null) { return player.getName().equalsIgnoreCase(name); } diff --git a/src/main/java/com/Acrobot/ChestShop/UUIDs/NameManager.java b/src/main/java/com/Acrobot/ChestShop/UUIDs/NameManager.java index 4d11f6a..299d48f 100644 --- a/src/main/java/com/Acrobot/ChestShop/UUIDs/NameManager.java +++ b/src/main/java/com/Acrobot/ChestShop/UUIDs/NameManager.java @@ -7,6 +7,7 @@ import com.Acrobot.ChestShop.ChestShop; import com.Acrobot.ChestShop.Configuration.Properties; import com.Acrobot.ChestShop.Database.Account; import com.Acrobot.ChestShop.Database.DaoCreator; +import com.Acrobot.ChestShop.Events.AccountQueryEvent; import com.Acrobot.ChestShop.Permission; import com.Acrobot.ChestShop.Signs.ChestShopSign; import com.j256.ormlite.dao.Dao; @@ -15,6 +16,8 @@ import org.apache.commons.lang.Validate; import org.bukkit.Bukkit; import org.bukkit.OfflinePlayer; import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; import java.sql.SQLException; import java.util.Date; @@ -30,7 +33,7 @@ import static com.Acrobot.ChestShop.Permission.OTHER_NAME; * @author Andrzej Pomirski (Acrobot) */ @SuppressWarnings("UnusedAssignment") // I deliberately set the variables to null while initializing -public class NameManager { +public class NameManager implements Listener { private static Dao accounts; private static SimpleCache usernameToAccount = new SimpleCache<>(Properties.CACHE_SIZE); @@ -116,6 +119,13 @@ public class NameManager { } } + @EventHandler + public static void onAccountQuery(AccountQueryEvent event) { + if (event.getAccount() == null) { + event.setAccount(getLastAccountFromShortName(event.getName())); + } + } + /** * Get account info from a username that might be shortened. * If no account was found it will try to search all known players and create an account. @@ -123,6 +133,7 @@ public class NameManager { * @param shortName The name of the player to get the account info * @return The account info or null if none was found * @throws IllegalArgumentException if the username is empty + * @deprecated Use the {@link AccountQueryEvent} instead! */ public static Account getAccountFromShortName(String shortName) { return getAccountFromShortName(shortName, true); @@ -170,8 +181,10 @@ public class NameManager { * * @param shortName The name of the player to get the last account for * @return The last account or null if none was found - * @throws IllegalArgumentException if the username is not a shortened name and longer than 15 chars + * @throws IllegalArgumentException if the username is empty + * @deprecated Use the {@link AccountQueryEvent} instead! */ + @Deprecated public static Account getLastAccountFromShortName(String shortName) { Account account = getAccountFromShortName(shortName); // first get the account associated with the short name if (account != null) { @@ -239,7 +252,9 @@ public class NameManager { */ @Deprecated public static String getFullUsername(String shortName) { - Account account = getLastAccountFromShortName(shortName); + AccountQueryEvent accountQueryEvent = new AccountQueryEvent(shortName); + Bukkit.getPluginManager().callEvent(accountQueryEvent); + Account account = accountQueryEvent.getAccount(); if (account != null) { return account.getName(); } @@ -276,8 +291,7 @@ public class NameManager { } if (latestAccount == null) { - latestAccount = new Account(player.getName(), player.getUniqueId()); - latestAccount.setShortName(getNewShortenedName(player)); + latestAccount = new Account(player.getName(), getNewShortenedName(player), player.getUniqueId()); } latestAccount.setLastSeen(new Date());