Skip to content

Commit dff6c29

Browse files
committed
configurable delimiter in EnchantmentModifier
1 parent 6056f4c commit dff6c29

File tree

1 file changed

+23
-8
lines changed

1 file changed

+23
-8
lines changed

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

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

1213
/**
1314
* Spigot modifier that applies enchantments to items.
@@ -43,14 +44,24 @@ public class EnchantmentModifier implements SpigotItemModifier {
4344

4445
private final Function<UnaryOperator<String>, Map<Enchantment, Integer>> enchantments;
4546

47+
/**
48+
* Creates an EnchantmentModifier from a list of enchantment strings
49+
*
50+
* @param enchantments the list of enchantment strings
51+
* @param delimiters the character to separate the enchantment name and level
52+
*/
53+
public EnchantmentModifier(List<String> enchantments, char... delimiters) {
54+
this.enchantments = translator -> getParsed(enchantments, delimiters, translator);
55+
}
56+
4657
/**
4758
* Creates an EnchantmentModifier from a list of enchantment strings.
4859
* Format: "ENCHANTMENT_NAME level" or "ENCHANTMENT_NAME,level"
4960
*
5061
* @param enchantments the list of enchantment strings
5162
*/
5263
public EnchantmentModifier(List<String> enchantments) {
53-
this.enchantments = translator -> getParsed(enchantments, translator);
64+
this(enchantments, ',', ' ');
5465
}
5566

5667
/**
@@ -74,21 +85,25 @@ private static String normalizeEnchantmentName(String name) {
7485

7586
/**
7687
* Parses enchantment strings into a map of Enchantment objects.
77-
* Supports both space and comma delimiters between name and level.
7888
*
7989
* @param enchantments the list of enchantment strings
90+
* @param delimiters the character to separate the enchantment name and level
8091
* @param translator the string translator for variable substitution
8192
* @return map of valid enchantments to their levels
8293
*/
83-
private static Map<Enchantment, Integer> getParsed(List<String> enchantments, UnaryOperator<String> translator) {
94+
private static Map<Enchantment, Integer> getParsed(List<String> enchantments, char[] delimiters, UnaryOperator<String> translator) {
8495
Map<Enchantment, Integer> enchantmentMap = new LinkedHashMap<>();
8596
for (String string : enchantments) {
8697
String replaced = translator.apply(string);
87-
String[] split;
88-
if (replaced.indexOf(',') != -1) {
89-
split = replaced.split(",", 2);
90-
} else {
91-
split = replaced.split(" ", 2);
98+
String[] split = null;
99+
for (char delimiter : delimiters) {
100+
if (replaced.indexOf(delimiter) >= 0) {
101+
split = replaced.split(Pattern.quote(String.valueOf(delimiter)), 2);
102+
break;
103+
}
104+
}
105+
if (split == null) {
106+
split = new String[]{replaced};
92107
}
93108
Optional<Enchantment> enchantment = Optional.of(split[0].trim()).map(EnchantmentModifier::normalizeEnchantmentName).map(ENCHANTMENT_MAP::get);
94109
int level = 1;

0 commit comments

Comments
 (0)