Skip to content

Commit faf8d1a

Browse files
authored
Merge pull request #3129 from Multiverse/ben/mv5/material-converter
Re-add MaterialConverter class and add tests
2 parents 1eeb265 + d415a7f commit faf8d1a

File tree

3 files changed

+58
-19
lines changed

3 files changed

+58
-19
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package org.mvplugins.multiverse.core.utils;
2+
3+
import de.themoep.idconverter.IdMappings;
4+
import org.bukkit.Material;
5+
import org.jetbrains.annotations.Nullable;
6+
7+
/**
8+
* A tool for converting values which may be an old type ID to a Material.
9+
*/
10+
public class MaterialConverter {
11+
12+
/**
13+
* Converts a string representing a numeric id or flattened material name to a Material.
14+
*
15+
* @param value The value to convert.
16+
* @return The converted Material type or null if no matching type.
17+
*/
18+
@Nullable
19+
public static Material stringToMaterial(@Nullable String value) {
20+
IdMappings.Mapping mapping = IdMappings.getById(value != null ? value : "");
21+
if (mapping != null) {
22+
return Material.matchMaterial(mapping.getFlatteningType());
23+
} else {
24+
return Material.matchMaterial(value != null ? value : "");
25+
}
26+
}
27+
}

src/main/java/org/mvplugins/multiverse/core/world/config/CurrencySerializer.java

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
package org.mvplugins.multiverse.core.world.config;
22

3-
import de.themoep.idconverter.IdMappings;
43
import io.vavr.control.Option;
54
import org.bukkit.Material;
6-
import org.jetbrains.annotations.Nullable;
75
import org.mvplugins.multiverse.core.configuration.functions.NodeSerializer;
86
import org.mvplugins.multiverse.core.economy.MVEconomist;
7+
import org.mvplugins.multiverse.core.utils.MaterialConverter;
98

109
/**
1110
* Converts the material name to/from a {@link Material} enum, with the special case of "vault-economy"
@@ -26,27 +25,11 @@ public Material deserialize(Object object, Class<Material> type) {
2625
if (materialStr.equalsIgnoreCase(VAULT_ECONOMY_CODE)) {
2726
return MVEconomist.VAULT_ECONOMY_MATERIAL;
2827
}
29-
return stringToMaterial(materialStr);
28+
return MaterialConverter.stringToMaterial(materialStr);
3029
})
3130
.getOrElse(MVEconomist.VAULT_ECONOMY_MATERIAL);
3231
}
3332

34-
/**
35-
* Converts a string representing a numeric id or flattened material name to a Material.
36-
*
37-
* @param value The value to convert.
38-
* @return The converted Material type or null if no matching type.
39-
*/
40-
@Nullable
41-
private Material stringToMaterial(@Nullable String value) {
42-
IdMappings.Mapping mapping = IdMappings.getById(value != null ? value : "");
43-
if (mapping != null) {
44-
return Material.matchMaterial(mapping.getFlatteningType());
45-
} else {
46-
return Material.matchMaterial(value != null ? value : "");
47-
}
48-
}
49-
5033
/**
5134
* {@inheritDoc}
5235
*/
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package org.mvplugins.multiverse.core.utils
2+
3+
import org.bukkit.Material
4+
import kotlin.test.Test
5+
import kotlin.test.assertEquals
6+
import kotlin.test.assertNull
7+
8+
class MaterialConverterTest {
9+
10+
@Test
11+
fun `Convert dirt name to material`() {
12+
assertEquals(Material.DIRT, MaterialConverter.stringToMaterial("dirt"))
13+
}
14+
15+
@Test
16+
fun `Convert Spruce Planks numerical id to material`() {
17+
assertEquals(Material.SPRUCE_PLANKS, MaterialConverter.stringToMaterial("5:1"))
18+
}
19+
20+
@Test
21+
fun `Convert Oak Sapling item id to material`() {
22+
assertEquals(Material.OAK_SAPLING, MaterialConverter.stringToMaterial("minecraft:oak_sapling"))
23+
}
24+
25+
@Test
26+
fun `Convert invalid string to material`() {
27+
assertNull(MaterialConverter.stringToMaterial("invalid"))
28+
}
29+
}

0 commit comments

Comments
 (0)