Merge pull request #3129 from Multiverse/ben/mv5/material-converter

Re-add MaterialConverter class and add tests
This commit is contained in:
Ben Woo 2024-11-21 11:49:24 +08:00 committed by GitHub
commit faf8d1a7d0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 58 additions and 19 deletions

View File

@ -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 : "");
}
}
}

View File

@ -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<Material> {
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}
*/

View File

@ -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"))
}
}