Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow enchantments to contain leading and trailing white spaces #130

Merged
merged 1 commit into from
Aug 31, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -916,69 +916,7 @@ private Map<Integer, TreeMap<Integer, MenuItem>> loadMenuItems(FileConfiguration
}
}

if (c.contains(currentPath + "enchantments")) {
List<String> enchantments = c.getStringList(currentPath + "enchantments");

if (!enchantments.isEmpty()) {

Map<Enchantment, Integer> 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: - '<Enchantment name>;<level>"
);
}

} else {
DeluxeMenus.debug(
DebugLevel.HIGHEST,
Level.WARNING,
"Enchantment format is incorrect for item " + key + " in GUI " + name + "!",
"Correct format: - '<Enchantment name>;<level>"
);
}

}
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"));
Expand Down Expand Up @@ -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<String> configEnchantments = c.getStringList(currentPath + "enchantments");
final Map<Enchantment, Integer> 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: - '<Enchantment name>;<level>"
);
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: - '<Enchantment name>;<level>"
);
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;

Expand Down