Skip to content

Commit

Permalink
Allow retrieving model loading plugins (#4269)
Browse files Browse the repository at this point in the history
* Allow retrieving model loading plugins

* Convert PreparableModelLoadingPlugin.Holder to an interface
  • Loading branch information
PepperCode1 authored Dec 5, 2024
1 parent fa62a02 commit 7b6b225
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 6 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,14 @@

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.ApiStatus;
import org.jetbrains.annotations.UnmodifiableView;

import net.minecraft.resource.ResourceManager;

import net.fabricmc.fabric.impl.client.model.loading.ModelLoadingPluginManager;
Expand All @@ -41,6 +45,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 +75,15 @@ interface DataLoader<T> {
*/
CompletableFuture<T> load(ResourceManager resourceManager, Executor executor);
}

/**
* Bundles a {@link PreparableModelLoadingPlugin} with its corresponding {@link DataLoader}
* for retrieval through {@link #getAll()}.
*/
@ApiStatus.NonExtendable
interface 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<HolderImpl<?>> 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 HolderImpl<>(loader, plugin));
}

public static CompletableFuture<List<ModelLoadingPlugin>> preparePlugins(ResourceManager resourceManager, Executor executor) {
Expand All @@ -54,19 +60,20 @@ public static CompletableFuture<List<ModelLoadingPlugin>> preparePlugins(Resourc
futures.add(CompletableFuture.completedFuture(plugin));
}

for (PreparablePluginHolder<?> holder : PREPARABLE_PLUGINS) {
for (HolderImpl<?> 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) {
private static <T> CompletableFuture<ModelLoadingPlugin> preparePlugin(HolderImpl<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) { }
private record HolderImpl<T>(PreparableModelLoadingPlugin.DataLoader<T> loader, PreparableModelLoadingPlugin<T> plugin) implements PreparableModelLoadingPlugin.Holder<T> {
}
}

0 comments on commit 7b6b225

Please sign in to comment.