From f233c626e14ceec73d0cc83b102d446697f3165d Mon Sep 17 00:00:00 2001 From: Eric Date: Tue, 3 Jan 2017 15:35:35 +0100 Subject: [PATCH] Add way to invert mouse buttons Closes #35 --- .../de/epiceric/shopchest/config/Config.java | 10 + .../listeners/ShopInteractListener.java | 211 +++++++++--------- src/main/resources/config.yml | 8 +- 3 files changed, 119 insertions(+), 110 deletions(-) diff --git a/src/main/java/de/epiceric/shopchest/config/Config.java b/src/main/java/de/epiceric/shopchest/config/Config.java index df1ba71..af64538 100644 --- a/src/main/java/de/epiceric/shopchest/config/Config.java +++ b/src/main/java/de/epiceric/shopchest/config/Config.java @@ -117,6 +117,15 @@ public class Config { /** Whether the item amount should be calculated to fit the available money or inventory space **/ public boolean auto_calculate_item_amount; + /** + *

Whether the mouse buttons are inverted

+ * + * Default:
+ * Right-Click: Buy
+ * Left-Click: Sell + **/ + public boolean invert_mouse_buttons; + /** Amount the hologram should be lifted **/ public double two_line_hologram_lift; @@ -313,6 +322,7 @@ public class Config { append_potion_level_to_item_name = plugin.getConfig().getBoolean("append-potion-level-to-item-name"); show_shop_items = plugin.getConfig().getBoolean("show-shop-items"); remove_shop_on_error = plugin.getConfig().getBoolean("remove-shop-on-error"); + invert_mouse_buttons = plugin.getConfig().getBoolean("invert-mouse-buttons"); two_line_hologram_lift = plugin.getConfig().getDouble("two-line-hologram-lift"); maximal_distance = plugin.getConfig().getDouble("maximal-distance"); maximal_item_distance = plugin.getConfig().getDouble("maximal-item-distance"); diff --git a/src/main/java/de/epiceric/shopchest/listeners/ShopInteractListener.java b/src/main/java/de/epiceric/shopchest/listeners/ShopInteractListener.java index 32c8e4d..28a5f42 100644 --- a/src/main/java/de/epiceric/shopchest/listeners/ShopInteractListener.java +++ b/src/main/java/de/epiceric/shopchest/listeners/ShopInteractListener.java @@ -153,14 +153,12 @@ public class ShopInteractListener implements Listener { private void handleInteractEvent(PlayerInteractEvent e, boolean calledFromInteractEvent) { Block b = e.getClickedBlock(); Player p = e.getPlayer(); + boolean inverted = config.invert_mouse_buttons; if (e.getAction() == Action.RIGHT_CLICK_BLOCK || e.getAction() == Action.LEFT_CLICK_BLOCK) { - if (b.getType().equals(Material.CHEST) || b.getType().equals(Material.TRAPPED_CHEST)) { - - if (e.getAction() == Action.RIGHT_CLICK_BLOCK) { - - if (ClickType.getPlayerClickType(p) != null) { + if (ClickType.getPlayerClickType(p) != null) { + if (e.getAction() == Action.RIGHT_CLICK_BLOCK) { switch (ClickType.getPlayerClickType(p).getClickType()) { case INFO: @@ -199,13 +197,19 @@ public class ShopInteractListener implements Listener { break; } + } + } else { + if (shopUtils.isShop(b.getLocation())) { + Shop shop = shopUtils.getShop(b.getLocation()); - } else { - - if (shopUtils.isShop(b.getLocation())) { - Shop shop = shopUtils.getShop(b.getLocation()); - + if (e.getAction() == Action.LEFT_CLICK_BLOCK) { if (p.isSneaking()) { + return; + } + } + + 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())) { @@ -228,127 +232,116 @@ public class ShopInteractListener implements Listener { } } } - } else { - e.setCancelled(true); - if (shop.getShopType() == ShopType.ADMIN || !shop.getVendor().getUniqueId().equals(p.getUniqueId())) { - plugin.debug(p.getName() + " wants to buy"); - if (shop.getBuyPrice() > 0) { - if (p.hasPermission(Permissions.BUY)) { - boolean worldGuardAllowed = true; - if (shop.getShopType() == ShopType.ADMIN) { - if (plugin.hasWorldGuard() && config.enable_worldguard_integration) { - RegionContainer container = worldGuard.getRegionContainer(); - RegionQuery query = container.createQuery(); - worldGuardAllowed = query.testState(b.getLocation(), p, ShopFlag.USE_ADMIN_SHOP); - } + return; + } + } - if (worldGuardAllowed || p.hasPermission(Permissions.WORLDGUARD_BYPASS)) { + if ((e.getAction() == Action.RIGHT_CLICK_BLOCK && !inverted) || (e.getAction() == Action.LEFT_CLICK_BLOCK && inverted)) { + e.setCancelled(true); + + if (shop.getShopType() == ShopType.ADMIN || !shop.getVendor().getUniqueId().equals(p.getUniqueId())) { + plugin.debug(p.getName() + " wants to buy"); + + if (shop.getBuyPrice() > 0) { + if (p.hasPermission(Permissions.BUY)) { + boolean worldGuardAllowed = true; + + if (shop.getShopType() == ShopType.ADMIN) { + if (plugin.hasWorldGuard() && config.enable_worldguard_integration) { + RegionContainer container = worldGuard.getRegionContainer(); + RegionQuery query = container.createQuery(); + worldGuardAllowed = query.testState(b.getLocation(), p, ShopFlag.USE_ADMIN_SHOP); + } + + if (worldGuardAllowed || p.hasPermission(Permissions.WORLDGUARD_BYPASS)) { + buy(p, shop); + } else { + plugin.debug(p.getName() + " doesn't have worldguard permission"); + p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.NO_PERMISSION_WG_BUY)); + } + } else { + if (plugin.hasWorldGuard() && config.enable_worldguard_integration) { + RegionContainer container = worldGuard.getRegionContainer(); + RegionQuery query = container.createQuery(); + worldGuardAllowed = query.testState(b.getLocation(), p, ShopFlag.USE_SHOP); + } + + if (worldGuardAllowed || p.hasPermission(Permissions.WORLDGUARD_BYPASS)) { + Chest c = (Chest) b.getState(); + if (Utils.getAmount(c.getInventory(), shop.getProduct()) >= shop.getProduct().getAmount()) { buy(p, shop); } else { - plugin.debug(p.getName() + " doesn't have worldguard permission"); - p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.NO_PERMISSION_WG_BUY)); - } - } else { - if (plugin.hasWorldGuard() && config.enable_worldguard_integration) { - RegionContainer container = worldGuard.getRegionContainer(); - RegionQuery query = container.createQuery(); - worldGuardAllowed = query.testState(b.getLocation(), p, ShopFlag.USE_SHOP); - } - - if (worldGuardAllowed || p.hasPermission(Permissions.WORLDGUARD_BYPASS)) { - Chest c = (Chest) b.getState(); - if (Utils.getAmount(c.getInventory(), shop.getProduct()) >= shop.getProduct().getAmount()) { + if (config.auto_calculate_item_amount && Utils.getAmount(c.getInventory(), shop.getProduct()) > 0) { buy(p, shop); } else { - if (config.auto_calculate_item_amount && Utils.getAmount(c.getInventory(), shop.getProduct()) > 0) { - buy(p, shop); - } else { - p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.OUT_OF_STOCK)); - plugin.debug("Shop is out of stock"); - } + p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.OUT_OF_STOCK)); + plugin.debug("Shop is out of stock"); } + } + } else { + plugin.debug(p.getName() + " doesn't have worldguard permission"); + p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.NO_PERMISSION_WG_BUY)); + } + } + } else { + p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.NO_PERMISSION_BUY)); + plugin.debug(p.getName() + " is not permitted to buy"); + } + } else { + p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.BUYING_DISABLED)); + plugin.debug("Buying is disabled"); + } + } + + } else if ((e.getAction() == Action.LEFT_CLICK_BLOCK && !inverted) || (e.getAction() == Action.RIGHT_CLICK_BLOCK && inverted)) { + e.setCancelled(true); + + if ((shop.getShopType() == ShopType.ADMIN) || (!shop.getVendor().getUniqueId().equals(p.getUniqueId()))) { + plugin.debug(p.getName() + " wants to sell"); + + if (shop.getSellPrice() > 0) { + if (p.hasPermission(Permissions.SELL)) { + boolean worldGuardAllowed = true; + + if (plugin.hasWorldGuard() && config.enable_worldguard_integration) { + RegionContainer container = worldGuard.getRegionContainer(); + RegionQuery query = container.createQuery(); + + StateFlag flag = (shop.getShopType() == ShopType.ADMIN ? ShopFlag.USE_ADMIN_SHOP : ShopFlag.USE_SHOP); + worldGuardAllowed = query.testState(b.getLocation(), p, flag); + } + + if (worldGuardAllowed || p.hasPermission(Permissions.WORLDGUARD_BYPASS)) { + if (Utils.getAmount(p.getInventory(), shop.getProduct()) >= shop.getProduct().getAmount()) { + sell(p, shop); + } else { + if (config.auto_calculate_item_amount && Utils.getAmount(p.getInventory(), shop.getProduct()) > 0) { + sell(p, shop); } else { - plugin.debug(p.getName() + " doesn't have worldguard permission"); - p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.NO_PERMISSION_WG_BUY)); + p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.NOT_ENOUGH_ITEMS)); + plugin.debug(p.getName() + " doesn't have enough items"); } } } else { - p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.NO_PERMISSION_BUY)); - plugin.debug(p.getName() + " is not permitted to buy"); + plugin.debug(p.getName() + " doesn't have worldguard permission"); + p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.NO_PERMISSION_WG_SELL)); } } else { - p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.BUYING_DISABLED)); - plugin.debug("Buying is disabled"); + p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.NO_PERMISSION_SELL)); + plugin.debug(p.getName() + " is not permitted to sell"); } } else { - e.setCancelled(false); - if (!calledFromInteractEvent) { - p.openInventory(shop.getInventoryHolder().getInventory()); - } - } - } - } - } - - - } else if (e.getAction() == Action.LEFT_CLICK_BLOCK) { - - if (shopUtils.isShop(b.getLocation())) { - if (p.isSneaking()) - return; - - e.setCancelled(true); - Shop shop = shopUtils.getShop(b.getLocation()); - - if ((shop.getShopType() == ShopType.ADMIN) || (!shop.getVendor().getUniqueId().equals(p.getUniqueId()))) { - plugin.debug(p.getName() + " wants to sell"); - if (shop.getSellPrice() > 0) { - if (p.hasPermission(Permissions.SELL)) { - boolean worldGuardAllowed = true; - - if (plugin.hasWorldGuard() && config.enable_worldguard_integration) { - RegionContainer container = worldGuard.getRegionContainer(); - RegionQuery query = container.createQuery(); - - StateFlag flag = (shop.getShopType() == ShopType.ADMIN ? ShopFlag.USE_ADMIN_SHOP : ShopFlag.USE_SHOP); - worldGuardAllowed = query.testState(b.getLocation(), p, flag); - } - - if (worldGuardAllowed || p.hasPermission(Permissions.WORLDGUARD_BYPASS)) { - if (Utils.getAmount(p.getInventory(), shop.getProduct()) >= shop.getProduct().getAmount()) { - sell(p, shop); - } else { - if (config.auto_calculate_item_amount && Utils.getAmount(p.getInventory(), shop.getProduct()) > 0) { - sell(p, shop); - } else { - p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.NOT_ENOUGH_ITEMS)); - plugin.debug(p.getName() + " doesn't have enough items"); - } - } - } else { - plugin.debug(p.getName() + " doesn't have worldguard permission"); - p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.NO_PERMISSION_WG_SELL)); - } - } else { - p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.NO_PERMISSION_SELL)); - plugin.debug(p.getName() + " is not permitted to sell"); + p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.SELLING_DISABLED)); + plugin.debug("Selling is disabled"); } } else { - p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.SELLING_DISABLED)); - plugin.debug("Selling is disabled"); + e.setCancelled(false); } - } else { - e.setCancelled(false); } } - } - } - - } else { - ClickType.removePlayerClickType(p); } } diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 4625c02..b61c399 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -82,10 +82,16 @@ append-potion-level-to-item-name: false # This might be useful if you're removing shop chests with WorldEdit, resetting plots, or similar remove-shop-on-error: false +# Set whether the mouse buttons should be inverted. +# Default: +# Right-Click -> Buy +# Left-Click -> Sell +invert-mouse-buttons: false + # Set the maximal distance (in blocks) to the shop where the player can see the hologram. maximal-distance: 2 -# Set the maximal distance (in blocks) to the shop where the player can see the floatig shop item. +# Set the maximal distance (in blocks) to the shop where the player can see the floating shop item. maximal-item-distance: 40 # Set the time in seconds between automatic shop reloads.