|
8 | 8 | import java.util.*; |
9 | 9 | import java.util.function.Function; |
10 | 10 | import java.util.function.UnaryOperator; |
11 | | -import java.util.regex.Pattern; |
12 | 11 |
|
13 | 12 | /** |
14 | 13 | * Spigot modifier that applies enchantments to items. |
@@ -95,22 +94,30 @@ private static Map<Enchantment, Integer> getParsed(List<String> enchantments, ch |
95 | 94 | Map<Enchantment, Integer> enchantmentMap = new LinkedHashMap<>(); |
96 | 95 | for (String string : enchantments) { |
97 | 96 | String replaced = translator.apply(string); |
98 | | - String[] split = null; |
| 97 | + if (replaced == null || replaced.isEmpty()) { |
| 98 | + continue; |
| 99 | + } |
| 100 | + String enchantmentName = null; |
| 101 | + String enchantmentLevel = null; |
99 | 102 | for (char delimiter : delimiters) { |
100 | | - if (replaced.indexOf(delimiter) >= 0) { |
101 | | - split = replaced.split(Pattern.quote(String.valueOf(delimiter)), 2); |
| 103 | + int delimiterIndex = replaced.indexOf(delimiter); |
| 104 | + if (delimiterIndex >= 0) { |
| 105 | + enchantmentName = replaced.substring(0, delimiterIndex).trim(); |
| 106 | + enchantmentLevel = replaced.substring(delimiterIndex + 1).trim(); |
| 107 | + if (enchantmentLevel.isEmpty()) { |
| 108 | + enchantmentLevel = null; |
| 109 | + } |
102 | 110 | break; |
103 | 111 | } |
104 | 112 | } |
105 | | - if (split == null) { |
106 | | - split = new String[]{replaced}; |
| 113 | + if (enchantmentName == null) { |
| 114 | + enchantmentName = replaced.trim(); |
107 | 115 | } |
108 | | - Optional<Enchantment> enchantment = Optional.of(split[0].trim()).map(EnchantmentModifier::normalizeEnchantmentName).map(ENCHANTMENT_MAP::get); |
| 116 | + Optional<Enchantment> enchantment = Optional.of(enchantmentName).map(EnchantmentModifier::normalizeEnchantmentName).map(ENCHANTMENT_MAP::get); |
109 | 117 | int level = 1; |
110 | | - if (split.length > 1) { |
111 | | - String rawLevel = split[1].trim(); |
| 118 | + if (enchantmentLevel != null) { |
112 | 119 | try { |
113 | | - level = Integer.parseInt(rawLevel); |
| 120 | + level = Integer.parseInt(enchantmentLevel); |
114 | 121 | } catch (NumberFormatException e) { |
115 | 122 | continue; |
116 | 123 | } |
|
0 commit comments