Adds a new enchantmentBlocklist option

The enchantmentBlocklist allows specifying a list of enchantments blacksmiths should be blocked from adding to items. Especially curse of binding and curse of vanishing are problematic as the effect is quite bad. Mending is possibly undesirable as it's supposed to be quite rare.
This commit is contained in:
2022-11-05 04:21:47 +01:00
parent 012d1df099
commit 89c1c4a56c
10 changed files with 118 additions and 9 deletions

View File

@ -1,5 +1,8 @@
package net.knarcraft.blacksmith.util;
import java.util.ArrayList;
import java.util.List;
/**
* A helper class for getting an object value as the correct type
*/
@ -9,6 +12,25 @@ public final class ConfigHelper {
}
/**
* Gets the given value as a string list
*
* @param value <p>The raw string list value</p>
* @return <p>The value as a string list</p>
*/
public static List<String> asStringList(Object value) {
if (value instanceof String) {
return List.of(((String) value).split(","));
} else {
List<String> strings = new ArrayList<>();
List<?> list = (List<?>) value;
for (Object object : list) {
strings.add(String.valueOf(object));
}
return strings;
}
}
/**
* Gets the given value as a double
*

View File

@ -32,12 +32,27 @@ public final class TabCompleteValuesHelper {
case POSITIVE_DOUBLE -> getPositiveDoubles();
case STRING -> getStrings();
case PERCENTAGE -> getPercentages();
case STRING_LIST -> getReforgeAbleMaterials();
case REFORGE_ABLE_ITEMS -> getReforgeAbleMaterials();
case MATERIAL -> getAllReforgeAbleMaterials();
case ENCHANTMENT -> getAllEnchantments();
case STRING_LIST -> getExampleEnchantmentBlockLists();
};
}
/**
* Gets example enchantment block lists
*
* @return <p>Some example enchantment block lists</p>
*/
private static List<String> getExampleEnchantmentBlockLists() {
List<String> exampleBlockLists = new ArrayList<>();
exampleBlockLists.add(Enchantment.VANISHING_CURSE.getKey().getKey() + "," +
Enchantment.BINDING_CURSE.getKey().getKey() + "," + Enchantment.MENDING.getKey().getKey());
exampleBlockLists.add(Enchantment.VANISHING_CURSE.getKey().getKey() + "," +
Enchantment.BINDING_CURSE.getKey().getKey());
return exampleBlockLists;
}
/**
* Gets a complete list of all reforge-able materials
*

View File

@ -4,6 +4,8 @@ import net.knarcraft.blacksmith.config.SettingValueType;
import net.knarcraft.blacksmith.formatting.TranslatableMessage;
import org.bukkit.command.CommandSender;
import java.util.List;
import static net.knarcraft.blacksmith.formatting.StringFormatter.displayErrorMessage;
/**
@ -31,7 +33,7 @@ public final class TypeValidationHelper {
case POSITIVE_INTEGER -> isPositiveInteger(value, sender);
case PERCENTAGE -> isPercentage(value, sender);
case BOOLEAN -> true;
case STRING_LIST -> isStringList(value, sender);
case STRING_LIST, REFORGE_ABLE_ITEMS -> isStringList(value, sender);
case MATERIAL, ENCHANTMENT -> false;
};
} catch (ClassCastException exception) {
@ -48,7 +50,7 @@ public final class TypeValidationHelper {
* @return <p>True if the value is a string list</p>
*/
private static boolean isStringList(Object value, CommandSender sender) {
boolean isStringList = value instanceof String[] || value instanceof String;
boolean isStringList = value instanceof String[] || value instanceof List<?> || value instanceof String;
if (!isStringList && sender != null) {
displayErrorMessage(sender, TranslatableMessage.INPUT_STRING_LIST_REQUIRED);
}
@ -65,7 +67,7 @@ public final class TypeValidationHelper {
private static boolean isPercentage(Object value, CommandSender sender) {
try {
int intValue = ConfigHelper.asInt(value);
return intValue > 0 && intValue <= 100;
return intValue >= 0 && intValue <= 100;
} catch (NumberFormatException | NullPointerException exception) {
if (sender != null) {
displayErrorMessage(sender, TranslatableMessage.INPUT_PERCENTAGE_REQUIRED);