Handle enchantment registration more safely

This commit is contained in:
Pim van der Loos 2022-01-02 07:49:53 +01:00
parent afdce9c0e2
commit a305858854
No known key found for this signature in database
GPG Key ID: C16F020ADAE6D5A8

View File

@ -197,25 +197,7 @@ public class ConfigLoader
defaultAllowedEnchantments = addNewConfigOption(config, "allowedEnchantments", defaultAllowedEnchantments,
enchantmentsComment);
allowedEnchantments = new LinkedHashSet<>();
defaultAllowedEnchantments.forEach(
fullKey ->
{
String[] keyParts = fullKey.split(":", 2);
if (keyParts.length < 2)
{
Bukkit.getLogger().warning("\"" + fullKey + "\" is not a valid NamespacedKey!");
return;
}
//noinspection deprecation
final NamespacedKey key = new NamespacedKey(keyParts[0], keyParts[1]);
final Enchantment enchantment = Enchantment.getByKey(key);
if (enchantment == null)
{
Bukkit.getLogger().warning("The enchantment \"" + fullKey + "\" could not be found!");
return;
}
allowedEnchantments.add(enchantment);
});
defaultAllowedEnchantments.forEach(this::addNameSpacedKey);
allowMultipleProtectionEnchantments = addNewConfigOption(config, "allowMultipleProtectionEnchantments", false,
allowMultipleProtectionEnchantmentsComment);
@ -236,6 +218,32 @@ public class ConfigLoader
writeConfig();
}
private void addNameSpacedKey(String fullKey)
{
try
{
final String[] keyParts = fullKey.strip().split(":", 2);
if (keyParts.length < 2)
{
Bukkit.getLogger().warning("\"" + fullKey + "\" is not a valid NamespacedKey!");
return;
}
//noinspection deprecation
final NamespacedKey key = new NamespacedKey(keyParts[0], keyParts[1]);
final Enchantment enchantment = Enchantment.getByKey(key);
if (enchantment == null)
{
Bukkit.getLogger().warning("The enchantment \"" + fullKey + "\" could not be found!");
return;
}
allowedEnchantments.add(enchantment);
}
catch (Exception e)
{
plugin.getLogger().log(Level.WARNING, e, () -> "Failed to register NamespacedKey key: '" + fullKey + "'");
}
}
private <T> T addNewConfigOption(FileConfiguration config, String optionName, T defaultValue, String[] comment)
{
ConfigOption<T> option = new ConfigOption<>(plugin, config, optionName, defaultValue, comment);