aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/events/CustomItemModelEvent.kt
blob: 21ee326bb7caad1bab0acc54bfbe0838c60b442e (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
39
40
41
42
43
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
import moe.nea.firmament.util.mc.IntrospectableItemModelManager

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

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

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

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

	fun overrideIfEmpty(identifier: Identifier) {
		if (overrideModel == null)
			overrideModel = identifier
	}
}