mirror of
https://gitlab.bixilon.de/bixilon/minosoft.git
synced 2025-09-14 01:48:04 -04:00
block property iteration and unpacking
This commit is contained in:
parent
abdc3d143c
commit
4b3298c6f3
@ -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),
|
||||||
|
))
|
||||||
|
}
|
||||||
|
}
|
@ -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.dirt.SnowyBlock
|
||||||
import de.bixilon.minosoft.data.registries.blocks.types.building.snow.SnowLayerBlock
|
import de.bixilon.minosoft.data.registries.blocks.types.building.snow.SnowLayerBlock
|
||||||
import de.bixilon.minosoft.data.registries.blocks.types.fluid.FluidBlock
|
import de.bixilon.minosoft.data.registries.blocks.types.fluid.FluidBlock
|
||||||
|
import java.util.*
|
||||||
|
|
||||||
@Deprecated("Fallback data")
|
@Deprecated("Fallback data")
|
||||||
object BlockProperties {
|
object BlockProperties {
|
||||||
@ -117,6 +118,7 @@ object BlockProperties {
|
|||||||
|
|
||||||
val AXIS = EnumProperty("axis", Axes).register()
|
val AXIS = EnumProperty("axis", Axes).register()
|
||||||
val FACING = EnumProperty("facing", Directions).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 ROTATION = IntProperty("rotation").register()
|
||||||
val ORIENTATION = EnumProperty("orientation", Orientations).register()
|
val ORIENTATION = EnumProperty("orientation", Orientations).register()
|
||||||
|
|
||||||
|
@ -16,7 +16,7 @@ package de.bixilon.minosoft.data.registries.blocks.properties
|
|||||||
|
|
||||||
abstract class BlockProperty<T>(
|
abstract class BlockProperty<T>(
|
||||||
val name: String,
|
val name: String,
|
||||||
) {
|
) : Iterable<T> {
|
||||||
|
|
||||||
override fun toString(): String {
|
override fun toString(): String {
|
||||||
return name
|
return name
|
||||||
|
@ -17,8 +17,8 @@ import de.bixilon.kutil.enums.ValuesEnum
|
|||||||
|
|
||||||
class EnumProperty<T : Enum<*>>(
|
class EnumProperty<T : Enum<*>>(
|
||||||
name: String,
|
name: String,
|
||||||
private val values: ValuesEnum<T>,
|
val values: ValuesEnum<T>,
|
||||||
private val allowed: Set<T>? = null,
|
val allowed: Set<T>? = null,
|
||||||
) : BlockProperty<T>(name) {
|
) : BlockProperty<T>(name) {
|
||||||
|
|
||||||
override fun parse(value: Any): T {
|
override fun parse(value: Any): T {
|
||||||
@ -36,4 +36,8 @@ class EnumProperty<T : Enum<*>>(
|
|||||||
if (other.name != name) return false
|
if (other.name != name) return false
|
||||||
return other.values == values && allowed == other.allowed
|
return other.values == values && allowed == other.allowed
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun iterator(): Iterator<T> {
|
||||||
|
return allowed?.iterator() ?: values.iterator()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -18,8 +18,10 @@ import de.bixilon.minosoft.data.registries.blocks.properties.BlockProperty
|
|||||||
interface BlockPropertyList {
|
interface BlockPropertyList {
|
||||||
operator fun get(name: String): BlockProperty<*>?
|
operator fun get(name: String): BlockProperty<*>?
|
||||||
|
|
||||||
|
fun unpack(): List<Map<BlockProperty<*>, Any>>
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
|
|
||||||
fun of(vararg properties: BlockProperty<*>): BlockPropertyList {
|
fun of(vararg properties: BlockProperty<*>): BlockPropertyList {
|
||||||
if (properties.isEmpty()) return EmptyPropertyList
|
if (properties.isEmpty()) return EmptyPropertyList
|
||||||
|
|
||||||
|
@ -13,6 +13,10 @@
|
|||||||
|
|
||||||
package de.bixilon.minosoft.data.registries.blocks.properties.list
|
package de.bixilon.minosoft.data.registries.blocks.properties.list
|
||||||
|
|
||||||
|
import de.bixilon.minosoft.data.registries.blocks.properties.BlockProperty
|
||||||
|
|
||||||
object EmptyPropertyList : BlockPropertyList {
|
object EmptyPropertyList : BlockPropertyList {
|
||||||
override fun get(name: String) = null
|
override fun get(name: String) = null
|
||||||
|
|
||||||
|
override fun unpack(): List<Map<BlockProperty<*>, Any>> = emptyList()
|
||||||
}
|
}
|
||||||
|
@ -14,6 +14,7 @@
|
|||||||
package de.bixilon.minosoft.data.registries.blocks.properties.list
|
package de.bixilon.minosoft.data.registries.blocks.properties.list
|
||||||
|
|
||||||
import de.bixilon.minosoft.data.registries.blocks.properties.BlockProperty
|
import de.bixilon.minosoft.data.registries.blocks.properties.BlockProperty
|
||||||
|
import java.util.*
|
||||||
|
|
||||||
class MapPropertyList : BlockPropertyList {
|
class MapPropertyList : BlockPropertyList {
|
||||||
private var properties: MutableMap<String, BlockProperty<*>> = HashMap(0, 0.1f)
|
private var properties: MutableMap<String, BlockProperty<*>> = HashMap(0, 0.1f)
|
||||||
@ -33,4 +34,30 @@ class MapPropertyList : BlockPropertyList {
|
|||||||
if (properties.isEmpty()) return EmptyPropertyList
|
if (properties.isEmpty()) return EmptyPropertyList
|
||||||
return this
|
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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -17,6 +17,7 @@ import de.bixilon.kutil.primitive.BooleanUtil.toBoolean
|
|||||||
import de.bixilon.minosoft.data.registries.blocks.properties.BlockProperty
|
import de.bixilon.minosoft.data.registries.blocks.properties.BlockProperty
|
||||||
|
|
||||||
class BooleanProperty(name: String) : BlockProperty<Boolean>(name) {
|
class BooleanProperty(name: String) : BlockProperty<Boolean>(name) {
|
||||||
|
val values get() = VALUES
|
||||||
|
|
||||||
override fun parse(value: Any): Boolean {
|
override fun parse(value: Any): Boolean {
|
||||||
return value.toBoolean()
|
return value.toBoolean()
|
||||||
@ -26,4 +27,12 @@ class BooleanProperty(name: String) : BlockProperty<Boolean>(name) {
|
|||||||
if (other !is BooleanProperty) return false
|
if (other !is BooleanProperty) return false
|
||||||
return other.name == name
|
return other.name == name
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun iterator(): Iterator<Boolean> {
|
||||||
|
return values.iterator()
|
||||||
|
}
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
val VALUES = booleanArrayOf(false, true)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -13,6 +13,7 @@
|
|||||||
|
|
||||||
package de.bixilon.minosoft.data.registries.blocks.properties.primitives
|
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.kutil.primitive.IntUtil.toInt
|
||||||
import de.bixilon.minosoft.data.registries.blocks.properties.BlockProperty
|
import de.bixilon.minosoft.data.registries.blocks.properties.BlockProperty
|
||||||
|
|
||||||
@ -34,4 +35,8 @@ class IntProperty(
|
|||||||
if (other.name != name) return false
|
if (other.name != name) return false
|
||||||
return other.range == range
|
return other.range == range
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun iterator(): Iterator<Int> {
|
||||||
|
return range?.iterator() ?: emptyIterator()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user