Refactoring

This commit is contained in:
filoghost 2020-06-14 13:43:07 +02:00
parent c31216f569
commit b497889650
12 changed files with 78 additions and 75 deletions

View File

@ -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;
}

View File

@ -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;
}

View File

@ -31,8 +31,8 @@ import me.filoghost.chestcommands.util.Utils;
public class AdvancedIconMenu extends BaseIconMenu<AdvancedIcon> {
private final String fileName;
private final String permission;
private String permission;
private List<Action> openActions;
private int refreshTicks;
@ -50,7 +50,7 @@ public class AdvancedIconMenu extends BaseIconMenu<AdvancedIcon> {
this.openActions = Utils.nullableCopy(openAction);
}
public String getPermission() {
public String getOpenPermission() {
return permission;
}
@ -82,7 +82,7 @@ public class AdvancedIconMenu extends BaseIconMenu<AdvancedIcon> {
if (player.hasPermission(permission)) {
open(player);
} else {
sendNoPermissionMessage(player);
sendNoOpenPermissionMessage(player);
}
}
@ -95,28 +95,30 @@ public class AdvancedIconMenu extends BaseIconMenu<AdvancedIcon> {
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));

View File

@ -31,50 +31,49 @@ import me.filoghost.chestcommands.util.ErrorCollector;
public class MenuManager {
private static Map<String, AdvancedIconMenu> fileNameToMenuMap;
private static Map<String, AdvancedIconMenu> commandsToMenuMap;
private static Map<OpenTrigger, AdvancedIconMenu> openTriggers;
private static Map<String, AdvancedIconMenu> menusByFile;
private static Map<String, AdvancedIconMenu> menusByCommand;
private static Map<OpenTrigger, AdvancedIconMenu> 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<String> 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<String> getMenuFileNames() {
return Collections.unmodifiableCollection(fileNameToMenuMap.keySet());
return Collections.unmodifiableCollection(menusByFile.keySet());
}

View File

@ -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;
}

View File

@ -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 {

View File

@ -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;

View File

@ -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;
}
}

View File

@ -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) {

View File

@ -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;

View File

@ -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);

View File

@ -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;