package net.minecraft.client.renderer.entity.layers;

import net.minecraft.world.entity.Entity;
import com.mojang.blaze3d.vertex.VertexConsumer;
import net.minecraft.world.item.ItemStack;
import net.minecraft.client.renderer.texture.OverlayTexture;
import net.minecraft.client.renderer.entity.ItemRenderer;
import net.minecraft.world.entity.player.PlayerModelPart;
import net.minecraft.client.player.AbstractClientPlayer;
import net.minecraft.world.item.Items;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.client.renderer.MultiBufferSource;
import com.mojang.blaze3d.vertex.PoseStack;
import net.minecraft.client.renderer.entity.RenderLayerParent;
import net.minecraft.client.model.ElytraModel;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.client.model.EntityModel;
import net.minecraft.world.entity.LivingEntity;

public class ElytraLayer<T extends LivingEntity, M extends EntityModel<T>> extends RenderLayer<T, M> {
    private static final ResourceLocation WINGS_LOCATION;
    private final ElytraModel<T> elytraModel;
    
    public ElytraLayer(final RenderLayerParent<T, M> dwv) {
        super(dwv);
        this.elytraModel = new ElytraModel<T>();
    }
    
    @Override
    public void render(final PoseStack cyl, final MultiBufferSource dqt, final int integer, final T akz, final float float5, final float float6, final float float7, final float float8, final float float9, final float float10) {
        final ItemStack ben12 = akz.getItemBySlot(EquipmentSlot.CHEST);
        if (ben12.getItem() != Items.ELYTRA) {
            return;
        }
        ResourceLocation sm13;
        if (akz instanceof AbstractClientPlayer) {
            final AbstractClientPlayer dpy14 = (AbstractClientPlayer)akz;
            if (dpy14.isElytraLoaded() && dpy14.getElytraTextureLocation() != null) {
                sm13 = dpy14.getElytraTextureLocation();
            }
            else if (dpy14.isCapeLoaded() && dpy14.getCloakTextureLocation() != null && dpy14.isModelPartShown(PlayerModelPart.CAPE)) {
                sm13 = dpy14.getCloakTextureLocation();
            }
            else {
                sm13 = ElytraLayer.WINGS_LOCATION;
            }
        }
        else {
            sm13 = ElytraLayer.WINGS_LOCATION;
        }
        cyl.pushPose();
        cyl.translate(0.0, 0.0, 0.125);
        this.getParentModel().copyPropertiesTo(this.elytraModel);
        this.elytraModel.setupAnim(akz, float5, float6, float8, float9, float10);
        final VertexConsumer cyo14 = ItemRenderer.getFoilBuffer(dqt, this.elytraModel.renderType(sm13), false, ben12.hasFoil());
        this.elytraModel.renderToBuffer(cyl, cyo14, integer, OverlayTexture.NO_OVERLAY, 1.0f, 1.0f, 1.0f, 1.0f);
        cyl.popPose();
    }
    
    static {
        WINGS_LOCATION = new ResourceLocation("textures/entity/elytra.png");
    }
}
