Re-implement ArmorRenderingRegistry (#1607)

* reimplement ArmorRenderingRegistry

* fix license headers

* remove todo

* move registry to static method

* minor reformat

* reduce api classes into one

* reformat

* make registry static

* reformat

* change Item in registry to ItemConvertible

* register mixin properly this time

* explain test mod

* reformat

* stricter null check
This commit is contained in:
Aelpecyem 2021-08-08 19:17:54 +02:00 committed by GitHub
parent edbb166422
commit 3c3f33ee93
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 246 additions and 1 deletions

View file

@ -0,0 +1,86 @@
/*
* Copyright (c) 2016, 2017, 2018, 2019 FabricMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.fabricmc.fabric.api.client.rendering.v1;
import net.minecraft.client.model.Model;
import net.minecraft.client.render.OverlayTexture;
import net.minecraft.client.render.RenderLayer;
import net.minecraft.client.render.VertexConsumer;
import net.minecraft.client.render.VertexConsumerProvider;
import net.minecraft.client.render.entity.feature.FeatureRenderer;
import net.minecraft.client.render.entity.model.BipedEntityModel;
import net.minecraft.client.render.item.ItemRenderer;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.entity.EquipmentSlot;
import net.minecraft.entity.LivingEntity;
import net.minecraft.item.ItemConvertible;
import net.minecraft.item.ItemStack;
import net.minecraft.util.Identifier;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.fabricmc.fabric.impl.client.rendering.ArmorRendererRegistryImpl;
/**
* Armor renderers render worn armor items with custom code.
* They may be used to render armor with special models or effects.
*
* <p>The renderers are registered with {@link net.fabricmc.fabric.api.client.rendering.v1.ArmorRenderer#register(ArmorRenderer, ItemConvertible...)}.
*/
@Environment(EnvType.CLIENT)
@FunctionalInterface
public interface ArmorRenderer {
/**
* Registers the armor renderer for the specified items.
* @param renderer the renderer
* @param items the items
* @throws IllegalArgumentException if an item already has a registered armor renderer
* @throws NullPointerException if either an item or the renderer is null
*/
static void register(ArmorRenderer renderer, ItemConvertible... items) {
ArmorRendererRegistryImpl.register(renderer, items);
}
/**
* Helper method for rendering a specific armor model, comes after setting visibility.
*
* <p>This primarily handles applying glint and the correct {@link RenderLayer}
* @param matrices the matrix stack
* @param vertexConsumers the vertex consumer provider
* @param light packed lightmap coordinates
* @param stack the item stack of the armor item
* @param model the model to be rendered
* @param texture the texture to be applied
*/
static void renderPart(MatrixStack matrices, VertexConsumerProvider vertexConsumers, int light, ItemStack stack, Model model, Identifier texture) {
VertexConsumer vertexConsumer = ItemRenderer.getArmorGlintConsumer(vertexConsumers, RenderLayer.getArmorCutoutNoCull(texture), false, stack.hasGlint());
model.render(matrices, vertexConsumer, light, OverlayTexture.DEFAULT_UV, 1, 1, 1, 1);
}
/**
* Renders an armor part.
*
* @param matrices the matrix stack
* @param vertexConsumers the vertex consumer provider
* @param stack the item stack of the armor item
* @param entity the entity wearing the armor item
* @param slot the equipment slot in which the armor stack is worn
* @param light packed lightmap coordinates
* @param contextModel the model provided by {@link FeatureRenderer#getContextModel()}
*/
void render(MatrixStack matrices, VertexConsumerProvider vertexConsumers, ItemStack stack, LivingEntity entity, EquipmentSlot slot, int light, BipedEntityModel<LivingEntity> contextModel);
}

View file

@ -0,0 +1,53 @@
/*
* Copyright (c) 2016, 2017, 2018, 2019 FabricMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.fabricmc.fabric.impl.client.rendering;
import java.util.HashMap;
import java.util.Objects;
import org.jetbrains.annotations.Nullable;
import net.minecraft.item.ItemConvertible;
import net.minecraft.item.Item;
import net.minecraft.util.registry.Registry;
import net.fabricmc.fabric.api.client.rendering.v1.ArmorRenderer;
public class ArmorRendererRegistryImpl {
private static final HashMap<Item, ArmorRenderer> RENDERERS = new HashMap<>();
public static void register(ArmorRenderer renderer, ItemConvertible... items) {
Objects.requireNonNull(renderer, "renderer is null");
if (items.length == 0) {
throw new IllegalArgumentException("Armor renderer registered for no item");
}
for (ItemConvertible item : items) {
Objects.requireNonNull(item.asItem(), "armor item is null");
if (RENDERERS.putIfAbsent(item.asItem(), renderer) != null) {
throw new IllegalArgumentException("Custom armor renderer already exists for " + Registry.ITEM.getId(item.asItem()));
}
}
}
@Nullable
public static ArmorRenderer get(Item item) {
return RENDERERS.get(item);
}
}

View file

@ -0,0 +1,53 @@
/*
* Copyright (c) 2016, 2017, 2018, 2019 FabricMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.fabricmc.fabric.mixin.client.rendering;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import net.minecraft.client.render.VertexConsumerProvider;
import net.minecraft.client.render.entity.feature.ArmorFeatureRenderer;
import net.minecraft.client.render.entity.feature.FeatureRenderer;
import net.minecraft.client.render.entity.feature.FeatureRendererContext;
import net.minecraft.client.render.entity.model.BipedEntityModel;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.entity.EquipmentSlot;
import net.minecraft.entity.LivingEntity;
import net.minecraft.item.ItemStack;
import net.fabricmc.fabric.api.client.rendering.v1.ArmorRenderer;
import net.fabricmc.fabric.impl.client.rendering.ArmorRendererRegistryImpl;
@Mixin(ArmorFeatureRenderer.class)
public abstract class MixinArmorFeatureRenderer extends FeatureRenderer<LivingEntity, BipedEntityModel<LivingEntity>> {
private MixinArmorFeatureRenderer(FeatureRendererContext<LivingEntity, BipedEntityModel<LivingEntity>> context) {
super(context);
}
@Inject(method = "renderArmor", at = @At("HEAD"), cancellable = true)
private void renderArmor(MatrixStack matrices, VertexConsumerProvider vertexConsumers, LivingEntity entity, EquipmentSlot armorSlot, int light, BipedEntityModel<LivingEntity> model, CallbackInfo ci) {
ItemStack stack = entity.getEquippedStack(armorSlot);
ArmorRenderer renderer = ArmorRendererRegistryImpl.get(stack.getItem());
if (renderer != null) {
renderer.render(matrices, vertexConsumers, stack, entity, armorSlot, light, getContextModel());
ci.cancel();
}
}
}

View file

@ -3,6 +3,7 @@
"package": "net.fabricmc.fabric.mixin.client.rendering", "package": "net.fabricmc.fabric.mixin.client.rendering",
"compatibilityLevel": "JAVA_16", "compatibilityLevel": "JAVA_16",
"client": [ "client": [
"MixinArmorFeatureRenderer",
"MixinBlockColorMap", "MixinBlockColorMap",
"MixinBuiltinModelItemRenderer", "MixinBuiltinModelItemRenderer",
"MixinInGameHud", "MixinInGameHud",

View file

@ -0,0 +1,51 @@
/*
* Copyright (c) 2016, 2017, 2018, 2019 FabricMC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package net.fabricmc.fabric.test.rendering.client;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.render.entity.model.BipedEntityModel;
import net.minecraft.client.render.entity.model.EntityModelLayers;
import net.minecraft.entity.EquipmentSlot;
import net.minecraft.entity.LivingEntity;
import net.minecraft.item.Items;
import net.minecraft.util.Identifier;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.rendering.v1.ArmorRenderer;
public class ArmorRenderingTests implements ClientModInitializer {
private BipedEntityModel<LivingEntity> armorModel;
private final Identifier texture = new Identifier("textures/block/dirt.png");
// Renders a biped model with dirt texture, replacing diamond helmet and diamond chest plate rendering
@Override
public void onInitializeClient() {
ArmorRenderer.register((matrices, vertexConsumers, stack, entity, slot, light, model) -> {
if (armorModel == null) {
armorModel = new BipedEntityModel<>(MinecraftClient.getInstance().getEntityModelLoader().getModelPart(EntityModelLayers.PLAYER_OUTER_ARMOR));
}
model.setAttributes(armorModel);
armorModel.setVisible(false);
armorModel.body.visible = slot == EquipmentSlot.CHEST;
armorModel.leftArm.visible = slot == EquipmentSlot.CHEST;
armorModel.rightArm.visible = slot == EquipmentSlot.CHEST;
armorModel.head.visible = slot == EquipmentSlot.HEAD;
ArmorRenderer.renderPart(matrices, vertexConsumers, light, stack, armorModel, texture);
}, Items.DIAMOND_HELMET, Items.DIAMOND_CHESTPLATE);
}
}

View file

@ -10,7 +10,8 @@
}, },
"entrypoints": { "entrypoints": {
"client": [ "client": [
"net.fabricmc.fabric.test.rendering.client.WorldRenderEventsTests" "net.fabricmc.fabric.test.rendering.client.WorldRenderEventsTests",
"net.fabricmc.fabric.test.rendering.client.ArmorRenderingTests"
] ]
} }
} }