diff --git a/ShopChest/src/main/java/de/epiceric/shopchest/Commands.java b/ShopChest/src/main/java/de/epiceric/shopchest/Commands.java index 7ee7ae3..7e6d169 100644 --- a/ShopChest/src/main/java/de/epiceric/shopchest/Commands.java +++ b/ShopChest/src/main/java/de/epiceric/shopchest/Commands.java @@ -2,9 +2,14 @@ package de.epiceric.shopchest; import de.epiceric.shopchest.config.Config; import de.epiceric.shopchest.config.Regex; +import de.epiceric.shopchest.event.ShopPreCreateEvent; +import de.epiceric.shopchest.event.ShopPreInfoEvent; +import de.epiceric.shopchest.event.ShopPreRemoveEvent; +import de.epiceric.shopchest.event.ShopReloadEvent; import de.epiceric.shopchest.language.LanguageUtils; import de.epiceric.shopchest.language.LocalizedMessage; import de.epiceric.shopchest.nms.IJsonBuilder; +import de.epiceric.shopchest.shop.Shop; import de.epiceric.shopchest.shop.Shop.ShopType; import de.epiceric.shopchest.utils.ClickType; import de.epiceric.shopchest.utils.ClickType.EnumClickType; @@ -204,6 +209,10 @@ public class Commands extends BukkitCommand { * @param player The command executor */ private void reload(Player player) { + ShopReloadEvent event = new ShopReloadEvent(player); + Bukkit.getPluginManager().callEvent(event); + if (event.isCancelled()) return; + player.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.RELOADED_SHOPS, new LocalizedMessage.ReplacedRegex(Regex.AMOUNT, String.valueOf(ShopUtils.reloadShops())))); } @@ -320,9 +329,13 @@ public class Commands extends BukkitCommand { } } - ClickType.setPlayerClickType(p, new ClickType(EnumClickType.CREATE, itemStack, buyPrice, sellPrice, shopType)); - p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.CLICK_CHEST_CREATE)); + ShopPreCreateEvent event = new ShopPreCreateEvent(p, Shop.createImaginaryShop(p, itemStack, buyPrice, sellPrice, shopType)); + Bukkit.getPluginManager().callEvent(event); + if (!event.isCancelled()) { + ClickType.setPlayerClickType(p, new ClickType(EnumClickType.CREATE, itemStack, buyPrice, sellPrice, shopType)); + p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.CLICK_CHEST_CREATE)); + } } /** @@ -330,6 +343,10 @@ public class Commands extends BukkitCommand { * @param p The command executor */ private void remove(Player p) { + ShopPreRemoveEvent event = new ShopPreRemoveEvent(p); + Bukkit.getPluginManager().callEvent(event); + if (event.isCancelled()) return; + p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.CLICK_CHEST_REMOVE)); ClickType.setPlayerClickType(p, new ClickType(EnumClickType.REMOVE)); } @@ -339,6 +356,10 @@ public class Commands extends BukkitCommand { * @param p The command executor */ private void info(Player p) { + ShopPreInfoEvent event = new ShopPreInfoEvent(p); + Bukkit.getPluginManager().callEvent(event); + if (event.isCancelled()) return; + p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.CLICK_CHEST_INFO)); ClickType.setPlayerClickType(p, new ClickType(EnumClickType.INFO)); } diff --git a/ShopChest/src/main/java/de/epiceric/shopchest/event/ShopBuySellEvent.java b/ShopChest/src/main/java/de/epiceric/shopchest/event/ShopBuySellEvent.java new file mode 100644 index 0000000..9cfb7a9 --- /dev/null +++ b/ShopChest/src/main/java/de/epiceric/shopchest/event/ShopBuySellEvent.java @@ -0,0 +1,46 @@ +package de.epiceric.shopchest.event; + +import de.epiceric.shopchest.shop.Shop; +import org.bukkit.entity.Player; +import org.bukkit.event.Cancellable; + +public class ShopBuySellEvent extends ShopEvent implements Cancellable { + private Player player; + private Shop shop; + private Type type; + private boolean cancelled; + + public ShopBuySellEvent(Player player, Shop shop, Type type) { + this.player = player; + this.shop = shop; + } + + @Override + public Shop getShop() { + return shop; + } + + public Type getType() { + return type; + } + + @Override + public Player getPlayer() { + return player; + } + + @Override + public boolean isCancelled() { + return cancelled; + } + + @Override + public void setCancelled(boolean b) { + cancelled = b; + } + + public enum Type { + BUY, + SELL; + } +} diff --git a/ShopChest/src/main/java/de/epiceric/shopchest/event/ShopCreateEvent.java b/ShopChest/src/main/java/de/epiceric/shopchest/event/ShopCreateEvent.java new file mode 100644 index 0000000..4678e20 --- /dev/null +++ b/ShopChest/src/main/java/de/epiceric/shopchest/event/ShopCreateEvent.java @@ -0,0 +1,43 @@ +package de.epiceric.shopchest.event; + +import de.epiceric.shopchest.shop.Shop; +import org.bukkit.entity.Player; +import org.bukkit.event.Cancellable; + +public class ShopCreateEvent extends ShopEvent implements Cancellable{ + private Player player; + private Shop shop; + private double creationPrice; + private boolean cancelled; + + public ShopCreateEvent(Player player, Shop shop, double creationPrice) { + this.player = player; + this.shop = shop; + this.creationPrice = creationPrice; + } + + @Override + public Shop getShop() { + return shop; + } + + @Override + public Player getPlayer() { + return player; + } + + public double getCreationPrice() { + return creationPrice; + } + + @Override + public boolean isCancelled() { + return cancelled; + } + + @Override + public void setCancelled(boolean b) { + cancelled = b; + } + +} diff --git a/ShopChest/src/main/java/de/epiceric/shopchest/event/ShopEvent.java b/ShopChest/src/main/java/de/epiceric/shopchest/event/ShopEvent.java new file mode 100644 index 0000000..96b1284 --- /dev/null +++ b/ShopChest/src/main/java/de/epiceric/shopchest/event/ShopEvent.java @@ -0,0 +1,21 @@ +package de.epiceric.shopchest.event; + +import de.epiceric.shopchest.shop.Shop; +import org.bukkit.entity.Player; +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; + +public abstract class ShopEvent extends Event { + + private static final HandlerList handlers = new HandlerList(); + + public abstract Shop getShop(); + + public abstract Player getPlayer(); + + @Override + public HandlerList getHandlers() { + return handlers; + } + +} diff --git a/ShopChest/src/main/java/de/epiceric/shopchest/event/ShopInfoEvent.java b/ShopChest/src/main/java/de/epiceric/shopchest/event/ShopInfoEvent.java new file mode 100644 index 0000000..6a654e2 --- /dev/null +++ b/ShopChest/src/main/java/de/epiceric/shopchest/event/ShopInfoEvent.java @@ -0,0 +1,36 @@ +package de.epiceric.shopchest.event; + +import de.epiceric.shopchest.shop.Shop; +import org.bukkit.entity.Player; +import org.bukkit.event.Cancellable; + +public class ShopInfoEvent extends ShopEvent implements Cancellable { + private Player player; + private Shop shop; + private boolean cancelled; + + public ShopInfoEvent(Player player, Shop shop) { + this.player = player; + this.shop = shop; + } + + @Override + public Shop getShop() { + return shop; + } + + @Override + public Player getPlayer() { + return player; + } + + @Override + public boolean isCancelled() { + return cancelled; + } + + @Override + public void setCancelled(boolean b) { + cancelled = b; + } +} diff --git a/ShopChest/src/main/java/de/epiceric/shopchest/event/ShopPreCreateEvent.java b/ShopChest/src/main/java/de/epiceric/shopchest/event/ShopPreCreateEvent.java new file mode 100644 index 0000000..4dcb897 --- /dev/null +++ b/ShopChest/src/main/java/de/epiceric/shopchest/event/ShopPreCreateEvent.java @@ -0,0 +1,35 @@ +package de.epiceric.shopchest.event; + +import de.epiceric.shopchest.shop.Shop; +import org.bukkit.entity.Player; +import org.bukkit.event.Cancellable; + +public class ShopPreCreateEvent extends ShopEvent implements Cancellable { + private Player player; + private Shop shop; + private boolean cancelled; + + public ShopPreCreateEvent(Player player, Shop shop) { + this.player = player; + this.shop = shop; + } + + public Player getPlayer() { + return player; + } + + @Override + public Shop getShop() { + return shop; + } + + @Override + public boolean isCancelled() { + return cancelled; + } + + @Override + public void setCancelled(boolean b) { + cancelled = b; + } +} diff --git a/ShopChest/src/main/java/de/epiceric/shopchest/event/ShopPreInfoEvent.java b/ShopChest/src/main/java/de/epiceric/shopchest/event/ShopPreInfoEvent.java new file mode 100644 index 0000000..dc64af5 --- /dev/null +++ b/ShopChest/src/main/java/de/epiceric/shopchest/event/ShopPreInfoEvent.java @@ -0,0 +1,32 @@ +package de.epiceric.shopchest.event; + +import org.bukkit.entity.Player; +import org.bukkit.event.Cancellable; +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; + +public class ShopPreInfoEvent extends Event implements Cancellable { + private static final HandlerList handlers = new HandlerList(); + + private Player player; + private boolean cancelled; + + public ShopPreInfoEvent(Player player) { + this.player = player; + } + + @Override + public boolean isCancelled() { + return cancelled; + } + + @Override + public void setCancelled(boolean b) { + this.cancelled = b; + } + + @Override + public HandlerList getHandlers() { + return handlers; + } +} diff --git a/ShopChest/src/main/java/de/epiceric/shopchest/event/ShopPreRemoveEvent.java b/ShopChest/src/main/java/de/epiceric/shopchest/event/ShopPreRemoveEvent.java new file mode 100644 index 0000000..6f67a76 --- /dev/null +++ b/ShopChest/src/main/java/de/epiceric/shopchest/event/ShopPreRemoveEvent.java @@ -0,0 +1,36 @@ +package de.epiceric.shopchest.event; + +import org.bukkit.entity.Player; +import org.bukkit.event.Cancellable; +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; + +public class ShopPreRemoveEvent extends Event implements Cancellable { + private static final HandlerList handlers = new HandlerList(); + + private Player player; + private boolean cancelled; + + public ShopPreRemoveEvent(Player player) { + this.player = player; + } + + public Player getPlayer() { + return player; + } + + @Override + public boolean isCancelled() { + return cancelled; + } + + @Override + public void setCancelled(boolean cancelled) { + this.cancelled = cancelled; + } + + @Override + public HandlerList getHandlers() { + return handlers; + } +} diff --git a/ShopChest/src/main/java/de/epiceric/shopchest/event/ShopReloadEvent.java b/ShopChest/src/main/java/de/epiceric/shopchest/event/ShopReloadEvent.java new file mode 100644 index 0000000..49b04a2 --- /dev/null +++ b/ShopChest/src/main/java/de/epiceric/shopchest/event/ShopReloadEvent.java @@ -0,0 +1,36 @@ +package de.epiceric.shopchest.event; + +import org.bukkit.entity.Player; +import org.bukkit.event.Cancellable; +import org.bukkit.event.Event; +import org.bukkit.event.HandlerList; + +public class ShopReloadEvent extends Event implements Cancellable { + private static final HandlerList handlers = new HandlerList(); + + private Player player; + private boolean cancelled; + + public ShopReloadEvent(Player player) { + this.player = player; + } + + public Player getPlayer() { + return player; + } + + @Override + public HandlerList getHandlers() { + return handlers; + } + + @Override + public boolean isCancelled() { + return cancelled; + } + + @Override + public void setCancelled(boolean b) { + cancelled = b; + } +} diff --git a/ShopChest/src/main/java/de/epiceric/shopchest/event/ShopRemoveEvent.java b/ShopChest/src/main/java/de/epiceric/shopchest/event/ShopRemoveEvent.java new file mode 100644 index 0000000..1f7afb1 --- /dev/null +++ b/ShopChest/src/main/java/de/epiceric/shopchest/event/ShopRemoveEvent.java @@ -0,0 +1,36 @@ +package de.epiceric.shopchest.event; + +import de.epiceric.shopchest.shop.Shop; +import org.bukkit.entity.Player; +import org.bukkit.event.Cancellable; + +public class ShopRemoveEvent extends ShopEvent implements Cancellable { + private Player player; + private Shop shop; + private boolean cancelled; + + public ShopRemoveEvent(Player player, Shop shop) { + this.player = player; + this.shop = shop; + } + + @Override + public Shop getShop() { + return shop; + } + + @Override + public Player getPlayer() { + return player; + } + + @Override + public boolean isCancelled() { + return cancelled; + } + + @Override + public void setCancelled(boolean b) { + cancelled = b; + } +} diff --git a/ShopChest/src/main/java/de/epiceric/shopchest/listeners/ShopInteractListener.java b/ShopChest/src/main/java/de/epiceric/shopchest/listeners/ShopInteractListener.java index 3334f4c..1557a34 100644 --- a/ShopChest/src/main/java/de/epiceric/shopchest/listeners/ShopInteractListener.java +++ b/ShopChest/src/main/java/de/epiceric/shopchest/listeners/ShopInteractListener.java @@ -5,6 +5,10 @@ import com.griefcraft.model.Protection; import de.epiceric.shopchest.ShopChest; import de.epiceric.shopchest.config.Config; import de.epiceric.shopchest.config.Regex; +import de.epiceric.shopchest.event.ShopBuySellEvent; +import de.epiceric.shopchest.event.ShopCreateEvent; +import de.epiceric.shopchest.event.ShopInfoEvent; +import de.epiceric.shopchest.event.ShopRemoveEvent; import de.epiceric.shopchest.language.LanguageUtils; import de.epiceric.shopchest.language.LocalizedMessage; import de.epiceric.shopchest.shop.Shop; @@ -248,6 +252,12 @@ public class ShopInteractListener implements Listener { } double creationPrice = (shopType == ShopType.NORMAL) ? Config.shop_creation_price_normal : Config.shop_creation_price_admin; + + ShopCreateEvent event = new ShopCreateEvent(executor, Shop.createImaginaryShop(executor, product, buyPrice, sellPrice,shopType), creationPrice); + Bukkit.getPluginManager().callEvent(event); + + if (event.isCancelled()) return; + EconomyResponse r = plugin.getEconomy().withdrawPlayer(executor, creationPrice); if (!r.transactionSuccess()) { executor.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.ERROR_OCCURRED, new LocalizedMessage.ReplacedRegex(Regex.ERROR, r.errorMessage))); @@ -271,6 +281,10 @@ public class ShopInteractListener implements Listener { * @param shop Shop to be removed */ private void remove(Player executor, Shop shop) { + ShopRemoveEvent event = new ShopRemoveEvent(executor, shop); + Bukkit.getPluginManager().callEvent(event); + if (event.isCancelled()) return; + ShopUtils.removeShop(shop, true); executor.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.SHOP_REMOVED)); } @@ -281,6 +295,10 @@ public class ShopInteractListener implements Listener { * @param shop Shop from which the information will be retrieved */ private void info(Player executor, Shop shop) { + ShopInfoEvent event = new ShopInfoEvent(executor, shop); + Bukkit.getPluginManager().callEvent(event); + if (event.isCancelled()) return; + Chest c = (Chest) shop.getLocation().getBlock().getState(); int amount = Utils.getAmount(c.getInventory(), shop.getProduct()); @@ -402,6 +420,15 @@ public class ShopInteractListener implements Listener { if (r.transactionSuccess()) { if (r2 != null) { if (r2.transactionSuccess()) { + ShopBuySellEvent event = new ShopBuySellEvent(executor, shop, ShopBuySellEvent.Type.BUY); + Bukkit.getPluginManager().callEvent(event); + + if (event.isCancelled()) { + econ.depositPlayer(executor, shop.getBuyPrice()); + econ.withdrawPlayer(shop.getVendor(), shop.getBuyPrice()); + return; + } + addToInventory(inventory, product); removeFromInventory(c.getInventory(), product); executor.updateInventory(); @@ -419,6 +446,14 @@ public class ShopInteractListener implements Listener { executor.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.ERROR_OCCURRED, new LocalizedMessage.ReplacedRegex(Regex.ERROR, r2.errorMessage))); } } else { + ShopBuySellEvent event = new ShopBuySellEvent(executor, shop, ShopBuySellEvent.Type.BUY); + Bukkit.getPluginManager().callEvent(event); + + if (event.isCancelled()) { + econ.depositPlayer(executor, shop.getBuyPrice()); + return; + } + addToInventory(inventory, product); executor.updateInventory(); executor.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.BUY_SUCESS_ADMIN, new LocalizedMessage.ReplacedRegex(Regex.AMOUNT, String.valueOf(product.getAmount())), @@ -476,6 +511,15 @@ public class ShopInteractListener implements Listener { if (r.transactionSuccess()) { if (r2 != null) { if (r2.transactionSuccess()) { + ShopBuySellEvent event = new ShopBuySellEvent(executor, shop, ShopBuySellEvent.Type.SELL); + Bukkit.getPluginManager().callEvent(event); + + if (event.isCancelled()) { + econ.withdrawPlayer(executor, shop.getBuyPrice()); + econ.depositPlayer(shop.getVendor(), shop.getBuyPrice()); + return; + } + addToInventory(inventory, product); removeFromInventory(executor.getInventory(), product); executor.updateInventory(); @@ -494,6 +538,14 @@ public class ShopInteractListener implements Listener { } } else { + ShopBuySellEvent event = new ShopBuySellEvent(executor, shop, ShopBuySellEvent.Type.SELL); + Bukkit.getPluginManager().callEvent(event); + + if (event.isCancelled()) { + econ.withdrawPlayer(executor, shop.getBuyPrice()); + return; + } + removeFromInventory(executor.getInventory(), product); executor.updateInventory(); executor.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.SELL_SUCESS_ADMIN, new LocalizedMessage.ReplacedRegex(Regex.AMOUNT, String.valueOf(product.getAmount())), diff --git a/ShopChest/src/main/java/de/epiceric/shopchest/shop/Shop.java b/ShopChest/src/main/java/de/epiceric/shopchest/shop/Shop.java index 42dccab..5728e35 100644 --- a/ShopChest/src/main/java/de/epiceric/shopchest/shop/Shop.java +++ b/ShopChest/src/main/java/de/epiceric/shopchest/shop/Shop.java @@ -63,6 +63,16 @@ public class Shop { if (item == null || item.isDead()) createItem(); } + private Shop(OfflinePlayer vendor, ItemStack product, double buyPrice, double sellPrice, ShopType shopType) { + this.id = 0; + this.vendor = vendor; + this.product = product; + this.location = null; + this.buyPrice = buyPrice; + this.sellPrice = sellPrice; + this.shopType = shopType; + } + /** * Creates the hologram of the shop if it doesn't exist */ @@ -262,6 +272,13 @@ public class Shop { return chest; } + /** + * @return A shop, which is not really a shop. It's just for "storing" the data (used in some events). + */ + public static Shop createImaginaryShop(OfflinePlayer vendor, ItemStack product, double buyPrice, double sellPrice, ShopType shopType) { + return new Shop(vendor, product, buyPrice, sellPrice, shopType); + } + public enum ShopType { NORMAL, ADMIN