diff options
Diffstat (limited to 'src/main/kotlin')
-rw-r--r-- | src/main/kotlin/events/CustomItemModelEvent.kt | 2 | ||||
-rw-r--r-- | src/main/kotlin/features/texturepack/BakedModelExtra.kt | 23 | ||||
-rw-r--r-- | src/main/kotlin/features/texturepack/TintOverrides.kt | 18 | ||||
-rw-r--r-- | src/main/kotlin/util/ErrorUtil.kt | 25 |
4 files changed, 54 insertions, 14 deletions
diff --git a/src/main/kotlin/events/CustomItemModelEvent.kt b/src/main/kotlin/events/CustomItemModelEvent.kt index d5c08f4..4328d77 100644 --- a/src/main/kotlin/events/CustomItemModelEvent.kt +++ b/src/main/kotlin/events/CustomItemModelEvent.kt @@ -17,7 +17,7 @@ data class CustomItemModelEvent( val cache = WeakCache.memoize<ItemStack, ItemModels, Optional<BakedModel>>("CustomItemModels") { stack, models -> val modelId = getModelIdentifier(stack) ?: return@memoize Optional.empty() - ErrorUtil.softCheck("Model Id needs to have an inventory variant") { modelId.variant() == "inventory" } + ErrorUtil.softCheck("Model Id needs to have an inventory variant", modelId.variant() == "inventory") val bakedModel = models.getModel(modelId.id) if (bakedModel == null || bakedModel === models.missingModelSupplier.get()) return@memoize Optional.empty() Optional.of(bakedModel) diff --git a/src/main/kotlin/features/texturepack/BakedModelExtra.kt b/src/main/kotlin/features/texturepack/BakedModelExtra.kt index 32f419a..6305748 100644 --- a/src/main/kotlin/features/texturepack/BakedModelExtra.kt +++ b/src/main/kotlin/features/texturepack/BakedModelExtra.kt @@ -1,11 +1,30 @@ - package moe.nea.firmament.features.texturepack +import net.fabricmc.fabric.api.renderer.v1.model.WrapperBakedModel as WrapperBakedModelFabric import net.minecraft.client.render.model.BakedModel +import net.minecraft.client.render.model.WrapperBakedModel +import moe.nea.firmament.util.ErrorUtil interface BakedModelExtra { + companion object { + @JvmStatic + fun cast(originalModel: BakedModel): BakedModelExtra? { + var p = originalModel + for (i in 0..256) { + p = when (p) { + is BakedModelExtra -> return p + is WrapperBakedModel -> p.wrapped + is WrapperBakedModelFabric -> WrapperBakedModelFabric.unwrap(p) + else -> break + } + } + ErrorUtil.softError("Could not find a baked model for $originalModel") + return null + } + } + var tintOverrides_firmament: TintOverrides? fun getHeadModel_firmament(): BakedModel? - fun setHeadModel_firmament(headModel: BakedModel?) + fun setHeadModel_firmament(headModel: BakedModel?) } diff --git a/src/main/kotlin/features/texturepack/TintOverrides.kt b/src/main/kotlin/features/texturepack/TintOverrides.kt index 8006db8..85fcae4 100644 --- a/src/main/kotlin/features/texturepack/TintOverrides.kt +++ b/src/main/kotlin/features/texturepack/TintOverrides.kt @@ -3,7 +3,6 @@ package moe.nea.firmament.features.texturepack import com.google.gson.JsonObject import com.google.gson.JsonPrimitive import moe.nea.firmament.util.ErrorUtil -import moe.nea.firmament.util.assertNotNullOr data class TintOverrides( val layerMap: Map<Int, TintOverride> = mapOf() @@ -14,21 +13,22 @@ data class TintOverrides( val EMPTY = TintOverrides() private val threadLocal = object : ThreadLocal<TintOverrides>() {} fun enter(overrides: TintOverrides?) { - ErrorUtil.softCheck("Double entered tintOverrides") { - threadLocal.get() == null - } + ErrorUtil.softCheck("Double entered tintOverrides", + threadLocal.get() == null) threadLocal.set(overrides ?: EMPTY) } fun exit(overrides: TintOverrides?) { - ErrorUtil.softCheck("Exited with non matching enter tintOverrides") { - threadLocal.get() == (overrides ?: EMPTY) - } + ErrorUtil.softCheck("Exited with non matching enter tintOverrides", + threadLocal.get() == (overrides ?: EMPTY)) threadLocal.remove() } - fun getCurrentOverrides() = - assertNotNullOr(threadLocal.get(), "Got current tintOverrides without entering") { EMPTY } + fun getCurrentOverrides(): TintOverrides { + return ErrorUtil.notNullOr(threadLocal.get(), "Got current tintOverrides without entering") { + EMPTY + } + } fun parse(jsonObject: JsonObject): TintOverrides { val map = mutableMapOf<Int, TintOverride>() diff --git a/src/main/kotlin/util/ErrorUtil.kt b/src/main/kotlin/util/ErrorUtil.kt index afecf25..b06093b 100644 --- a/src/main/kotlin/util/ErrorUtil.kt +++ b/src/main/kotlin/util/ErrorUtil.kt @@ -1,25 +1,46 @@ +@file:OptIn(ExperimentalContracts::class) + package moe.nea.firmament.util +import kotlin.contracts.ExperimentalContracts +import kotlin.contracts.InvocationKind +import kotlin.contracts.contract import moe.nea.firmament.Firmament +@Suppress("NOTHING_TO_INLINE") // Suppressed since i want the logger to not pick up the ErrorUtil stack-frame object ErrorUtil { var aggressiveErrors = run { Thread.currentThread().stackTrace.any { it.className.startsWith("org.junit.") } || Firmament.DEBUG + || ErrorUtil::class.java.desiredAssertionStatus() + } + + inline fun softCheck(message: String, check: Boolean) { + if (!check) softError(message) } - inline fun softCheck(message: String, func: () -> Boolean) { + inline fun lazyCheck(message: String, func: () -> Boolean) { + contract { + callsInPlace(func, InvocationKind.AT_MOST_ONCE) + } if (!aggressiveErrors) return if (func()) return error(message) } - @Suppress("NOTHING_TO_INLINE") // Suppressed since i want the logger to not pick up the ErrorUtil stack-frame + inline fun softError(message: String, exception: Throwable) { + if (aggressiveErrors) throw IllegalStateException(message, exception) + else Firmament.logger.error(message, exception) + } + inline fun softError(message: String) { if (aggressiveErrors) error(message) else Firmament.logger.error(message) } inline fun <T : Any> notNullOr(nullable: T?, message: String, orElse: () -> T): T { + contract { + callsInPlace(orElse, InvocationKind.AT_MOST_ONCE) + } if (nullable == null) { softError(message) return orElse() |