diff --git a/src/main/java/org/maxgamer/quickshop/Shop/ShopCreateEvent.java b/src/main/java/org/maxgamer/quickshop/Event/ShopCreateEvent.java similarity index 93% rename from src/main/java/org/maxgamer/quickshop/Shop/ShopCreateEvent.java rename to src/main/java/org/maxgamer/quickshop/Event/ShopCreateEvent.java index 15de4e756..b8e04eaa7 100644 --- a/src/main/java/org/maxgamer/quickshop/Shop/ShopCreateEvent.java +++ b/src/main/java/org/maxgamer/quickshop/Event/ShopCreateEvent.java @@ -1,9 +1,10 @@ -package org.maxgamer.quickshop.Shop; +package org.maxgamer.quickshop.Event; import org.bukkit.entity.Player; import org.bukkit.event.Cancellable; import org.bukkit.event.Event; import org.bukkit.event.HandlerList; +import org.maxgamer.quickshop.Shop.Shop; public class ShopCreateEvent extends Event implements Cancellable { private static final HandlerList handlers = new HandlerList(); diff --git a/src/main/java/org/maxgamer/quickshop/Event/ShopDeleteEvent.java b/src/main/java/org/maxgamer/quickshop/Event/ShopDeleteEvent.java new file mode 100644 index 000000000..c5539bc47 --- /dev/null +++ b/src/main/java/org/maxgamer/quickshop/Event/ShopDeleteEvent.java @@ -0,0 +1,62 @@ +package org.maxgamer.quickshop.Event; + +import org.bukkit.event.Cancellable; +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; +import org.maxgamer.quickshop.Shop.Shop; + +public class ShopDeleteEvent extends Event implements Cancellable { + private static final HandlerList handlers = new HandlerList(); + private Shop shop; + private boolean cancelled; + private boolean fromMemory; + + /** + * Call the event when shop is deleteing. + * The ShopUnloadEvent will call after ShopDeleteEvent + * + * @param shop Target shop + * @param fromMemory Only delete from the memory? false = delete both in memory and database + */ + public ShopDeleteEvent(Shop shop, boolean fromMemory) { + this.shop = shop; + this.fromMemory = fromMemory; + } + + /** + * Get is only delete from the memory + * + * @return only from memory + */ + public boolean isFromMemory() { + return fromMemory; + } + + /** + * The shop to be deleted + * + * @return The shop to be deleted + */ + public Shop getShop() { + return this.shop; + } + + @Override + public HandlerList getHandlers() { + return handlers; + } + + public static HandlerList getHandlerList() { + return handlers; + } + + @Override + public boolean isCancelled() { + return this.cancelled; + } + + @Override + public void setCancelled(boolean cancel) { + this.cancelled = cancel; + } +} diff --git a/src/main/java/org/maxgamer/quickshop/Event/ShopDisplayItemDespawnEvent.java b/src/main/java/org/maxgamer/quickshop/Event/ShopDisplayItemDespawnEvent.java new file mode 100644 index 000000000..3cda19581 --- /dev/null +++ b/src/main/java/org/maxgamer/quickshop/Event/ShopDisplayItemDespawnEvent.java @@ -0,0 +1,70 @@ +package org.maxgamer.quickshop.Event; + +import org.bukkit.event.Cancellable; +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; +import org.bukkit.inventory.ItemStack; +import org.maxgamer.quickshop.Shop.DisplayItem; +import org.maxgamer.quickshop.Shop.DisplayType; +import org.maxgamer.quickshop.Shop.Shop; + +/** + * This event is called after DisplayItem removed + */ +public class ShopDisplayItemDespawnEvent extends Event implements Cancellable { + private static final HandlerList handlers = new HandlerList(); + private boolean cancelled; + private Shop shop; + private ItemStack iStack; + private DisplayType displayType; + + /* + * This event is called before the shop display item created + */ + public ShopDisplayItemDespawnEvent(Shop shop, ItemStack iStack) { + this.shop = shop; + this.iStack = iStack; + this.displayType = DisplayItem.getNowUsing(); + } + + /* + * This event is called before the shop display item created + */ + public ShopDisplayItemDespawnEvent(Shop shop, ItemStack iStack, DisplayType displayType) { + this.shop = shop; + this.iStack = iStack; + this.displayType = displayType; + } + + public Shop getShop() { + return shop; + } + + public DisplayType getDisplayType() { + return displayType; + } + + public ItemStack getItemStack() { + return iStack; + } + + @Override + public HandlerList getHandlers() { + return handlers; + } + + public static HandlerList getHandlerList() { + return handlers; + } + + @Override + public boolean isCancelled() { + return this.cancelled; + } + + @Override + public void setCancelled(boolean cancel) { + this.cancelled = cancel; + } + +} diff --git a/src/main/java/org/maxgamer/quickshop/Shop/ShopDisplayItemSpawnEvent.java b/src/main/java/org/maxgamer/quickshop/Event/ShopDisplayItemSpawnEvent.java similarity index 76% rename from src/main/java/org/maxgamer/quickshop/Shop/ShopDisplayItemSpawnEvent.java rename to src/main/java/org/maxgamer/quickshop/Event/ShopDisplayItemSpawnEvent.java index 02ee7bb95..4b7fd0864 100644 --- a/src/main/java/org/maxgamer/quickshop/Shop/ShopDisplayItemSpawnEvent.java +++ b/src/main/java/org/maxgamer/quickshop/Event/ShopDisplayItemSpawnEvent.java @@ -1,64 +1,70 @@ -package org.maxgamer.quickshop.Shop; - -import org.bukkit.event.Cancellable; -import org.bukkit.event.Event; -import org.bukkit.event.HandlerList; -import org.bukkit.inventory.ItemStack; - -/** - * This event is called before the shop display item created - */ -public class ShopDisplayItemSpawnEvent extends Event implements Cancellable { - private static final HandlerList handlers = new HandlerList(); - private boolean cancelled; - private Shop shop; - private ItemStack iStack; - private boolean fakeItem; - - /* - * This event is called before the shop display item created - */ - public ShopDisplayItemSpawnEvent(Shop shop, ItemStack iStack) { - this.shop = shop; - this.iStack = iStack; - } - - /* - * This event is called before the shop display item created - */ - public ShopDisplayItemSpawnEvent(Shop shop, ItemStack iStack, boolean fakeItem) { - this.shop = shop; - this.iStack = iStack; - this.fakeItem = fakeItem; - } - - public Shop getShop() { - return shop; - } - - public ItemStack getItemStack() { - return iStack; - } - - public boolean getFakeItem() {return fakeItem;} - - @Override - public HandlerList getHandlers() { - return handlers; - } - - public static HandlerList getHandlerList() { - return handlers; - } - - @Override - public boolean isCancelled() { - return this.cancelled; - } - - @Override - public void setCancelled(boolean cancel) { - this.cancelled = cancel; - } - -} +package org.maxgamer.quickshop.Event; + +import org.bukkit.event.Cancellable; +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; +import org.bukkit.inventory.ItemStack; +import org.maxgamer.quickshop.Shop.DisplayItem; +import org.maxgamer.quickshop.Shop.DisplayType; +import org.maxgamer.quickshop.Shop.Shop; + +/** + * This event is called before the shop display item created + */ +public class ShopDisplayItemSpawnEvent extends Event implements Cancellable { + private static final HandlerList handlers = new HandlerList(); + private boolean cancelled; + private Shop shop; + private ItemStack iStack; + private DisplayType displayType; + + /* + * This event is called before the shop display item created + */ + public ShopDisplayItemSpawnEvent(Shop shop, ItemStack iStack, DisplayType displayType) { + this.shop = shop; + this.iStack = iStack; + this.displayType = displayType; + } + + /* + * This event is called before the shop display item created + */ + public ShopDisplayItemSpawnEvent(Shop shop, ItemStack iStack) { + this.shop = shop; + this.iStack = iStack; + this.displayType = DisplayItem.getNowUsing(); + } + + public Shop getShop() { + return shop; + } + + public DisplayType getDisplayType() { + return displayType; + } + + public ItemStack getItemStack() { + return iStack; + } + + @Override + public HandlerList getHandlers() { + return handlers; + } + + public static HandlerList getHandlerList() { + return handlers; + } + + @Override + public boolean isCancelled() { + return this.cancelled; + } + + @Override + public void setCancelled(boolean cancel) { + this.cancelled = cancel; + } + +} diff --git a/src/main/java/org/maxgamer/quickshop/Event/ShopInventoryPreview.java b/src/main/java/org/maxgamer/quickshop/Event/ShopInventoryPreview.java new file mode 100644 index 000000000..ee4d916a6 --- /dev/null +++ b/src/main/java/org/maxgamer/quickshop/Event/ShopInventoryPreview.java @@ -0,0 +1,63 @@ +package org.maxgamer.quickshop.Event; + +import org.bukkit.entity.Player; +import org.bukkit.event.Cancellable; +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; +import org.bukkit.inventory.ItemStack; + +public class ShopInventoryPreview extends Event implements Cancellable { + + private static final HandlerList handlers = new HandlerList(); + private boolean cancelled; + private Player p; + private ItemStack itemStack; + + /** + * Build a event when player using GUI preview + * + * @param p Target plugin + * @param itemStack The preview item, with preview flag. + */ + public ShopInventoryPreview(Player p, ItemStack itemStack) { + this.p = p; + this.itemStack = itemStack; + } + + /** + * Get the preview ItemStack + * + * @return Itemstack for previewing + */ + public ItemStack getItemStack() { + return itemStack; + } + + /** + * The player who is preview item + * + * @return The player who is previewing + */ + public Player getPlayer() { + return p; + } + + @Override + public HandlerList getHandlers() { + return handlers; + } + + public static HandlerList getHandlerList() { + return handlers; + } + + @Override + public boolean isCancelled() { + return this.cancelled; + } + + @Override + public void setCancelled(boolean cancel) { + this.cancelled = cancel; + } +} diff --git a/src/main/java/org/maxgamer/quickshop/Shop/ShopPreCreateEvent.java b/src/main/java/org/maxgamer/quickshop/Event/ShopPreCreateEvent.java similarity index 97% rename from src/main/java/org/maxgamer/quickshop/Shop/ShopPreCreateEvent.java rename to src/main/java/org/maxgamer/quickshop/Event/ShopPreCreateEvent.java index 5c82b9c12..88db5d822 100644 --- a/src/main/java/org/maxgamer/quickshop/Shop/ShopPreCreateEvent.java +++ b/src/main/java/org/maxgamer/quickshop/Event/ShopPreCreateEvent.java @@ -1,4 +1,4 @@ -package org.maxgamer.quickshop.Shop; +package org.maxgamer.quickshop.Event; import org.bukkit.Location; import org.bukkit.entity.Player; diff --git a/src/main/java/org/maxgamer/quickshop/Shop/ShopPurchaseEvent.java b/src/main/java/org/maxgamer/quickshop/Event/ShopPurchaseEvent.java similarity index 90% rename from src/main/java/org/maxgamer/quickshop/Shop/ShopPurchaseEvent.java rename to src/main/java/org/maxgamer/quickshop/Event/ShopPurchaseEvent.java index e940febaf..ca94db798 100644 --- a/src/main/java/org/maxgamer/quickshop/Shop/ShopPurchaseEvent.java +++ b/src/main/java/org/maxgamer/quickshop/Event/ShopPurchaseEvent.java @@ -1,9 +1,10 @@ -package org.maxgamer.quickshop.Shop; +package org.maxgamer.quickshop.Event; import org.bukkit.entity.Player; import org.bukkit.event.Cancellable; import org.bukkit.event.Event; import org.bukkit.event.HandlerList; +import org.maxgamer.quickshop.Shop.Shop; public class ShopPurchaseEvent extends Event implements Cancellable { private static final HandlerList handlers = new HandlerList(); @@ -14,6 +15,7 @@ public class ShopPurchaseEvent extends Event implements Cancellable { /** * Builds a new shop purchase event + * This time, purchase not start, please listen the ShopSuccessPurchaseEvent. * * @param shop The shop bought from * @param p The player buying diff --git a/src/main/java/org/maxgamer/quickshop/Event/ShopSuccessPurchaseEvent.java b/src/main/java/org/maxgamer/quickshop/Event/ShopSuccessPurchaseEvent.java new file mode 100644 index 000000000..533aa2473 --- /dev/null +++ b/src/main/java/org/maxgamer/quickshop/Event/ShopSuccessPurchaseEvent.java @@ -0,0 +1,86 @@ +package org.maxgamer.quickshop.Event; + +import org.bukkit.entity.Player; +import org.bukkit.event.Cancellable; +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; +import org.maxgamer.quickshop.Shop.Shop; + +public class ShopSuccessPurchaseEvent extends Event implements Cancellable { + private static final HandlerList handlers = new HandlerList(); + private Shop shop; + private Player p; + private int amount; + private boolean cancelled; + private double tax; + + /** + * Builds a new shop purchase event + * This time, purchase not start, please listen the ShopSuccessPurchaseEvent. + * + * @param shop The shop bought from + * @param p The player buying + * @param amount The amount they're buying + */ + public ShopSuccessPurchaseEvent(Shop shop, Player p, int amount, double tax) { + this.shop = shop; + this.p = p; + this.amount = amount; + this.tax = tax; + } + + /** + * The shop used in this event + * + * @return The shop used in this event + */ + public Shop getShop() { + return this.shop; + } + + /** + * The player trading with the shop + * + * @return The player trading with the shop + */ + public Player getPlayer() { + return this.p; + } + + /** + * The amount the purchase was for + * + * @return The amount the purchase was for + */ + public int getAmount() { + return this.amount; + } + + /** + * Get the tax in this purchase + * + * @return Tax + */ + public double getTax() { + return tax; + } + + @Override + public HandlerList getHandlers() { + return handlers; + } + + public static HandlerList getHandlerList() { + return handlers; + } + + @Override + public boolean isCancelled() { + return this.cancelled; + } + + @Override + public void setCancelled(boolean cancel) { + this.cancelled = cancel; + } +} \ No newline at end of file diff --git a/src/main/java/org/maxgamer/quickshop/Shop/ShopUnloadEvent.java b/src/main/java/org/maxgamer/quickshop/Event/ShopUnloadEvent.java similarity index 88% rename from src/main/java/org/maxgamer/quickshop/Shop/ShopUnloadEvent.java rename to src/main/java/org/maxgamer/quickshop/Event/ShopUnloadEvent.java index 889fc125f..19a1558d0 100644 --- a/src/main/java/org/maxgamer/quickshop/Shop/ShopUnloadEvent.java +++ b/src/main/java/org/maxgamer/quickshop/Event/ShopUnloadEvent.java @@ -1,28 +1,29 @@ -package org.maxgamer.quickshop.Shop; - -import org.bukkit.event.Event; -import org.bukkit.event.HandlerList; - -/** Getting the unloading shop, Can't cancel. **/ -public class ShopUnloadEvent extends Event { - /* Getting the unloading shop, Can't cancel. **/ - public ShopUnloadEvent(Shop shop) { - this.shop = shop; - } - - private static final HandlerList handlers = new HandlerList(); - private Shop shop; - - public Shop getShop() { - return shop; - } - - @Override - public HandlerList getHandlers() { - return handlers; - } - - public static HandlerList getHandlerList() { - return handlers; - } -} +package org.maxgamer.quickshop.Event; + +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; +import org.maxgamer.quickshop.Shop.Shop; + +/** Getting the unloading shop, Can't cancel. **/ +public class ShopUnloadEvent extends Event { + /* Getting the unloading shop, Can't cancel. **/ + public ShopUnloadEvent(Shop shop) { + this.shop = shop; + } + + private static final HandlerList handlers = new HandlerList(); + private Shop shop; + + public Shop getShop() { + return shop; + } + + @Override + public HandlerList getHandlers() { + return handlers; + } + + public static HandlerList getHandlerList() { + return handlers; + } +} diff --git a/src/main/java/org/maxgamer/quickshop/Event/ShopUpdateEvent.java b/src/main/java/org/maxgamer/quickshop/Event/ShopUpdateEvent.java new file mode 100644 index 000000000..d0f0a80d6 --- /dev/null +++ b/src/main/java/org/maxgamer/quickshop/Event/ShopUpdateEvent.java @@ -0,0 +1,50 @@ +package org.maxgamer.quickshop.Event; + +import org.bukkit.event.Cancellable; +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; +import org.maxgamer.quickshop.Shop.Shop; + +public class ShopUpdateEvent extends Event implements Cancellable { + private static final HandlerList handlers = new HandlerList(); + private Shop shop; + private boolean cancelled; + + /** + * Call when shop is trying updated to database + * + * @param shop The shop bought from + */ + public ShopUpdateEvent(Shop shop) { + this.shop = shop; + } + + /** + * The shop used in this event + * + * @return The shop used in this event + */ + public Shop getShop() { + return this.shop; + } + + @Override + public HandlerList getHandlers() { + return handlers; + } + + public static HandlerList getHandlerList() { + return handlers; + } + + @Override + public boolean isCancelled() { + return this.cancelled; + } + + @Override + public void setCancelled(boolean cancel) { + this.cancelled = cancel; + } + +} diff --git a/src/main/java/org/maxgamer/quickshop/QuickShop.java b/src/main/java/org/maxgamer/quickshop/QuickShop.java index 9ef917f7e..6fc982e0e 100644 --- a/src/main/java/org/maxgamer/quickshop/QuickShop.java +++ b/src/main/java/org/maxgamer/quickshop/QuickShop.java @@ -667,7 +667,7 @@ public class QuickShop extends JavaPlugin { } if (selectedVersion == 27) { getConfig().set("queue.enable", true); - getConfig().set("queue.shops-per-tick", 5); + getConfig().set("queue.shops-per-tick", 20); getConfig().set("config-version", 28); selectedVersion = 28; saveConfig(); diff --git a/src/main/java/org/maxgamer/quickshop/Shop/ArmorStandDisplayItem.java b/src/main/java/org/maxgamer/quickshop/Shop/ArmorStandDisplayItem.java index 5e8fff915..0d2ef2a9d 100644 --- a/src/main/java/org/maxgamer/quickshop/Shop/ArmorStandDisplayItem.java +++ b/src/main/java/org/maxgamer/quickshop/Shop/ArmorStandDisplayItem.java @@ -10,6 +10,8 @@ import org.bukkit.entity.EntityType; import org.bukkit.inventory.ItemStack; import org.bukkit.util.EulerAngle; import org.jetbrains.annotations.*; +import org.maxgamer.quickshop.Event.ShopDisplayItemDespawnEvent; +import org.maxgamer.quickshop.Event.ShopDisplayItemSpawnEvent; import org.maxgamer.quickshop.QuickShop; import org.maxgamer.quickshop.Util.Util; @@ -103,6 +105,8 @@ public class ArmorStandDisplayItem implements DisplayItem { this.armorStand.remove(); this.armorStand = null; this.guardedIstack = null; + ShopDisplayItemDespawnEvent shopDisplayItemDespawnEvent = new ShopDisplayItemDespawnEvent(this.shop, this.originalItemStack); + Bukkit.getPluginManager().callEvent(shopDisplayItemDespawnEvent); } @Override diff --git a/src/main/java/org/maxgamer/quickshop/Shop/ContainerShop.java b/src/main/java/org/maxgamer/quickshop/Shop/ContainerShop.java index 4525ba61b..58719a572 100644 --- a/src/main/java/org/maxgamer/quickshop/Shop/ContainerShop.java +++ b/src/main/java/org/maxgamer/quickshop/Shop/ContainerShop.java @@ -22,6 +22,9 @@ import org.bukkit.inventory.InventoryHolder; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.Damageable; import org.jetbrains.annotations.*; +import org.maxgamer.quickshop.Event.ShopDeleteEvent; +import org.maxgamer.quickshop.Event.ShopUnloadEvent; +import org.maxgamer.quickshop.Event.ShopUpdateEvent; import org.maxgamer.quickshop.QuickShop; import org.maxgamer.quickshop.Util.MsgUtil; import org.maxgamer.quickshop.Util.Util; @@ -211,6 +214,13 @@ public class ContainerShop implements Shop { * Upates the shop into the database. */ public void update() { + + ShopUpdateEvent shopUpdateEvent = new ShopUpdateEvent(this); + if (shopUpdateEvent.isCancelled()) { + Util.debugLog("Shop update action was cancelled by a plugin."); + return; + } + int x = this.getLocation().getBlockX(); int y = this.getLocation().getBlockY(); int z = this.getLocation().getBlockZ(); @@ -639,6 +649,12 @@ public class ContainerShop implements Shop { * you are iterating* */ public void delete(boolean fromMemory) { + ShopDeleteEvent shopDeleteEvent = new ShopDeleteEvent(this, fromMemory); + Bukkit.getPluginManager().callEvent(shopDeleteEvent); + if (shopDeleteEvent.isCancelled()) { + Util.debugLog("Shop deltetion was cancelled cause a plugin cancel that."); + return; + } // Unload the shop if (isLoaded) this.onUnload(); @@ -755,12 +771,20 @@ public class ContainerShop implements Shop { this.displayItem = null; } this.isLoaded = false; + ShopUnloadEvent shopUnloadEvent = new ShopUnloadEvent(this); + Bukkit.getPluginManager().callEvent(shopUnloadEvent); } /** * Load ContainerShop. */ public void onLoad() { + ShopLoadEvent shopLoadEvent = new ShopLoadEvent(this); + Bukkit.getPluginManager().callEvent(shopLoadEvent); + if (shopLoadEvent.isCancelled()) { + return; + } + if (this.isLoaded) { Util.debugLog("Dupe load request, cancelled."); return; diff --git a/src/main/java/org/maxgamer/quickshop/Shop/InventoryPreview.java b/src/main/java/org/maxgamer/quickshop/Shop/InventoryPreview.java index d6634045c..03c3b6044 100644 --- a/src/main/java/org/maxgamer/quickshop/Shop/InventoryPreview.java +++ b/src/main/java/org/maxgamer/quickshop/Shop/InventoryPreview.java @@ -11,7 +11,9 @@ import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; import org.jetbrains.annotations.*; +import org.maxgamer.quickshop.Event.ShopInventoryPreview; import org.maxgamer.quickshop.Util.MsgUtil; +import org.maxgamer.quickshop.Util.Util; /** * A class to create a GUI item preview quickly @@ -52,10 +54,18 @@ public class InventoryPreview implements Listener { public void show() { if (inventory != null) // Not inited close(); + if (itemStack == null) // Null pointer exception + return; if (player == null) // Null pointer exception return; if (player.isSleeping()) // Bed bug return; + ShopInventoryPreview shopInventoryPreview = new ShopInventoryPreview(player, itemStack); + Bukkit.getPluginManager().callEvent(shopInventoryPreview); + if (shopInventoryPreview.isCancelled()) { + Util.debugLog("Inventory preview was cancelled by a plugin."); + return; + } final int size = 9; inventory = Bukkit.createInventory(null, size, MsgUtil.getMessage("menu.preview")); for (int i = 0; i < size; i++) { diff --git a/src/main/java/org/maxgamer/quickshop/Shop/RealDisplayItem.java b/src/main/java/org/maxgamer/quickshop/Shop/RealDisplayItem.java index 7a673652c..1bf222113 100644 --- a/src/main/java/org/maxgamer/quickshop/Shop/RealDisplayItem.java +++ b/src/main/java/org/maxgamer/quickshop/Shop/RealDisplayItem.java @@ -7,6 +7,7 @@ import org.bukkit.entity.Item; import org.bukkit.inventory.ItemStack; import org.bukkit.util.Vector; import org.jetbrains.annotations.*; +import org.maxgamer.quickshop.Event.ShopDisplayItemSpawnEvent; import org.maxgamer.quickshop.QuickShop; import org.maxgamer.quickshop.Util.Util; diff --git a/src/main/java/org/maxgamer/quickshop/Shop/ShopDisplayItemSpawnedEvent.java b/src/main/java/org/maxgamer/quickshop/Shop/ShopDisplayItemSpawnedEvent.java deleted file mode 100644 index ccf19e20c..000000000 --- a/src/main/java/org/maxgamer/quickshop/Shop/ShopDisplayItemSpawnedEvent.java +++ /dev/null @@ -1,64 +0,0 @@ -package org.maxgamer.quickshop.Shop; - -import org.bukkit.entity.Item; -import org.bukkit.event.Cancellable; -import org.bukkit.event.Event; -import org.bukkit.event.HandlerList; - -/** - * This event is called after the shop display item created - */ -public class ShopDisplayItemSpawnedEvent extends Event implements Cancellable { - private static final HandlerList handlers = new HandlerList(); - private boolean cancelled; - private Shop shop; - private Item item; - private boolean fakeItem; - - /* - * This event is called after the shop display item created - */ - public ShopDisplayItemSpawnedEvent(Shop shop, Item item) { - this.shop = shop; - this.item = item; - } - - /* - * This event is called after the shop display item created - */ - public ShopDisplayItemSpawnedEvent(Shop shop, Item item, boolean fakeItem) { - this.shop = shop; - this.item = item; - this.fakeItem = fakeItem; - } - - public Shop getShop() { - return shop; - } - - public Item getItem() { - return item; - } - - public boolean getFakeItem() {return fakeItem;} - - @Override - public HandlerList getHandlers() { - return handlers; - } - - public static HandlerList getHandlerList() { - return handlers; - } - - @Override - public boolean isCancelled() { - return this.cancelled; - } - - @Override - public void setCancelled(boolean cancel) { - this.cancelled = cancel; - } - -} diff --git a/src/main/java/org/maxgamer/quickshop/Shop/ShopManager.java b/src/main/java/org/maxgamer/quickshop/Shop/ShopManager.java index b595a2ac2..a9429b800 100644 --- a/src/main/java/org/maxgamer/quickshop/Shop/ShopManager.java +++ b/src/main/java/org/maxgamer/quickshop/Shop/ShopManager.java @@ -17,6 +17,10 @@ import org.bukkit.event.block.BlockBreakEvent; import org.bukkit.inventory.ItemStack; import org.bukkit.plugin.RegisteredListener; import org.jetbrains.annotations.*; +import org.maxgamer.quickshop.Event.ShopCreateEvent; +import org.maxgamer.quickshop.Event.ShopPreCreateEvent; +import org.maxgamer.quickshop.Event.ShopPurchaseEvent; +import org.maxgamer.quickshop.Event.ShopSuccessPurchaseEvent; import org.maxgamer.quickshop.QuickShop; import org.maxgamer.quickshop.Util.MsgUtil; import org.maxgamer.quickshop.Util.Util; @@ -174,12 +178,7 @@ public class ShopManager { * @param shop The shop to add */ private void addShop(@NotNull String world, @NotNull Shop shop) { - - ShopLoadEvent shopLoadEvent = new ShopLoadEvent(shop); - Bukkit.getPluginManager().callEvent(shopLoadEvent); - if (shopLoadEvent.isCancelled()) { - return; - } + shop.onLoad(); HashMap> inWorld = this.getShops().get(world); // There's no world storage yet. We need to create that hashmap. @@ -212,8 +211,7 @@ public class ShopManager { * @param shop The shop to remove */ public void removeShop(@NotNull Shop shop) { - ShopUnloadEvent shopUnloadEvent = new ShopUnloadEvent(shop); - Bukkit.getPluginManager().callEvent(shopUnloadEvent); + shop.onUnload(); Location loc = shop.getLocation(); String world = loc.getWorld().getName(); HashMap> inWorld = this.getShops().get(world); @@ -381,7 +379,7 @@ public class ShopManager { } @SuppressWarnings("deprecation") - private void actionBuy(@NotNull Player p, @NotNull HashMap actions2, @NotNull Info info, @NotNull String message, @NotNull Shop shop, @NotNull int amount) { + private void actionBuy(@NotNull Player p, @NotNull HashMap actions2, @NotNull Info info, @NotNull String message, @NotNull Shop shop, int amount) { int space = shop.getRemainingSpace(); if (space == -1) space = 10000; @@ -411,15 +409,16 @@ public class ShopManager { if (e.isCancelled()) return; // Cancelled // Money handling + double tax = plugin.getConfig().getDouble("tax"); + double total = amount * shop.getPrice(); if (!p.getUniqueId().equals(shop.getOwner())) { // Don't tax them if they're purchasing from // themselves. // Do charge an amount of tax though. - double tax = plugin.getConfig().getDouble("tax"); if (shop.getOwner().equals(p.getUniqueId())) { tax = 0; } - double total = amount * shop.getPrice(); + if (!shop.isUnlimited() || plugin.getConfig().getBoolean("shop.pay-unlimited-shop-owners")) { // Tries to check their balance nicely to see if // they can afford it. @@ -453,12 +452,14 @@ public class ShopManager { } shop.buy(p, amount); MsgUtil.sendSellSuccess(p, shop, amount); + ShopSuccessPurchaseEvent se = new ShopSuccessPurchaseEvent(shop, p, amount, tax * total); + Bukkit.getPluginManager().callEvent(se); plugin.log(p.getName() + " sold " + amount + " for " + (shop.getPrice() * amount) + " to " + shop.toString()); //shop.setSignText(); // Update the signs count } @SuppressWarnings("deprecation") - private void actionSell(@NotNull Player p, @NotNull HashMap actions2, @NotNull Info info, @NotNull String message, @NotNull Shop shop2, @NotNull int amount) { + private void actionSell(@NotNull Player p, @NotNull HashMap actions2, @NotNull Info info, @NotNull String message, @NotNull Shop shop2, int amount) { Shop shop = plugin.getShopManager().getShop(info.getLocation()); // It's not valid anymore if (shop == null || !Util.canBeShop(info.getLocation().getBlock())) { @@ -496,6 +497,8 @@ public class ShopManager { if (e.isCancelled()) return; // Cancelled // Money handling + double tax = plugin.getConfig().getDouble("tax"); + double total = amount * shop.getPrice(); if (!p.getUniqueId().equals(shop.getOwner())) { // Check their balance. Works with *most* economy // plugins* @@ -508,12 +511,9 @@ public class ShopManager { // Don't tax them if they're purchasing from // themselves. // Do charge an amount of tax though. - double tax = plugin.getConfig().getDouble("tax"); if (shop.getOwner().equals(p.getUniqueId())) { tax = 0; } - - double total = amount * shop.getPrice(); if (!plugin.getEcon().withdraw(p.getUniqueId(), total)) { p.sendMessage(MsgUtil .getMessage("you-cant-afford-to-buy", format(amount * shop.getPrice()), format(plugin.getEcon() @@ -550,6 +550,8 @@ public class ShopManager { } shop.sell(p, amount); MsgUtil.sendPurchaseSuccess(p, shop, amount); + ShopSuccessPurchaseEvent se = new ShopSuccessPurchaseEvent(shop, p, amount, tax * total); + Bukkit.getPluginManager().callEvent(se); plugin.log(p.getName() + " bought " + amount + " for " + (shop.getPrice() * amount) + " from " + shop.toString()); }