Skip to content

Commit

Permalink
Option to show available items/free space of shops
Browse files Browse the repository at this point in the history
  • Loading branch information
Brokkonaut committed Nov 12, 2018
1 parent a9bd710 commit 10ce119
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 7 deletions.
32 changes: 32 additions & 0 deletions src/main/java/com/Acrobot/Breeze/Utils/InventoryUtil.java
Expand Up @@ -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
*
Expand Down
4 changes: 2 additions & 2 deletions 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;
Expand Down Expand Up @@ -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);

Expand Down
11 changes: 8 additions & 3 deletions src/main/java/com/Acrobot/ChestShop/Configuration/Messages.java
Expand Up @@ -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!";
Expand Down Expand Up @@ -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!";

Expand All @@ -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;
}
}
Expand Up @@ -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;
Expand Down
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);

Expand All @@ -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);
}

Expand Down

0 comments on commit 10ce119

Please sign in to comment.