Skip to content

Commit

Permalink
feat: Update to Minecraft 1.21.4
Browse files Browse the repository at this point in the history
  • Loading branch information
BlayTheNinth committed Dec 3, 2024
1 parent 3f31711 commit 841d0be
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 105 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,110 +1,48 @@
package net.blay09.mods.balm.neoforge.client.rendering;

import com.mojang.datafixers.util.Pair;
import com.mojang.logging.LogUtils;
import net.blay09.mods.balm.api.DeferredObject;
import net.blay09.mods.balm.api.client.rendering.BalmModels;
import net.minecraft.client.renderer.block.BlockModelShaper;
import net.minecraft.client.Minecraft;
import net.minecraft.client.resources.model.*;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.block.Block;
import net.neoforged.bus.api.IEventBus;
import net.neoforged.bus.api.SubscribeEvent;
import net.neoforged.fml.ModLoadingContext;
import net.neoforged.neoforge.client.event.ModelEvent;
import org.slf4j.Logger;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Supplier;

public class NeoForgeBalmModels implements BalmModels {

private static final Logger LOGGER = LogUtils.getLogger();

private abstract static class DeferredModel extends DeferredObject<BakedModel> {
private final ModelResourceLocation modelResourceLocation;

public DeferredModel(ModelResourceLocation modelResourceLocation) {
super(modelResourceLocation.id());
this.modelResourceLocation = modelResourceLocation;
}

public void resolveAndSet(ModelBakery modelBakery, Map<ModelResourceLocation, BakedModel> modelRegistry, ModelBakery.TextureGetter textureGetter) {
try {
set(resolve(modelBakery, modelRegistry, textureGetter));
} catch (Exception exception) {
LOGGER.warn("Unable to bake model: '{}':", getIdentifier(), exception);
set(modelBakery.getBakedTopLevelModels().get(MissingBlockModel.VARIANT));
}
}

public abstract BakedModel resolve(ModelBakery modelBakery, Map<ModelResourceLocation, BakedModel> modelRegistry, ModelBakery.TextureGetter textureGetter);

public ModelResourceLocation getModelResourceLocation() {
return modelResourceLocation;
}
}

public final List<DeferredModel> modelsToBake = Collections.synchronizedList(new ArrayList<>());

private static class Registrations {
public final List<DeferredModel> additionalModels = new ArrayList<>();
public final List<Pair<Supplier<Block>, Supplier<BakedModel>>> overrides = new ArrayList<>();
private ModelBakery.TextureGetter textureGetter;

public void setTextureGetter(ModelBakery.TextureGetter textureGetter) {
this.textureGetter = textureGetter;
}
public final List<ResourceLocation> additionalModels = new ArrayList<>();

@SubscribeEvent
public void onRegisterAdditionalModels(ModelEvent.RegisterAdditional event) {
additionalModels.forEach(it -> event.register(it.getModelResourceLocation()));
}

@SubscribeEvent
public void onModelBakingCompleted(ModelEvent.ModifyBakingResult event) {
for (Pair<Supplier<Block>, Supplier<BakedModel>> override : overrides) {
Block block = override.getFirst().get();
BakedModel bakedModel = override.getSecond().get();
block.getStateDefinition().getPossibleStates().forEach(state -> {
ModelResourceLocation modelLocation = BlockModelShaper.stateToModelLocation(state);
event.getModels().put(modelLocation, bakedModel);
});
}
}

@SubscribeEvent
public void onModelBakingCompleted(ModelEvent.BakingCompleted event) {
for (DeferredModel deferredModel : additionalModels) {
deferredModel.resolveAndSet(event.getModelBakery(), event.getModels(), textureGetter);
}

textureGetter = null;
additionalModels.forEach(event::register);
}
}

private final Map<String, Registrations> registrations = new ConcurrentHashMap<>();

public void onBakeModels(ModelBakery modelBakery, ModelBakery.TextureGetter textureGetter) {
registrations.values().forEach(it -> it.setTextureGetter(textureGetter));

synchronized (modelsToBake) {
for (DeferredModel deferredModel : modelsToBake) {
deferredModel.resolveAndSet(modelBakery, modelBakery.getBakedTopLevelModels(), textureGetter);
}
}
}

@Override
public DeferredObject<BakedModel> loadModel(ResourceLocation identifier) {
DeferredModel deferredModel = new DeferredModel(new ModelResourceLocation(identifier, "standalone")) {
final var modelResourceLocation = new ModelResourceLocation(identifier, "standalone");
final var deferredModel = new DeferredObject<BakedModel>(identifier) {
@Override
public BakedModel resolve() {
return Minecraft.getInstance().getModelManager().getModel(modelResourceLocation);
}

@Override
public BakedModel resolve(ModelBakery bakery, Map<ModelResourceLocation, BakedModel> modelRegistry, ModelBakery.TextureGetter textureGetter) {
return modelRegistry.get(getModelResourceLocation());
public boolean canResolve() {
final var modelManager = Minecraft.getInstance().getModelManager();
final var foundModel = modelManager.getModel(modelResourceLocation);
return foundModel != modelManager.getMissingModel();
}
};
getActiveRegistrations().additionalModels.add(deferredModel);
getActiveRegistrations().additionalModels.add(identifier);
return deferredModel;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.mojang.datafixers.util.Pair;
import net.blay09.mods.balm.api.client.rendering.BalmRenderers;
import net.minecraft.client.color.block.BlockColor;
import net.minecraft.client.color.item.ItemColor;
import net.minecraft.client.model.geom.ModelLayerLocation;
import net.minecraft.client.model.geom.builders.LayerDefinition;
import net.minecraft.client.renderer.RenderType;
Expand Down Expand Up @@ -55,7 +54,6 @@ private static class Registrations {
public final List<Pair<Supplier<BlockEntityType<?>>, BlockEntityRendererProvider<BlockEntity>>> blockEntityRenderers = new ArrayList<>();
public final List<Pair<Supplier<EntityType<?>>, EntityRendererProvider<Entity>>> entityRenderers = new ArrayList<>();
public final List<ColorRegistration<BlockColor, Block>> blockColors = new ArrayList<>();
public final List<ColorRegistration<ItemColor, ItemLike>> itemColors = new ArrayList<>();

@SubscribeEvent
public void setupClient(FMLClientSetupEvent event) {
Expand Down Expand Up @@ -85,13 +83,6 @@ public void initBlockColors(RegisterColorHandlersEvent.Block event) {
event.register(blockColor.getColor(), blockColor.getObjects().get());
}
}

@SubscribeEvent
public void initItemColors(RegisterColorHandlersEvent.Item event) {
for (ColorRegistration<ItemColor, ItemLike> itemColor : itemColors) {
event.register(itemColor.getColor(), itemColor.getObjects().get());
}
}
}

private final Map<String, Registrations> registrations = new ConcurrentHashMap<>();
Expand Down
3 changes: 1 addition & 2 deletions neoforge/src/main/resources/balm.neoforge.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
"MouseHandlerAccessor",
"ScreenAccessor",
"MinecraftMixin",
"LootTableMixin",
"ModelBakeryMixin"
"LootTableMixin"
],
"injectors": {
"defaultRequire": 1
Expand Down

0 comments on commit 841d0be

Please sign in to comment.