From f653355cc20eae5415dfc67e5694c8b50caea88c Mon Sep 17 00:00:00 2001 From: Eric Date: Mon, 5 Jun 2017 15:02:49 +0200 Subject: [PATCH] Added way to retrieve shop info by clicking it with a specific item Closes #104 --- .../de/epiceric/shopchest/config/Config.java | 6 +++++ .../listeners/ShopInteractListener.java | 23 ++++++++++++++++--- .../epiceric/shopchest/utils/ItemUtils.java | 2 ++ src/main/resources/config.yml | 4 ++++ 4 files changed, 32 insertions(+), 3 deletions(-) diff --git a/src/main/java/de/epiceric/shopchest/config/Config.java b/src/main/java/de/epiceric/shopchest/config/Config.java index d7d3a31..e1bc2a2 100644 --- a/src/main/java/de/epiceric/shopchest/config/Config.java +++ b/src/main/java/de/epiceric/shopchest/config/Config.java @@ -4,7 +4,9 @@ import de.epiceric.shopchest.ShopChest; import de.epiceric.shopchest.event.ShopUpdateEvent; import de.epiceric.shopchest.language.LanguageUtils; import de.epiceric.shopchest.sql.Database; +import de.epiceric.shopchest.utils.ItemUtils; import org.bukkit.configuration.InvalidConfigurationException; +import org.bukkit.inventory.ItemStack; import java.io.BufferedReader; import java.io.File; @@ -25,6 +27,9 @@ public class Config { /** The quality of hologram and item updating (performance saving, or better quality) **/ public ShopUpdateEvent.UpdateQuality update_quality; + /** The item with which a player can click a shop to retrieve information **/ + public ItemStack shop_info_item; + /** The default value for the custom WorldGuard flag 'create-shop' **/ public boolean wg_allow_create_shop_default; @@ -334,6 +339,7 @@ public class Config { plugin.reloadConfig(); update_quality = ShopUpdateEvent.UpdateQuality.valueOf(plugin.getConfig().getString("update-quality")); + shop_info_item = ItemUtils.getItemStack(plugin.getConfig().getString("shop-info-item")); wg_allow_create_shop_default = plugin.getConfig().getBoolean("worldguard-default-flag-values.create-shop"); wg_allow_use_admin_shop_default = plugin.getConfig().getBoolean("worldguard-default-flag-values.use-admin-shop"); wg_allow_use_shop_default = plugin.getConfig().getBoolean("worldguard-default-flag-values.use-shop"); diff --git a/src/main/java/de/epiceric/shopchest/listeners/ShopInteractListener.java b/src/main/java/de/epiceric/shopchest/listeners/ShopInteractListener.java index 4ff9be6..6679a01 100644 --- a/src/main/java/de/epiceric/shopchest/listeners/ShopInteractListener.java +++ b/src/main/java/de/epiceric/shopchest/listeners/ShopInteractListener.java @@ -394,12 +394,29 @@ public class ShopInteractListener implements Listener { } } } else { - if (shopUtils.isShop(b.getLocation())) { - Shop shop = shopUtils.getShop(b.getLocation()); - + Shop shop = shopUtils.getShop(b.getLocation()); + if (shop != null) { if (e.getAction() == Action.LEFT_CLICK_BLOCK && p.isSneaking() && Utils.hasAxeInHand(p)) { return; } + ItemStack infoItem = config.shop_info_item; + if (infoItem != null) { + if (e.getAction() == Action.RIGHT_CLICK_BLOCK || e.getAction() == Action.LEFT_CLICK_BLOCK) { + ItemStack item = Utils.getItemInMainHand(p); + if (item == null || !(infoItem.getType() == item.getType() && infoItem.getDurability() == item.getDurability())) { + item = Utils.getItemInMainHand(p); + if (item != null && infoItem.getType() == item.getType() && infoItem.getDurability() == item.getDurability()) { + e.setCancelled(true); + info(p, shop); + return; + } + } else { + e.setCancelled(true); + info(p, shop); + return; + } + } + } if (e.getAction() == Action.RIGHT_CLICK_BLOCK && p.getUniqueId().equals(shop.getVendor().getUniqueId()) && shop.getShopType() != ShopType.ADMIN) { return; diff --git a/src/main/java/de/epiceric/shopchest/utils/ItemUtils.java b/src/main/java/de/epiceric/shopchest/utils/ItemUtils.java index 7436e4a..38bfa68 100644 --- a/src/main/java/de/epiceric/shopchest/utils/ItemUtils.java +++ b/src/main/java/de/epiceric/shopchest/utils/ItemUtils.java @@ -74,6 +74,8 @@ public class ItemUtils { * @return The de-serialized ItemStack or {@code null} if the serialized item is invalid */ public static ItemStack getItemStack(String item) { + if (item.trim().isEmpty()) return null; + if (item.contains(":")) { Material mat = Material.getMaterial(item.split(":")[0]); if (mat == null) return null; diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 885e415..18fdbfa 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -23,6 +23,10 @@ show-shop-items: true # may lead to TPS loss. update-quality: NORMAL +# Set the item with which a player can click a shop to retrieve information. +# You can set this to an empty string to disable this feature. +shop-info-item: "STICK" + # Set whether interaction with the hologram should be enabled. # If set to true, a player can do the exact same thing with the # hologram, as with the chest. You can even open the chest if you