diff --git a/src/main/java/org/mvplugins/multiverse/core/utils/MaterialConverter.java b/src/main/java/org/mvplugins/multiverse/core/utils/MaterialConverter.java new file mode 100644 index 00000000..6726d15a --- /dev/null +++ b/src/main/java/org/mvplugins/multiverse/core/utils/MaterialConverter.java @@ -0,0 +1,27 @@ +package org.mvplugins.multiverse.core.utils; + +import de.themoep.idconverter.IdMappings; +import org.bukkit.Material; +import org.jetbrains.annotations.Nullable; + +/** + * A tool for converting values which may be an old type ID to a Material. + */ +public class MaterialConverter { + + /** + * Converts a string representing a numeric id or flattened material name to a Material. + * + * @param value The value to convert. + * @return The converted Material type or null if no matching type. + */ + @Nullable + public static Material stringToMaterial(@Nullable String value) { + IdMappings.Mapping mapping = IdMappings.getById(value != null ? value : ""); + if (mapping != null) { + return Material.matchMaterial(mapping.getFlatteningType()); + } else { + return Material.matchMaterial(value != null ? value : ""); + } + } +} diff --git a/src/main/java/org/mvplugins/multiverse/core/world/config/CurrencySerializer.java b/src/main/java/org/mvplugins/multiverse/core/world/config/CurrencySerializer.java index 620b2e34..052878df 100644 --- a/src/main/java/org/mvplugins/multiverse/core/world/config/CurrencySerializer.java +++ b/src/main/java/org/mvplugins/multiverse/core/world/config/CurrencySerializer.java @@ -1,11 +1,10 @@ package org.mvplugins.multiverse.core.world.config; -import de.themoep.idconverter.IdMappings; import io.vavr.control.Option; import org.bukkit.Material; -import org.jetbrains.annotations.Nullable; import org.mvplugins.multiverse.core.configuration.functions.NodeSerializer; import org.mvplugins.multiverse.core.economy.MVEconomist; +import org.mvplugins.multiverse.core.utils.MaterialConverter; /** * Converts the material name to/from a {@link Material} enum, with the special case of "vault-economy" @@ -26,27 +25,11 @@ public class CurrencySerializer implements NodeSerializer { if (materialStr.equalsIgnoreCase(VAULT_ECONOMY_CODE)) { return MVEconomist.VAULT_ECONOMY_MATERIAL; } - return stringToMaterial(materialStr); + return MaterialConverter.stringToMaterial(materialStr); }) .getOrElse(MVEconomist.VAULT_ECONOMY_MATERIAL); } - /** - * Converts a string representing a numeric id or flattened material name to a Material. - * - * @param value The value to convert. - * @return The converted Material type or null if no matching type. - */ - @Nullable - private Material stringToMaterial(@Nullable String value) { - IdMappings.Mapping mapping = IdMappings.getById(value != null ? value : ""); - if (mapping != null) { - return Material.matchMaterial(mapping.getFlatteningType()); - } else { - return Material.matchMaterial(value != null ? value : ""); - } - } - /** * {@inheritDoc} */ diff --git a/src/test/java/org/mvplugins/multiverse/core/utils/MaterialConverterTest.kt b/src/test/java/org/mvplugins/multiverse/core/utils/MaterialConverterTest.kt new file mode 100644 index 00000000..4e00fea0 --- /dev/null +++ b/src/test/java/org/mvplugins/multiverse/core/utils/MaterialConverterTest.kt @@ -0,0 +1,29 @@ +package org.mvplugins.multiverse.core.utils + +import org.bukkit.Material +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertNull + +class MaterialConverterTest { + + @Test + fun `Convert dirt name to material`() { + assertEquals(Material.DIRT, MaterialConverter.stringToMaterial("dirt")) + } + + @Test + fun `Convert Spruce Planks numerical id to material`() { + assertEquals(Material.SPRUCE_PLANKS, MaterialConverter.stringToMaterial("5:1")) + } + + @Test + fun `Convert Oak Sapling item id to material`() { + assertEquals(Material.OAK_SAPLING, MaterialConverter.stringToMaterial("minecraft:oak_sapling")) + } + + @Test + fun `Convert invalid string to material`() { + assertNull(MaterialConverter.stringToMaterial("invalid")) + } +}