From f5e3e5e680a33eafa53f042df7f6432970791edb Mon Sep 17 00:00:00 2001 From: zrdzn Date: Sun, 15 Sep 2024 15:24:15 +0200 Subject: [PATCH] Separate one method into multiple ones --- .../minecraft/lovelydrop/menu/MenuFacade.java | 1 - .../lovelydrop/menu/MenuFactory.java | 248 ++++++++++-------- 2 files changed, 137 insertions(+), 112 deletions(-) diff --git a/plugin/src/main/java/io/github/zrdzn/minecraft/lovelydrop/menu/MenuFacade.java b/plugin/src/main/java/io/github/zrdzn/minecraft/lovelydrop/menu/MenuFacade.java index 4c1339d..ddb6ae9 100644 --- a/plugin/src/main/java/io/github/zrdzn/minecraft/lovelydrop/menu/MenuFacade.java +++ b/plugin/src/main/java/io/github/zrdzn/minecraft/lovelydrop/menu/MenuFacade.java @@ -37,7 +37,6 @@ public MenuFacade(PluginConfig config, MenuFactory menuFactory, MessageFacade me */ public void open(Player player) { MessageConfig messageConfig = this.config.messages; - MenuConfig menuConfig = this.config.menu; // Get user settings from the cache. Optional userSettingMaybe = this.userSettingFacade.findUserSettingByPlayerIdFromCache(player.getUniqueId()); diff --git a/plugin/src/main/java/io/github/zrdzn/minecraft/lovelydrop/menu/MenuFactory.java b/plugin/src/main/java/io/github/zrdzn/minecraft/lovelydrop/menu/MenuFactory.java index f14b55b..8dea417 100644 --- a/plugin/src/main/java/io/github/zrdzn/minecraft/lovelydrop/menu/MenuFactory.java +++ b/plugin/src/main/java/io/github/zrdzn/minecraft/lovelydrop/menu/MenuFactory.java @@ -17,6 +17,7 @@ import org.apache.commons.lang.StringUtils; import org.bukkit.entity.Player; import org.bukkit.event.inventory.ClickType; +import org.bukkit.event.inventory.InventoryClickEvent; import org.bukkit.inventory.ItemStack; public class MenuFactory { @@ -37,145 +38,170 @@ public Gui createDropMenu(Player player, UserSetting userSetting) { // Forbid the player from getting the item from the inventory. menu.setDefaultClickAction(event -> event.setCancelled(true)); - menuConfig.items.forEach((menuSlotItemKey, menuSlotItem) -> { + setupMenuItems(menu, player, userSetting, menuConfig, menuConfig.items); + + // Fill the rest inventory with the specified item if enabled. + MenuConfig.FillerConfig filler = menuConfig.filler; + if (filler.enabled) { + menu.getFiller().fill(ItemBuilder.from(filler.item).asGuiItem()); + } + + return menu; + } + + private void setupMenuItems(Gui menu, Player player, UserSetting userSetting, MenuConfig menuConfig, Map menuConfigItems) { + menuConfigItems.forEach((menuSlotItemKey, menuSlotItem) -> { MenuItemConfig.SlotConfig slot = menuSlotItem.slot; int row = slot.row; int column = slot.column; - Map actions = menuSlotItem.clickAction; - DropConfig drop = this.config.drops.get(menuSlotItemKey); - - // Set item that does not have drop item assigned to it. if (drop == null) { - menu.setItem(row, column, ItemBuilder.from(menuSlotItem.item) - .asGuiItem(event -> { - ClickType click = event.getClick(); - - if (actions.containsKey(click) && actions.get(click) == MenuAction.CLOSE_MENU) { - menu.close(player, true); - } - })); - + setDefaultItem(menu, row, column, menuSlotItem.item, menuSlotItem.clickAction, player); return; } - IntRangeFormatConfig heightFormat = menuConfig.heightFormat; - IntRange heightRange = drop.height; - String minimumHeight = String.valueOf(heightRange.getMin()); - String maximumHeight = String.valueOf(heightRange.getMax()); - - ItemStack menuItemStack = menuSlotItem.item; - List lore = menuItemStack.getItemMeta().getLore().stream() - .map(line -> { - String formattedHeight; - if (minimumHeight.equals(maximumHeight)) { - formattedHeight = StringUtils.replace(heightFormat.getFixed().getText(), "{HEIGHT}", minimumHeight); - } else { - formattedHeight = StringUtils.replaceEach(heightFormat.getDifferent().getText(), - new String[]{ "{HEIGHT-MIN}", "{HEIGHT-MAX}" }, - new String[]{ minimumHeight, maximumHeight }); - } - - return StringUtils.replace(line, "{HEIGHT}", formattedHeight); - }) - .collect(Collectors.toList()); - - Map fortunes = drop.fortune; - for (int level = 0; level < fortunes.size(); level++) { - DropConfig.FortuneConfig fortune = fortunes.get(level); - - IntRangeFormatConfig amountFormat = menuConfig.amountFormat; - IntRange amountRange = fortune.amount; - String minimumAmount = String.valueOf(amountRange.getMin()); - String maximumAmount = String.valueOf(amountRange.getMax()); - - for (int lineNumber = 0; lineNumber < lore.size(); lineNumber++) { - // Format lore with the chance and experience. - String line = StringUtils.replaceEach(lore.get(lineNumber), - new String[]{ String.format("{CHANCE-%d}", level), String.format("{EXPERIENCE-%d}", level) }, - new String[]{ fortune.chance.getFormattedValue(), String.valueOf(fortune.experience) }); - - // Format lore with the fixed amount. - String placeholder = String.format("{AMOUNT-%d}", level); - if (minimumAmount.equals(maximumAmount)) { - line = StringUtils.replace(line, placeholder, - StringUtils.replace(amountFormat.getFixed().getText(), "{AMOUNT}", minimumAmount)); - } else { - line = StringUtils.replace(line, placeholder, - StringUtils.replaceEach(amountFormat.getDifferent().getText(), - new String[]{ "{AMOUNT-MIN}", "{AMOUNT-MAX}" }, - new String[]{ minimumAmount, maximumAmount })); - } - - lore.set(lineNumber, line); - } + List lore = formatLoreForDrop(menuSlotItem.item, drop, menuConfig.heightFormat); + setItemWithDrop(menu, row, column, menuSlotItem.item, menuSlotItem.clickAction, player, userSetting, menuSlotItemKey, drop, menuConfig, lore); + }); + } + + private void setDefaultItem(Gui menu, int row, int column, ItemStack item, Map actions, Player player) { + menu.setItem(row, column, ItemBuilder.from(item).asGuiItem(event -> { + ClickType click = event.getClick(); + if (actions.containsKey(click) && actions.get(click) == MenuAction.CLOSE_MENU) { + menu.close(player, true); } + })); + } + + private List formatLoreForDrop(ItemStack itemStack, DropConfig drop, IntRangeFormatConfig heightFormat) { + IntRange heightRange = drop.height; + String minimumHeight = String.valueOf(heightRange.getMin()); + String maximumHeight = String.valueOf(heightRange.getMax()); + + return itemStack.getItemMeta().getLore().stream() + .map(line -> { + String formattedHeight = formatHeight(minimumHeight, maximumHeight, heightFormat); + return StringUtils.replace(line, "{HEIGHT}", formattedHeight); + }) + .collect(Collectors.toList()); + } + + private String formatHeight(String minHeight, String maxHeight, IntRangeFormatConfig heightFormat) { + if (minHeight.equals(maxHeight)) { + return StringUtils.replace(heightFormat.getFixed().getText(), "{HEIGHT}", minHeight); + } else { + return StringUtils.replaceEach(heightFormat.getDifferent().getText(), + new String[]{ "{HEIGHT-MIN}", "{HEIGHT-MAX}" }, + new String[]{ minHeight, maxHeight }); + } + } - MenuConfig.SwitchConfig dropSwitch = menuConfig.dropSwitch; - MenuConfig.SwitchConfig inventorySwitch = menuConfig.inventoryDropSwitch; + private void setItemWithDrop(Gui menu, int row, int column, ItemStack item, Map actions, Player player, UserSetting userSetting, String menuSlotItemKey, DropConfig drop, MenuConfig menuConfig, List lore) { + for (int level = 0; level < drop.fortune.size(); level++) { + formatLoreForFortune(lore, level, drop.fortune.get(level), menuConfig.amountFormat); + } - ItemBuilder menuItemBuilder = ItemBuilder.from(menuItemStack) - .setLore(this.formatLore(lore, userSetting, menuSlotItemKey, dropSwitch, inventorySwitch)); + MenuConfig.SwitchConfig dropSwitch = menuConfig.dropSwitch; + MenuConfig.SwitchConfig inventorySwitch = menuConfig.inventoryDropSwitch; - GuiItem menuItem = menuItemBuilder.asGuiItem(); + ItemBuilder menuItemBuilder = ItemBuilder.from(item) + .setLore(this.formatLore(lore, userSetting, menuSlotItemKey, dropSwitch, inventorySwitch)); - // Perform specific actions when player clicks the item. - menuItem.setAction(event -> { - ClickType click = event.getClick(); - if (actions.containsKey(click)) { - MenuAction action = actions.get(click); - this.handleClickAction(player, userSetting, menu, menuSlotItemKey, action); - } + GuiItem menuItem = menuItemBuilder.asGuiItem(); + menuItem.setAction(event -> handleItemClick(event, actions, player, userSetting, menu, menuSlotItemKey, dropSwitch, inventorySwitch, lore)); + menu.setItem(row, column, menuItem); + } - ItemStack actionItem = ItemBuilder.from(menuItem.getItemStack()) - .setLore(this.formatLore(lore, userSetting, menuSlotItemKey, dropSwitch, inventorySwitch)) - .build(); + private void formatLoreForFortune(List lore, int level, DropConfig.FortuneConfig fortune, IntRangeFormatConfig amountFormat) { + IntRange amountRange = fortune.amount; + String minimumAmount = String.valueOf(amountRange.getMin()); + String maximumAmount = String.valueOf(amountRange.getMax()); - menu.updateItem(row, column, actionItem); - }); + lore.replaceAll(line -> formatLineWithFortune(line, level, fortune, amountFormat, minimumAmount, maximumAmount)); + } - menu.setItem(row, column, menuItem); - }); + private String formatLineWithFortune(String line, int level, DropConfig.FortuneConfig fortune, IntRangeFormatConfig amountFormat, String minAmount, String maxAmount) { + line = StringUtils.replaceEach(line, + new String[]{ String.format("{CHANCE-%d}", level), String.format("{EXPERIENCE-%d}", level) }, + new String[]{ fortune.chance.getFormattedValue(), String.valueOf(fortune.experience) }); + + String placeholder = String.format("{AMOUNT-%d}", level); + if (minAmount.equals(maxAmount)) { + return StringUtils.replace(line, placeholder, + StringUtils.replace(amountFormat.getFixed().getText(), "{AMOUNT}", minAmount)); + } else { + return StringUtils.replace(line, placeholder, + StringUtils.replaceEach(amountFormat.getDifferent().getText(), + new String[]{ "{AMOUNT-MIN}", "{AMOUNT-MAX}" }, + new String[]{ minAmount, maxAmount })); + } + } - // Fill the rest inventory with the specified item if enabled. - MenuConfig.FillerConfig filler = menuConfig.filler; - if (filler.enabled) { - menu.getFiller().fill(ItemBuilder.from(filler.item).asGuiItem()); + private void handleItemClick(InventoryClickEvent event, Map actions, Player player, UserSetting userSetting, Gui menu, String menuSlotItemKey, MenuConfig.SwitchConfig dropSwitch, MenuConfig.SwitchConfig inventorySwitch, List lore) { + ClickType click = event.getClick(); + if (actions.containsKey(click)) { + MenuAction action = actions.get(click); + this.handleClickAction(player, userSetting, menu, menuSlotItemKey, action); } - return menu; + ItemStack actionItem = ItemBuilder.from(event.getCurrentItem()) + .setLore(this.formatLore(lore, userSetting, menuSlotItemKey, dropSwitch, inventorySwitch)) + .build(); + + menu.updateItem(event.getSlot(), actionItem); } private void handleClickAction(Player player, UserSetting userSetting, Gui menu, String menuSlotItemKey, MenuAction action) { - if (action == MenuAction.CLOSE_MENU) { - menu.close(player, true); - } else if (action == MenuAction.SWITCH_DROP) { - if (userSetting.hasDisabledDrop(menuSlotItemKey)) { - userSetting.removeDisabledDrop(menuSlotItemKey); - } else { - userSetting.addDisabledDrop(menuSlotItemKey); - } + switch (action) { + case CLOSE_MENU: + closeMenu(player, menu); + break; + case SWITCH_DROP: + toggleDrop(player, userSetting, menuSlotItemKey); + break; + case SWITCH_DROP_TO_INVENTORY: + toggleDropToInventory(player, userSetting, menuSlotItemKey); + break; + default: + throw new IllegalArgumentException("Unknown action: " + action); + } + } - this.messageFacade.sendMessage(player, this.config.messages.dropSwitched, "{DROP}", menuSlotItemKey); - } else if (action == MenuAction.SWITCH_DROP_TO_INVENTORY) { - userSetting.setDropToInventory(menuSlotItemKey, !userSetting.hasDropToInventory(menuSlotItemKey)); - this.messageFacade.sendMessage(player, this.config.messages.dropSwitchedInventory, "{DROP}", menuSlotItemKey); + private void closeMenu(Player player, Gui menu) { + menu.close(player, true); + } + + private void toggleDrop(Player player, UserSetting userSetting, String menuSlotItemKey) { + if (userSetting.hasDisabledDrop(menuSlotItemKey)) { + userSetting.removeDisabledDrop(menuSlotItemKey); + } else { + userSetting.addDisabledDrop(menuSlotItemKey); } + this.messageFacade.sendMessage(player, this.config.messages.dropSwitched, "{DROP}", menuSlotItemKey); } - private List formatLore(List lore, UserSetting userSetting, String menuSlotItemKey, SwitchConfig dropSwitch, - SwitchConfig inventorySwitch) { + private void toggleDropToInventory(Player player, UserSetting userSetting, String menuSlotItemKey) { + userSetting.setDropToInventory(menuSlotItemKey, !userSetting.hasDropToInventory(menuSlotItemKey)); + this.messageFacade.sendMessage(player, this.config.messages.dropSwitchedInventory, "{DROP}", menuSlotItemKey); + } + + private List formatLore(List lore, UserSetting userSetting, String menuSlotItemKey, SwitchConfig dropSwitch, SwitchConfig inventorySwitch) { return lore.stream() - .map(line -> { - String[] replacements = { - userSetting.hasDisabledDrop(menuSlotItemKey) ? dropSwitch.disabled.getText() : dropSwitch.enabled.getText(), - userSetting.hasDropToInventory(menuSlotItemKey) ? inventorySwitch.enabled.getText() : inventorySwitch.disabled.getText() - }; - - return StringUtils.replaceEach(line, new String[]{ "{SWITCH}", "{SWITCH_INVENTORY}" }, replacements); - }) - .collect(Collectors.toList()); + .map(line -> formatLine(line, userSetting, menuSlotItemKey, dropSwitch, inventorySwitch)) + .collect(Collectors.toList()); + } + + private String formatLine(String line, UserSetting userSetting, String menuSlotItemKey, SwitchConfig dropSwitch, SwitchConfig inventorySwitch) { + String[] replacements = getReplacements(userSetting, menuSlotItemKey, dropSwitch, inventorySwitch); + return StringUtils.replaceEach(line, new String[]{ "{SWITCH}", "{SWITCH_INVENTORY}" }, replacements); + } + + private String[] getReplacements(UserSetting userSetting, String menuSlotItemKey, SwitchConfig dropSwitch, SwitchConfig inventorySwitch) { + String dropText = userSetting.hasDisabledDrop(menuSlotItemKey) ? dropSwitch.disabled.getText() : dropSwitch.enabled.getText(); + String inventoryText = userSetting.hasDropToInventory(menuSlotItemKey) ? inventorySwitch.enabled.getText() : inventorySwitch.disabled.getText(); + return new String[]{ dropText, inventoryText }; } }