A lot new event

Former-commit-id: 63e1aa1a1fd8e80562e370aa0fcad54316e7c7e4 [formerly 0f9c9055b6ec769b6c2d83abc5b37d38367df37b]
Former-commit-id: 7082d9d792e09860885481cde758f0173f08a814
This commit is contained in:
Ghost_chu committed 2019-06-08 16:56:16 +08:00
1 parent 2ff2083555
commit 29958d6d3d
17 files changed
+493 -175

No files matched your search

@@ -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();
@@ -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;
}
}
@@ -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;
}
}
@@ -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;
}
}
@@ -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;
}
}
@@ -1,4 +1,4 @@
package org.maxgamer.quickshop.Shop;
package org.maxgamer.quickshop.Event;
import org.bukkit.Location;
import org.bukkit.entity.Player;
@@ -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
@@ -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;
}
}
@@ -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;
}
}
@@ -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;
}
}
@@ -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();
@@ -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
@@ -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;
@@ -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++) {
@@ -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;
@@ -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;
}
}
@@ -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<ShopChunk, HashMap<Location, Shop>> 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<ShopChunk, HashMap<Location, Shop>> inWorld = this.getShops().get(world);
@@ -381,7 +379,7 @@ public class ShopManager {
}
@SuppressWarnings("deprecation")
private void actionBuy(@NotNull Player p, @NotNull HashMap<UUID, Info> actions2, @NotNull Info info, @NotNull String message, @NotNull Shop shop, @NotNull int amount) {
private void actionBuy(@NotNull Player p, @NotNull HashMap<UUID, Info> 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<UUID, Info> actions2, @NotNull Info info, @NotNull String message, @NotNull Shop shop2, @NotNull int amount) {
private void actionSell(@NotNull Player p, @NotNull HashMap<UUID, Info> 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());
}