Skip to content

Commit

Permalink
Allow retrieving model loading plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
PepperCode1 committed Dec 3, 2024
1 parent 32952c4 commit 29a3b8a
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -42,6 +44,14 @@ static void register(ModelLoadingPlugin plugin) {
ModelLoadingPluginManager.registerPlugin(plugin);
}

/**
* Gets a list of all registered model loading plugins.
*/
@UnmodifiableView
static List<ModelLoadingPlugin> 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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -41,6 +44,14 @@ static <T> void register(DataLoader<T> loader, PreparableModelLoadingPlugin<T> p
ModelLoadingPluginManager.registerPlugin(loader, plugin);
}

/**
* Gets a list of all registered preparable model loading plugins.
*/
@UnmodifiableView
static List<Holder<?>> 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.
Expand All @@ -63,4 +74,11 @@ interface DataLoader<T> {
*/
CompletableFuture<T> load(ResourceManager resourceManager, Executor executor);
}

/**
* Bundles a {@link PreparableModelLoadingPlugin} with its corresponding {@link DataLoader}
* for retrieval through {@link #getAll()}.
*/
record Holder<T>(DataLoader<T> loader, PreparableModelLoadingPlugin<T> plugin) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -30,9 +33,12 @@

public final class ModelLoadingPluginManager {
private static final List<ModelLoadingPlugin> PLUGINS = new ArrayList<>();
private static final List<PreparablePluginHolder<?>> PREPARABLE_PLUGINS = new ArrayList<>();
private static final List<PreparableModelLoadingPlugin.Holder<?>> PREPARABLE_PLUGINS = new ArrayList<>();

public static final ThreadLocal<List<ModelLoadingPlugin>> CURRENT_PLUGINS = new ThreadLocal<>();
@UnmodifiableView
public static final List<ModelLoadingPlugin> PLUGINS_VIEW = Collections.unmodifiableList(PLUGINS);
@UnmodifiableView
public static final List<PreparableModelLoadingPlugin.Holder<?>> PREPARABLE_PLUGINS_VIEW = Collections.unmodifiableList(PREPARABLE_PLUGINS);

public static void registerPlugin(ModelLoadingPlugin plugin) {
Objects.requireNonNull(plugin, "plugin must not be null");
Expand All @@ -44,7 +50,7 @@ public static <T> void registerPlugin(PreparableModelLoadingPlugin.DataLoader<T>
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<List<ModelLoadingPlugin>> preparePlugins(ResourceManager resourceManager, Executor executor) {
Expand All @@ -54,19 +60,17 @@ public static CompletableFuture<List<ModelLoadingPlugin>> 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 <T> CompletableFuture<ModelLoadingPlugin> preparePlugin(PreparablePluginHolder<T> holder, ResourceManager resourceManager, Executor executor) {
CompletableFuture<T> dataFuture = holder.loader.load(resourceManager, executor);
return dataFuture.thenApply(data -> pluginContext -> holder.plugin.initialize(data, pluginContext));
private static <T> CompletableFuture<ModelLoadingPlugin> preparePlugin(PreparableModelLoadingPlugin.Holder<T> holder, ResourceManager resourceManager, Executor executor) {
CompletableFuture<T> dataFuture = holder.loader().load(resourceManager, executor);
return dataFuture.thenApply(data -> pluginContext -> holder.plugin().initialize(data, pluginContext));
}

private ModelLoadingPluginManager() { }

private record PreparablePluginHolder<T>(PreparableModelLoadingPlugin.DataLoader<T> loader, PreparableModelLoadingPlugin<T> plugin) { }
}

0 comments on commit 29a3b8a

Please sign in to comment.