Skip to content

Commit

Permalink
fix: Use ModelLoadingPlugin to fix additional model loading in Fabric
Browse files Browse the repository at this point in the history
  • Loading branch information
BlayTheNinth committed Feb 19, 2024
1 parent bde2ecc commit 539a798
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
package net.blay09.mods.balm.fabric.client;

import net.blay09.mods.balm.api.Balm;
import net.blay09.mods.balm.api.client.BalmClient;
import net.blay09.mods.balm.api.event.client.DisconnectedFromServerEvent;
import net.blay09.mods.balm.fabric.client.rendering.FabricBalmModels;
import net.blay09.mods.balm.fabric.network.FabricBalmNetworking;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientLifecycleEvents;
import net.fabricmc.fabric.api.client.model.loading.v1.ModelLoadingPlugin;

public class FabricBalmClient implements ClientModInitializer {
@Override
public void onInitializeClient() {
ClientLifecycleEvents.CLIENT_STARTED.register(client -> FabricBalmNetworking.initializeClientHandlers());

Balm.getEvents().onEvent(DisconnectedFromServerEvent.class, event -> Balm.getConfig().resetToBackingConfigs());

ModelLoadingPlugin.register((FabricBalmModels) BalmClient.getModels());
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package net.blay09.mods.balm.fabric.client.rendering;

import com.mojang.datafixers.util.Pair;
import com.mojang.logging.LogUtils;
import com.mojang.math.Transformation;
import net.blay09.mods.balm.api.DeferredObject;
import net.blay09.mods.balm.api.client.rendering.BalmModels;
import net.fabricmc.fabric.api.client.model.loading.v1.ModelLoadingPlugin;
import net.fabricmc.loader.api.FabricLoader;
import net.fabricmc.loader.api.MappingResolver;
import net.minecraft.client.renderer.RenderType;
Expand All @@ -16,7 +16,6 @@
import net.minecraft.world.level.block.state.BlockState;
import org.jetbrains.annotations.Nullable;
import org.joml.Matrix4f;
import org.slf4j.Logger;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
Expand All @@ -26,38 +25,37 @@
import java.util.function.Function;
import java.util.function.Supplier;

public class FabricBalmModels implements BalmModels {

private static final Logger LOGGER = LogUtils.getLogger();
public class FabricBalmModels implements BalmModels, ModelLoadingPlugin {

private static abstract class DeferredModel extends DeferredObject<BakedModel> {
public DeferredModel(ResourceLocation identifier) {
super(identifier);
}

public void resolveAndSet(ModelBakery modelBakery, BiFunction<ResourceLocation, Material, TextureAtlasSprite> spriteBiFunction) {
try {
set(resolve(modelBakery, spriteBiFunction));
} catch (Exception exception) {
LOGGER.warn("Unable to bake model: '{}':", getIdentifier(), exception);
set(modelBakery.getBakedTopLevelModels().get(ModelBakery.MISSING_MODEL_LOCATION));
}
}

public abstract BakedModel resolve(ModelBakery modelBakery, BiFunction<ResourceLocation, Material, TextureAtlasSprite> spriteBiFunction);
}

@Override
public void set(BakedModel object) {
super.set(object);
}
}

private final List<ResourceLocation> additionalModels = Collections.synchronizedList(new ArrayList<>());
private final List<DeferredModel> modelsToBake = Collections.synchronizedList(new ArrayList<>());
public final List<Pair<Supplier<Block>, Supplier<BakedModel>>> overrides = Collections.synchronizedList(new ArrayList<>());
private ModelBakery modelBakery;

@Override
public void onInitializeModelLoader(Context context) {
context.addModels(additionalModels);
}

public void onBakeModels(ModelBakery modelBakery, BiFunction<ResourceLocation, Material, TextureAtlasSprite> spriteBiFunction) {
this.modelBakery = modelBakery;

synchronized (modelsToBake) {
for (DeferredModel model : modelsToBake) {
model.resolveAndSet(modelBakery, spriteBiFunction);
model.set(model.resolve(modelBakery, spriteBiFunction));
}
}

Expand All @@ -78,13 +76,10 @@ public DeferredObject<BakedModel> loadModel(final ResourceLocation identifier) {
DeferredModel deferredModel = new DeferredModel(identifier) {
@Override
public BakedModel resolve(ModelBakery bakery, BiFunction<ResourceLocation, Material, TextureAtlasSprite> spriteBiFunction) {
UnbakedModel model = bakery.getModel(identifier);
ModelBaker baker = createBaker(identifier, spriteBiFunction);
Function<Material, TextureAtlasSprite> modelTextureGetter = createModelTextureGetter(identifier, spriteBiFunction);
return model.bake(baker, modelTextureGetter, getModelState(Transformation.identity()), identifier);
return modelBakery.getBakedTopLevelModels().get(identifier);
}
};
modelsToBake.add(deferredModel);
additionalModels.add(identifier);
return deferredModel;
}

Expand Down

0 comments on commit 539a798

Please sign in to comment.