From 9b42572464d0920202fb11449d4ad15e9144ef7e Mon Sep 17 00:00:00 2001 From: BlitzOffline <52609756+BlitzOffline@users.noreply.github.com> Date: Sun, 4 Aug 2024 22:29:51 +0300 Subject: [PATCH] allow enchantments to contain leading and trailing white spaces --- .../deluxemenus/config/DeluxeMenusConfig.java | 127 +++++++++--------- 1 file changed, 62 insertions(+), 65 deletions(-) diff --git a/src/main/java/com/extendedclip/deluxemenus/config/DeluxeMenusConfig.java b/src/main/java/com/extendedclip/deluxemenus/config/DeluxeMenusConfig.java index 5b1a553..d7e24d7 100644 --- a/src/main/java/com/extendedclip/deluxemenus/config/DeluxeMenusConfig.java +++ b/src/main/java/com/extendedclip/deluxemenus/config/DeluxeMenusConfig.java @@ -916,69 +916,7 @@ private Map> loadMenuItems(FileConfiguration } } - if (c.contains(currentPath + "enchantments")) { - List enchantments = c.getStringList(currentPath + "enchantments"); - - if (!enchantments.isEmpty()) { - - Map enchants = new HashMap<>(); - - for (String e : enchantments) { - - if (e.contains(";")) { - String[] parts = e.split(";"); - - if (parts.length == 2) { - - Enchantment enc = Enchantment.getByName(parts[0].toUpperCase()); - int level = 1; - - if (enc != null) { - try { - level = Integer.parseInt(parts[1]); - } catch (NumberFormatException ex) { - DeluxeMenus.debug( - DebugLevel.HIGHEST, - Level.WARNING, - "Enchantment level is incorrect for item " + key + " in menu " + name + "!" - ); - } - - enchants.put(enc, level); - - } else { - DeluxeMenus.debug( - DebugLevel.HIGHEST, - Level.WARNING, - "Enchantment " + parts[0] + " for item " + key + " in menu " + name - + " is not a valid enchantment name!" - ); - } - - } else { - DeluxeMenus.debug( - DebugLevel.HIGHEST, - Level.WARNING, - "Enchantment format is incorrect for item " + key + " in GUI " + name + "!", - "Correct format: - ';" - ); - } - - } else { - DeluxeMenus.debug( - DebugLevel.HIGHEST, - Level.WARNING, - "Enchantment format is incorrect for item " + key + " in GUI " + name + "!", - "Correct format: - ';" - ); - } - - } - if (!enchants.isEmpty()) { - builder.enchantments(enchants); - } - } - } + addEnchantmentsOptionToBuilder(c, currentPath, key, name, builder); if (c.contains(currentPath + "view_requirement")) { builder.viewRequirements(this.getRequirements(c, currentPath + "view_requirement")); @@ -1545,8 +1483,67 @@ public File getMenuDirector() { return menuDirectory; } - public void addDamageOptionToBuilder(FileConfiguration c, String currentPath, String itemKey, String menuName, - MenuItemOptions.MenuItemOptionsBuilder builder) { + public void addEnchantmentsOptionToBuilder(final FileConfiguration c, final String currentPath, + final String itemKey, final String menuName, + final MenuItemOptions.MenuItemOptionsBuilder builder) { + if (!c.contains(currentPath + "enchantments")) { + return; + } + + final List configEnchantments = c.getStringList(currentPath + "enchantments"); + final Map parsedEnchantments = new HashMap<>(); + + for (final String configEnchantment : configEnchantments) { + if (configEnchantment == null || !configEnchantment.contains(";")) { + DeluxeMenus.debug( + DebugLevel.HIGHEST, + Level.WARNING, + "Enchantment format '" + configEnchantment + "' is incorrect for item " + itemKey + + " in GUI " + menuName + "!", + "Correct format: - ';" + ); + continue; + } + + String[] parts = configEnchantment.split(";", 2); + if (parts.length != 2 || parts[0] == null || parts[1] == null) { + DeluxeMenus.debug( + DebugLevel.HIGHEST, + Level.WARNING, + "Enchantment format '" + configEnchantment + "' is incorrect for item " + itemKey + + " in GUI " + menuName + "!", + "Correct format: - ';" + ); + continue; + } + + final Enchantment enchantment = Enchantment.getByName(parts[0].strip().toUpperCase()); + if (enchantment == null) { + DeluxeMenus.debug( + DebugLevel.HIGHEST, + Level.WARNING, + "Enchantment '" + parts[0].strip() + "' for item " + itemKey + + " in menu " + menuName + " is not a valid enchantment name!" + ); + } + + Integer level = Ints.tryParse(parts[1].strip()); + if (level == null) { + level = 1; + DeluxeMenus.debug( + DebugLevel.HIGHEST, + Level.WARNING, + "Enchantment level '" + parts[1].strip() + "' is incorrect for item " + itemKey + + " in menu " + menuName + "!" + ); + } + parsedEnchantments.put(enchantment, level); + } + builder.enchantments(parsedEnchantments); + } + + public void addDamageOptionToBuilder(final FileConfiguration c, final String currentPath, final String itemKey, + final String menuName, final MenuItemOptions.MenuItemOptionsBuilder builder) { boolean damageOptionIsPresent = false; String damageValue = null;