diff --git a/src/main/java/de/epiceric/shopchest/ShopCommand.java b/src/main/java/de/epiceric/shopchest/ShopCommand.java index e3a3e4e..f652e20 100644 --- a/src/main/java/de/epiceric/shopchest/ShopCommand.java +++ b/src/main/java/de/epiceric/shopchest/ShopCommand.java @@ -1,10 +1,7 @@ package de.epiceric.shopchest; 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.event.*; import de.epiceric.shopchest.language.LanguageUtils; import de.epiceric.shopchest.language.LocalizedMessage; import de.epiceric.shopchest.nms.JsonBuilder; @@ -62,6 +59,7 @@ class ShopCommand extends BukkitCommand { case "REMOVE": case "INFO": case "LIMITS": + case "OPEN": sender.sendMessage(ChatColor.RED + "Only players can use this command."); return true; } @@ -104,6 +102,9 @@ class ShopCommand extends BukkitCommand { p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.OCCUPIED_SHOP_SLOTS, new LocalizedMessage.ReplacedRegex(Regex.LIMIT, (limit < 0 ? "∞" : String.valueOf(limit))), new LocalizedMessage.ReplacedRegex(Regex.AMOUNT, String.valueOf(shopUtils.getShopAmount(p))))); + } else if (args[0].equalsIgnoreCase("open")) { + needsHelp = false; + open(p); } } @@ -474,6 +475,25 @@ class ShopCommand extends BukkitCommand { ClickType.setPlayerClickType(p, new ClickType(EnumClickType.INFO)); } + /** + * A given player opens a shop + * @param p The command executor + */ + private void open(Player p) { + plugin.debug(p.getName() + " wants to open a shop"); + + ShopPreOpenEvent event = new ShopPreOpenEvent(p); + Bukkit.getPluginManager().callEvent(event); + if (event.isCancelled()) { + plugin.debug("Shop pre open event cancelled"); + return; + } + + plugin.debug(p.getName() + " can now click a chest"); + p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.CLICK_CHEST_OPEN)); + ClickType.setPlayerClickType(p, new ClickType(EnumClickType.OPEN)); + } + /** * Sends the basic help message * @param sender {@link CommandSender} who will receive the message @@ -491,6 +511,7 @@ class ShopCommand extends BukkitCommand { sender.sendMessage(ChatColor.GREEN + "/" + plugin.getShopChestConfig().main_command_name + " remove - " + LanguageUtils.getMessage(LocalizedMessage.Message.COMMAND_DESC_REMOVE)); sender.sendMessage(ChatColor.GREEN + "/" + plugin.getShopChestConfig().main_command_name + " info - " + LanguageUtils.getMessage(LocalizedMessage.Message.COMMAND_DESC_INFO)); sender.sendMessage(ChatColor.GREEN + "/" + plugin.getShopChestConfig().main_command_name + " limits - " + LanguageUtils.getMessage(LocalizedMessage.Message.COMMAND_DESC_LIMITS)); + sender.sendMessage(ChatColor.GREEN + "/" + plugin.getShopChestConfig().main_command_name + " open - " + LanguageUtils.getMessage(LocalizedMessage.Message.COMMAND_DESC_OPEN)); } if (sender.hasPermission(Permissions.RELOAD)) { diff --git a/src/main/java/de/epiceric/shopchest/event/ShopBuySellEvent.java b/src/main/java/de/epiceric/shopchest/event/ShopBuySellEvent.java index b365c99..e47d2a1 100644 --- a/src/main/java/de/epiceric/shopchest/event/ShopBuySellEvent.java +++ b/src/main/java/de/epiceric/shopchest/event/ShopBuySellEvent.java @@ -8,26 +8,18 @@ import org.bukkit.event.Cancellable; * Called when a player buys or sells something from or to a shop */ public class ShopBuySellEvent extends ShopEvent implements Cancellable { - private Player player; - private Shop shop; private Type type; private int newAmount; private double newPrice; private boolean cancelled; public ShopBuySellEvent(Player player, Shop shop, Type type, int newAmount, double newPrice) { - this.player = player; - this.shop = shop; + super(player, shop); this.type = type; this.newAmount = newAmount; this.newPrice = newPrice; } - @Override - public Shop getShop() { - return shop; - } - /** * @return Whether the player buys or sells something */ @@ -49,11 +41,6 @@ public class ShopBuySellEvent extends ShopEvent implements Cancellable { return newPrice; } - @Override - public Player getPlayer() { - return player; - } - @Override public boolean isCancelled() { return cancelled; diff --git a/src/main/java/de/epiceric/shopchest/event/ShopCreateEvent.java b/src/main/java/de/epiceric/shopchest/event/ShopCreateEvent.java index 95f0a7a..13ce1bb 100644 --- a/src/main/java/de/epiceric/shopchest/event/ShopCreateEvent.java +++ b/src/main/java/de/epiceric/shopchest/event/ShopCreateEvent.java @@ -8,27 +8,13 @@ import org.bukkit.event.Cancellable; * Called when a player creates a shop (clicks on a chest) */ 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; + super(player, shop); this.creationPrice = creationPrice; } - - @Override - public Shop getShop() { - return shop; - } - - @Override - public Player getPlayer() { - return player; - } - /** * @return The price the player has to pay in order to create the shop (only if the event is not cancelled) */ diff --git a/src/main/java/de/epiceric/shopchest/event/ShopEvent.java b/src/main/java/de/epiceric/shopchest/event/ShopEvent.java index c1a5b1b..615b123 100644 --- a/src/main/java/de/epiceric/shopchest/event/ShopEvent.java +++ b/src/main/java/de/epiceric/shopchest/event/ShopEvent.java @@ -8,16 +8,27 @@ import org.bukkit.event.HandlerList; public abstract class ShopEvent extends Event { private static final HandlerList handlers = new HandlerList(); + private Shop shop; + private Player player; + + public ShopEvent(Player player, Shop shop) { + this.player = player; + this.shop = shop; + } /** * @return Shop which is involved in this event */ - public abstract Shop getShop(); + public Shop getShop() { + return shop; + } /** * @return Player who is involved in this event */ - public abstract Player getPlayer(); + public Player getPlayer() { + return player; + } public static HandlerList getHandlerList() { return handlers; diff --git a/src/main/java/de/epiceric/shopchest/event/ShopInfoEvent.java b/src/main/java/de/epiceric/shopchest/event/ShopInfoEvent.java index c427161..d86bcff 100644 --- a/src/main/java/de/epiceric/shopchest/event/ShopInfoEvent.java +++ b/src/main/java/de/epiceric/shopchest/event/ShopInfoEvent.java @@ -8,23 +8,10 @@ import org.bukkit.event.Cancellable; * Called when a player retrieves information about a shop (clicks on a chest) */ 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; + super(player, shop); } @Override diff --git a/src/main/java/de/epiceric/shopchest/event/ShopPreCreateEvent.java b/src/main/java/de/epiceric/shopchest/event/ShopPreCreateEvent.java index 7851488..18493fc 100644 --- a/src/main/java/de/epiceric/shopchest/event/ShopPreCreateEvent.java +++ b/src/main/java/de/epiceric/shopchest/event/ShopPreCreateEvent.java @@ -8,23 +8,10 @@ import org.bukkit.event.Cancellable; * Called when a player wants to create a shop (enters the command) */ 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; - } - - @Override - public Player getPlayer() { - return player; - } - - @Override - public Shop getShop() { - return shop; + super(player, shop); } @Override diff --git a/src/main/java/de/epiceric/shopchest/event/ShopRemoveEvent.java b/src/main/java/de/epiceric/shopchest/event/ShopRemoveEvent.java index 1e4c7d4..dc0a44e 100644 --- a/src/main/java/de/epiceric/shopchest/event/ShopRemoveEvent.java +++ b/src/main/java/de/epiceric/shopchest/event/ShopRemoveEvent.java @@ -8,23 +8,10 @@ import org.bukkit.event.Cancellable; * Called when a player removes a shop (clicks on a chest) */ 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; + super(player, shop); } @Override diff --git a/src/main/java/de/epiceric/shopchest/event/ShopUpdateEvent.java b/src/main/java/de/epiceric/shopchest/event/ShopUpdateEvent.java index 0f4b29d..939ce81 100644 --- a/src/main/java/de/epiceric/shopchest/event/ShopUpdateEvent.java +++ b/src/main/java/de/epiceric/shopchest/event/ShopUpdateEvent.java @@ -3,6 +3,10 @@ package de.epiceric.shopchest.event; import org.bukkit.event.Event; import org.bukkit.event.HandlerList; +/** + * Called when the shop updater runs
+ * It's not recommended to listen to this event! + */ public class ShopUpdateEvent extends Event { public enum UpdateQuality { diff --git a/src/main/java/de/epiceric/shopchest/listeners/ShopInteractListener.java b/src/main/java/de/epiceric/shopchest/listeners/ShopInteractListener.java index 8e5fcb0..0859f39 100644 --- a/src/main/java/de/epiceric/shopchest/listeners/ShopInteractListener.java +++ b/src/main/java/de/epiceric/shopchest/listeners/ShopInteractListener.java @@ -218,6 +218,27 @@ public class ShopInteractListener implements Listener { ClickType.removePlayerClickType(p); break; + case OPEN: + e.setCancelled(true); + + if (shopUtils.isShop(b.getLocation())) { + Shop shop = shopUtils.getShop(b.getLocation()); + if (p.getUniqueId().equals(shop.getVendor().getUniqueId()) || p.hasPermission(Permissions.OPEN_OTHER)) { + e.setCancelled(false); + if (!calledFromInteractEvent) { + p.openInventory(shop.getInventoryHolder().getInventory()); + } + } else { + p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.NO_PERMISSION_OPEN_OTHERS)); + plugin.debug(p.getName() + " is not permitted to open another player's shop"); + } + } else { + p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.CHEST_NO_SHOP)); + plugin.debug("Chest is not a shop"); + } + + ClickType.removePlayerClickType(p); + break; } } } else { @@ -230,35 +251,6 @@ public class ShopInteractListener implements Listener { } } - if (e.getAction() == Action.RIGHT_CLICK_BLOCK) { - if (p.isSneaking() || (shop.getShopType() != ShopType.ADMIN && shop.getVendor().getUniqueId().equals(p.getUniqueId()))) { - if (Utils.getPreferredItemInHand(p) == null) { - e.setCancelled(true); - if (!shop.getVendor().getUniqueId().equals(p.getUniqueId())) { - if (p.hasPermission(Permissions.OPEN_OTHER)) { - String vendorName = (shop.getVendor().getName() == null ? shop.getVendor().getUniqueId().toString() : shop.getVendor().getName()); - p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.OPENED_SHOP, new LocalizedMessage.ReplacedRegex(Regex.VENDOR, vendorName))); - plugin.debug(p.getName() + " is opening " + vendorName + "'s shop (#" + shop.getID() + ")"); - e.setCancelled(false); - if (!calledFromInteractEvent) { - p.openInventory(shop.getInventoryHolder().getInventory()); - } - } else { - p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.NO_PERMISSION_OPEN_OTHERS)); - plugin.debug(p.getName() + " is not permitted to open another player's shop"); - } - } else { - e.setCancelled(false); - if (!calledFromInteractEvent) { - p.openInventory(shop.getInventoryHolder().getInventory()); - } - } - } - - return; - } - } - if ((e.getAction() == Action.RIGHT_CLICK_BLOCK && !inverted) || (e.getAction() == Action.LEFT_CLICK_BLOCK && inverted)) { e.setCancelled(true);