diff options
Diffstat (limited to 'src/main/kotlin/events')
-rw-r--r-- | src/main/kotlin/events/BakeExtraModelsEvent.kt | 25 | ||||
-rw-r--r-- | src/main/kotlin/events/CustomItemModelEvent.kt | 46 | ||||
-rw-r--r-- | src/main/kotlin/events/FinalizeResourceManagerEvent.kt | 41 | ||||
-rw-r--r-- | src/main/kotlin/events/IsSlotProtectedEvent.kt | 2 | ||||
-rw-r--r-- | src/main/kotlin/events/RegisterCustomShadersEvent.kt | 24 | ||||
-rw-r--r-- | src/main/kotlin/events/SlotRenderEvents.kt | 15 |
6 files changed, 62 insertions, 91 deletions
diff --git a/src/main/kotlin/events/BakeExtraModelsEvent.kt b/src/main/kotlin/events/BakeExtraModelsEvent.kt index f75bedc..adaa495 100644 --- a/src/main/kotlin/events/BakeExtraModelsEvent.kt +++ b/src/main/kotlin/events/BakeExtraModelsEvent.kt @@ -1,21 +1,24 @@ - package moe.nea.firmament.events -import java.util.function.Consumer +import java.util.function.BiConsumer +import net.minecraft.client.render.model.ReferencedModelsCollector import net.minecraft.client.util.ModelIdentifier +import net.minecraft.util.Identifier +// TODO: Rename this event, since it is not really directly baking models anymore class BakeExtraModelsEvent( - private val addItemModel: Consumer<ModelIdentifier>, - private val addAnyModel: Consumer<ModelIdentifier>, + private val addAnyModel: BiConsumer<ModelIdentifier, Identifier>, ) : FirmamentEvent() { - fun addNonItemModel(modelIdentifier: ModelIdentifier) { - this.addAnyModel.accept(modelIdentifier) - } + fun addNonItemModel(modelIdentifier: ModelIdentifier, identifier: Identifier) { + this.addAnyModel.accept(modelIdentifier, identifier) + } - fun addItemModel(modelIdentifier: ModelIdentifier) { - this.addItemModel.accept(modelIdentifier) - } + fun addItemModel(modelIdentifier: ModelIdentifier) { + addNonItemModel( + modelIdentifier, + modelIdentifier.id.withPrefixedPath(ReferencedModelsCollector.ITEM_DIRECTORY)) + } - companion object : FirmamentEventBus<BakeExtraModelsEvent>() + companion object : FirmamentEventBus<BakeExtraModelsEvent>() } diff --git a/src/main/kotlin/events/CustomItemModelEvent.kt b/src/main/kotlin/events/CustomItemModelEvent.kt index e50eca4..d5c08f4 100644 --- a/src/main/kotlin/events/CustomItemModelEvent.kt +++ b/src/main/kotlin/events/CustomItemModelEvent.kt @@ -2,35 +2,37 @@ 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.render.model.BakedModelManager 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, + val itemStack: ItemStack, + var overrideModel: ModelIdentifier? = null, ) : FirmamentEvent() { - companion object : FirmamentEventBus<CustomItemModelEvent>() { - val cache = - WeakCache.memoize<ItemStack, BakedModelManager, Optional<BakedModel>>("CustomItemModels") { stack, models -> - val modelId = getModelIdentifier(stack) ?: return@memoize Optional.empty() - val bakedModel = models.getModel(modelId) - if (bakedModel === models.missingModel) return@memoize Optional.empty() - Optional.of(bakedModel) - } + 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 getModelIdentifier(itemStack: ItemStack?): ModelIdentifier? { + if (itemStack == null) return null + return publish(CustomItemModelEvent(itemStack)).overrideModel + } - @JvmStatic - fun getModel(itemStack: ItemStack?, thing: BakedModelManager): BakedModel? { - if (itemStack == null) return null - return cache.invoke(itemStack, thing).getOrNull() - } - } + @JvmStatic + fun getModel(itemStack: ItemStack?, thing: ItemModels): BakedModel? { + if (itemStack == null) return null + return cache.invoke(itemStack, thing).getOrNull() + } + } } diff --git a/src/main/kotlin/events/FinalizeResourceManagerEvent.kt b/src/main/kotlin/events/FinalizeResourceManagerEvent.kt index 0d411f1..12167f8 100644 --- a/src/main/kotlin/events/FinalizeResourceManagerEvent.kt +++ b/src/main/kotlin/events/FinalizeResourceManagerEvent.kt @@ -5,31 +5,28 @@ import java.util.concurrent.Executor import net.minecraft.resource.ReloadableResourceManagerImpl import net.minecraft.resource.ResourceManager import net.minecraft.resource.ResourceReloader -import net.minecraft.util.profiler.Profiler data class FinalizeResourceManagerEvent( - val resourceManager: ReloadableResourceManagerImpl, + val resourceManager: ReloadableResourceManagerImpl, ) : FirmamentEvent() { - companion object : FirmamentEventBus<FinalizeResourceManagerEvent>() + companion object : FirmamentEventBus<FinalizeResourceManagerEvent>() - inline fun registerOnApply(name: String, crossinline function: () -> Unit) { - resourceManager.registerReloader(object : ResourceReloader { - override fun reload( - synchronizer: ResourceReloader.Synchronizer, - manager: ResourceManager?, - prepareProfiler: Profiler?, - applyProfiler: Profiler?, - prepareExecutor: Executor?, - applyExecutor: Executor - ): CompletableFuture<Void> { - return CompletableFuture.completedFuture(Unit) - .thenCompose(synchronizer::whenPrepared) - .thenAcceptAsync({ function() }, applyExecutor) - } + inline fun registerOnApply(name: String, crossinline function: () -> Unit) { + resourceManager.registerReloader(object : ResourceReloader { + override fun reload( + synchronizer: ResourceReloader.Synchronizer, + manager: ResourceManager, + prepareExecutor: Executor, + applyExecutor: Executor + ): CompletableFuture<Void> { + return CompletableFuture.completedFuture(Unit) + .thenCompose(synchronizer::whenPrepared) + .thenAcceptAsync({ function() }, applyExecutor) + } - override fun getName(): String { - return name - } - }) - } + override fun getName(): String { + return name + } + }) + } } diff --git a/src/main/kotlin/events/IsSlotProtectedEvent.kt b/src/main/kotlin/events/IsSlotProtectedEvent.kt index cd431f7..cd2b676 100644 --- a/src/main/kotlin/events/IsSlotProtectedEvent.kt +++ b/src/main/kotlin/events/IsSlotProtectedEvent.kt @@ -37,7 +37,7 @@ data class IsSlotProtectedEvent( val event = IsSlotProtectedEvent(slot, action, false, itemStackOverride) publish(event) if (event.isProtected && !event.silent) { - MC.player?.sendMessage(Text.translatable("firmament.protectitem").append(event.itemStack.name)) + MC.sendChat(Text.translatable("firmament.protectitem").append(event.itemStack.name)) CommonSoundEffects.playFailure() } return event.isProtected diff --git a/src/main/kotlin/events/RegisterCustomShadersEvent.kt b/src/main/kotlin/events/RegisterCustomShadersEvent.kt deleted file mode 100644 index 2f6d1f8..0000000 --- a/src/main/kotlin/events/RegisterCustomShadersEvent.kt +++ /dev/null @@ -1,24 +0,0 @@ -package moe.nea.firmament.events - -import com.mojang.datafixers.util.Pair -import java.util.function.Consumer -import net.minecraft.client.gl.ShaderProgram -import net.minecraft.client.render.VertexFormat -import net.minecraft.resource.ResourceFactory -import moe.nea.firmament.Firmament - -data class RegisterCustomShadersEvent( - val list: MutableList<Pair<ShaderProgram, Consumer<ShaderProgram>>>, - val resourceFactory: ResourceFactory, -) : FirmamentEvent() { - companion object : FirmamentEventBus<RegisterCustomShadersEvent>() - - fun register(name: String, vertexFormat: VertexFormat, saver: Consumer<ShaderProgram>) { - require(name.startsWith("firmament_")) - try { - list.add(Pair.of(ShaderProgram(resourceFactory, name, vertexFormat), saver)) - } catch (ex: Exception) { - Firmament.logger.fatal("Could not load firmament shader $name", ex) - } - } -} diff --git a/src/main/kotlin/events/SlotRenderEvents.kt b/src/main/kotlin/events/SlotRenderEvents.kt index 5431573..5234176 100644 --- a/src/main/kotlin/events/SlotRenderEvents.kt +++ b/src/main/kotlin/events/SlotRenderEvents.kt @@ -3,20 +3,19 @@ package moe.nea.firmament.events import net.minecraft.client.gui.DrawContext +import net.minecraft.client.render.RenderLayer import net.minecraft.client.texture.Sprite import net.minecraft.screen.slot.Slot import net.minecraft.util.Identifier import moe.nea.firmament.util.MC +import moe.nea.firmament.util.render.drawGuiTexture interface SlotRenderEvents { val context: DrawContext val slot: Slot - val mouseX: Int - val mouseY: Int - val delta: Float - fun highlight(sprite: Sprite) { - context.drawSprite( + fun highlight(sprite: Identifier) { + context.drawGuiTexture( slot.x, slot.y, 0, 16, 16, sprite ) @@ -24,9 +23,6 @@ interface SlotRenderEvents { data class Before( override val context: DrawContext, override val slot: Slot, - override val mouseX: Int, - override val mouseY: Int, - override val delta: Float ) : FirmamentEvent(), SlotRenderEvents { companion object : FirmamentEventBus<Before>() @@ -34,9 +30,6 @@ interface SlotRenderEvents { data class After( override val context: DrawContext, override val slot: Slot, - override val mouseX: Int, - override val mouseY: Int, - override val delta: Float ) : FirmamentEvent(), SlotRenderEvents { companion object : FirmamentEventBus<After>() |