Skip to content

Commit

Permalink
update porting lib, use FAPI for model swapping
Browse files Browse the repository at this point in the history
  • Loading branch information
TropheusJ committed Aug 3, 2023
1 parent b2fa876 commit 2b00ebc
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 19 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
@@ -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<ResourceLocation, NonNullFunction<BakedModel, ? extends BakedModel>> swaps = null;

public CustomBlockModels getCustomBlockModels() {
return customBlockModels;
}
Expand All @@ -34,26 +40,24 @@ public CustomItemModels getCustomItemModels() {
return customItemModels;
}

public void onModelBake(Map<ResourceLocation, BakedModel> 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 <T extends BakedModel> void swapModels(Map<ResourceLocation, BakedModel> modelRegistry,
List<ModelResourceLocation> locations, Function<BakedModel, T> factory) {
locations.forEach(location -> {
swapModels(modelRegistry, location, factory);
});
@Override
public BakedModel modifyModelAfterBake(BakedModel model, Context context) {
if (swaps == null)
collectSwaps();
NonNullFunction<BakedModel, ? extends BakedModel> swap = swaps.get(context.id());
return swap != null ? swap.apply(model) : model;
}

public static <T extends BakedModel> void swapModels(Map<ResourceLocation, BakedModel> modelRegistry,
ModelResourceLocation location, Function<BakedModel, T> 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<ModelResourceLocation> getAllBlockStateModelLocations(Block block) {
Expand Down

0 comments on commit 2b00ebc

Please sign in to comment.