block property iteration and unpacking

This commit is contained in:
Moritz Zwerger 2023-10-14 00:05:56 +02:00
parent abdc3d143c
commit 4b3298c6f3
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
9 changed files with 114 additions and 3 deletions

View File

@ -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 <https://www.gnu.org/licenses/>.
*
* 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),
))
}
}

View File

@ -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()

View File

@ -16,7 +16,7 @@ package de.bixilon.minosoft.data.registries.blocks.properties
abstract class BlockProperty<T>(
val name: String,
) {
) : Iterable<T> {
override fun toString(): String {
return name

View File

@ -17,8 +17,8 @@ import de.bixilon.kutil.enums.ValuesEnum
class EnumProperty<T : Enum<*>>(
name: String,
private val values: ValuesEnum<T>,
private val allowed: Set<T>? = null,
val values: ValuesEnum<T>,
val allowed: Set<T>? = null,
) : BlockProperty<T>(name) {
override fun parse(value: Any): T {
@ -36,4 +36,8 @@ class EnumProperty<T : Enum<*>>(
if (other.name != name) return false
return other.values == values && allowed == other.allowed
}
override fun iterator(): Iterator<T> {
return allowed?.iterator() ?: values.iterator()
}
}

View File

@ -18,8 +18,10 @@ import de.bixilon.minosoft.data.registries.blocks.properties.BlockProperty
interface BlockPropertyList {
operator fun get(name: String): BlockProperty<*>?
fun unpack(): List<Map<BlockProperty<*>, Any>>
companion object {
fun of(vararg properties: BlockProperty<*>): BlockPropertyList {
if (properties.isEmpty()) return EmptyPropertyList

View File

@ -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<Map<BlockProperty<*>, Any>> = emptyList()
}

View File

@ -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<String, BlockProperty<*>> = HashMap(0, 0.1f)
@ -33,4 +34,30 @@ class MapPropertyList : BlockPropertyList {
if (properties.isEmpty()) return EmptyPropertyList
return this
}
private fun unpack(iterator: Stack<BlockProperty<*>>, map: Map<BlockProperty<*>, Any>, entries: MutableList<Map<BlockProperty<*>, 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<Map<BlockProperty<*>, Any>> {
val entries: MutableList<Map<BlockProperty<*>, Any>> = mutableListOf()
val stack = Stack<BlockProperty<*>>()
for (property in this.properties.values) {
stack.push(property)
}
unpack(stack, emptyMap(), entries)
return entries
}
}

View File

@ -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<Boolean>(name) {
val values get() = VALUES
override fun parse(value: Any): Boolean {
return value.toBoolean()
@ -26,4 +27,12 @@ class BooleanProperty(name: String) : BlockProperty<Boolean>(name) {
if (other !is BooleanProperty) return false
return other.name == name
}
override fun iterator(): Iterator<Boolean> {
return values.iterator()
}
companion object {
val VALUES = booleanArrayOf(false, true)
}
}

View File

@ -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<Int> {
return range?.iterator() ?: emptyIterator()
}
}