diff --git a/src/main/java/de/bixilon/minosoft/data/registries/blocks/BlockState.kt b/src/main/java/de/bixilon/minosoft/data/registries/blocks/BlockState.kt index 32940c57f..87a43aaad 100644 --- a/src/main/java/de/bixilon/minosoft/data/registries/blocks/BlockState.kt +++ b/src/main/java/de/bixilon/minosoft/data/registries/blocks/BlockState.kt @@ -107,9 +107,9 @@ data class BlockState( fun Any.asShape(): VoxelShape { return if (this is Int) { - registries.shapes[this] + registries.shape[this] } else { - VoxelShape(registries.shapes, this) + VoxelShape(registries.shape, this) } } diff --git a/src/main/java/de/bixilon/minosoft/data/registries/registries/Registries.kt b/src/main/java/de/bixilon/minosoft/data/registries/registries/Registries.kt index 78480b75e..a7d4ad673 100644 --- a/src/main/java/de/bixilon/minosoft/data/registries/registries/Registries.kt +++ b/src/main/java/de/bixilon/minosoft/data/registries/registries/Registries.kt @@ -12,7 +12,6 @@ */ package de.bixilon.minosoft.data.registries.registries -import de.bixilon.kutil.array.ArrayUtil.cast import de.bixilon.kutil.cast.CastUtil.unsafeCast import de.bixilon.kutil.concurrent.pool.DefaultThreadPool import de.bixilon.kutil.concurrent.worker.task.TaskWorker @@ -54,8 +53,7 @@ import de.bixilon.minosoft.data.registries.materials.Material import de.bixilon.minosoft.data.registries.misc.MiscData import de.bixilon.minosoft.data.registries.particle.ParticleType import de.bixilon.minosoft.data.registries.registries.registry.* -import de.bixilon.minosoft.data.registries.shapes.AABB -import de.bixilon.minosoft.data.registries.shapes.VoxelShape +import de.bixilon.minosoft.data.registries.shapes.ShapeRegistry import de.bixilon.minosoft.data.registries.sound.SoundGroup import de.bixilon.minosoft.data.registries.statistics.Statistic import de.bixilon.minosoft.datafixer.enumeration.EntityDataTypesFixer @@ -77,7 +75,9 @@ import kotlin.reflect.jvm.javaField class Registries : Parentable { val registries: MutableMap> = mutableMapOf() - var shapes: Array = emptyArray() + + val shape = ShapeRegistry() + val motif: Registry = register("motif", Registry(codec = Motif)) val block: Registry = register("block", Registry(flattened = true, codec = Block, metaType = MetaTypes.BLOCKS)) val item: ItemRegistry = register("item", ItemRegistry()) @@ -160,7 +160,7 @@ class Registries : Parentable { val worker = TaskWorker(errorHandler = { _, it -> if (error == null) error = it }, criticalErrorHandler = { _, it -> if (error == null) error = it }) val stopwatch = Stopwatch() // enums - worker += WorkerTask(this::shapes) { loadShapes(pixlyzerData["shapes"]?.toJsonObject()) } + worker += WorkerTask(this::shape) { this.shape.load(pixlyzerData["shapes"]?.toJsonObject()) } worker += WorkerTask(this::equipmentSlot) { equipmentSlot.initialize(pixlyzerData["equipment_slots"]) } worker += WorkerTask(this::handEquipmentSlot) { handEquipmentSlot.initialize(pixlyzerData["hand_equipment_slots"]) } @@ -194,7 +194,7 @@ class Registries : Parentable { worker += WorkerTask(this::biome) { biome.rawUpdate(pixlyzerData["biomes"]?.toJsonObject(), this) } worker += WorkerTask(this::dimension) { dimension.rawUpdate(pixlyzerData["dimensions"]?.toJsonObject(), this) } worker += WorkerTask(this::fluid) { fluid.rawUpdate(pixlyzerData["fluids"]?.toJsonObject(), this) } - worker += WorkerTask(this::block, dependencies = arrayOf(this::material, this::fluid, this::shapes, this::soundGroup, this::particleType)) { block.rawUpdate(pixlyzerData["blocks"]?.toJsonObject(), this) } + worker += WorkerTask(this::block, dependencies = arrayOf(this::material, this::fluid, this::shape, this::soundGroup, this::particleType)) { block.rawUpdate(pixlyzerData["blocks"]?.toJsonObject(), this) } worker += WorkerTask(this::item, dependencies = arrayOf(this::material, this::block, this::entityType, this::fluid, this::statusEffect, this::soundEvent)) { item.rawUpdate(pixlyzerData["items"]?.toJsonObject(), this) } worker += WorkerTask(this::blockEntityType, dependencies = arrayOf(this::block)) { blockEntityType.rawUpdate(pixlyzerData["block_entities"]?.toJsonObject(), this) } @@ -232,30 +232,10 @@ class Registries : Parentable { inner.dec() inner.await() isFullyLoaded = true - shapes = emptyArray() + shape.cleanup() Log.log(LogMessageType.VERSION_LOADING, LogLevels.INFO) { "Registries for $version loaded in ${stopwatch.totalTime()}" } } - private fun loadShapes(data: Map?) { - data ?: return - val aabbs = loadAABBs(data["aabbs"].unsafeCast()) - loadVoxelShapes(data["shapes"].unsafeCast(), aabbs) - } - - private fun loadVoxelShapes(data: Collection, aabbs: Array) { - this.shapes = arrayOfNulls(data.size).cast() - for ((index, shape) in data.withIndex()) { - this.shapes[index] = VoxelShape(shape, aabbs) - } - } - - private fun loadAABBs(data: Collection>): Array { - val aabbs: Array = arrayOfNulls(data.size) - for ((index, aabb) in data.withIndex()) { - aabbs[index] = AABB(aabb) - } - return aabbs.cast() - } fun clear() { for (field in this::class.java.fields) { diff --git a/src/main/java/de/bixilon/minosoft/data/registries/shapes/ShapeRegistry.kt b/src/main/java/de/bixilon/minosoft/data/registries/shapes/ShapeRegistry.kt new file mode 100644 index 000000000..8ea74dbb2 --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/data/registries/shapes/ShapeRegistry.kt @@ -0,0 +1,56 @@ +/* + * 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.shapes + +import de.bixilon.kutil.array.ArrayUtil.cast +import de.bixilon.kutil.cast.CastUtil.unsafeCast +import de.bixilon.kutil.json.JsonObject + +class ShapeRegistry { + private var shapes: Array = emptyArray() + + fun load(data: JsonObject?) { + if (data == null) { + return + } + val aabbs = loadAABBs(data["aabbs"].unsafeCast()) + loadShapes(data["shapes"].unsafeCast(), aabbs) + } + + private fun loadShapes(data: Collection, aabbs: Array) { + this.shapes = arrayOfNulls(data.size).cast() + + for ((index, shape) in data.withIndex()) { + this.shapes[index] = VoxelShape(shape, aabbs) + } + } + + private fun loadAABBs(data: Collection>): Array { + val aabbs: Array = arrayOfNulls(data.size) + + for ((index, aabb) in data.withIndex()) { + aabbs[index] = AABB(aabb) + } + return aabbs.cast() + } + + fun cleanup() { + this.shapes = emptyArray() + } + + + operator fun get(index: Int): VoxelShape { + return shapes[index] + } +} diff --git a/src/main/java/de/bixilon/minosoft/data/registries/shapes/VoxelShape.kt b/src/main/java/de/bixilon/minosoft/data/registries/shapes/VoxelShape.kt index 1605f3eb7..b6eca6fa5 100644 --- a/src/main/java/de/bixilon/minosoft/data/registries/shapes/VoxelShape.kt +++ b/src/main/java/de/bixilon/minosoft/data/registries/shapes/VoxelShape.kt @@ -1,6 +1,6 @@ /* * Minosoft - * Copyright (C) 2020-2022 Moritz Zwerger + * 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. * @@ -46,7 +46,7 @@ class VoxelShape(private val aabbs: MutableList = mutableListOf()) : Itera } // somehow, the kotlin compiler gives an error if both constructors have the "same" signature JsonElement, List<> - constructor(voxelShapes: Array, data: Any) : this() { + constructor(voxelShapes: ShapeRegistry, data: Any) : this() { when (data) { is Collection<*> -> { for (index in data) {