diff --git a/src/main/java/com/Acrobot/Breeze/Utils/InventoryUtil.java b/src/main/java/com/Acrobot/Breeze/Utils/InventoryUtil.java index 63f495460..12af56dfc 100644 --- a/src/main/java/com/Acrobot/Breeze/Utils/InventoryUtil.java +++ b/src/main/java/com/Acrobot/Breeze/Utils/InventoryUtil.java @@ -82,6 +82,38 @@ public static boolean hasItems(ItemStack[] items, Inventory inventory) { return true; } + /** + * Returns the free space for items of a certain type + * + * @param item + * Item to check + * @param inventory + * inventory + * @return free space for the item + */ + public static int getFreeSpace(ItemStack item, Inventory inventory) { + if (inventory.getType() == null) { + return Integer.MAX_VALUE; + } + + // Special case required because AdminInventory has no storage contents + if (inventory instanceof AdminInventory) { + return Integer.MAX_VALUE; + } + + int freeSpace = 0; + int maxStack = Math.max(item.getMaxStackSize(), 1); + for (ItemStack content : inventory.getStorageContents()) { + if (item.isSimilar(content)) { + freeSpace += Math.max(maxStack - content.getAmount(), 0); + } else if (MaterialUtil.isEmpty(content)) { + freeSpace += maxStack; + } + } + + return freeSpace; + } + /** * Checks if the item fits the inventory * diff --git a/src/main/java/com/Acrobot/ChestShop/Commands/ItemInfo.java b/src/main/java/com/Acrobot/ChestShop/Commands/ItemInfo.java index 1ed315e2d..101693e2c 100644 --- a/src/main/java/com/Acrobot/ChestShop/Commands/ItemInfo.java +++ b/src/main/java/com/Acrobot/ChestShop/Commands/ItemInfo.java @@ -1,6 +1,6 @@ package com.Acrobot.ChestShop.Commands; -import static com.Acrobot.ChestShop.Configuration.Messages.iteminfo; +import static com.Acrobot.ChestShop.Configuration.Messages.ITEM_INFO; import org.bukkit.ChatColor; import org.bukkit.command.Command; @@ -43,7 +43,7 @@ public boolean onCommand(CommandSender sender, Command cmd, String label, String } public static void showItemInfo(CommandSender sender, ItemStack item) { - sender.sendMessage(Messages.prefix(iteminfo)); + sender.sendMessage(Messages.prefix(ITEM_INFO)); String name = StringUtil.capitalizeFirstLetter(item.getType().name(), '_'); sender.sendMessage(" " + ChatColor.WHITE + name); diff --git a/src/main/java/com/Acrobot/ChestShop/Configuration/Messages.java b/src/main/java/com/Acrobot/ChestShop/Configuration/Messages.java index 4ba32f535..08e0732f8 100644 --- a/src/main/java/com/Acrobot/ChestShop/Configuration/Messages.java +++ b/src/main/java/com/Acrobot/ChestShop/Configuration/Messages.java @@ -8,8 +8,9 @@ * @author Acrobot */ public class Messages { - public static String prefix = ChatColor.GREEN + "[Shop] " + ChatColor.RESET; - public static String iteminfo = ChatColor.GREEN + "Item Information: " + ChatColor.RESET; + public static String PREFIX = ChatColor.GREEN + "[Shop] " + ChatColor.RESET; + public static String ITEM_INFO = ChatColor.GREEN + "Item Information: " + ChatColor.RESET; + public static String SHOP_INFO = ChatColor.GREEN + "Shop Information: " + ChatColor.RESET; @PrecededBySpace public static String ACCESS_DENIED = "You don't have permission to do that!"; @@ -57,6 +58,10 @@ public class Messages { public static String SHOP_REFUNDED = "You have been refunded %amount."; public static String ITEM_GIVEN = "Given %item to %player."; + @PrecededBySpace + public static String AVAILABLE_ITEMS = "%amount items are available in this shop."; + public static String AVAILABLE_SPACE = "There is free space for %amount items."; + @PrecededBySpace public static String RESTRICTED_SIGN_CREATED = "Sign successfully created!"; @@ -74,6 +79,6 @@ public class Messages { public static String TOGGLE_MESSAGES_ON = "You will now receive messages from your shop(s)."; public static String prefix(String message) { - return prefix + message; + return PREFIX + message; } } diff --git a/src/main/java/com/Acrobot/ChestShop/Configuration/Properties.java b/src/main/java/com/Acrobot/ChestShop/Configuration/Properties.java index fd44ef31f..5a7cb588e 100644 --- a/src/main/java/com/Acrobot/ChestShop/Configuration/Properties.java +++ b/src/main/java/com/Acrobot/ChestShop/Configuration/Properties.java @@ -82,6 +82,9 @@ public class Properties { @ConfigurationComment("Do you want to show \"Somebody bought/sold... \" messages?") public static boolean SHOW_TRANSACTION_INFORMATION_OWNER = true; + @ConfigurationComment("Do you want to show information about available items/free space of a shop?") + public static boolean SHOW_SHOP_INFORMATION_ON_SHIFT_CLICK = true; + @PrecededBySpace @ConfigurationComment("If true, plugin will log transactions in its own file") public static boolean LOG_TO_FILE = false; diff --git a/src/main/java/com/Acrobot/ChestShop/Listeners/Player/PlayerInteract.java b/src/main/java/com/Acrobot/ChestShop/Listeners/Player/PlayerInteract.java index 69a09d0db..b63bfa619 100644 --- a/src/main/java/com/Acrobot/ChestShop/Listeners/Player/PlayerInteract.java +++ b/src/main/java/com/Acrobot/ChestShop/Listeners/Player/PlayerInteract.java @@ -89,7 +89,7 @@ public static void onInteract(PlayerInteractEvent event) { if (event.getPlayer().isSneaking()) { if (action == LEFT_CLICK_BLOCK || action == RIGHT_CLICK_BLOCK) { - showSoldItem(player, sign); + showShopInfo(player, sign); } return; } @@ -127,7 +127,7 @@ public static void onInteract(PlayerInteractEvent event) { Bukkit.getPluginManager().callEvent(tEvent); } - private static void showSoldItem(Player player, Sign sign) { + private static void showShopInfo(Player player, Sign sign) { String material = sign.getLine(ITEM_LINE); ItemStack item = MaterialUtil.getItem(material); @@ -136,6 +136,24 @@ private static void showSoldItem(Player player, Sign sign) { return; } + if (Properties.SHOW_SHOP_INFORMATION_ON_SHIFT_CLICK) { + if (!ChestShopSign.isAdminShop(sign)) { + Chest chest = uBlock.findConnectedChest(sign); + if (chest != null) { + player.sendMessage(Messages.prefix(Messages.SHOP_INFO)); + String prices = sign.getLine(PRICE_LINE); + Inventory inventory = chest.getInventory(); + if (PriceUtil.getSellPrice(prices) != PriceUtil.NO_PRICE) { + int free = InventoryUtil.getFreeSpace(item, inventory); + player.sendMessage(" " + Messages.AVAILABLE_SPACE.replace("%amount", Integer.toString(free))); + } + if (PriceUtil.getBuyPrice(prices) != PriceUtil.NO_PRICE) { + int available = InventoryUtil.getAmount(item, inventory); + player.sendMessage(" " + Messages.AVAILABLE_ITEMS.replace("%amount", Integer.toString(available))); + } + } + } + } ItemInfo.showItemInfo(player, item); }