Skip to content

Commit

Permalink
Custom item renderers
Browse files Browse the repository at this point in the history
  • Loading branch information
DawnKiro committed Dec 26, 2024
1 parent 154218d commit 498d173
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/main/java/dev/jaegerwald/voidlings/item/ModItems.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,6 @@ private static Item registerItem(String name, Item item) {
}

public static void registerModItems() {
Renderers.register();
}
}
43 changes: 43 additions & 0 deletions src/main/java/dev/jaegerwald/voidlings/item/Renderers.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package dev.jaegerwald.voidlings.item;

import dev.jaegerwald.voidlings.Voidlings;
import net.minecraft.client.render.model.json.ModelTransformationMode;
import net.minecraft.client.util.ModelIdentifier;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.Identifier;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.BiPredicate;
import java.util.function.Consumer;

public class Renderers {
public static Map<Item, Map<BiPredicate<ItemStack, ModelTransformationMode>, ModelIdentifier>> RENDERERS = new HashMap<>();
public static final List<ModelIdentifier> FORCELOAD = new ArrayList<>();

public static void register() {
RENDERERS.put(ModItems.SURGEONS_BLADE, Map.of((stack, modelTransformationMode) -> {
return modelTransformationMode != ModelTransformationMode.GUI; // The condition to apply the model
}, forceLoad("fried_lamina")));
}

public static void apply(ItemStack stack, ModelTransformationMode mode, Consumer<ModelIdentifier> consumer) {
Map<BiPredicate<ItemStack, ModelTransformationMode>, ModelIdentifier> renderer = Renderers.RENDERERS.get(stack.getItem());
if (renderer != null) renderer.forEach(((predicate, modelIdentifier) -> {
if (predicate.test(stack, mode)) consumer.accept(modelIdentifier);
}));
}

private static ModelIdentifier forceLoad(String path) {
ModelIdentifier modelID = modelID(path);
FORCELOAD.add(modelID);
return modelID;
}

private static ModelIdentifier modelID(String path) {
return ModelIdentifier.ofInventoryVariant(Identifier.of(Voidlings.MOD_ID, path));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package dev.jaegerwald.voidlings.mixin;

import com.llamalad7.mixinextras.sugar.Local;
import com.llamalad7.mixinextras.sugar.ref.LocalRef;
import dev.jaegerwald.voidlings.item.Renderers;
import net.minecraft.client.render.VertexConsumerProvider;
import net.minecraft.client.render.item.ItemModels;
import net.minecraft.client.render.item.ItemRenderer;
import net.minecraft.client.render.model.BakedModel;
import net.minecraft.client.render.model.json.ModelTransformationMode;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.item.ItemStack;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(ItemRenderer.class)
public class ItemRendererMixin {
@Shadow
@Final
private ItemModels models;

@Inject(method = "renderItem(Lnet/minecraft/item/ItemStack;Lnet/minecraft/client/render/model/json/ModelTransformationMode;ZLnet/minecraft/client/util/math/MatrixStack;Lnet/minecraft/client/render/VertexConsumerProvider;IILnet/minecraft/client/render/model/BakedModel;)V", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/render/model/BakedModel;getTransformation()Lnet/minecraft/client/render/model/json/ModelTransformation;"))
private void interruptModelRendering(ItemStack stack, ModelTransformationMode renderMode, boolean leftHanded, MatrixStack matrices, VertexConsumerProvider vertexConsumers, int light, int overlay, BakedModel model, CallbackInfo ci, @Local(argsOnly = true) LocalRef<BakedModel> modelRef) {
Renderers.apply(stack, renderMode, (modelIdentifier) -> modelRef.set(models.getModelManager().getModel(modelIdentifier)));
}
}
28 changes: 28 additions & 0 deletions src/main/java/dev/jaegerwald/voidlings/mixin/ModelLoaderMixin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package dev.jaegerwald.voidlings.mixin;

import dev.jaegerwald.voidlings.item.Renderers;
import net.minecraft.client.color.block.BlockColors;
import net.minecraft.client.render.model.BlockStatesLoader;
import net.minecraft.client.render.model.ModelLoader;
import net.minecraft.client.render.model.json.JsonUnbakedModel;
import net.minecraft.client.util.ModelIdentifier;
import net.minecraft.util.Identifier;
import net.minecraft.util.profiler.Profiler;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import java.util.List;
import java.util.Map;

@Mixin(ModelLoader.class)
public abstract class ModelLoaderMixin {
@Shadow protected abstract void loadItemModel(ModelIdentifier id);

@Inject(method = "<init>", at = @At(value = "INVOKE", target = "Lnet/minecraft/client/render/model/ModelLoader;loadItemModel(Lnet/minecraft/client/util/ModelIdentifier;)V", ordinal = 0))
private void loadForcedModels(BlockColors blockColors, Profiler profiler, Map<Identifier, JsonUnbakedModel> jsonUnbakedModels, Map<Identifier, List<BlockStatesLoader.SourceTrackedData>> blockStates, CallbackInfo ci) {
Renderers.FORCELOAD.forEach(this::loadItemModel);
}
}
6 changes: 4 additions & 2 deletions src/main/resources/voidlings.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
"package": "dev.jaegerwald.voidlings.mixin",
"compatibilityLevel": "JAVA_21",
"mixins": [
"ExampleMixin",
"ExampleClientMixin",
"ExampleMixin",
"LightmapTextureManagerMixin"
],
"injectors": {
"defaultRequire": 1
},
"client": [
"LivingEntityRendererMixin"
"ItemRendererMixin",
"LivingEntityRendererMixin",
"ModelLoaderMixin"
]
}

0 comments on commit 498d173

Please sign in to comment.