Skip to content

Commit

Permalink
Convert PreparableModelLoadingPlugin.Holder to an interface
Browse files Browse the repository at this point in the history
  • Loading branch information
PepperCode1 committed Dec 5, 2024
1 parent 29a3b8a commit 998d135
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
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;
Expand Down Expand Up @@ -79,6 +80,10 @@ interface DataLoader<T> {
* Bundles a {@link PreparableModelLoadingPlugin} with its corresponding {@link DataLoader}
* for retrieval through {@link #getAll()}.
*/
record Holder<T>(DataLoader<T> loader, PreparableModelLoadingPlugin<T> plugin) {
@ApiStatus.NonExtendable
interface Holder<T> {
DataLoader<T> loader();

PreparableModelLoadingPlugin<T> plugin();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

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

@UnmodifiableView
public static final List<ModelLoadingPlugin> PLUGINS_VIEW = Collections.unmodifiableList(PLUGINS);
Expand All @@ -50,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 PreparableModelLoadingPlugin.Holder<>(loader, plugin));
PREPARABLE_PLUGINS.add(new HolderImpl<>(loader, plugin));
}

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

for (PreparableModelLoadingPlugin.Holder<?> holder : PREPARABLE_PLUGINS) {
for (HolderImpl<?> holder : PREPARABLE_PLUGINS) {
futures.add(preparePlugin(holder, resourceManager, executor));
}

return Util.combineSafe(futures);
}

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 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 HolderImpl<T>(PreparableModelLoadingPlugin.DataLoader<T> loader, PreparableModelLoadingPlugin<T> plugin) implements PreparableModelLoadingPlugin.Holder<T> {
}
}

0 comments on commit 998d135

Please sign in to comment.