From 29a3b8a17eec31748b0217a5b0fb2febafc08892 Mon Sep 17 00:00:00 2001 From: PepperCode1 <44146161+PepperCode1@users.noreply.github.com> Date: Tue, 3 Dec 2024 13:08:52 -0800 Subject: [PATCH] Allow retrieving model loading plugins --- .../model/loading/v1/ModelLoadingPlugin.java | 10 +++++++++ .../v1/PreparableModelLoadingPlugin.java | 18 +++++++++++++++ .../loading/ModelLoadingPluginManager.java | 22 +++++++++++-------- 3 files changed, 41 insertions(+), 9 deletions(-) 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 615c69712c..30bc4ea274 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 @@ -17,8 +17,10 @@ package net.fabricmc.fabric.api.client.model.loading.v1; import java.util.Collection; +import java.util.List; import org.jetbrains.annotations.ApiStatus; +import org.jetbrains.annotations.UnmodifiableView; import net.minecraft.block.Block; import net.minecraft.resource.ResourceManager; @@ -42,6 +44,14 @@ static void register(ModelLoadingPlugin plugin) { ModelLoadingPluginManager.registerPlugin(plugin); } + /** + * Gets a list of all registered model loading plugins. + */ + @UnmodifiableView + static List getAll() { + return ModelLoadingPluginManager.PLUGINS_VIEW; + } + /** * Called towards the beginning of the model loading process, every time resource are (re)loaded. * Use the context object to extend model loading as desired. diff --git a/fabric-model-loading-api-v1/src/client/java/net/fabricmc/fabric/api/client/model/loading/v1/PreparableModelLoadingPlugin.java b/fabric-model-loading-api-v1/src/client/java/net/fabricmc/fabric/api/client/model/loading/v1/PreparableModelLoadingPlugin.java index b0a3eb526a..943c8145c1 100644 --- a/fabric-model-loading-api-v1/src/client/java/net/fabricmc/fabric/api/client/model/loading/v1/PreparableModelLoadingPlugin.java +++ b/fabric-model-loading-api-v1/src/client/java/net/fabricmc/fabric/api/client/model/loading/v1/PreparableModelLoadingPlugin.java @@ -16,10 +16,13 @@ package net.fabricmc.fabric.api.client.model.loading.v1; +import java.util.List; import java.util.concurrent.CompletableFuture; import java.util.concurrent.Executor; import java.util.function.Supplier; +import org.jetbrains.annotations.UnmodifiableView; + import net.minecraft.resource.ResourceManager; import net.fabricmc.fabric.impl.client.model.loading.ModelLoadingPluginManager; @@ -41,6 +44,14 @@ static void register(DataLoader loader, PreparableModelLoadingPlugin p ModelLoadingPluginManager.registerPlugin(loader, plugin); } + /** + * Gets a list of all registered preparable model loading plugins. + */ + @UnmodifiableView + static List> getAll() { + return ModelLoadingPluginManager.PREPARABLE_PLUGINS_VIEW; + } + /** * Called towards the beginning of the model loading process, every time resource are (re)loaded. * Use the context object to extend model loading as desired. @@ -63,4 +74,11 @@ interface DataLoader { */ CompletableFuture load(ResourceManager resourceManager, Executor executor); } + + /** + * Bundles a {@link PreparableModelLoadingPlugin} with its corresponding {@link DataLoader} + * for retrieval through {@link #getAll()}. + */ + record Holder(DataLoader loader, PreparableModelLoadingPlugin plugin) { + } } diff --git a/fabric-model-loading-api-v1/src/client/java/net/fabricmc/fabric/impl/client/model/loading/ModelLoadingPluginManager.java b/fabric-model-loading-api-v1/src/client/java/net/fabricmc/fabric/impl/client/model/loading/ModelLoadingPluginManager.java index 21f43bd2a4..fd878fdd40 100644 --- a/fabric-model-loading-api-v1/src/client/java/net/fabricmc/fabric/impl/client/model/loading/ModelLoadingPluginManager.java +++ b/fabric-model-loading-api-v1/src/client/java/net/fabricmc/fabric/impl/client/model/loading/ModelLoadingPluginManager.java @@ -17,11 +17,14 @@ package net.fabricmc.fabric.impl.client.model.loading; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import java.util.Objects; import java.util.concurrent.CompletableFuture; import java.util.concurrent.Executor; +import org.jetbrains.annotations.UnmodifiableView; + import net.minecraft.resource.ResourceManager; import net.minecraft.util.Util; @@ -30,9 +33,12 @@ public final class ModelLoadingPluginManager { private static final List PLUGINS = new ArrayList<>(); - private static final List> PREPARABLE_PLUGINS = new ArrayList<>(); + private static final List> PREPARABLE_PLUGINS = new ArrayList<>(); - public static final ThreadLocal> CURRENT_PLUGINS = new ThreadLocal<>(); + @UnmodifiableView + public static final List PLUGINS_VIEW = Collections.unmodifiableList(PLUGINS); + @UnmodifiableView + public static final List> PREPARABLE_PLUGINS_VIEW = Collections.unmodifiableList(PREPARABLE_PLUGINS); public static void registerPlugin(ModelLoadingPlugin plugin) { Objects.requireNonNull(plugin, "plugin must not be null"); @@ -44,7 +50,7 @@ public static void registerPlugin(PreparableModelLoadingPlugin.DataLoader Objects.requireNonNull(loader, "data loader must not be null"); Objects.requireNonNull(plugin, "plugin must not be null"); - PREPARABLE_PLUGINS.add(new PreparablePluginHolder<>(loader, plugin)); + PREPARABLE_PLUGINS.add(new PreparableModelLoadingPlugin.Holder<>(loader, plugin)); } public static CompletableFuture> preparePlugins(ResourceManager resourceManager, Executor executor) { @@ -54,19 +60,17 @@ public static CompletableFuture> preparePlugins(Resourc futures.add(CompletableFuture.completedFuture(plugin)); } - for (PreparablePluginHolder holder : PREPARABLE_PLUGINS) { + for (PreparableModelLoadingPlugin.Holder holder : PREPARABLE_PLUGINS) { futures.add(preparePlugin(holder, resourceManager, executor)); } return Util.combineSafe(futures); } - private static CompletableFuture preparePlugin(PreparablePluginHolder holder, ResourceManager resourceManager, Executor executor) { - CompletableFuture dataFuture = holder.loader.load(resourceManager, executor); - return dataFuture.thenApply(data -> pluginContext -> holder.plugin.initialize(data, pluginContext)); + private static CompletableFuture preparePlugin(PreparableModelLoadingPlugin.Holder holder, ResourceManager resourceManager, Executor executor) { + CompletableFuture dataFuture = holder.loader().load(resourceManager, executor); + return dataFuture.thenApply(data -> pluginContext -> holder.plugin().initialize(data, pluginContext)); } private ModelLoadingPluginManager() { } - - private record PreparablePluginHolder(PreparableModelLoadingPlugin.DataLoader loader, PreparableModelLoadingPlugin plugin) { } }