From 81ef954524507c5633943816204f1ffdc6b0f581 Mon Sep 17 00:00:00 2001 From: octarine-noise Date: Sat, 6 May 2017 09:36:32 +0200 Subject: [PATCH] add support for multipart models --- .../client/integration/RubberIntegration.kt | 3 ++- .../kotlin/mods/betterfoliage/loader/Refs.kt | 6 ++--- .../client/resource/ModelProcessor.kt | 27 ++++++++++--------- .../octarinecore/client/resource/Utils.kt | 16 +++++------ 4 files changed, 25 insertions(+), 27 deletions(-) diff --git a/src/main/kotlin/mods/betterfoliage/client/integration/RubberIntegration.kt b/src/main/kotlin/mods/betterfoliage/client/integration/RubberIntegration.kt index a663afb..2bb3c70 100644 --- a/src/main/kotlin/mods/betterfoliage/client/integration/RubberIntegration.kt +++ b/src/main/kotlin/mods/betterfoliage/client/integration/RubberIntegration.kt @@ -21,6 +21,7 @@ import mods.octarinecore.common.rotate import mods.octarinecore.metaprog.ClassRef import mods.octarinecore.metaprog.MethodRef import mods.octarinecore.metaprog.allAvailable +import mods.octarinecore.tryDefault import net.minecraft.block.properties.PropertyDirection import net.minecraft.block.state.IBlockState import net.minecraft.client.renderer.block.model.ModelResourceLocation @@ -133,7 +134,7 @@ object IC2LogSupport : RubberLogSupportBase() { override fun processModelLoad(state: IBlockState, modelLoc: ModelResourceLocation, model: IModel): RubberLogModelInfo? { // check for proper block class, existence of ModelBlock, and "state" blockstate property if (!IC2Integration.BlockRubWood.isInstance(state.block)) return null - val blockLoc = model.modelBlockAndLoc ?: return null + val blockLoc = model.modelBlockAndLoc.firstOrNull() ?: return null val type = state.properties.entries.find { it.key.getName() == "state" }?.value?.toString() ?: return null // logs with no rubber spot diff --git a/src/main/kotlin/mods/betterfoliage/loader/Refs.kt b/src/main/kotlin/mods/betterfoliage/loader/Refs.kt index ae9376e..a5de296 100644 --- a/src/main/kotlin/mods/betterfoliage/loader/Refs.kt +++ b/src/main/kotlin/mods/betterfoliage/loader/Refs.kt @@ -63,14 +63,12 @@ object Refs { val VanillaModelWrapper = ClassRef("net.minecraftforge.client.model.ModelLoader\$VanillaModelWrapper") val model_VMW = FieldRef(VanillaModelWrapper, "model", ModelBlock) val location_VMW = FieldRef(VanillaModelWrapper, "location", ModelBlock) -// val WeightedPartWrapper = ClassRef("net.minecraftforge.client.model.ModelLoader\$WeightedPartWrapper") -// val model_WPW = FieldRef(WeightedPartWrapper, "model", IModel) val WeightedRandomModel = ClassRef("net.minecraftforge.client.model.ModelLoader\$WeightedRandomModel") val models_WRM = FieldRef(WeightedRandomModel, "models", List) val MultiModel = ClassRef("net.minecraftforge.client.model.MultiModel") val base_MM = FieldRef(MultiModel, "base", IModel) - val WeightedBakedModel = ClassRef("net.minecraft.client.renderer.block.model.WeightedBakedModel") - val models_WBM = FieldRef(WeightedBakedModel, "models", List) + val MultipartModel = ClassRef("net.minecraftforge.client.model.ModelLoader\$MultipartModel") + val partModels_MPM = FieldRef(MultipartModel, "partModels", List) val resetChangedState = MethodRef(ClassRef("net.minecraftforge.common.config.Configuration"), "resetChangedState", ClassRef.void) diff --git a/src/main/kotlin/mods/octarinecore/client/resource/ModelProcessor.kt b/src/main/kotlin/mods/octarinecore/client/resource/ModelProcessor.kt index c575a8a..5527f09 100644 --- a/src/main/kotlin/mods/octarinecore/client/resource/ModelProcessor.kt +++ b/src/main/kotlin/mods/octarinecore/client/resource/ModelProcessor.kt @@ -75,23 +75,24 @@ interface TextureListModelProcessor : ModelProcessor, T2> { logger?.log(Level.DEBUG, "$logName: block state ${state.toString()}") logger?.log(Level.DEBUG, "$logName: class ${state.block.javaClass.name} matches ${matchClass.name}") - val blockLoc = model.modelBlockAndLoc - if (blockLoc == null) { + val allModels = model.modelBlockAndLoc + if (allModels.isEmpty()) { logger?.log(Level.DEBUG, "$logName: no models found") return null } - val modelMatch = modelTextures.firstOrNull { blockLoc.derivesFrom(it.modelLocation) } - if (modelMatch == null) { - logger?.log(Level.DEBUG, "$logName: no matching models found") - return null + allModels.forEach { blockLoc -> + modelTextures.firstOrNull { blockLoc.derivesFrom(it.modelLocation) }?.let{ modelMatch -> + logger?.log(Level.DEBUG, "$logName: model ${blockLoc.second} matches ${modelMatch.modelLocation.toString()}") + + val textures = modelMatch.textureNames.map { it to blockLoc.first.resolveTextureName(it) } + val texMapString = Joiner.on(", ").join(textures.map { "${it.first}=${it.second}" }) + logger?.log(Level.DEBUG, "$logName: textures [$texMapString]") + + return if (textures.all { it.second != "missingno" }) textures.map { it.second } else null + } } - logger?.log(Level.DEBUG, "$logName: model ${blockLoc.second} matches ${modelMatch.modelLocation.toString()}") - - val textures = modelMatch.textureNames.map { it to blockLoc.first.resolveTextureName(it) } - val texMapString = Joiner.on(", ").join(textures.map { "${it.first}=${it.second}" }) - logger?.log(Level.DEBUG, "$logName: textures [$texMapString]") - - return if (textures.all { it.second != "missingno" }) textures.map { it.second } else null + logger?.log(Level.DEBUG, "$logName: no matching models found") + return null } } diff --git a/src/main/kotlin/mods/octarinecore/client/resource/Utils.kt b/src/main/kotlin/mods/octarinecore/client/resource/Utils.kt index 56a9fac..499f43c 100644 --- a/src/main/kotlin/mods/octarinecore/client/resource/Utils.kt +++ b/src/main/kotlin/mods/octarinecore/client/resource/Utils.kt @@ -100,19 +100,19 @@ fun textureLocation(iconName: String) = ResourceLocation(iconName).let { } @Suppress("UNCHECKED_CAST") -val IModel.modelBlockAndLoc: Pair? get() { +val IModel.modelBlockAndLoc: List> get() { if (Refs.VanillaModelWrapper.isInstance(this)) - return Pair(Refs.model_VMW.get(this) as ModelBlock, Refs.location_VMW.get(this) as ResourceLocation) + return listOf(Pair(Refs.model_VMW.get(this) as ModelBlock, Refs.location_VMW.get(this) as ResourceLocation)) else if (Refs.WeightedRandomModel.isInstance(this)) Refs.models_WRM.get(this)?.let { - (it as List).forEach { - it.modelBlockAndLoc.let { if (it != null) return it } - } + return (it as List).flatMap(IModel::modelBlockAndLoc) } else if (Refs.MultiModel.isInstance(this)) Refs.base_MM.get(this)?.let { return (it as IModel).modelBlockAndLoc } - // TODO support net.minecraftforge.client.model.ModelLoader.MultipartModel - return null + else if (Refs.MultipartModel.isInstance(this)) Refs.partModels_MPM.get(this)?.let { + return (it as Map).flatMap { it.value.modelBlockAndLoc } + } + return listOf() } fun Pair.derivesFrom(targetLocation: ResourceLocation): Boolean { @@ -121,5 +121,3 @@ fun Pair.derivesFrom(targetLocation: ResourceLocat return Pair(first.parent, first.parentLocation!!).derivesFrom(targetLocation) return false } - -fun IModel.derivesFromModel(modelLoc: String) = modelBlockAndLoc?.derivesFrom(ResourceLocation(modelLoc)) ?: false