mirror of
https://github.com/EarthMC/QuickShop-Reremake.git
synced 2026-11-25 01:49:49 -05:00
Optimize the code
Former-commit-id: a2ff744118fa020a08c247acd8079a3e6b7bfdf4
This commit is contained in:
1 parent
14603b5536
commit
c82ed1e292
3 files changed
+29
-22
No files matched your search
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user