diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/utils/biomes/BiomeMap.java b/src/main/java/io/github/thebusybiscuit/slimefun4/utils/biomes/BiomeMap.java index 60c8488a9..4c7413ea9 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/utils/biomes/BiomeMap.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/utils/biomes/BiomeMap.java @@ -72,6 +72,12 @@ public class BiomeMap implements Keyed { dataMap.putAll(map.dataMap); } + public boolean remove(@Nonnull Biome biome) { + Validate.notNull(biome, "The biome cannot be null."); + + return dataMap.remove(biome) != null; + } + /** * {@inheritDoc} */ diff --git a/src/test/java/io/github/thebusybiscuit/slimefun4/utils/biomes/TestBiomeMapParser.java b/src/test/java/io/github/thebusybiscuit/slimefun4/utils/biomes/TestBiomeMapParser.java index aaa436432..71d9dd235 100644 --- a/src/test/java/io/github/thebusybiscuit/slimefun4/utils/biomes/TestBiomeMapParser.java +++ b/src/test/java/io/github/thebusybiscuit/slimefun4/utils/biomes/TestBiomeMapParser.java @@ -2,9 +2,11 @@ package io.github.thebusybiscuit.slimefun4.utils.biomes; import java.util.function.Function; +import javax.annotation.Nonnull; import javax.annotation.ParametersAreNonnullByDefault; import org.bukkit.NamespacedKey; +import org.bukkit.block.Biome; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeAll; @@ -69,6 +71,91 @@ class TestBiomeMapParser { assertMisconfiguration(AS_STRING, "[{\"biomes\": []}]"); } + @Test + @DisplayName("Test Biome entry being misconfigured") + void testInvalidBiomeEntry() { + assertMisconfiguration(AS_STRING, "[{\"value\": \"hello\", \"biomes\": {}}]"); + assertMisconfiguration(AS_STRING, "[{\"value\": \"hello\", \"biomes\": 1234}]"); + assertMisconfiguration(AS_STRING, "[{\"value\": \"hello\", \"biomes\": \"I am Groot.\"}]"); + } + + @Test + @DisplayName("Test unknown Biomes") + void testUnknownBiomes() { + assertMisconfiguration(AS_INT, "[{\"value\": 1, \"biomes\": [1, 2, 3]}]"); + assertMisconfiguration(AS_INT, "[{\"value\": 1, \"biomes\": [\"hello world\"]}]"); + assertMisconfiguration(AS_INT, "[{\"value\": 1, \"biomes\": [\"slimefun:slime_forest\"]}]"); + assertMisconfiguration(AS_INT, "[{\"value\": 1, \"biomes\": [\"minecraft:walshy_desert\"]}]"); + } + + @Test + @DisplayName("Test double Biome registration") + void testDoubleBiomes() { + Biome biome = Biome.OCEAN; + + assertMisconfiguration(AS_INT, "[{\"value\": 1, \"biomes\": [\"" + biome.getKey() + "\"]}, {\"value\": 2, \"biomes\": [\"" + biome.getKey() + "\"]}]"); + } + + @Test + @DisplayName("Test different value converters") + void testValueConversion() { + Biome biome = Biome.OCEAN; + + String stringValue = "hello world"; + BiomeMap stringMap = createBiomeMap(AS_STRING, "[{\"value\": \"" + stringValue + "\", \"biomes\": [\"" + biome.getKey() + "\"]}]"); + Assertions.assertEquals(stringValue, stringMap.get(biome)); + + int intValue = 1024; + BiomeMap intMap = createBiomeMap(AS_INT, "[{\"value\": \"" + intValue + "\", \"biomes\": [\"" + biome.getKey() + "\"]}]"); + Assertions.assertEquals(intValue, intMap.get(biome)); + } + + @Test + @DisplayName("Test working BiomeMap from JSON") + void testBiomeMapFromJson() { + Biome biome1 = Biome.JUNGLE; + Biome biome2 = Biome.OCEAN; + Biome biome3 = Biome.DESERT; + BiomeMap biomes = createBiomeMap(AS_INT, "[{\"value\":1,\"biomes\":[\"" + biome1.getKey() + "\", \"" + biome2.getKey() + "\"]}, {\"value\":2, \"biomes\":[\"" + biome3.getKey() + "\"]}]"); + + Assertions.assertTrue(biomes.contains(biome1)); + Assertions.assertEquals(1, biomes.get(biome1)); + + Assertions.assertTrue(biomes.contains(biome2)); + Assertions.assertEquals(1, biomes.get(biome2)); + + Assertions.assertTrue(biomes.contains(biome3)); + Assertions.assertEquals(2, biomes.get(biome3)); + } + + @Test + @DisplayName("Test working BiomeMap manually") + void testManualBiomeMap() { + BiomeMap biomes = new BiomeMap<>(key); + Biome biome = Biome.OCEAN; + String value = "Under the sea"; + + Assertions.assertTrue(biomes.put(biome, value)); + Assertions.assertTrue(biomes.contains(biome)); + Assertions.assertEquals(value, biomes.get(biome)); + + Assertions.assertTrue(biomes.remove(biome)); + Assertions.assertFalse(biomes.contains(biome)); + } + + @ParametersAreNonnullByDefault + private @Nonnull BiomeMap createBiomeMap(Function function, String json) { + BiomeMapParser parser = new BiomeMapParser<>(key, function); + + try { + parser.read(json); + } catch (BiomeMapException e) { + Assertions.fail(e); + } + + return parser.buildBiomeMap(); + } + @ParametersAreNonnullByDefault private void assertMisconfiguration(Function function, String json) { BiomeMapParser parser = new BiomeMapParser<>(key, function);