fix loading crash with no assets

Reported-By: turtius
This commit is contained in:
Moritz Zwerger 2023-07-30 17:13:13 +02:00
parent 702aee0974
commit 7ba4e45edf
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
4 changed files with 8 additions and 7 deletions

View File

@ -40,7 +40,7 @@ class BlockModelTest {
assets.push(minosoft("models/block/named.json"), json) assets.push(minosoft("models/block/named.json"), json)
return loader.block.loadBlock(minosoft("block/named")) return loader.block.loadBlock(minosoft("block/named"))!!
} }
fun emptyModel() { fun emptyModel() {

View File

@ -198,8 +198,8 @@ data class SingleBlockStateApply(
return SingleBlockStateApply(model, uvLock, x, y) return SingleBlockStateApply(model, uvLock, x, y)
} }
fun deserialize(loader: BlockLoader, data: JsonObject): SingleBlockStateApply { fun deserialize(loader: BlockLoader, data: JsonObject): SingleBlockStateApply? {
val model = loader.loadBlock(data["model"].toString().toResourceLocation()) val model = loader.loadBlock(data["model"].toString().toResourceLocation()) ?: return null
return deserialize(model, data) return deserialize(model, data)
} }

View File

@ -53,9 +53,10 @@ data class WeightedBlockStateApply(
for (entry in data) { for (entry in data) {
var weight = entry["weight"]?.toInt() ?: 1 var weight = entry["weight"]?.toInt() ?: 1
if (weight < 0) weight = 1 if (weight < 0) weight = 1
val apply = SingleBlockStateApply.deserialize(loader, entry) val apply = SingleBlockStateApply.deserialize(loader, entry) ?: continue
models += WeightedApply(weight, apply) models += WeightedApply(weight, apply)
} }
if(models.isEmpty()) return null
return WeightedBlockStateApply(models) return WeightedBlockStateApply(models)
} }

View File

@ -27,9 +27,9 @@ class BlockLoader(private val loader: ModelLoader) {
val assets = loader.context.connection.assetsManager val assets = loader.context.connection.assetsManager
val version = loader.context.connection.version val version = loader.context.connection.version
fun loadBlock(name: ResourceLocation): BlockModel { fun loadBlock(name: ResourceLocation): BlockModel? {
val file = name.model("block/") val file = name.model("block/")
val data = assets[file].readJsonObject() val data = assets.getOrNull(file)?.readJsonObject() ?: return null
val parent = data["parent"]?.toString()?.let { loadBlock(it.toResourceLocation()) } val parent = data["parent"]?.toString()?.let { loadBlock(it.toResourceLocation()) }
@ -39,7 +39,7 @@ class BlockLoader(private val loader: ModelLoader) {
fun loadState(block: Block): DirectBlockModel? { fun loadState(block: Block): DirectBlockModel? {
val file = (if (block is CustomBlockModel) block.getModelName(version) else block.identifier)?.blockState() ?: return null val file = (if (block is CustomBlockModel) block.getModelName(version) else block.identifier)?.blockState() ?: return null
val data = assets[file].readJsonObject() val data = assets.getOrNull(file)?.readJsonObject() ?: return null
return DirectBlockModel.deserialize(this, data) return DirectBlockModel.deserialize(this, data)
} }