From c82ed1e2924df12e6739d07205bafb437737dbd7 Mon Sep 17 00:00:00 2001 From: Ghost_chu <2908803755@qq.com> Date: Mon, 6 Jan 2020 14:16:11 +0800 Subject: [PATCH] Optimize the code Former-commit-id: a2ff744118fa020a08c247acd8079a3e6b7bfdf4 --- .../quickshop/Shop/ContainerShop.java | 14 +++++------- .../maxgamer/quickshop/Shop/ShopManager.java | 22 ++++++++----------- .../org/maxgamer/quickshop/Util/Util.java | 15 +++++++++++++ 3 files changed, 29 insertions(+), 22 deletions(-) diff --git a/src/main/java/org/maxgamer/quickshop/Shop/ContainerShop.java b/src/main/java/org/maxgamer/quickshop/Shop/ContainerShop.java index 12a3fac1d..842a6fc6e 100644 --- a/src/main/java/org/maxgamer/quickshop/Shop/ContainerShop.java +++ b/src/main/java/org/maxgamer/quickshop/Shop/ContainerShop.java @@ -207,8 +207,7 @@ public class ContainerShop implements Shop { @Override public void setPrice(double price) { ShopPriceChangeEvent event = new ShopPriceChangeEvent(this, this.price, price); - Bukkit.getPluginManager().callEvent(event); - if (event.isCancelled()) { + if (Util.fireCancellableEvent(event)) { Util.debugLog("A plugin cancelled the price change event."); return; } @@ -223,7 +222,7 @@ public class ContainerShop implements Shop { @Override public void update() { ShopUpdateEvent shopUpdateEvent = new ShopUpdateEvent(this); - if (shopUpdateEvent.isCancelled()) { + if (Util.fireCancellableEvent(shopUpdateEvent)) { Util.debugLog("The Shop update action was canceled by a plugin."); return; } @@ -358,8 +357,7 @@ public class ContainerShop implements Shop { @Override public void delete(boolean fromMemory) { ShopDeleteEvent shopDeleteEvent = new ShopDeleteEvent(this, fromMemory); - Bukkit.getPluginManager().callEvent(shopDeleteEvent); - if (shopDeleteEvent.isCancelled()) { + if (Util.fireCancellableEvent(shopDeleteEvent)) { Util.debugLog("Shop deletion was canceled because a plugin canceled it."); return; } @@ -874,8 +872,7 @@ public class ContainerShop implements Shop { return; } ShopLoadEvent shopLoadEvent = new ShopLoadEvent(this); - Bukkit.getPluginManager().callEvent(shopLoadEvent); - if (shopLoadEvent.isCancelled()) { + if (Util.fireCancellableEvent(shopLoadEvent)) { return; } @@ -921,8 +918,7 @@ public class ContainerShop implements Shop { @Override public void onClick() { ShopClickEvent event = new ShopClickEvent(this); - Bukkit.getPluginManager().callEvent(event); - if (event.isCancelled()) { + if (Util.fireCancellableEvent(event)) { Util.debugLog("Ignore shop click, because some plugin cancel it."); return; } diff --git a/src/main/java/org/maxgamer/quickshop/Shop/ShopManager.java b/src/main/java/org/maxgamer/quickshop/Shop/ShopManager.java index fb154c02b..5f014436b 100644 --- a/src/main/java/org/maxgamer/quickshop/Shop/ShopManager.java +++ b/src/main/java/org/maxgamer/quickshop/Shop/ShopManager.java @@ -99,8 +99,7 @@ public class ShopManager { return; } ShopPurchaseEvent e = new ShopPurchaseEvent(shop, p, amount); - Bukkit.getPluginManager().callEvent(e); - if (e.isCancelled()) { + if (Util.fireCancellableEvent(e)) { return; // Cancelled } // Money handling @@ -287,7 +286,7 @@ public class ShopManager { // money back. if (createCost > 0) { if (!plugin.getEconomy().withdraw(p.getUniqueId(), createCost)) { - p.sendMessage(MsgUtil.getMessage("you-cant-afford-a-new-shop", p, format(createCost))); + p.sendMessage(MsgUtil.getMessage("you-cant-afford-a-new-shop", p, Objects.requireNonNull(format(createCost)))); return; } try { @@ -304,8 +303,7 @@ public class ShopManager { } shop.onLoad(); ShopCreateEvent e = new ShopCreateEvent(shop, p); - Bukkit.getPluginManager().callEvent(e); - if (e.isCancelled()) { + if (Util.fireCancellableEvent(e)) { shop.onUnload(); return; } @@ -370,8 +368,7 @@ public class ShopManager { return; } ShopPurchaseEvent e = new ShopPurchaseEvent(shop, p, amount); - Bukkit.getPluginManager().callEvent(e); - if (e.isCancelled()) { + if (Util.fireCancellableEvent(e)) { return; // Cancelled } // Money handling @@ -391,7 +388,7 @@ public class ShopManager { boolean successA = eco.withdraw(p.getUniqueId(), total); //Withdraw owner's money if (!successA) { p.sendMessage(MsgUtil - .getMessage("you-cant-afford-to-buy", p, format(total), format(eco.getBalance(p.getUniqueId())))); + .getMessage("you-cant-afford-to-buy", p, Objects.requireNonNull(format(total)), Objects.requireNonNull(format(eco.getBalance(p.getUniqueId()))))); return; } boolean shouldPayOwner = !shop.isUnlimited() || (plugin.getConfig().getBoolean("shop.pay-unlimited-shop-owners") && shop @@ -489,7 +486,7 @@ public class ShopManager { if (ownerCanAfford == 0 && (!shop.isUnlimited() || plugin.getConfig().getBoolean("shop.pay-unlimited-shop-owners"))) { // when typed 'all' but the shop owner doesn't have enough money to buy at least 1 item (and shop isn't unlimited or pay-unlimited is true) p.sendMessage(MsgUtil - .getMessage("the-owner-cant-afford-to-buy-from-you", p, format(shop.getPrice()), format(ownerBalance))); + .getMessage("the-owner-cant-afford-to-buy-from-you", p, Objects.requireNonNull(format(shop.getPrice())), Objects.requireNonNull(format(ownerBalance)))); return; } // when typed 'all' but player doesn't have any items to sell @@ -533,7 +530,7 @@ public class ShopManager { p.sendMessage(MsgUtil.getMessage("not-enough-space", p, String.valueOf(invHaveSpaces))); return; } - p.sendMessage(MsgUtil.getMessage("you-cant-afford-to-buy", p, format(price), format(balance))); + p.sendMessage(MsgUtil.getMessage("you-cant-afford-to-buy", p, Objects.requireNonNull(format(price)), Objects.requireNonNull(format(balance)))); return; } } @@ -624,7 +621,7 @@ public class ShopManager { } ShopPreCreateEvent spce = new ShopPreCreateEvent(p, b.getLocation()); Bukkit.getPluginManager().callEvent(spce); - if (spce.isCancelled()) { + if (Util.fireCancellableEvent(spce)) { return false; } } finally { @@ -668,8 +665,7 @@ public class ShopManager { throw new IllegalStateException("The owner creating the shop is offline or not exist"); } ShopCreateEvent ssShopCreateEvent = new ShopCreateEvent(shop, player); - Bukkit.getPluginManager().callEvent(ssShopCreateEvent); - if (ssShopCreateEvent.isCancelled()) { + if (Util.fireCancellableEvent(ssShopCreateEvent)) { return; } Location loc = shop.getLocation(); diff --git a/src/main/java/org/maxgamer/quickshop/Util/Util.java b/src/main/java/org/maxgamer/quickshop/Util/Util.java index 92320fb82..cd20b0d6f 100644 --- a/src/main/java/org/maxgamer/quickshop/Util/Util.java +++ b/src/main/java/org/maxgamer/quickshop/Util/Util.java @@ -27,6 +27,8 @@ import org.bukkit.block.data.Directional; import org.bukkit.configuration.InvalidConfigurationException; import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.entity.Player; +import org.bukkit.event.Cancellable; +import org.bukkit.event.Event; import org.bukkit.inventory.DoubleChestInventory; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.InventoryHolder; @@ -1301,4 +1303,17 @@ public class Util { } return minecrftVersion.replace("_","."); } + + /** + * Call a event and check it is cancelled. + * @param event The event implement the Cancellable interface. + * @return The event is cancelled. + */ + public static boolean fireCancellableEvent(@NotNull Cancellable event){ + if(!(event instanceof Event)){ + throw new IllegalArgumentException("Cancellable must is event implement"); + } + Bukkit.getPluginManager().callEvent((Event) event); + return event.isCancelled(); + } }