aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/events/CustomItemModelEvent.kt
blob: e7b6eb85e442b9dfdebd47407ab22c540bc61cb6 (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
package moe.nea.firmament.events

import java.util.Optional
import kotlin.jvm.optionals.getOrNull
import net.minecraft.item.ItemStack
import net.minecraft.util.Identifier
import moe.nea.firmament.util.collections.WeakCache

// TODO: assert an order on these events
data class CustomItemModelEvent(
	val itemStack: ItemStack,
	var overrideModel: Identifier? = null,
) : FirmamentEvent() {
	companion object : FirmamentEventBus<CustomItemModelEvent>() {
		val cache = WeakCache.memoize("ItemModelIdentifier", ::getModelIdentifier0)

		@JvmStatic
		fun getModelIdentifier(itemStack: ItemStack?): Identifier? {
			if (itemStack == null) return null
			return cache.invoke(itemStack).getOrNull()
		}

		fun getModelIdentifier0(itemStack: ItemStack): Optional<Identifier> {
			// TODO: add an error / warning if the model does not exist
			return Optional.ofNullable(publish(CustomItemModelEvent(itemStack)).overrideModel)
		}
	}

	fun overrideIfExists(overrideModel: Identifier) {
		this.overrideModel = overrideModel
	}
}