mirror of
https://gitlab.bixilon.de/bixilon/minosoft.git
synced 2025-09-13 09:26:11 -04:00
improve block property cycling
This commit is contained in:
parent
4875909966
commit
868e902fa4
@ -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<Enum<*>>
|
||||
return withProperties(property to values.next(currentValue))
|
||||
}
|
||||
else -> {
|
||||
return this
|
||||
}
|
||||
return withProperties(property to block.properties[property]!!.next(currentValue))
|
||||
}
|
||||
|
||||
private fun <T> List<T>.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]
|
||||
}
|
||||
}
|
||||
|
@ -54,6 +54,7 @@ open class Block(
|
||||
protected set
|
||||
open lateinit var item: Item
|
||||
protected set
|
||||
open lateinit var properties: Map<BlockProperties, List<Any>>
|
||||
|
||||
override fun postInit(registries: Registries) {
|
||||
item = registries.itemRegistry[itemId]
|
||||
@ -107,6 +108,7 @@ open class Block(
|
||||
else -> Block(resourceLocation, mappings, data)
|
||||
}
|
||||
|
||||
val properties: MutableMap<BlockProperties, MutableSet<Any>> = mutableMapOf()
|
||||
|
||||
val states: MutableSet<BlockState> = 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<BlockProperties, List<Any>> = 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
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user