aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/events/CustomItemModelEvent.kt
blob: d5c08f445ff6f5882ba49e0951d12df8defa6cff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package moe.nea.firmament.events

import java.util.Optional
import kotlin.jvm.optionals.getOrNull
import net.minecraft.client.render.item.ItemModels
import net.minecraft.client.render.model.BakedModel
import net.minecraft.client.util.ModelIdentifier
import net.minecraft.item.ItemStack
import moe.nea.firmament.util.ErrorUtil
import moe.nea.firmament.util.collections.WeakCache

data class CustomItemModelEvent(
	val itemStack: ItemStack,
	var overrideModel: ModelIdentifier? = null,
) : FirmamentEvent() {
	companion object : FirmamentEventBus<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" }
				val bakedModel = models.getModel(modelId.id)
				if (bakedModel == null || bakedModel === models.missingModelSupplier.get()) return@memoize Optional.empty()
				Optional.of(bakedModel)
			}

		@JvmStatic
		fun getModelIdentifier(itemStack: ItemStack?): ModelIdentifier? {
			if (itemStack == null) return null
			return publish(CustomItemModelEvent(itemStack)).overrideModel
		}

		@JvmStatic
		fun getModel(itemStack: ItemStack?, thing: ItemModels): BakedModel? {
			if (itemStack == null) return null
			return cache.invoke(itemStack, thing).getOrNull()
		}
	}
}