Skip to content

Commit 9ac2e83

Browse files
committed
parse enchantment with substring
1 parent dff6c29 commit 9ac2e83

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

spigot/modifier/src/main/java/io/github/projectunified/craftitem/spigot/modifier/EnchantmentModifier.java

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import java.util.*;
99
import java.util.function.Function;
1010
import java.util.function.UnaryOperator;
11-
import java.util.regex.Pattern;
1211

1312
/**
1413
* Spigot modifier that applies enchantments to items.
@@ -95,22 +94,30 @@ private static Map<Enchantment, Integer> getParsed(List<String> enchantments, ch
9594
Map<Enchantment, Integer> enchantmentMap = new LinkedHashMap<>();
9695
for (String string : enchantments) {
9796
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;
99102
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+
}
102110
break;
103111
}
104112
}
105-
if (split == null) {
106-
split = new String[]{replaced};
113+
if (enchantmentName == null) {
114+
enchantmentName = replaced.trim();
107115
}
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);
109117
int level = 1;
110-
if (split.length > 1) {
111-
String rawLevel = split[1].trim();
118+
if (enchantmentLevel != null) {
112119
try {
113-
level = Integer.parseInt(rawLevel);
120+
level = Integer.parseInt(enchantmentLevel);
114121
} catch (NumberFormatException e) {
115122
continue;
116123
}

0 commit comments

Comments
 (0)