diff --git a/src/main/java/de/bixilon/minosoft/data/mappings/blocks/BlockState.kt b/src/main/java/de/bixilon/minosoft/data/mappings/blocks/BlockState.kt index 1f5526b54..fed97104a 100644 --- a/src/main/java/de/bixilon/minosoft/data/mappings/blocks/BlockState.kt +++ b/src/main/java/de/bixilon/minosoft/data/mappings/blocks/BlockState.kt @@ -30,12 +30,10 @@ import de.bixilon.minosoft.gui.rendering.chunk.models.loading.BlockModel import de.bixilon.minosoft.gui.rendering.chunk.models.renderable.BlockLikeRenderer import de.bixilon.minosoft.gui.rendering.chunk.models.renderable.BlockRenderer import de.bixilon.minosoft.gui.rendering.chunk.models.renderable.MultipartRenderer -import de.bixilon.minosoft.util.enum.ValuesEnum import glm_.vec3.Vec3i import java.util.* import kotlin.math.abs import kotlin.random.Random -import kotlin.reflect.full.companionObjectInstance data class BlockState( val block: Block, @@ -269,24 +267,16 @@ data class BlockState( fun cycle(property: BlockProperties): BlockState { val currentValue = properties[property] ?: throw IllegalArgumentException("$this has no property $property") - when (currentValue) { - is Boolean -> { - return withProperties(property to !currentValue) - } - is Number -> { - return try { - withProperties(property to (currentValue.toInt() + 1)) - } catch (exception: IllegalArgumentException) { - withProperties(property to 0) - } - } - is Enum<*> -> { - val values = currentValue::class.companionObjectInstance as ValuesEnum> - return withProperties(property to values.next(currentValue)) - } - else -> { - return this - } + return withProperties(property to block.properties[property]!!.next(currentValue)) + } + + private fun List.next(current: T): T { + val index = this.indexOf(current) + check(index >= 0) { "List does not contain $current" } + + if (index == this.size - 1) { + return this[0] } + return this[index + 1] } } diff --git a/src/main/java/de/bixilon/minosoft/data/mappings/blocks/types/Block.kt b/src/main/java/de/bixilon/minosoft/data/mappings/blocks/types/Block.kt index 5e702e47a..bd81d2cf1 100644 --- a/src/main/java/de/bixilon/minosoft/data/mappings/blocks/types/Block.kt +++ b/src/main/java/de/bixilon/minosoft/data/mappings/blocks/types/Block.kt @@ -54,6 +54,7 @@ open class Block( protected set open lateinit var item: Item protected set + open lateinit var properties: Map> override fun postInit(registries: Registries) { item = registries.itemRegistry[itemId] @@ -107,6 +108,7 @@ open class Block( else -> Block(resourceLocation, mappings, data) } + val properties: MutableMap> = mutableMapOf() val states: MutableSet = mutableSetOf() for ((stateId, stateJson) in data["states"].asJsonObject.entrySet()) { @@ -114,10 +116,20 @@ open class Block( val state = BlockState.deserialize(block, mappings, stateJson, mappings.models) mappings.blockStateIdMap[stateId.toInt()] = state states.add(state) + for ((property, value) in state.properties) { + properties.getOrPut(property) { mutableSetOf() } += value + } + } + + val propertiesOut: MutableMap> = mutableMapOf() + + for ((property, values) in properties) { + propertiesOut[property] = values.toList() } block.states = states.toSet() block.defaultState = mappings.blockStateIdMap[data["default_state"].asInt]!! + block.properties = propertiesOut.toMap() return block } }