From fe474d6b0541cc53dad90dc558d07885b49db02c Mon Sep 17 00:00:00 2001 From: PepperCode1 <44146161+PepperCode1@users.noreply.github.com> Date: Sat, 8 Jun 2024 06:02:54 -0700 Subject: [PATCH] Port Model Loading API v1 to 1.21 (#3824) * Port Model Loading API v1 * Fix checkstyle and address review comments --- fabric-model-loading-api-v1/build.gradle | 1 + .../model/loading/v1/BlockStateResolver.java | 6 +- .../loading/v1/DelegatingUnbakedModel.java | 8 +- .../loading/v1/FabricBakedModelManager.java | 14 +- .../model/loading/v1/ModelLoadingPlugin.java | 8 +- .../model/loading/v1/ModelModifier.java | 64 ++++-- .../model/loading/v1/ModelResolver.java | 6 +- ...riantProvider.java => BakerImplHooks.java} | 14 +- .../model/loading/BlockStatesLoaderHooks.java | 30 +++ .../model/loading/ModelLoaderHooks.java | 10 +- .../model/loading/ModelLoadingConstants.java | 42 ++++ .../loading/ModelLoadingEventDispatcher.java | 200 ++++++----------- ...ava => ModelLoadingPluginContextImpl.java} | 82 +------ .../model/loading/BakedModelManagerMixin.java | 20 +- .../model/loading/BlockStatesLoaderMixin.java | 51 +++++ .../loading/ModelLoaderBakerImplMixin.java | 32 ++- .../model/loading/ModelLoaderMixin.java | 204 +++++++++--------- .../fabric-model-loading-api-v1.mixins.json | 1 + .../src/client/resources/fabric.mod.json | 3 - .../model/loading/ModelTestModClient.java | 28 ++- .../model/loading/NestedModelLoadingTest.java | 31 ++- .../model/loading/PreparablePluginTest.java | 15 +- .../loading/SpecificModelReloadListener.java | 20 +- fabric-renderer-api-v1/build.gradle | 2 +- .../renderer/client/ModelResolverImpl.java | 11 +- .../renderer/client/RendererClientTest.java | 8 +- settings.gradle | 2 +- 27 files changed, 469 insertions(+), 444 deletions(-) rename fabric-model-loading-api-v1/src/client/java/net/fabricmc/fabric/impl/client/model/loading/{LegacyModelVariantProvider.java => BakerImplHooks.java} (66%) create mode 100644 fabric-model-loading-api-v1/src/client/java/net/fabricmc/fabric/impl/client/model/loading/BlockStatesLoaderHooks.java create mode 100644 fabric-model-loading-api-v1/src/client/java/net/fabricmc/fabric/impl/client/model/loading/ModelLoadingConstants.java rename fabric-model-loading-api-v1/src/client/java/net/fabricmc/fabric/impl/client/model/loading/{ModelLoaderPluginContextImpl.java => ModelLoadingPluginContextImpl.java} (66%) create mode 100644 fabric-model-loading-api-v1/src/client/java/net/fabricmc/fabric/mixin/client/model/loading/BlockStatesLoaderMixin.java diff --git a/fabric-model-loading-api-v1/build.gradle b/fabric-model-loading-api-v1/build.gradle index ca3eb5eb1..3d2819264 100644 --- a/fabric-model-loading-api-v1/build.gradle +++ b/fabric-model-loading-api-v1/build.gradle @@ -4,6 +4,7 @@ moduleDependencies(project, ['fabric-api-base']) testDependencies(project, [ ':fabric-renderer-api-v1', + ':fabric-renderer-indigo', ':fabric-rendering-v1', ':fabric-resource-loader-v0' ]) diff --git a/fabric-model-loading-api-v1/src/client/java/net/fabricmc/fabric/api/client/model/loading/v1/BlockStateResolver.java b/fabric-model-loading-api-v1/src/client/java/net/fabricmc/fabric/api/client/model/loading/v1/BlockStateResolver.java index dacd2f1a4..4f350e43d 100644 --- a/fabric-model-loading-api-v1/src/client/java/net/fabricmc/fabric/api/client/model/loading/v1/BlockStateResolver.java +++ b/fabric-model-loading-api-v1/src/client/java/net/fabricmc/fabric/api/client/model/loading/v1/BlockStateResolver.java @@ -22,7 +22,6 @@ import net.minecraft.block.Block; import net.minecraft.block.BlockState; import net.minecraft.client.render.model.ModelLoader; import net.minecraft.client.render.model.UnbakedModel; -import net.minecraft.client.util.ModelIdentifier; import net.minecraft.util.Identifier; /** @@ -72,7 +71,7 @@ public interface BlockStateResolver { void setModel(BlockState state, UnbakedModel model); /** - * Loads a model using an {@link Identifier} or {@link ModelIdentifier}, or gets it if it was already loaded. + * Loads a model using an {@link Identifier}, or gets it if it was already loaded. * * @param id the model identifier * @return the unbaked model, or a missing model if it is not present @@ -81,9 +80,6 @@ public interface BlockStateResolver { /** * The current model loader instance, which changes between resource reloads. - * - *

Do not call {@link ModelLoader#getOrLoadModel} as it does not supported nested model resolution; - * use {@link #getOrLoadModel} from the context instead. */ ModelLoader loader(); } diff --git a/fabric-model-loading-api-v1/src/client/java/net/fabricmc/fabric/api/client/model/loading/v1/DelegatingUnbakedModel.java b/fabric-model-loading-api-v1/src/client/java/net/fabricmc/fabric/api/client/model/loading/v1/DelegatingUnbakedModel.java index ee37263fd..081f5f6a0 100644 --- a/fabric-model-loading-api-v1/src/client/java/net/fabricmc/fabric/api/client/model/loading/v1/DelegatingUnbakedModel.java +++ b/fabric-model-loading-api-v1/src/client/java/net/fabricmc/fabric/api/client/model/loading/v1/DelegatingUnbakedModel.java @@ -27,7 +27,6 @@ import net.minecraft.client.render.model.Baker; import net.minecraft.client.render.model.ModelBakeSettings; import net.minecraft.client.render.model.UnbakedModel; import net.minecraft.client.texture.Sprite; -import net.minecraft.client.util.ModelIdentifier; import net.minecraft.client.util.SpriteIdentifier; import net.minecraft.util.Identifier; @@ -43,7 +42,7 @@ public final class DelegatingUnbakedModel implements UnbakedModel { /** * Constructs a new delegating model. * - * @param delegate The identifier (can be a {@link ModelIdentifier}) of the underlying baked model. + * @param delegate The identifier of the underlying baked model. */ public DelegatingUnbakedModel(Identifier delegate) { this.delegate = delegate; @@ -57,11 +56,12 @@ public final class DelegatingUnbakedModel implements UnbakedModel { @Override public void setParents(Function modelLoader) { + modelLoader.apply(delegate).setParents(modelLoader); } - @Nullable @Override - public BakedModel bake(Baker baker, Function textureGetter, ModelBakeSettings rotationContainer, Identifier modelId) { + @Nullable + public BakedModel bake(Baker baker, Function textureGetter, ModelBakeSettings rotationContainer) { return baker.bake(delegate, rotationContainer); } } diff --git a/fabric-model-loading-api-v1/src/client/java/net/fabricmc/fabric/api/client/model/loading/v1/FabricBakedModelManager.java b/fabric-model-loading-api-v1/src/client/java/net/fabricmc/fabric/api/client/model/loading/v1/FabricBakedModelManager.java index dbc5e0c4c..e0b8caba0 100644 --- a/fabric-model-loading-api-v1/src/client/java/net/fabricmc/fabric/api/client/model/loading/v1/FabricBakedModelManager.java +++ b/fabric-model-loading-api-v1/src/client/java/net/fabricmc/fabric/api/client/model/loading/v1/FabricBakedModelManager.java @@ -16,8 +16,6 @@ package net.fabricmc.fabric.api.client.model.loading.v1; -import org.jetbrains.annotations.Nullable; - import net.minecraft.client.render.model.BakedModel; import net.minecraft.client.render.model.BakedModelManager; import net.minecraft.client.util.ModelIdentifier; @@ -30,20 +28,18 @@ import net.minecraft.util.Identifier; */ public interface FabricBakedModelManager { /** - * An alternative to {@link BakedModelManager#getModel(ModelIdentifier)} that accepts an - * {@link Identifier} instead. Models loaded using {@link ModelLoadingPlugin.Context#addModels} - * do not have a corresponding {@link ModelIdentifier}, so the vanilla method cannot be used to - * retrieve them. The {@link Identifier} that was used to load them can be used in this method - * to retrieve them. + * Similar to {@link BakedModelManager#getModel(ModelIdentifier)}, but accepts an {@link Identifier} instead of a + * {@link ModelIdentifier}. Use this method to retrieve models loaded using + * {@link ModelLoadingPlugin.Context#addModels}, since those models do not have corresponding + * {@link ModelIdentifier}s. * *

This method, as well as its vanilla counterpart, should only be used after the * {@link BakedModelManager} has completed reloading. Otherwise, the result will be - * outdated or null. + * outdated or an exception will be thrown. * * @param id the id of the model * @return the model */ - @Nullable default BakedModel getModel(Identifier id) { throw new UnsupportedOperationException("Implemented via mixin."); } diff --git a/fabric-model-loading-api-v1/src/client/java/net/fabricmc/fabric/api/client/model/loading/v1/ModelLoadingPlugin.java b/fabric-model-loading-api-v1/src/client/java/net/fabricmc/fabric/api/client/model/loading/v1/ModelLoadingPlugin.java index 002deb2fa..5d15c8428 100644 --- a/fabric-model-loading-api-v1/src/client/java/net/fabricmc/fabric/api/client/model/loading/v1/ModelLoadingPlugin.java +++ b/fabric-model-loading-api-v1/src/client/java/net/fabricmc/fabric/api/client/model/loading/v1/ModelLoadingPlugin.java @@ -22,7 +22,6 @@ import org.jetbrains.annotations.ApiStatus; import net.minecraft.block.Block; import net.minecraft.client.render.model.json.JsonUnbakedModel; -import net.minecraft.client.util.ModelIdentifier; import net.minecraft.resource.ResourceManager; import net.minecraft.util.Identifier; @@ -53,13 +52,14 @@ public interface ModelLoadingPlugin { @ApiStatus.NonExtendable interface Context { /** - * Adds one or more models (can be {@link ModelIdentifier}s) to the list of models that will be loaded and - * baked. + * Adds one or more models that will be loaded, baked, and made available through + * {@link FabricBakedModelManager#getModel(Identifier)}. */ void addModels(Identifier... ids); /** - * Adds multiple models (can be {@link ModelIdentifier}s) to the list of models that will be loaded and baked. + * Adds multiple models that will be loaded, baked, and made available through + * {@link FabricBakedModelManager#getModel(Identifier)}. */ void addModels(Collection ids); diff --git a/fabric-model-loading-api-v1/src/client/java/net/fabricmc/fabric/api/client/model/loading/v1/ModelModifier.java b/fabric-model-loading-api-v1/src/client/java/net/fabricmc/fabric/api/client/model/loading/v1/ModelModifier.java index da6777996..0d8596b6d 100644 --- a/fabric-model-loading-api-v1/src/client/java/net/fabricmc/fabric/api/client/model/loading/v1/ModelModifier.java +++ b/fabric-model-loading-api-v1/src/client/java/net/fabricmc/fabric/api/client/model/loading/v1/ModelModifier.java @@ -20,6 +20,7 @@ import java.util.function.Function; import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.Nullable; +import org.jetbrains.annotations.UnknownNullability; import net.minecraft.client.render.model.BakedModel; import net.minecraft.client.render.model.Baker; @@ -39,7 +40,7 @@ import net.fabricmc.fabric.api.event.Event; * *

Example use cases: *