Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[1.21.4] Add model loading plugins to replace ModelEvent.ModifyBakingResult #1884

Draft
wants to merge 3 commits into
base: 1.21.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,18 @@
}

public ModelBakery.BakingResult bakeModels(ModelBakery.TextureGetter p_352431_) {
@@ -68,7 +_,10 @@
Map<ModelResourceLocation, BakedModel> map = new HashMap<>(this.unbakedBlockStateModels.size());
this.unbakedBlockStateModels.forEach((p_386257_, p_386258_) -> {
try {
- BakedModel bakedmodel1 = p_386258_.bake(new ModelBakery.ModelBakerImpl(p_352431_, p_386257_::toString));
+ ModelBakerImpl baker = new ModelBakerImpl(p_352431_, p_386257_::toString);
+ p_386258_ = net.neoforged.neoforge.client.model.loadingplugin.ModelLoadingPluginManager.modifyBlockModelBeforeBake(p_386257_, p_386258_, baker);
+ BakedModel bakedmodel1 = p_386258_.bake(baker);
+ bakedmodel1 = net.neoforged.neoforge.client.model.loadingplugin.ModelLoadingPluginManager.modifyBlockModelAfterBake(p_386257_, bakedmodel1, p_386258_, baker);
map.put(p_386257_, bakedmodel1);
} catch (Exception exception) {
LOGGER.warn("Unable to bake model: '{}': {}", p_386257_, exception);
@@ -92,7 +_,18 @@
LOGGER.warn("Unable to bake item model: '{}'", p_390101_, exception);
}
Expand Down Expand Up @@ -82,3 +94,13 @@
}

@OnlyIn(Dist.CLIENT)
@@ -142,7 +_,9 @@
return bakedmodel;
} else {
UnbakedModel unbakedmodel = this.getModel(p_252176_);
+ unbakedmodel = net.neoforged.neoforge.client.model.loadingplugin.ModelLoadingPluginManager.modifyModelBeforeBake(p_252176_, unbakedmodel, p_249765_, this);
BakedModel bakedmodel1 = UnbakedModel.bakeWithTopModelValues(unbakedmodel, this, p_249765_);
+ bakedmodel1 = net.neoforged.neoforge.client.model.loadingplugin.ModelLoadingPluginManager.modifyModelAfterBake(p_252176_, bakedmodel1, unbakedmodel, p_249765_, this);
ModelBakery.this.bakedCache.put(modelbakery$bakedcachekey, bakedmodel1);
return bakedmodel1;
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,11 @@
}

public void addRoot(ResolvableModel p_388596_) {
@@ -55,6 +_,7 @@

private UnbakedModel loadBlockModel(ResourceLocation p_361274_) {
UnbakedModel unbakedmodel = this.inputModels.get(p_361274_);
+ unbakedmodel = net.neoforged.neoforge.client.model.loadingplugin.ModelLoadingPluginManager.modifyModelOnLoad(p_361274_, unbakedmodel);
if (unbakedmodel == null) {
LOGGER.warn("Missing block model: '{}'", p_361274_);
return this.missingModel;
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,21 @@
}

public ClientItem.Properties getItemProperties(ResourceLocation p_390438_) {
@@ -115,6 +_,7 @@
@@ -115,11 +_,13 @@
public final CompletableFuture<Void> reload(
PreparableReloadListener.PreparationBarrier p_249079_, ResourceManager p_251134_, Executor p_250550_, Executor p_249221_
) {
+ net.neoforged.neoforge.client.model.UnbakedModelParser.init();
UnbakedModel unbakedmodel = MissingBlockModel.missingModel();
CompletableFuture<EntityModelSet> completablefuture = CompletableFuture.supplyAsync(EntityModelSet::vanilla, p_250550_);
CompletableFuture<SpecialBlockModelRenderer> completablefuture1 = completablefuture.thenApplyAsync(SpecialBlockModelRenderer::vanilla, p_250550_);
CompletableFuture<Map<ResourceLocation, UnbakedModel>> completablefuture2 = loadBlockModels(p_251134_, p_250550_);
- CompletableFuture<BlockStateModelLoader.LoadedModels> completablefuture3 = BlockStateModelLoader.loadBlockStates(unbakedmodel, p_251134_, p_250550_);
+ CompletableFuture<BlockStateModelLoader.LoadedModels> completablefuture3 = BlockStateModelLoader.loadBlockStates(unbakedmodel, p_251134_, p_250550_)
+ .thenApplyAsync(net.neoforged.neoforge.client.model.loadingplugin.ModelLoadingPluginManager::modifyBlockModelsOnLoad, p_250550_);
CompletableFuture<ClientItemInfoLoader.LoadedClientInfos> completablefuture4 = ClientItemInfoLoader.scheduleLoad(p_251134_, p_250550_);
CompletableFuture<ModelDiscovery> completablefuture5 = CompletableFuture.allOf(completablefuture2, completablefuture3, completablefuture4)
.thenApplyAsync(
@@ -154,8 +_,10 @@
completablefuture3.join().plainModels(),
completablefuture4.join().contents(),
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/net/neoforged/neoforge/client/ClientHooks.java
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@
import net.neoforged.neoforge.client.gui.GuiLayerManager;
import net.neoforged.neoforge.client.gui.map.MapDecorationRendererManager;
import net.neoforged.neoforge.client.model.data.ModelData;
import net.neoforged.neoforge.client.model.loadingplugin.ModelLoadingPluginManager;
import net.neoforged.neoforge.client.renderstate.RegisterRenderStateModifiersEvent;
import net.neoforged.neoforge.common.NeoForge;
import net.neoforged.neoforge.common.NeoForgeConfig;
Expand Down Expand Up @@ -998,6 +999,7 @@ public static void initClientHooks(Minecraft mc, ReloadableResourceManager resou
DimensionTransitionScreenManager.init();
AnimationTypeManager.init();
CoreShaderManager.init();
ModelLoadingPluginManager.init();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,24 @@
import com.google.common.base.Preconditions;
import java.util.Collections;
import java.util.Map;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.client.resources.model.Material;
import net.minecraft.client.resources.model.ModelBakery;
import net.minecraft.client.resources.model.ModelManager;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.block.Block;
import net.neoforged.bus.api.Event;
import net.neoforged.bus.api.ICancellableEvent;
import net.neoforged.fml.LogicalSide;
import net.neoforged.fml.event.IModBusEvent;
import net.neoforged.neoforge.client.model.UnbakedModelLoader;
import net.neoforged.neoforge.client.model.loadingplugin.BlockStateResolver;
import net.neoforged.neoforge.client.model.loadingplugin.ModelModifier;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.Nullable;

/**
* Houses events related to models.
Expand All @@ -43,7 +48,10 @@ protected ModelEvent() {}
* <p>This event is not {@linkplain ICancellableEvent cancellable}.</p>
*
* <p>This event is fired on the mod-specific event bus, only on the {@linkplain LogicalSide#CLIENT logical client}.</p>
*
* @deprecated Use {@link BlockStateResolver}s or {@link ModelModifier}s instead
*/
@Deprecated(forRemoval = true, since = "1.21.4")
public static class ModifyBakingResult extends ModelEvent implements IModBusEvent {
private final ModelBakery.BakingResult bakingResult;
private final Function<Material, TextureAtlasSprite> textureGetter;
Expand Down Expand Up @@ -86,7 +94,7 @@ public ModelBakery getModelBakery() {
* Fired when the {@link ModelManager} is notified of the resource manager reloading.
* Called after the model registry is set up and cached in the {@link net.minecraft.client.renderer.block.BlockModelShaper}.<br>
* The model registry given by this event is unmodifiable. To modify the model registry, use
* {@link ModelEvent.ModifyBakingResult} instead.
* {@link BlockStateResolver}s or {@link ModelModifier}s instead.
*
* <p>This event is not {@linkplain ICancellableEvent cancellable}.</p>
*
Expand Down Expand Up @@ -182,4 +190,27 @@ public void register(ResourceLocation key, UnbakedModelLoader<?> loader) {
loaders.put(key, loader);
}
}

public static class RegisterLoadingPlugins extends ModelEvent implements IModBusEvent {
private final Map<Block, BlockStateResolver> resolvers;
private final BiFunction<ResourceLocation, ModelModifier, @Nullable ModelModifier> modifierRegistrar;

@ApiStatus.Internal
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See other comment about this being internal; any mod implementing dynamic model loading probably needs to fire this (if the other class stays internal).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I personally would prefer exposing the necessary parts of ModelLoadingPluginManager.

public RegisterLoadingPlugins(Map<Block, BlockStateResolver> resolvers, BiFunction<ResourceLocation, ModelModifier, @Nullable ModelModifier> modifierRegistrar) {
this.resolvers = resolvers;
this.modifierRegistrar = modifierRegistrar;
}

public void registerResolver(Block block, BlockStateResolver resolver) {
if (resolvers.putIfAbsent(block, resolver) != null) {
throw new IllegalStateException("Duplicate BlockStateResolver registration for block " + block);
}
}

public void registerModifier(ResourceLocation id, ModelModifier modifier) {
if (modifierRegistrar.apply(id, modifier) != null) {
throw new IllegalStateException("Duplicate ModelLoadingPlugin registration with ID " + id);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright (c) NeoForged and contributors
* SPDX-License-Identifier: LGPL-2.1-only
*/

package net.neoforged.neoforge.client.model.loadingplugin;

import net.minecraft.client.renderer.block.model.UnbakedBlockStateModel;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
import org.jetbrains.annotations.Nullable;

public interface BlockStateResolver {
void resolveBlockStates(Context context);

interface Context {
Block getBlock();

@Nullable
UnbakedBlockStateModel getModel(BlockState state);

void setModel(BlockState state, UnbakedBlockStateModel model);
}
}
Loading
Loading