Skip to content

Commit

Permalink
Re-add MaterialConverter class and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
benwoo1110 committed Nov 17, 2024
1 parent e889e40 commit d415a7f
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -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 {

Check warning on line 10 in src/main/java/org/mvplugins/multiverse/core/utils/MaterialConverter.java

View workflow job for this annotation

GitHub Actions / checkstyle / checkstyle

[checkstyle] reported by reviewdog 🐶 Utility classes should not have a public or default constructor. Raw Output: /github/workspace/./src/main/java/org/mvplugins/multiverse/core/utils/MaterialConverter.java:10:1: warning: Utility classes should not have a public or default constructor. (com.puppycrawl.tools.checkstyle.checks.design.HideUtilityClassConstructorCheck)

/**
* 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 : "");
}
}
}
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -26,27 +25,11 @@ public Material deserialize(Object object, Class<Material> type) {
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}
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -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"))
}
}

0 comments on commit d415a7f

Please sign in to comment.