-
Notifications
You must be signed in to change notification settings - Fork 298
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Re-add MaterialConverter class and add tests
- Loading branch information
1 parent
e889e40
commit d415a7f
Showing
3 changed files
with
58 additions
and
19 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
src/main/java/org/mvplugins/multiverse/core/utils/MaterialConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 GitHub Actions / checkstyle / checkstyle
|
||
|
||
/** | ||
* 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 : ""); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
src/test/java/org/mvplugins/multiverse/core/utils/MaterialConverterTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) | ||
} | ||
} |