package gvclib.client.event;

import gvclib.item.ItemGunBase;
import gvclib.mod_GVCLib;
import net.minecraft.client.model.HumanoidModel;
import net.minecraft.client.renderer.entity.LivingEntityRenderer;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.client.event.RenderHandEvent;
import net.minecraftforge.client.event.RenderLivingEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;

public class RenderGun_ThirdEvent {
    @OnlyIn(Dist.CLIENT)
    @SubscribeEvent
    public void rendergun(RenderLivingEvent.Pre event)
    {
        Entity entity = event.getEntity();
        if(entity != null) {
            String name = String.valueOf(entity.getName());
            LivingEntityRenderer render = event.getRenderer();
            boolean flag = false;
            if ((render instanceof LivingEntityRenderer))
            {
                LivingEntityRenderer renderbase = (LivingEntityRenderer) render;
                if ((renderbase.getModel() instanceof HumanoidModel)) {
                    for(int ii = 0; ii < 512; ++ii) {
                        if(mod_GVCLib.gunlayer_mob_name[ii] != null && mod_GVCLib.gunlayer_mob_name[ii].equals(name)) {
                            flag = true;
                            break;
                        }
                    }
                    if(!flag) {
                        renderbase.addLayer(new LayerGunBase(renderbase));
                        ++mod_GVCLib.gunlayer_mob;
                        mod_GVCLib.gunlayer_mob_name[mod_GVCLib.gunlayer_mob] = name;
                        System.out.println(String.format("road"));
                    }
                }
                if(entity instanceof LivingEntity) {
                    LivingEntity living = (LivingEntity)entity;
                    ItemStack itemstack = ((LivingEntity) (living)).getItemBySlot(EquipmentSlot.MAINHAND);
                    ItemStack itemstackl = ((LivingEntity) (living)).getItemBySlot(EquipmentSlot.OFFHAND);
                    if (!itemstack.isEmpty() && itemstack.getItem() instanceof ItemGunBase) {//item
                        ItemGunBase gun = (ItemGunBase) itemstack.getItem();
                        gun.ModelLoad();
                    }
                    if (!itemstackl.isEmpty()  && itemstackl.getItem() instanceof ItemGunBase) {//item
                        ItemGunBase gun = (ItemGunBase) itemstackl.getItem();
                        gun.ModelLoad();
                    }
                }


            }
        }
    }
}
