diff --git a/Plugin/src/main/java/me/filoghost/chestcommands/api/impl/ConfigurableIconImpl.java b/Plugin/src/main/java/me/filoghost/chestcommands/api/impl/ConfigurableIconImpl.java index 1d39611..2bdc273 100644 --- a/Plugin/src/main/java/me/filoghost/chestcommands/api/impl/ConfigurableIconImpl.java +++ b/Plugin/src/main/java/me/filoghost/chestcommands/api/impl/ConfigurableIconImpl.java @@ -37,6 +37,8 @@ import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.inventory.meta.LeatherArmorMeta; import org.bukkit.inventory.meta.SkullMeta; +import com.google.common.base.Preconditions; + import me.filoghost.chestcommands.ChestCommands; import me.filoghost.chestcommands.api.ClickHandler; import me.filoghost.chestcommands.api.ClickResult; @@ -89,10 +91,8 @@ public class ConfigurableIconImpl implements ConfigurableIcon { @Override public void setAmount(int amount) { - if (amount < 1) amount = 1; - else if (amount > 127) amount = 127; - - this.amount = amount; + Preconditions.checkArgument(amount >= 1, "Amount must 1 or greater"); + this.amount = Math.min(amount, 127); } @Override @@ -102,8 +102,7 @@ public class ConfigurableIconImpl implements ConfigurableIcon { @Override public void setDurability(short durability) { - if (durability < 0) durability = 0; - + Preconditions.checkArgument(durability >= 0, "Durability must not be negative"); this.durability = durability; } diff --git a/Plugin/src/main/java/me/filoghost/chestcommands/command/CommandHandler.java b/Plugin/src/main/java/me/filoghost/chestcommands/command/CommandHandler.java index 70f086d..4a7b04a 100644 --- a/Plugin/src/main/java/me/filoghost/chestcommands/command/CommandHandler.java +++ b/Plugin/src/main/java/me/filoghost/chestcommands/command/CommandHandler.java @@ -107,8 +107,8 @@ public class CommandHandler extends CommandFramework { AdvancedIconMenu menu = menuManager.getMenuByFileName(menuName); CommandValidate.notNull(menu, "The menu \"" + menuName + "\" was not found."); - if (!sender.hasPermission(menu.getPermission())) { - menu.sendNoPermissionMessage(sender); + if (!sender.hasPermission(menu.getOpenPermission())) { + menu.sendNoOpenPermissionMessage(sender); return; } diff --git a/Plugin/src/main/java/me/filoghost/chestcommands/menu/AdvancedIconMenu.java b/Plugin/src/main/java/me/filoghost/chestcommands/menu/AdvancedIconMenu.java index c8ccd32..7b5572f 100644 --- a/Plugin/src/main/java/me/filoghost/chestcommands/menu/AdvancedIconMenu.java +++ b/Plugin/src/main/java/me/filoghost/chestcommands/menu/AdvancedIconMenu.java @@ -31,8 +31,8 @@ import me.filoghost.chestcommands.util.Utils; public class AdvancedIconMenu extends BaseIconMenu { private final String fileName; + private final String permission; - private String permission; private List openActions; private int refreshTicks; @@ -50,7 +50,7 @@ public class AdvancedIconMenu extends BaseIconMenu { this.openActions = Utils.nullableCopy(openAction); } - public String getPermission() { + public String getOpenPermission() { return permission; } @@ -82,7 +82,7 @@ public class AdvancedIconMenu extends BaseIconMenu { if (player.hasPermission(permission)) { open(player); } else { - sendNoPermissionMessage(player); + sendNoOpenPermissionMessage(player); } } @@ -95,28 +95,30 @@ public class AdvancedIconMenu extends BaseIconMenu { if (icon.hasViewPermission() || icon.hasVariables()) { // Then we have to refresh it - if (icon.canViewIcon(player)) { - - if (inventory.getItem(i) == null) { - ItemStack newItem = hideAttributes(icon.createItemStack(player)); - inventory.setItem(i, newItem); - } else { - // Performance, only update name and lore - ItemStack oldItem = hideAttributes(inventory.getItem(i)); - ItemMeta meta = oldItem.getItemMeta(); - meta.setDisplayName(icon.calculateName(player)); - meta.setLore(icon.calculateLore(player)); - oldItem.setItemMeta(meta); - } - - } else { - inventory.setItem(i, null); - } + refreshIcon(player, inventory, icon, i); } } } - public void sendNoPermissionMessage(CommandSender sender) { + private void refreshIcon(Player player, Inventory inventory, AdvancedIcon icon, int inventorySlot) { + if (icon.canViewIcon(player)) { + if (inventory.getItem(inventorySlot) == null) { + ItemStack newItem = hideAttributes(icon.createItemStack(player)); + inventory.setItem(inventorySlot, newItem); + } else { + // Performance, only update name and lore + ItemStack oldItem = hideAttributes(inventory.getItem(inventorySlot)); + ItemMeta meta = oldItem.getItemMeta(); + meta.setDisplayName(icon.calculateName(player)); + meta.setLore(icon.calculateLore(player)); + oldItem.setItemMeta(meta); + } + } else { + inventory.setItem(inventorySlot, null); + } + } + + public void sendNoOpenPermissionMessage(CommandSender sender) { String noPermMessage = ChestCommands.getLang().no_open_permission; if (noPermMessage != null && !noPermMessage.isEmpty()) { sender.sendMessage(noPermMessage.replace("{permission}", this.permission)); diff --git a/Plugin/src/main/java/me/filoghost/chestcommands/menu/MenuManager.java b/Plugin/src/main/java/me/filoghost/chestcommands/menu/MenuManager.java index 1d57452..dce645b 100644 --- a/Plugin/src/main/java/me/filoghost/chestcommands/menu/MenuManager.java +++ b/Plugin/src/main/java/me/filoghost/chestcommands/menu/MenuManager.java @@ -31,50 +31,49 @@ import me.filoghost.chestcommands.util.ErrorCollector; public class MenuManager { - private static Map fileNameToMenuMap; - private static Map commandsToMenuMap; - - private static Map openTriggers; + private static Map menusByFile; + private static Map menusByCommand; + private static Map menusByOpenTrigger; public MenuManager() { - fileNameToMenuMap = CaseInsensitiveMap.create(); - commandsToMenuMap = CaseInsensitiveMap.create(); - openTriggers = new HashMap<>(); + menusByFile = CaseInsensitiveMap.create(); + menusByCommand = CaseInsensitiveMap.create(); + menusByOpenTrigger = new HashMap<>(); } public void clear() { - fileNameToMenuMap.clear(); - commandsToMenuMap.clear(); - openTriggers.clear(); + menusByFile.clear(); + menusByCommand.clear(); + menusByOpenTrigger.clear(); } public AdvancedIconMenu getMenuByFileName(String fileName) { - return fileNameToMenuMap.get(fileName); + return menusByFile.get(fileName); } public void registerMenu(String fileName, Collection triggerCommands, AdvancedIconMenu menu, ErrorCollector errorCollector) { - if (fileNameToMenuMap.containsKey(fileName)) { + if (menusByFile.containsKey(fileName)) { errorCollector.addError("Two menus have the same file name \"" + fileName + "\" with different cases. There will be problems opening one of these two menus."); } - fileNameToMenuMap.put(fileName, menu); + menusByFile.put(fileName, menu); for (String triggerCommand : triggerCommands) { if (!triggerCommand.isEmpty()) { - if (commandsToMenuMap.containsKey(triggerCommand)) { - errorCollector.addError("The menus \"" + commandsToMenuMap.get(triggerCommand).getFileName() + "\" and \"" + fileName + "\" have the same command \"" + triggerCommand + "\". Only one will be opened."); + if (menusByCommand.containsKey(triggerCommand)) { + errorCollector.addError("The menus \"" + menusByCommand.get(triggerCommand).getFileName() + "\" and \"" + fileName + "\" have the same command \"" + triggerCommand + "\". Only one will be opened."); } - commandsToMenuMap.put(triggerCommand, menu); + menusByCommand.put(triggerCommand, menu); } } } public void registerTriggerItem(OpenTrigger openTrigger, AdvancedIconMenu menu) { - openTriggers.put(openTrigger, menu); + menusByOpenTrigger.put(openTrigger, menu); } public void openMenuByItem(Player player, ItemStack itemInHand, Action clickAction) { - openTriggers.forEach((openTrigger, menu) -> { + menusByOpenTrigger.forEach((openTrigger, menu) -> { if (openTrigger.matches(itemInHand, clickAction)) { menu.openCheckingPermission(player); } @@ -82,11 +81,11 @@ public class MenuManager { } public AdvancedIconMenu getMenuByCommand(String command) { - return commandsToMenuMap.get(command); + return menusByCommand.get(command); } public Collection getMenuFileNames() { - return Collections.unmodifiableCollection(fileNameToMenuMap.keySet()); + return Collections.unmodifiableCollection(menusByFile.keySet()); } diff --git a/Plugin/src/main/java/me/filoghost/chestcommands/menu/icon/AdvancedIcon.java b/Plugin/src/main/java/me/filoghost/chestcommands/menu/icon/AdvancedIcon.java index f29c8a6..a017422 100644 --- a/Plugin/src/main/java/me/filoghost/chestcommands/menu/icon/AdvancedIcon.java +++ b/Plugin/src/main/java/me/filoghost/chestcommands/menu/icon/AdvancedIcon.java @@ -94,7 +94,7 @@ public class AdvancedIcon extends ConfigurableIconImpl { } // If all requirements are satisfied, take their cost - boolean takenAllCosts = Requirement.takeAll(player, requiredPermission, requiredMoney, requiredExpLevel, requiredItems); + boolean takenAllCosts = Requirement.takeCostAll(player, requiredPermission, requiredMoney, requiredExpLevel, requiredItems); if (!takenAllCosts) { return closeOnClick; } diff --git a/Plugin/src/main/java/me/filoghost/chestcommands/menu/icon/PermissionChecker.java b/Plugin/src/main/java/me/filoghost/chestcommands/menu/icon/PermissionChecker.java index 9ad5f36..9fef005 100644 --- a/Plugin/src/main/java/me/filoghost/chestcommands/menu/icon/PermissionChecker.java +++ b/Plugin/src/main/java/me/filoghost/chestcommands/menu/icon/PermissionChecker.java @@ -51,7 +51,7 @@ public class PermissionChecker implements Requirement { @Override - public boolean hasRequirement(Player player) { + public boolean check(Player player) { if (hasPermission(player)) { return true; } else { diff --git a/Plugin/src/main/java/me/filoghost/chestcommands/menu/icon/RequiredExpLevel.java b/Plugin/src/main/java/me/filoghost/chestcommands/menu/icon/RequiredExpLevel.java index 3b8bf21..7be12e3 100644 --- a/Plugin/src/main/java/me/filoghost/chestcommands/menu/icon/RequiredExpLevel.java +++ b/Plugin/src/main/java/me/filoghost/chestcommands/menu/icon/RequiredExpLevel.java @@ -34,7 +34,7 @@ public class RequiredExpLevel implements Requirement { } @Override - public boolean hasRequirement(Player player) { + public boolean check(Player player) { if (player.getLevel() < levels) { player.sendMessage(ChestCommands.getLang().no_exp.replace("{levels}", Integer.toString(levels))); return false; diff --git a/Plugin/src/main/java/me/filoghost/chestcommands/menu/icon/RequiredItem.java b/Plugin/src/main/java/me/filoghost/chestcommands/menu/icon/RequiredItem.java index 37cbdc5..b979b52 100644 --- a/Plugin/src/main/java/me/filoghost/chestcommands/menu/icon/RequiredItem.java +++ b/Plugin/src/main/java/me/filoghost/chestcommands/menu/icon/RequiredItem.java @@ -22,8 +22,8 @@ import me.filoghost.chestcommands.util.Preconditions; public class RequiredItem { - private Material material; - private int amount; + private final Material material; + private final int amount; private short durability; private boolean isDurabilityRestrictive = false; @@ -57,16 +57,11 @@ public class RequiredItem { return isDurabilityRestrictive; } - private boolean isMatchingDurability(short data) { - if (!isDurabilityRestrictive) return true; - return data == this.durability; - } - public boolean isItemContainedIn(Inventory inventory) { int amountFound = 0; for (ItemStack item : inventory.getContents()) { - if (item != null && item.getType() == material && isMatchingDurability(item.getDurability())) { + if (isMatchingType(item)) { amountFound += item.getAmount(); } } @@ -78,18 +73,14 @@ public class RequiredItem { if (amount <= 0) { return true; } - - int itemsToTake = amount; //start from amount and decrease - + + int itemsToTake = amount; // Start from amount and decrease ItemStack[] contents = inventory.getContents(); - ItemStack current = null; - for (int i = 0; i < contents.length; i++) { + ItemStack current = contents[i]; - current = contents[i]; - - if (current != null && current.getType() == material && isMatchingDurability(current.getDurability())) { + if (isMatchingType(current)) { if (current.getAmount() > itemsToTake) { current.setAmount(current.getAmount() - itemsToTake); return true; @@ -105,4 +96,16 @@ public class RequiredItem { return false; } + + private boolean isMatchingType(ItemStack item) { + return item != null && item.getType() == material && isMatchingDurability(item.getDurability()); + } + + private boolean isMatchingDurability(short data) { + if (!isDurabilityRestrictive) { + return true; + } + return data == this.durability; + } + } diff --git a/Plugin/src/main/java/me/filoghost/chestcommands/menu/icon/RequiredItems.java b/Plugin/src/main/java/me/filoghost/chestcommands/menu/icon/RequiredItems.java index 59a034e..ed768a1 100644 --- a/Plugin/src/main/java/me/filoghost/chestcommands/menu/icon/RequiredItems.java +++ b/Plugin/src/main/java/me/filoghost/chestcommands/menu/icon/RequiredItems.java @@ -38,7 +38,7 @@ public class RequiredItems implements Requirement { } @Override - public boolean hasRequirement(Player player) { + public boolean check(Player player) { boolean missingItems = false; for (RequiredItem item : items) { diff --git a/Plugin/src/main/java/me/filoghost/chestcommands/menu/icon/RequiredMoney.java b/Plugin/src/main/java/me/filoghost/chestcommands/menu/icon/RequiredMoney.java index 082e768..38bfef3 100644 --- a/Plugin/src/main/java/me/filoghost/chestcommands/menu/icon/RequiredMoney.java +++ b/Plugin/src/main/java/me/filoghost/chestcommands/menu/icon/RequiredMoney.java @@ -36,7 +36,7 @@ public class RequiredMoney implements Requirement { } @Override - public boolean hasRequirement(Player player) { + public boolean check(Player player) { if (!EconomyBridge.hasValidEconomy()) { player.sendMessage(ChatColor.RED + "This action has a price, but Vault with a compatible economy plugin was not found. For security, the action has been blocked. Please inform the staff."); return false; diff --git a/Plugin/src/main/java/me/filoghost/chestcommands/menu/icon/Requirement.java b/Plugin/src/main/java/me/filoghost/chestcommands/menu/icon/Requirement.java index e589d9c..6631a8e 100644 --- a/Plugin/src/main/java/me/filoghost/chestcommands/menu/icon/Requirement.java +++ b/Plugin/src/main/java/me/filoghost/chestcommands/menu/icon/Requirement.java @@ -18,13 +18,13 @@ import org.bukkit.entity.Player; public interface Requirement { - boolean hasRequirement(Player player); + boolean check(Player player); boolean takeCost(Player player); public static boolean checkAll(Player player, Requirement... requirements) { for (Requirement requirement : requirements) { - if (requirement != null && !requirement.hasRequirement(player)) { + if (requirement != null && !requirement.check(player)) { return false; } } @@ -32,7 +32,7 @@ public interface Requirement { return true; } - public static boolean takeAll(Player player, Requirement... requirements) { + public static boolean takeCostAll(Player player, Requirement... requirements) { for (Requirement requirement : requirements) { if (requirement != null) { boolean success = requirement.takeCost(player); diff --git a/Plugin/src/main/java/me/filoghost/chestcommands/menu/settings/OpenTrigger.java b/Plugin/src/main/java/me/filoghost/chestcommands/menu/settings/OpenTrigger.java index fd8c23a..0d01487 100644 --- a/Plugin/src/main/java/me/filoghost/chestcommands/menu/settings/OpenTrigger.java +++ b/Plugin/src/main/java/me/filoghost/chestcommands/menu/settings/OpenTrigger.java @@ -22,8 +22,8 @@ import me.filoghost.chestcommands.util.Preconditions; public class OpenTrigger { - private Material material; - private ClickType clickType; + private final Material material; + private final ClickType clickType; private short durability; private boolean isRestrictiveDurability;