From 3bed1261aa2f78b1097aa39244629100170338e8 Mon Sep 17 00:00:00 2001 From: Phoenix616 Date: Sat, 2 May 2020 15:32:48 +0100 Subject: [PATCH] Move account access check to an event This should be the final addition that allows other plugins to implement multi-owner/group shops like requested in #119 and #133 by listening to all the transaction events to handle money flow and shop/account access events to enable access to the actual shop. --- .../ChestShop/Events/AccountAccessEvent.java | 57 +++++++++++++++++++ .../Acrobot/ChestShop/UUIDs/NameManager.java | 19 ++++++- 2 files changed, 74 insertions(+), 2 deletions(-) create mode 100644 src/main/java/com/Acrobot/ChestShop/Events/AccountAccessEvent.java diff --git a/src/main/java/com/Acrobot/ChestShop/Events/AccountAccessEvent.java b/src/main/java/com/Acrobot/ChestShop/Events/AccountAccessEvent.java new file mode 100644 index 0000000..9967e46 --- /dev/null +++ b/src/main/java/com/Acrobot/ChestShop/Events/AccountAccessEvent.java @@ -0,0 +1,57 @@ +package com.Acrobot.ChestShop.Events; + +import com.Acrobot.ChestShop.Database.Account; +import org.bukkit.entity.Player; +import org.bukkit.event.HandlerList; +import org.bukkit.event.player.PlayerEvent; + +/** + * Represents an access request for a specific account. + */ +public class AccountAccessEvent extends PlayerEvent { + private static final HandlerList handlers = new HandlerList(); + private final Account account; + private boolean canAccess = false; + + public AccountAccessEvent(Player player, Account account) { + super(player); + this.account = account; + } + + /** + * The account to check the access for + * + * @return The account + */ + public Account getAccount() { + return account; + } + + /** + * Whether or not the player can access the account. + * + * @return Whether or not the player can access the account + */ + public boolean canAccess() { + return canAccess; + } + + /** + * Set whether or not the player can access the account. + * + * @param canAccess Whether or not the player can access the account + */ + public void setAccess(boolean canAccess) { + this.canAccess = canAccess; + } + + @Override + public HandlerList getHandlers() { + return handlers; + } + + public static HandlerList getHandlerList() { + 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 3b6c99a..111e0d7 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.AccountAccessEvent; import com.Acrobot.ChestShop.Events.AccountQueryEvent; import com.Acrobot.ChestShop.Permission; import com.Acrobot.ChestShop.Signs.ChestShopSign; @@ -363,8 +364,22 @@ public class NameManager implements Listener { } Account account = getAccountFromShortName(name, false); - return account != null && (account.getUuid().equals(player.getUniqueId()) - || (!account.getName().equalsIgnoreCase(name) && Permission.otherName(player, base, account.getName()))); + if (account == null) { + return false; + } + if (!account.getName().equalsIgnoreCase(name) && Permission.otherName(player, base, account.getName())) { + return true; + } + AccountAccessEvent event = new AccountAccessEvent(player, account); + ChestShop.callEvent(event); + return event.canAccess(); + } + + @EventHandler + public static void onAccountAccessCheck(AccountAccessEvent event) { + if (!event.canAccess()) { + event.setAccess(event.getPlayer().getUniqueId().equals(event.getAccount().getUuid())); + } } public static boolean isAdminShop(UUID uuid) {