From 86cbf9d4221e85dec14300c11393abb325bffcef Mon Sep 17 00:00:00 2001 From: Linnea Gräf Date: Sun, 17 Nov 2024 19:41:18 +0100 Subject: fix: Item predicates not applying unless a vanilla predicate is present --- .../firmament/mixins/CustomModelEventPatch.java | 11 +- .../custommodels/GlobalModelOverridePatch.java | 31 --- .../TestForFirmamentOverridePredicatesPatch.java | 23 +- .../features/texturepack/BakedOverrideData.kt | 8 +- .../features/texturepack/CustomGlobalTextures.kt | 252 ++++++++++----------- .../features/texturepack/ModelOverrideData.kt | 14 +- src/main/kotlin/repo/HypixelStaticData.kt | 2 + src/main/resources/firmament.accesswidener | 1 + 8 files changed, 176 insertions(+), 166 deletions(-) delete mode 100644 src/main/java/moe/nea/firmament/mixins/custommodels/GlobalModelOverridePatch.java diff --git a/src/main/java/moe/nea/firmament/mixins/CustomModelEventPatch.java b/src/main/java/moe/nea/firmament/mixins/CustomModelEventPatch.java index e7207f4..e0a7544 100644 --- a/src/main/java/moe/nea/firmament/mixins/CustomModelEventPatch.java +++ b/src/main/java/moe/nea/firmament/mixins/CustomModelEventPatch.java @@ -3,9 +3,11 @@ package moe.nea.firmament.mixins; import moe.nea.firmament.events.CustomItemModelEvent; +import moe.nea.firmament.features.texturepack.CustomGlobalTextures; import net.minecraft.client.render.item.ItemModels; import net.minecraft.client.render.model.BakedModel; import net.minecraft.client.render.model.BakedModelManager; +import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.util.Identifier; import org.spongepowered.asm.mixin.Final; @@ -22,8 +24,13 @@ public class CustomModelEventPatch { @Inject(method = "getModel(Lnet/minecraft/item/ItemStack;)Lnet/minecraft/client/render/model/BakedModel;", at = @At("HEAD"), cancellable = true) public void onGetModel(ItemStack stack, CallbackInfoReturnable cir) { - var model = CustomItemModelEvent.getModel(stack, (ItemModels) (Object) this); - if (model != null) + var $this = (ItemModels) (Object) this; + var model = CustomItemModelEvent.getModel(stack, $this); + if (model == null) { + model = CustomGlobalTextures.replaceGlobalModel($this, stack); + } + if (model != null) { cir.setReturnValue(model); + } } } diff --git a/src/main/java/moe/nea/firmament/mixins/custommodels/GlobalModelOverridePatch.java b/src/main/java/moe/nea/firmament/mixins/custommodels/GlobalModelOverridePatch.java deleted file mode 100644 index c708862..0000000 --- a/src/main/java/moe/nea/firmament/mixins/custommodels/GlobalModelOverridePatch.java +++ /dev/null @@ -1,31 +0,0 @@ - -package moe.nea.firmament.mixins.custommodels; - -import moe.nea.firmament.features.texturepack.CustomGlobalTextures; -import net.minecraft.client.render.item.ItemModels; -import net.minecraft.client.render.item.ItemRenderer; -import net.minecraft.client.render.model.BakedModel; -import net.minecraft.entity.LivingEntity; -import net.minecraft.item.ItemStack; -import net.minecraft.world.World; -import org.spongepowered.asm.mixin.Final; -import org.spongepowered.asm.mixin.Mixin; -import org.spongepowered.asm.mixin.Shadow; -import org.spongepowered.asm.mixin.injection.At; -import org.spongepowered.asm.mixin.injection.Inject; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; - -@Mixin(ItemRenderer.class) -public abstract class GlobalModelOverridePatch { - - @Shadow - @Final - private ItemModels models; - - @Inject(method = "getModel(Lnet/minecraft/item/ItemStack;Lnet/minecraft/world/World;Lnet/minecraft/entity/LivingEntity;I)Lnet/minecraft/client/render/model/BakedModel;", at = @At("HEAD"), cancellable = true) - private void overrideGlobalModel( - ItemStack stack, World world, LivingEntity entity, - int seed, CallbackInfoReturnable cir) { - CustomGlobalTextures.replaceGlobalModel(this.models, stack, cir); - } -} diff --git a/src/main/java/moe/nea/firmament/mixins/custommodels/TestForFirmamentOverridePredicatesPatch.java b/src/main/java/moe/nea/firmament/mixins/custommodels/TestForFirmamentOverridePredicatesPatch.java index 81ae3b8..63f3cf0 100644 --- a/src/main/java/moe/nea/firmament/mixins/custommodels/TestForFirmamentOverridePredicatesPatch.java +++ b/src/main/java/moe/nea/firmament/mixins/custommodels/TestForFirmamentOverridePredicatesPatch.java @@ -2,7 +2,10 @@ package moe.nea.firmament.mixins.custommodels; import com.llamalad7.mixinextras.injector.ModifyExpressionValue; +import com.llamalad7.mixinextras.injector.wrapoperation.Operation; +import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation; import com.llamalad7.mixinextras.sugar.Local; +import moe.nea.firmament.Firmament; import moe.nea.firmament.features.texturepack.BakedOverrideData; import moe.nea.firmament.features.texturepack.CustomSkyBlockTextures; import moe.nea.firmament.features.texturepack.FirmamentModelPredicate; @@ -10,13 +13,23 @@ import moe.nea.firmament.features.texturepack.ModelOverrideData; import net.minecraft.client.render.model.json.ModelOverride; import net.minecraft.client.render.model.json.ModelOverrideList; import net.minecraft.item.ItemStack; +import net.minecraft.util.Identifier; +import org.objectweb.asm.Opcodes; +import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.ModifyArg; +import java.util.List; +import java.util.Objects; + @Mixin(ModelOverrideList.class) public class TestForFirmamentOverridePredicatesPatch { + @Shadow + private Identifier[] conditionTypes; + @ModifyArg(method = "(Lnet/minecraft/client/render/model/Baker;Ljava/util/List;)V", at = @At( value = "INVOKE", target = "Ljava/util/List;add(Ljava/lang/Object;)Z" @@ -26,8 +39,14 @@ public class TestForFirmamentOverridePredicatesPatch { @Local ModelOverride modelOverride ) { var bakedOverride = (ModelOverrideList.BakedOverride) element; - ((BakedOverrideData) (Object) bakedOverride) - .setFirmamentOverrides(((ModelOverrideData) (Object) modelOverride).getFirmamentOverrides()); + var modelOverrideData = ModelOverrideData.cast(modelOverride); + BakedOverrideData.cast(bakedOverride) + .setFirmamentOverrides(modelOverrideData.getFirmamentOverrides()); + if (conditionTypes.length == 0 && + modelOverrideData.getFirmamentOverrides() != null && + modelOverrideData.getFirmamentOverrides().length > 0) { + conditionTypes = new Identifier[]{Firmament.INSTANCE.identifier("sentinel/enforce_model_override_evaluation")}; + } return element; } diff --git a/src/main/kotlin/features/texturepack/BakedOverrideData.kt b/src/main/kotlin/features/texturepack/BakedOverrideData.kt index c012883..e9391f1 100644 --- a/src/main/kotlin/features/texturepack/BakedOverrideData.kt +++ b/src/main/kotlin/features/texturepack/BakedOverrideData.kt @@ -1,8 +1,14 @@ package moe.nea.firmament.features.texturepack +import net.minecraft.client.render.model.json.ModelOverrideList + interface BakedOverrideData { fun getFirmamentOverrides(): Array? fun setFirmamentOverrides(overrides: Array?) - + companion object{ + @Suppress("CAST_NEVER_SUCCEEDS") + @JvmStatic + fun cast(bakedOverride: ModelOverrideList.BakedOverride): BakedOverrideData = bakedOverride as BakedOverrideData + } } diff --git a/src/main/kotlin/features/texturepack/CustomGlobalTextures.kt b/src/main/kotlin/features/texturepack/CustomGlobalTextures.kt index a1203df..9ad8bc1 100644 --- a/src/main/kotlin/features/texturepack/CustomGlobalTextures.kt +++ b/src/main/kotlin/features/texturepack/CustomGlobalTextures.kt @@ -6,7 +6,6 @@ package moe.nea.firmament.features.texturepack import java.util.Optional import java.util.concurrent.CompletableFuture import org.slf4j.LoggerFactory -import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable import kotlinx.serialization.Serializable import kotlinx.serialization.UseSerializers import kotlin.jvm.optionals.getOrNull @@ -35,132 +34,131 @@ import moe.nea.firmament.util.json.SingletonSerializableList import moe.nea.firmament.util.runNull object CustomGlobalTextures : SinglePreparationResourceReloader(), - SubscriptionOwner { - override val delegateFeature: FirmamentFeature - get() = CustomSkyBlockTextures - - class CustomGuiTextureOverride( - val classes: List - ) - - @Serializable - data class GlobalItemOverride( - val screen: @Serializable(SingletonSerializableList::class) List, - val model: Identifier, - val predicate: FirmamentModelPredicate, - ) - - @Serializable - data class ScreenFilter( - val title: StringMatcher, - ) - - data class ItemOverrideCollection( - val screenFilter: ScreenFilter, - val overrides: List, - ) - - @Subscribe - fun onStart(event: FinalizeResourceManagerEvent) { - MC.resourceManager.registerReloader(this) - } - - @Subscribe - fun onEarlyReload(event: EarlyResourceReloadEvent) { - preparationFuture = CompletableFuture - .supplyAsync( - { - prepare(event.resourceManager) - }, event.preparationExecutor) - } - - @Subscribe - fun onBakeModels(event: BakeExtraModelsEvent) { - for (guiClassOverride in preparationFuture.join().classes) { - for (override in guiClassOverride.overrides) { - event.addItemModel(ModelIdentifier(override.model, "inventory")) - } - } - } - - @Volatile - var preparationFuture: CompletableFuture = CompletableFuture.completedFuture( - CustomGuiTextureOverride(listOf())) - - override fun prepare(manager: ResourceManager?, profiler: Profiler?): CustomGuiTextureOverride { - return preparationFuture.join() - } - - override fun apply(prepared: CustomGuiTextureOverride, manager: ResourceManager?, profiler: Profiler?) { - this.guiClassOverrides = prepared - } - - val logger = LoggerFactory.getLogger(CustomGlobalTextures::class.java) - fun prepare(manager: ResourceManager): CustomGuiTextureOverride { - val overrideResources = - manager.findResources("overrides/item") { it.namespace == "firmskyblock" && it.path.endsWith(".json") } - .mapNotNull { - Firmament.tryDecodeJsonFromStream(it.value.inputStream).getOrElse { ex -> - logger.error("Failed to load global item override at ${it.key}", ex) - null - } - } - - val byGuiClass = overrideResources.flatMap { override -> override.screen.toSet().map { it to override } } - .groupBy { it.first } - val guiClasses = byGuiClass.entries - .mapNotNull { - val key = it.key - val guiClassResource = - manager.getResource(Identifier.of(key.namespace, "filters/screen/${key.path}.json")) - .getOrNull() - ?: return@mapNotNull runNull { - logger.error("Failed to locate screen filter at $key") - } - val screenFilter = - Firmament.tryDecodeJsonFromStream(guiClassResource.inputStream) - .getOrElse { ex -> - logger.error("Failed to load screen filter at $key", ex) - return@mapNotNull null - } - ItemOverrideCollection(screenFilter, it.value.map { it.second }) - } - logger.info("Loaded ${overrideResources.size} global item overrides") - return CustomGuiTextureOverride(guiClasses) - } - - var guiClassOverrides = CustomGuiTextureOverride(listOf()) - - var matchingOverrides: Set = setOf() - - @Subscribe - fun onOpenGui(event: ScreenChangeEvent) { - val newTitle = event.new?.title ?: Text.empty() - matchingOverrides = guiClassOverrides.classes - .filterTo(mutableSetOf()) { it.screenFilter.title.matches(newTitle) } - } - - val overrideCache = WeakCache.memoize>("CustomGlobalTextureModelOverrides") { stack, models -> - matchingOverrides - .firstNotNullOfOrNull { - it.overrides - .asSequence() - .filter { it.predicate.test(stack) } - .map { models.getModel(it.model) } - .firstOrNull() - } - .intoOptional() - } - - @JvmStatic - fun replaceGlobalModel( - models: ItemModels, - stack: ItemStack, - cir: CallbackInfoReturnable - ) { - overrideCache.invoke(stack, models) - .ifPresent(cir::setReturnValue) - } + SubscriptionOwner { + override val delegateFeature: FirmamentFeature + get() = CustomSkyBlockTextures + + class CustomGuiTextureOverride( + val classes: List + ) + + @Serializable + data class GlobalItemOverride( + val screen: @Serializable(SingletonSerializableList::class) List, + val model: Identifier, + val predicate: FirmamentModelPredicate, + ) + + @Serializable + data class ScreenFilter( + val title: StringMatcher, + ) + + data class ItemOverrideCollection( + val screenFilter: ScreenFilter, + val overrides: List, + ) + + @Subscribe + fun onStart(event: FinalizeResourceManagerEvent) { + MC.resourceManager.registerReloader(this) + } + + @Subscribe + fun onEarlyReload(event: EarlyResourceReloadEvent) { + preparationFuture = CompletableFuture + .supplyAsync( + { + prepare(event.resourceManager) + }, event.preparationExecutor) + } + + @Subscribe + fun onBakeModels(event: BakeExtraModelsEvent) { + for (guiClassOverride in preparationFuture.join().classes) { + for (override in guiClassOverride.overrides) { + event.addItemModel(ModelIdentifier(override.model, "inventory")) + } + } + } + + @Volatile + var preparationFuture: CompletableFuture = CompletableFuture.completedFuture( + CustomGuiTextureOverride(listOf())) + + override fun prepare(manager: ResourceManager?, profiler: Profiler?): CustomGuiTextureOverride { + return preparationFuture.join() + } + + override fun apply(prepared: CustomGuiTextureOverride, manager: ResourceManager?, profiler: Profiler?) { + this.guiClassOverrides = prepared + } + + val logger = LoggerFactory.getLogger(CustomGlobalTextures::class.java) + fun prepare(manager: ResourceManager): CustomGuiTextureOverride { + val overrideResources = + manager.findResources("overrides/item") { it.namespace == "firmskyblock" && it.path.endsWith(".json") } + .mapNotNull { + Firmament.tryDecodeJsonFromStream(it.value.inputStream).getOrElse { ex -> + logger.error("Failed to load global item override at ${it.key}", ex) + null + } + } + + val byGuiClass = overrideResources.flatMap { override -> override.screen.toSet().map { it to override } } + .groupBy { it.first } + val guiClasses = byGuiClass.entries + .mapNotNull { + val key = it.key + val guiClassResource = + manager.getResource(Identifier.of(key.namespace, "filters/screen/${key.path}.json")) + .getOrNull() + ?: return@mapNotNull runNull { + logger.error("Failed to locate screen filter at $key") + } + val screenFilter = + Firmament.tryDecodeJsonFromStream(guiClassResource.inputStream) + .getOrElse { ex -> + logger.error("Failed to load screen filter at $key", ex) + return@mapNotNull null + } + ItemOverrideCollection(screenFilter, it.value.map { it.second }) + } + logger.info("Loaded ${overrideResources.size} global item overrides") + return CustomGuiTextureOverride(guiClasses) + } + + var guiClassOverrides = CustomGuiTextureOverride(listOf()) + + var matchingOverrides: Set = setOf() + + @Subscribe + fun onOpenGui(event: ScreenChangeEvent) { + val newTitle = event.new?.title ?: Text.empty() + matchingOverrides = guiClassOverrides.classes + .filterTo(mutableSetOf()) { it.screenFilter.title.matches(newTitle) } + } + + val overrideCache = + WeakCache.memoize>("CustomGlobalTextureModelOverrides") { stack, models -> + matchingOverrides + .firstNotNullOfOrNull { + it.overrides + .asSequence() + .filter { it.predicate.test(stack) } + .map { models.getModel(it.model) } + .firstOrNull() + } + .intoOptional() + } + + @JvmStatic + fun replaceGlobalModel( + models: ItemModels, + stack: ItemStack, + ): BakedModel? { + return overrideCache.invoke(stack, models).getOrNull() + } } diff --git a/src/main/kotlin/features/texturepack/ModelOverrideData.kt b/src/main/kotlin/features/texturepack/ModelOverrideData.kt index 1585bd7..29d9192 100644 --- a/src/main/kotlin/features/texturepack/ModelOverrideData.kt +++ b/src/main/kotlin/features/texturepack/ModelOverrideData.kt @@ -1,7 +1,15 @@ - package moe.nea.firmament.features.texturepack +import net.minecraft.client.render.model.json.ModelOverride + interface ModelOverrideData { - fun getFirmamentOverrides(): Array? - fun setFirmamentOverrides(overrides: Array?) + companion object { + + @JvmStatic + @Suppress("CAST_NEVER_SUCCEEDS") + fun cast(override: ModelOverride) = override as ModelOverrideData + } + + fun getFirmamentOverrides(): Array? + fun setFirmamentOverrides(overrides: Array?) } diff --git a/src/main/kotlin/repo/HypixelStaticData.kt b/src/main/kotlin/repo/HypixelStaticData.kt index 727a962..181aa70 100644 --- a/src/main/kotlin/repo/HypixelStaticData.kt +++ b/src/main/kotlin/repo/HypixelStaticData.kt @@ -6,6 +6,7 @@ import org.apache.logging.log4j.LogManager import org.lwjgl.glfw.GLFW import kotlinx.coroutines.async import kotlinx.coroutines.awaitAll +import kotlinx.coroutines.delay import kotlinx.coroutines.launch import kotlinx.coroutines.withTimeoutOrNull import kotlinx.serialization.SerialName @@ -74,6 +75,7 @@ object HypixelStaticData { while (true) { logger.info("Updating NEU prices") updatePrices() + delay(10.minutes) } } } diff --git a/src/main/resources/firmament.accesswidener b/src/main/resources/firmament.accesswidener index 91be071..c542fc8 100644 --- a/src/main/resources/firmament.accesswidener +++ b/src/main/resources/firmament.accesswidener @@ -6,6 +6,7 @@ accessible field net/minecraft/client/gui/hud/InGameHud SCOREBOARD_ENTRY_COMPARA accessible field net/minecraft/client/render/item/HeldItemRenderer itemRenderer Lnet/minecraft/client/render/item/ItemRenderer; accessible field net/minecraft/client/render/item/ItemModels missingModelSupplier Ljava/util/function/Supplier; +mutable field net/minecraft/client/render/model/json/ModelOverrideList conditionTypes [Lnet/minecraft/util/Identifier; accessible class net/minecraft/client/render/model/json/ModelOverride$Deserializer accessible class net/minecraft/client/render/model/json/ModelOverrideList$BakedOverride -- cgit