From d415a7fc5c6d672e6dbff21e1d638daa22ddc286 Mon Sep 17 00:00:00 2001 From: Ben Woo <30431861+benwoo1110@users.noreply.github.com> Date: Sun, 17 Nov 2024 15:05:12 +0800 Subject: [PATCH] Re-add MaterialConverter class and add tests --- .../core/utils/MaterialConverter.java | 27 +++++++++++++++++ .../core/world/config/CurrencySerializer.java | 21 ++------------ .../core/utils/MaterialConverterTest.kt | 29 +++++++++++++++++++ 3 files changed, 58 insertions(+), 19 deletions(-) create mode 100644 src/main/java/org/mvplugins/multiverse/core/utils/MaterialConverter.java create mode 100644 src/test/java/org/mvplugins/multiverse/core/utils/MaterialConverterTest.kt 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")) + } +}