Fix #602: Allow querying registered DynamicItemRenderers (#2779)

This commit is contained in:
Technici4n 2022-12-23 16:22:51 +01:00 committed by GitHub
parent f1e4495b13
commit 0b401b6a39
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 6 deletions

View file

@ -16,7 +16,7 @@
package net.fabricmc.fabric.api.client.rendering.v1;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import net.minecraft.client.render.VertexConsumerProvider;
import net.minecraft.client.render.model.json.ModelTransformation;
@ -75,7 +75,13 @@ public interface BuiltinItemRendererRegistry {
* @throws IllegalArgumentException if the item already has a registered renderer
* @throws NullPointerException if either the item or the renderer is null
*/
void register(@NotNull ItemConvertible item, @NotNull DynamicItemRenderer renderer);
void register(ItemConvertible item, DynamicItemRenderer renderer);
/**
* Returns the renderer for the item, or {@code null} if the item has no renderer.
*/
@Nullable
DynamicItemRenderer get(ItemConvertible item);
/**
* Dynamic item renderers render items with custom code.

View file

@ -58,8 +58,11 @@ public final class BuiltinItemRendererRegistryImpl implements BuiltinItemRendere
}
}
@Override
@Nullable
public static DynamicItemRenderer getRenderer(Item item) {
return RENDERERS.get(item);
public DynamicItemRenderer get(ItemConvertible item) {
Objects.requireNonNull(item.asItem(), "item is null");
return RENDERERS.get(item.asItem());
}
}

View file

@ -16,6 +16,7 @@
package net.fabricmc.fabric.mixin.client.rendering;
import org.jetbrains.annotations.Nullable;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
@ -28,13 +29,13 @@ import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.item.ItemStack;
import net.fabricmc.fabric.api.client.rendering.v1.BuiltinItemRendererRegistry;
import net.fabricmc.fabric.impl.client.rendering.BuiltinItemRendererRegistryImpl;
@Mixin(BuiltinModelItemRenderer.class)
abstract class BuiltinModelItemRendererMixin {
@Inject(method = "render", at = @At("HEAD"), cancellable = true)
private void fabric_onRender(ItemStack stack, ModelTransformation.Mode mode, MatrixStack matrices, VertexConsumerProvider vertexConsumers, int light, int overlay, CallbackInfo info) {
/* @Nullable */ BuiltinItemRendererRegistry.DynamicItemRenderer renderer = BuiltinItemRendererRegistryImpl.getRenderer(stack.getItem());
@Nullable
BuiltinItemRendererRegistry.DynamicItemRenderer renderer = BuiltinItemRendererRegistry.INSTANCE.get(stack.getItem());
if (renderer != null) {
renderer.render(stack, mode, matrices, vertexConsumers, light, overlay);