diff --git a/src/main/java/net/knarcraft/blacksmith/BlacksmithPlugin.java b/src/main/java/net/knarcraft/blacksmith/BlacksmithPlugin.java index 8832c66..feeb5f0 100644 --- a/src/main/java/net/knarcraft/blacksmith/BlacksmithPlugin.java +++ b/src/main/java/net/knarcraft/blacksmith/BlacksmithPlugin.java @@ -77,7 +77,7 @@ public class BlacksmithPlugin extends JavaPlugin { public void reload() { this.reloadConfig(); config.load(); - translator.loadLanguages(this.getDataFolder(), this.getConfig().getString("language", "en")); + translator.loadLanguages(this.getDataFolder(), this.getConfig().getString("language", "en"), "en"); } /** @@ -122,7 +122,7 @@ public class BlacksmithPlugin extends JavaPlugin { translator = new Translator(); translator.registerMessageCategory(TranslatableTimeUnit.UNIT_SECOND); translator.registerMessageCategory(BlacksmithTranslatableMessage.ITEM_TYPE_ENCHANTMENT); - translator.loadLanguages(this.getDataFolder(), fileConfiguration.getString("language", "en")); + translator.loadLanguages(this.getDataFolder(), fileConfiguration.getString("language", "en"), "en"); BlacksmithPlugin.stringFormatter = new StringFormatter(this.getDescription().getPrefix(), translator); //Set up Vault integration diff --git a/src/main/java/net/knarcraft/blacksmith/formatting/BlacksmithTranslatableMessage.java b/src/main/java/net/knarcraft/blacksmith/formatting/BlacksmithTranslatableMessage.java index 20c6711..678f66d 100644 --- a/src/main/java/net/knarcraft/blacksmith/formatting/BlacksmithTranslatableMessage.java +++ b/src/main/java/net/knarcraft/blacksmith/formatting/BlacksmithTranslatableMessage.java @@ -210,4 +210,5 @@ public enum BlacksmithTranslatableMessage implements TranslatableMessage { public TranslatableMessage[] getAllMessages() { return BlacksmithTranslatableMessage.values(); } + } diff --git a/src/main/java/net/knarcraft/blacksmith/util/TypeValidationHelper.java b/src/main/java/net/knarcraft/blacksmith/util/TypeValidationHelper.java index 5bef070..1665862 100644 --- a/src/main/java/net/knarcraft/blacksmith/util/TypeValidationHelper.java +++ b/src/main/java/net/knarcraft/blacksmith/util/TypeValidationHelper.java @@ -3,7 +3,9 @@ package net.knarcraft.blacksmith.util; import net.knarcraft.blacksmith.BlacksmithPlugin; import net.knarcraft.blacksmith.config.SettingValueType; import net.knarcraft.blacksmith.formatting.BlacksmithTranslatableMessage; +import org.bukkit.Material; import org.bukkit.command.CommandSender; +import org.bukkit.enchantments.Enchantment; import java.util.List; @@ -33,7 +35,8 @@ public final class TypeValidationHelper { case PERCENTAGE -> isPercentage(value, sender); case BOOLEAN -> true; case STRING_LIST, REFORGE_ABLE_ITEMS -> isStringList(value, sender); - case MATERIAL, ENCHANTMENT -> false; + case MATERIAL -> isMaterial(value, sender); + case ENCHANTMENT -> isEnchantment(value, sender); }; } catch (ClassCastException exception) { //This error signifies that an object is not a string, and of the wrong class @@ -41,6 +44,40 @@ public final class TypeValidationHelper { } } + /** + * Checks whether the given value is a material + * + * @param value

The value to check

+ * @param sender

The command sender to use for printing error messages

+ * @return

True if the value is a material

+ */ + private static boolean isMaterial(Object value, CommandSender sender) { + boolean isMaterial = value instanceof Material || (value instanceof String string && + InputParsingHelper.matchMaterial(string) != null); + if (!isMaterial && sender != null) { + BlacksmithPlugin.getStringFormatter().displayErrorMessage(sender, + BlacksmithTranslatableMessage.ITEM_TYPE_MATERIAL); + } + return isMaterial; + } + + /** + * Checks whether the given value is an enchantment + * + * @param value

The value to check

+ * @param sender

The command sender to use for printing error messages

+ * @return

True if the value is an enchantment

+ */ + private static boolean isEnchantment(Object value, CommandSender sender) { + boolean isEnchantment = value instanceof Enchantment || (value instanceof String string && + InputParsingHelper.matchEnchantment(string) != null); + if (!isEnchantment && sender != null) { + BlacksmithPlugin.getStringFormatter().displayErrorMessage(sender, + BlacksmithTranslatableMessage.ITEM_TYPE_ENCHANTMENT); + } + return isEnchantment; + } + /** * Checks whether the given value is a string list * @@ -65,16 +102,13 @@ public final class TypeValidationHelper { * @return

True if the value is a percentage

*/ private static boolean isPercentage(Object value, CommandSender sender) { - try { - int intValue = ConfigHelper.asInt(value); - return intValue >= 0 && intValue <= 100; - } catch (NumberFormatException | NullPointerException exception) { - if (sender != null) { - BlacksmithPlugin.getStringFormatter().displayErrorMessage(sender, - BlacksmithTranslatableMessage.INPUT_PERCENTAGE_REQUIRED); - } - return false; + boolean isPercentage = isPositiveInteger(value, null) && ConfigHelper.asInt(value) >= 0 && + ConfigHelper.asInt(value) <= 100; + if (!isPercentage && sender != null) { + BlacksmithPlugin.getStringFormatter().displayErrorMessage(sender, + BlacksmithTranslatableMessage.INPUT_PERCENTAGE_REQUIRED); } + return isPercentage; } /** diff --git a/src/test/java/net/knarcraft/blacksmith/util/TypeValidatorHelperTest.java b/src/test/java/net/knarcraft/blacksmith/util/TypeValidatorHelperTest.java new file mode 100644 index 0000000..57ed2fa --- /dev/null +++ b/src/test/java/net/knarcraft/blacksmith/util/TypeValidatorHelperTest.java @@ -0,0 +1,170 @@ +package net.knarcraft.blacksmith.util; + +import be.seeseemelk.mockbukkit.enchantments.EnchantmentMock; +import net.knarcraft.blacksmith.config.SettingValueType; +import org.bukkit.Material; +import org.bukkit.NamespacedKey; +import org.bukkit.enchantments.Enchantment; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * Tests for the type validator helper + */ +public class TypeValidatorHelperTest { + + private static Enchantment curseOfBinding; + + @BeforeAll + public static void setUp() { + //Note: When not testing against a real server, no default enchantments are loaded! + curseOfBinding = new EnchantmentMock(NamespacedKey.minecraft("binding_curse"), + "CURSE_OF_BINDING"); + Enchantment.registerEnchantment(curseOfBinding); + } + + @Test + public void isValidPositiveDoubleValidTest() { + SettingValueType type = SettingValueType.POSITIVE_DOUBLE; + assertTrue(TypeValidationHelper.isValid(type, 6578d, null)); + assertTrue(TypeValidationHelper.isValid(type, 5346.4534, null)); + assertTrue(TypeValidationHelper.isValid(type, "237.4378", null)); + assertTrue(TypeValidationHelper.isValid(type, 74567, null)); + assertTrue(TypeValidationHelper.isValid(type, "843552", null)); + } + + @Test + public void isValidPositiveDoubleInvalidTest() { + SettingValueType type = SettingValueType.POSITIVE_DOUBLE; + assertFalse(TypeValidationHelper.isValid(type, "potato", null)); + assertFalse(TypeValidationHelper.isValid(type, 354f, null)); + assertFalse(TypeValidationHelper.isValid(type, SettingValueType.STRING, null)); + assertFalse(TypeValidationHelper.isValid(type, -1, null)); + assertFalse(TypeValidationHelper.isValid(type, -0.45654, null)); + assertFalse(TypeValidationHelper.isValid(type, null, null)); + } + + @Test + public void isValidStringValidTest() { + assertTrue(TypeValidationHelper.isValid(SettingValueType.STRING, "potato", null)); + } + + @Test + public void isValidStringInvalidTest() { + SettingValueType type = SettingValueType.STRING; + assertFalse(TypeValidationHelper.isValid(type, 5467, null)); + assertFalse(TypeValidationHelper.isValid(type, SettingValueType.POSITIVE_DOUBLE, null)); + assertFalse(TypeValidationHelper.isValid(type, "", null)); + assertFalse(TypeValidationHelper.isValid(type, " ", null)); + assertFalse(TypeValidationHelper.isValid(type, null, null)); + } + + @Test + public void isValidPositiveIntegerValidTest() { + SettingValueType type = SettingValueType.POSITIVE_INTEGER; + assertTrue(TypeValidationHelper.isValid(type, 6574567, null)); + assertTrue(TypeValidationHelper.isValid(type, "785768", null)); + } + + @Test + public void isValidPositiveIntegerInvalidTest() { + SettingValueType type = SettingValueType.POSITIVE_INTEGER; + assertFalse(TypeValidationHelper.isValid(type, 0.2345, null)); + assertFalse(TypeValidationHelper.isValid(type, -1, null)); + assertFalse(TypeValidationHelper.isValid(type, "potato", null)); + assertFalse(TypeValidationHelper.isValid(type, null, null)); + } + + @Test + public void isValidPercentageValidTest() { + SettingValueType type = SettingValueType.PERCENTAGE; + assertTrue(TypeValidationHelper.isValid(type, 33, null)); + assertTrue(TypeValidationHelper.isValid(type, 100, null)); + assertTrue(TypeValidationHelper.isValid(type, 0, null)); + } + + @Test + public void isValidPercentageInvalidTest() { + SettingValueType type = SettingValueType.PERCENTAGE; + assertFalse(TypeValidationHelper.isValid(type, -1, null)); + assertFalse(TypeValidationHelper.isValid(type, -73, null)); + assertFalse(TypeValidationHelper.isValid(type, 101, null)); + assertFalse(TypeValidationHelper.isValid(type, 14.4, null)); + assertFalse(TypeValidationHelper.isValid(type, "potato", null)); + assertFalse(TypeValidationHelper.isValid(type, null, null)); + } + + @Test + public void isValidBooleanValidTest() { + SettingValueType type = SettingValueType.BOOLEAN; + //Note: As anything invalid will just be parsed to false, anything goes + assertTrue(TypeValidationHelper.isValid(type, null, null)); + assertTrue(TypeValidationHelper.isValid(type, "potato", null)); + assertTrue(TypeValidationHelper.isValid(type, "True", null)); + assertTrue(TypeValidationHelper.isValid(type, "False", null)); + assertTrue(TypeValidationHelper.isValid(type, "On", null)); + assertTrue(TypeValidationHelper.isValid(type, "Off", null)); + } + + @Test + public void isValidStringListValidTest() { + SettingValueType type = SettingValueType.STRING_LIST; + List testList = new ArrayList<>(); + testList.add("fish"); + testList.add("potato"); + assertTrue(TypeValidationHelper.isValid(type, "1, 2, 3, 4, 5, 6, 7, 8", null)); + assertTrue(TypeValidationHelper.isValid(type, new ArrayList<>(), null)); + assertTrue(TypeValidationHelper.isValid(type, testList, null)); + assertTrue(TypeValidationHelper.isValid(type, "potato", null)); + assertTrue(TypeValidationHelper.isValid(type, "", null)); + assertTrue(TypeValidationHelper.isValid(type, new String[]{"a", "b", "c"}, null)); + } + + @Test + public void isValidStringListInvalidTest() { + SettingValueType type = SettingValueType.STRING_LIST; + assertFalse(TypeValidationHelper.isValid(type, null, null)); + assertFalse(TypeValidationHelper.isValid(type, 456, null)); + assertFalse(TypeValidationHelper.isValid(type, -2345.4321, null)); + } + + @Test + public void isValidMaterialValidTest() { + SettingValueType type = SettingValueType.MATERIAL; + assertTrue(TypeValidationHelper.isValid(type, Material.POTATO, null)); + assertTrue(TypeValidationHelper.isValid(type, "POTATO", null)); + assertTrue(TypeValidationHelper.isValid(type, "minecraft:potato", null)); + } + + @Test + public void isValidMaterialInvalidTest() { + SettingValueType type = SettingValueType.MATERIAL; + assertFalse(TypeValidationHelper.isValid(type, null, null)); + assertFalse(TypeValidationHelper.isValid(type, 1, null)); + assertFalse(TypeValidationHelper.isValid(type, "random", null)); + assertFalse(TypeValidationHelper.isValid(type, -546.678, null)); + assertFalse(TypeValidationHelper.isValid(type, true, null)); + } + + @Test + public void isValidEnchantmentValidTest() { + SettingValueType type = SettingValueType.ENCHANTMENT; + assertTrue(TypeValidationHelper.isValid(type, curseOfBinding, null)); + assertTrue(TypeValidationHelper.isValid(type, "binding_curse", null)); + } + + @Test + public void isValidEnchantmentInvalidTest() { + SettingValueType type = SettingValueType.ENCHANTMENT; + assertFalse(TypeValidationHelper.isValid(type, "potato", null)); + assertFalse(TypeValidationHelper.isValid(type, 7675, null)); + assertFalse(TypeValidationHelper.isValid(type, null, null)); + } + +}