From 069a94f205fc2fda5a330a2bc68207ab54bf00c9 Mon Sep 17 00:00:00 2001 From: Dennis C Date: Sun, 11 Dec 2022 20:35:45 +0100 Subject: [PATCH] [1.19.3] Add event before baked models are cached by the BlockModelShaper (#9190) --- .../resources/model/ModelManager.java.patch | 9 ++++ .../client/ForgeHooksClient.java | 8 ++- .../client/event/ModelEvent.java | 51 ++++++++++++++++++- 3 files changed, 65 insertions(+), 3 deletions(-) diff --git a/patches/minecraft/net/minecraft/client/resources/model/ModelManager.java.patch b/patches/minecraft/net/minecraft/client/resources/model/ModelManager.java.patch index 519a7633db..f1aa662aaa 100644 --- a/patches/minecraft/net/minecraft/client/resources/model/ModelManager.java.patch +++ b/patches/minecraft/net/minecraft/client/resources/model/ModelManager.java.patch @@ -35,6 +35,15 @@ CompletableFuture> completablefuture = m_246704_(p_251134_, p_250550_); CompletableFuture>> completablefuture1 = m_246899_(p_251134_, p_250550_); CompletableFuture completablefuture2 = completablefuture.thenCombineAsync(completablefuture1, (p_251201_, p_251281_) -> { +@@ -172,6 +_,8 @@ + return " " + p_248692_.m_119193_() + ":" + p_248692_.m_119203_(); + }).collect(Collectors.joining("\n"))); + }); ++ p_252136_.m_6182_("forge_modify_baking_result"); ++ net.minecraftforge.client.ForgeHooksClient.onModifyBakingResult(p_248945_.m_119251_(), p_248945_); + p_252136_.m_6182_("dispatch"); + Map map = p_248945_.m_119251_(); + BakedModel bakedmodel = map.get(ModelBakery.f_119230_); @@ -201,6 +_,8 @@ this.f_119397_ = modelbakery.m_119251_(); this.f_119404_ = modelbakery.m_119355_(); diff --git a/src/main/java/net/minecraftforge/client/ForgeHooksClient.java b/src/main/java/net/minecraftforge/client/ForgeHooksClient.java index 0125362695..33d1aa6f95 100644 --- a/src/main/java/net/minecraftforge/client/ForgeHooksClient.java +++ b/src/main/java/net/minecraftforge/client/ForgeHooksClient.java @@ -169,6 +169,7 @@ import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.List; @@ -471,9 +472,14 @@ public class ForgeHooksClient return event; } + public static void onModifyBakingResult(Map models, ModelBakery modelBakery) + { + ModLoader.get().postEvent(new ModelEvent.ModifyBakingResult(models, modelBakery)); + } + public static void onModelBake(ModelManager modelManager, Map models, ModelBakery modelBakery) { - ModLoader.get().postEvent(new ModelEvent.BakingCompleted(modelManager, models, modelBakery)); + ModLoader.get().postEvent(new ModelEvent.BakingCompleted(modelManager, Collections.unmodifiableMap(models), modelBakery)); } public static BakedModel handleCameraTransforms(PoseStack poseStack, BakedModel model, ItemTransforms.TransformType cameraTransformType, boolean applyLeftHandTransform) diff --git a/src/main/java/net/minecraftforge/client/event/ModelEvent.java b/src/main/java/net/minecraftforge/client/event/ModelEvent.java index 7c576394ca..dfe59b9fe7 100644 --- a/src/main/java/net/minecraftforge/client/event/ModelEvent.java +++ b/src/main/java/net/minecraftforge/client/event/ModelEvent.java @@ -32,9 +32,56 @@ public abstract class ModelEvent extends Event { } + /** + * Fired while the {@link ModelManager} is reloading models, after the model registry is set up, but before it's + * passed to the {@link net.minecraft.client.renderer.block.BlockModelShaper} for caching. + * + *

+ * This event is fired from a worker thread and it is therefore not safe to access anything outside the + * model registry and {@link ModelBakery} provided in this event.
+ * The {@link ModelManager} firing this event is not fully set up with the latest data when this event fires and + * must therefore not be accessed in this event. + *

+ * + *

This event is not {@linkplain Cancelable cancellable}, and does not {@linkplain HasResult have a result}.

+ * + *

This event is fired on the {@linkplain FMLJavaModLoadingContext#getModEventBus() mod-specific event bus}, + * only on the {@linkplain LogicalSide#CLIENT logical client}.

+ */ + public static class ModifyBakingResult extends ModelEvent implements IModBusEvent + { + private final Map models; + private final ModelBakery modelBakery; + + @ApiStatus.Internal + public ModifyBakingResult(Map models, ModelBakery modelBakery) + { + this.models = models; + this.modelBakery = modelBakery; + } + + /** + * @return the modifiable registry map of models and their model names + */ + public Map getModels() + { + return models; + } + + /** + * @return the model loader + */ + public ModelBakery getModelBakery() + { + return modelBakery; + } + } + /** * Fired when the {@link ModelManager} is notified of the resource manager reloading. - * Called after model registry is set up, but before it's passed to {@link net.minecraft.client.renderer.block.BlockModelShaper}. + * Called after the model registry is set up and cached in the {@link net.minecraft.client.renderer.block.BlockModelShaper}.
+ * The model registry given by this event is unmodifiable. To modify the model registry, use + * {@link ModelEvent.ModifyBakingResult} instead. * *

This event is not {@linkplain Cancelable cancellable}, and does not {@linkplain HasResult have a result}.

* @@ -64,7 +111,7 @@ public abstract class ModelEvent extends Event } /** - * @return the modifiable registry map of models and their model names + * @return an unmodifiable view of the registry map of models and their model names */ public Map getModels() {