Handle enchantment registration more safely
This commit is contained in:
parent
afdce9c0e2
commit
a305858854
@ -197,25 +197,7 @@ public class ConfigLoader
|
|||||||
defaultAllowedEnchantments = addNewConfigOption(config, "allowedEnchantments", defaultAllowedEnchantments,
|
defaultAllowedEnchantments = addNewConfigOption(config, "allowedEnchantments", defaultAllowedEnchantments,
|
||||||
enchantmentsComment);
|
enchantmentsComment);
|
||||||
allowedEnchantments = new LinkedHashSet<>();
|
allowedEnchantments = new LinkedHashSet<>();
|
||||||
defaultAllowedEnchantments.forEach(
|
defaultAllowedEnchantments.forEach(this::addNameSpacedKey);
|
||||||
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);
|
|
||||||
});
|
|
||||||
|
|
||||||
allowMultipleProtectionEnchantments = addNewConfigOption(config, "allowMultipleProtectionEnchantments", false,
|
allowMultipleProtectionEnchantments = addNewConfigOption(config, "allowMultipleProtectionEnchantments", false,
|
||||||
allowMultipleProtectionEnchantmentsComment);
|
allowMultipleProtectionEnchantmentsComment);
|
||||||
@ -236,6 +218,32 @@ public class ConfigLoader
|
|||||||
writeConfig();
|
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)
|
private <T> T addNewConfigOption(FileConfiguration config, String optionName, T defaultValue, String[] comment)
|
||||||
{
|
{
|
||||||
ConfigOption<T> option = new ConfigOption<>(plugin, config, optionName, defaultValue, comment);
|
ConfigOption<T> option = new ConfigOption<>(plugin, config, optionName, defaultValue, comment);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user