diff --git a/gradle.properties b/gradle.properties index ef1e443874..3349f3bb4f 100644 --- a/gradle.properties +++ b/gradle.properties @@ -32,7 +32,7 @@ reach_entity_attributes_version = 2.4.0 registrate_version = 1.3.61-MC1.20 milk_lib_version = 1.2.60 -port_lib_version = 2.1.1089+1.20 +port_lib_version = 2.1.1093+1.20 # adding a module also requires adding a dependency to the FMJ port_lib_modules = accessors,base,entity,extensions,fake_players,networking,obj_loader,tags,transfer,models,tool_actions,client_events,brewing diff --git a/src/main/java/com/simibubi/create/foundation/utility/ModelSwapper.java b/src/main/java/com/simibubi/create/foundation/utility/ModelSwapper.java index 1e199540f0..72fd730279 100644 --- a/src/main/java/com/simibubi/create/foundation/utility/ModelSwapper.java +++ b/src/main/java/com/simibubi/create/foundation/utility/ModelSwapper.java @@ -1,31 +1,37 @@ package com.simibubi.create.foundation.utility; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.function.Function; +import java.util.function.UnaryOperator; import com.simibubi.create.foundation.block.render.CustomBlockModels; import com.simibubi.create.foundation.item.render.CustomItemModels; import com.simibubi.create.foundation.item.render.CustomRenderedItemModel; import com.simibubi.create.foundation.item.render.CustomRenderedItems; -import io.github.fabricators_of_create.porting_lib.models.events.ModelEvents; +import com.tterrag.registrate.util.nullness.NonNullFunction; + +import net.fabricmc.fabric.api.client.model.loading.v1.ModelLoadingPlugin; +import net.fabricmc.fabric.api.client.model.loading.v1.ModelModifier.AfterBake; import net.minecraft.client.renderer.block.BlockModelShaper; import net.minecraft.client.resources.model.BakedModel; import net.minecraft.client.resources.model.ModelBakery; -import net.minecraft.client.resources.model.ModelManager; import net.minecraft.client.resources.model.ModelResourceLocation; import net.minecraft.resources.ResourceLocation; import net.minecraft.world.item.Item; import net.minecraft.world.level.block.Block; -public class ModelSwapper { +public class ModelSwapper implements AfterBake { protected CustomBlockModels customBlockModels = new CustomBlockModels(); protected CustomItemModels customItemModels = new CustomItemModels(); + private Map> swaps = null; + public CustomBlockModels getCustomBlockModels() { return customBlockModels; } @@ -34,26 +40,24 @@ public CustomItemModels getCustomItemModels() { return customItemModels; } - public void onModelBake(Map modelRegistry, ModelBakery modelBakery) { - customBlockModels.forEach((block, modelFunc) -> swapModels(modelRegistry, getAllBlockStateModelLocations(block), modelFunc)); - customItemModels.forEach((item, modelFunc) -> swapModels(modelRegistry, getItemModelLocation(item), modelFunc)); - CustomRenderedItems.forEach(item -> swapModels(modelRegistry, getItemModelLocation(item), CustomRenderedItemModel::new)); - } - public void registerListeners() { - ModelEvents.MODIFY_BAKING_RESULT.register(this::onModelBake); + ModelLoadingPlugin.register(ctx -> ctx.modifyModelAfterBake().register(this)); } - public static void swapModels(Map modelRegistry, - List locations, Function factory) { - locations.forEach(location -> { - swapModels(modelRegistry, location, factory); - }); + @Override + public BakedModel modifyModelAfterBake(BakedModel model, Context context) { + if (swaps == null) + collectSwaps(); + NonNullFunction swap = swaps.get(context.id()); + return swap != null ? swap.apply(model) : model; } - public static void swapModels(Map modelRegistry, - ModelResourceLocation location, Function factory) { - modelRegistry.put(location, factory.apply(modelRegistry.get(location))); + private void collectSwaps() { + this.swaps = new HashMap<>(); + + customBlockModels.forEach((block, swapper) -> getAllBlockStateModelLocations(block).forEach(id -> swaps.put(id, swapper))); + customItemModels.forEach((item, swapper) -> swaps.put(getItemModelLocation(item), swapper)); + CustomRenderedItems.forEach(item -> swaps.put(getItemModelLocation(item), CustomRenderedItemModel::new)); } public static List getAllBlockStateModelLocations(Block block) {