package com.infinityraider.infinitylib.render.item;

import com.infinityraider.infinitylib.item.ICustomRenderedItem;
import com.google.common.collect.ImmutableList;
import net.minecraft.client.renderer.block.model.ModelResourceLocation;
import net.minecraft.client.resources.IResourceManager;
import net.minecraft.item.Item;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.client.model.ICustomModelLoader;
import net.minecraftforge.client.model.IModel;
import net.minecraftforge.client.model.ModelLoader;
import net.minecraftforge.client.model.ModelLoaderRegistry;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SideOnly(Side.CLIENT)
public class ItemRendererRegistry implements ICustomModelLoader {
    private static final ItemRendererRegistry INSTANCE = new ItemRendererRegistry();

    public static ItemRendererRegistry getInstance() {
        return INSTANCE;
    }

    private final Map<ResourceLocation, ItemRenderer<? extends Item>> renderers;
    private final List<ICustomRenderedItem<? extends Item>> items;

    private ItemRendererRegistry() {
        this.renderers = new HashMap<>();
        this.items = new ArrayList<>();
        ModelLoaderRegistry.registerLoader(this);
    }

    @Override
    public boolean accepts(ResourceLocation loc) {
        return renderers.containsKey(loc);
    }

    @Override
    public IModel loadModel(ResourceLocation loc) throws Exception {
        return renderers.get(loc);
    }

    @Override
    public void onResourceManagerReload(IResourceManager resourceManager) {}

    public List<ICustomRenderedItem<? extends Item>> getRegisteredItems() {
        return ImmutableList.copyOf(items);
    }

    @SideOnly(Side.CLIENT)
    @SuppressWarnings("unchecked")
    public void registerCustomItemRenderer(ICustomRenderedItem<? extends Item> customRenderedItem) {
        if (customRenderedItem == null || !(customRenderedItem instanceof Item)) {
            return;
        }
        Item item = (Item) customRenderedItem;
        ModelResourceLocation itemModel = customRenderedItem.getItemModelResourceLocation();
        IItemRenderingHandler<? extends Item> renderer = customRenderedItem.getRenderer();
        if (renderer != null) {
            ItemRenderer<? extends Item> instance = new ItemRenderer<>(renderer);
            renderers.put(itemModel, instance);
            ModelLoader.setCustomMeshDefinition(item, stack -> itemModel);

        }
        items.add(customRenderedItem);
    }
}

@@ -314,24 +314,61 @@
 .item.Item;%0A
+import net.minecraft.item.ItemStack;%0A
 import net.m
@@ -2383,147 +2383,701 @@
   if
- (renderer != null) %7B%0A            ItemRenderer%3C? extends Item%3E instance = new ItemRenderer%3C%3E(renderer);%0A            renderers.put(itemModel
+(item.getHasSubtypes()) %7B%0A            List%3CItemStack%3E subItems = new ArrayList%3C%3E();%0A            item.getSubItems(item, item.getCreativeTab(), subItems);%0A            for(ItemStack stack : subItems) %7B%0A                ModelLoader.setCustomModelResourceLocation(item, stack.getItemDamage(), itemModel);%0A            %7D%0A        %7D else %7B%0A            ModelLoader.setCustomModelResourceLocation(item, 0, itemModel);%0A        %7D%0A        if (renderer != null) %7B%0A            ItemRenderer%3C? extends Item%3E instance = new ItemRenderer%3C%3E(renderer);%0A            ResourceLocation loc = new ResourceLocation(itemModel.getResourceDomain(), %22models/item/%22 + itemModel.getResourcePath());%0A            renderers.put(loc
 , in
@@ -3160,17 +3160,16 @@
 Model);%0A
-%0A
         

Fix error in item model registering