diff --git a/src/integration-test/kotlin/de/bixilon/minosoft/data/registries/blocks/properties/list/MapPropertyListTest.kt b/src/integration-test/kotlin/de/bixilon/minosoft/data/registries/blocks/properties/list/MapPropertyListTest.kt new file mode 100644 index 000000000..21efb2f17 --- /dev/null +++ b/src/integration-test/kotlin/de/bixilon/minosoft/data/registries/blocks/properties/list/MapPropertyListTest.kt @@ -0,0 +1,58 @@ +/* + * Minosoft + * Copyright (C) 2020-2023 Moritz Zwerger + * + * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with this program. If not, see . + * + * This software is not affiliated with Mojang AB, the original developer of Minecraft. + */ + +package de.bixilon.minosoft.data.registries.blocks.properties.list + +import de.bixilon.minosoft.data.direction.Directions +import de.bixilon.minosoft.data.registries.blocks.properties.EnumProperty +import de.bixilon.minosoft.data.registries.blocks.properties.primitives.BooleanProperty +import org.testng.Assert.assertEquals +import org.testng.annotations.Test + +@Test(groups = ["blocks"]) +class MapPropertyListTest { + + fun `unpacking single boolean property`() { + val list = MapPropertyList() + val property = BooleanProperty("test") + list += property + assertEquals(list.unpack().toSet(), setOf(mapOf(property to false), mapOf(property to true))) + } + + fun `unpacking single enum property`() { + val list = MapPropertyList() + val property = EnumProperty("test", Directions) + list += property + assertEquals(list.unpack().toSet(), setOf( + mapOf(property to Directions.DOWN), + mapOf(property to Directions.UP), + mapOf(property to Directions.NORTH), + mapOf(property to Directions.SOUTH), + mapOf(property to Directions.WEST), + mapOf(property to Directions.EAST), + )) + } + + fun `unpacking 2 boolean properties`() { + val list = MapPropertyList() + val a = BooleanProperty("a") + val b = BooleanProperty("b") + list += a; list += b + assertEquals(list.unpack().toSet(), setOf( + mapOf(a to false, b to false), + mapOf(a to false, b to true), + mapOf(a to true, b to false), + mapOf(a to true, b to true), + )) + } +} diff --git a/src/main/java/de/bixilon/minosoft/data/registries/blocks/properties/BlockProperties.kt b/src/main/java/de/bixilon/minosoft/data/registries/blocks/properties/BlockProperties.kt index a714320a6..15b1f2291 100644 --- a/src/main/java/de/bixilon/minosoft/data/registries/blocks/properties/BlockProperties.kt +++ b/src/main/java/de/bixilon/minosoft/data/registries/blocks/properties/BlockProperties.kt @@ -22,6 +22,7 @@ import de.bixilon.minosoft.data.registries.blocks.types.Block import de.bixilon.minosoft.data.registries.blocks.types.building.dirt.SnowyBlock import de.bixilon.minosoft.data.registries.blocks.types.building.snow.SnowLayerBlock import de.bixilon.minosoft.data.registries.blocks.types.fluid.FluidBlock +import java.util.* @Deprecated("Fallback data") object BlockProperties { @@ -117,6 +118,7 @@ object BlockProperties { val AXIS = EnumProperty("axis", Axes).register() val FACING = EnumProperty("facing", Directions).register() + val FACING_HORIZONTAL = EnumProperty("facing", Directions, EnumSet.of(Directions.NORTH, Directions.SOUTH, Directions.WEST, Directions.EAST)) val ROTATION = IntProperty("rotation").register() val ORIENTATION = EnumProperty("orientation", Orientations).register() diff --git a/src/main/java/de/bixilon/minosoft/data/registries/blocks/properties/BlockProperty.kt b/src/main/java/de/bixilon/minosoft/data/registries/blocks/properties/BlockProperty.kt index 414ecbabc..4317a52ae 100644 --- a/src/main/java/de/bixilon/minosoft/data/registries/blocks/properties/BlockProperty.kt +++ b/src/main/java/de/bixilon/minosoft/data/registries/blocks/properties/BlockProperty.kt @@ -16,7 +16,7 @@ package de.bixilon.minosoft.data.registries.blocks.properties abstract class BlockProperty( val name: String, -) { +) : Iterable { override fun toString(): String { return name diff --git a/src/main/java/de/bixilon/minosoft/data/registries/blocks/properties/EnumProperty.kt b/src/main/java/de/bixilon/minosoft/data/registries/blocks/properties/EnumProperty.kt index d9c7cbed6..5b29a94db 100644 --- a/src/main/java/de/bixilon/minosoft/data/registries/blocks/properties/EnumProperty.kt +++ b/src/main/java/de/bixilon/minosoft/data/registries/blocks/properties/EnumProperty.kt @@ -17,8 +17,8 @@ import de.bixilon.kutil.enums.ValuesEnum class EnumProperty>( name: String, - private val values: ValuesEnum, - private val allowed: Set? = null, + val values: ValuesEnum, + val allowed: Set? = null, ) : BlockProperty(name) { override fun parse(value: Any): T { @@ -36,4 +36,8 @@ class EnumProperty>( if (other.name != name) return false return other.values == values && allowed == other.allowed } + + override fun iterator(): Iterator { + return allowed?.iterator() ?: values.iterator() + } } diff --git a/src/main/java/de/bixilon/minosoft/data/registries/blocks/properties/list/BlockPropertyList.kt b/src/main/java/de/bixilon/minosoft/data/registries/blocks/properties/list/BlockPropertyList.kt index 9ff72b7bf..8c0a7fa81 100644 --- a/src/main/java/de/bixilon/minosoft/data/registries/blocks/properties/list/BlockPropertyList.kt +++ b/src/main/java/de/bixilon/minosoft/data/registries/blocks/properties/list/BlockPropertyList.kt @@ -18,8 +18,10 @@ import de.bixilon.minosoft.data.registries.blocks.properties.BlockProperty interface BlockPropertyList { operator fun get(name: String): BlockProperty<*>? + fun unpack(): List, Any>> companion object { + fun of(vararg properties: BlockProperty<*>): BlockPropertyList { if (properties.isEmpty()) return EmptyPropertyList diff --git a/src/main/java/de/bixilon/minosoft/data/registries/blocks/properties/list/EmptyPropertyList.kt b/src/main/java/de/bixilon/minosoft/data/registries/blocks/properties/list/EmptyPropertyList.kt index 498b89aba..235ad3466 100644 --- a/src/main/java/de/bixilon/minosoft/data/registries/blocks/properties/list/EmptyPropertyList.kt +++ b/src/main/java/de/bixilon/minosoft/data/registries/blocks/properties/list/EmptyPropertyList.kt @@ -13,6 +13,10 @@ package de.bixilon.minosoft.data.registries.blocks.properties.list +import de.bixilon.minosoft.data.registries.blocks.properties.BlockProperty + object EmptyPropertyList : BlockPropertyList { override fun get(name: String) = null + + override fun unpack(): List, Any>> = emptyList() } diff --git a/src/main/java/de/bixilon/minosoft/data/registries/blocks/properties/list/MapPropertyList.kt b/src/main/java/de/bixilon/minosoft/data/registries/blocks/properties/list/MapPropertyList.kt index dd6105fb9..19748112c 100644 --- a/src/main/java/de/bixilon/minosoft/data/registries/blocks/properties/list/MapPropertyList.kt +++ b/src/main/java/de/bixilon/minosoft/data/registries/blocks/properties/list/MapPropertyList.kt @@ -14,6 +14,7 @@ package de.bixilon.minosoft.data.registries.blocks.properties.list import de.bixilon.minosoft.data.registries.blocks.properties.BlockProperty +import java.util.* class MapPropertyList : BlockPropertyList { private var properties: MutableMap> = HashMap(0, 0.1f) @@ -33,4 +34,30 @@ class MapPropertyList : BlockPropertyList { if (properties.isEmpty()) return EmptyPropertyList return this } + + private fun unpack(iterator: Stack>, map: Map, Any>, entries: MutableList, Any>>) { + if (iterator.size == 0) { + entries += map + return + } + val property = iterator.pop() + for (value in property) { + val map = map.toMutableMap() + map[property] = value!! + + unpack(iterator, map, entries) + } + iterator.push(property) + } + + override fun unpack(): List, Any>> { + val entries: MutableList, Any>> = mutableListOf() + val stack = Stack>() + for (property in this.properties.values) { + stack.push(property) + } + unpack(stack, emptyMap(), entries) + + return entries + } } diff --git a/src/main/java/de/bixilon/minosoft/data/registries/blocks/properties/primitives/BooleanProperty.kt b/src/main/java/de/bixilon/minosoft/data/registries/blocks/properties/primitives/BooleanProperty.kt index 572d51d5e..ea1da38c1 100644 --- a/src/main/java/de/bixilon/minosoft/data/registries/blocks/properties/primitives/BooleanProperty.kt +++ b/src/main/java/de/bixilon/minosoft/data/registries/blocks/properties/primitives/BooleanProperty.kt @@ -17,6 +17,7 @@ import de.bixilon.kutil.primitive.BooleanUtil.toBoolean import de.bixilon.minosoft.data.registries.blocks.properties.BlockProperty class BooleanProperty(name: String) : BlockProperty(name) { + val values get() = VALUES override fun parse(value: Any): Boolean { return value.toBoolean() @@ -26,4 +27,12 @@ class BooleanProperty(name: String) : BlockProperty(name) { if (other !is BooleanProperty) return false return other.name == name } + + override fun iterator(): Iterator { + return values.iterator() + } + + companion object { + val VALUES = booleanArrayOf(false, true) + } } diff --git a/src/main/java/de/bixilon/minosoft/data/registries/blocks/properties/primitives/IntProperty.kt b/src/main/java/de/bixilon/minosoft/data/registries/blocks/properties/primitives/IntProperty.kt index 111013921..06f9d7ea0 100644 --- a/src/main/java/de/bixilon/minosoft/data/registries/blocks/properties/primitives/IntProperty.kt +++ b/src/main/java/de/bixilon/minosoft/data/registries/blocks/properties/primitives/IntProperty.kt @@ -13,6 +13,7 @@ package de.bixilon.minosoft.data.registries.blocks.properties.primitives +import de.bixilon.kutil.collections.iterator.EmptyIterator.emptyIterator import de.bixilon.kutil.primitive.IntUtil.toInt import de.bixilon.minosoft.data.registries.blocks.properties.BlockProperty @@ -34,4 +35,8 @@ class IntProperty( if (other.name != name) return false return other.range == range } + + override fun iterator(): Iterator { + return range?.iterator() ?: emptyIterator() + } }