package net.minecraft.client.renderer;

import it.unimi.dsi.fastutil.objects.ObjectIterator;
import java.util.Map;
import javax.annotation.Nullable;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.ItemStack;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.world.level.ItemLike;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import net.minecraft.client.resources.model.ModelManager;
import net.minecraft.client.resources.model.BakedModel;
import net.minecraft.client.resources.model.ModelResourceLocation;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;

public class ItemModelShaper {
    public final Int2ObjectMap<ModelResourceLocation> shapes;
    private final Int2ObjectMap<BakedModel> shapesCache;
    private final ModelManager modelManager;
    
    public ItemModelShaper(final ModelManager enm) {
        this.shapes = (Int2ObjectMap<ModelResourceLocation>)new Int2ObjectOpenHashMap(256);
        this.shapesCache = (Int2ObjectMap<BakedModel>)new Int2ObjectOpenHashMap(256);
        this.modelManager = enm;
    }
    
    public TextureAtlasSprite getParticleIcon(final ItemLike bqa) {
        return this.getParticleIcon(new ItemStack(bqa));
    }
    
    public TextureAtlasSprite getParticleIcon(final ItemStack bki) {
        final BakedModel enh3 = this.getItemModel(bki);
        if (enh3 == this.modelManager.getMissingModel() && bki.getItem() instanceof BlockItem) {
            return this.modelManager.getBlockModelShaper().getParticleIcon(((BlockItem)bki.getItem()).getBlock().defaultBlockState());
        }
        return enh3.getParticleIcon();
    }
    
    public BakedModel getItemModel(final ItemStack bki) {
        final BakedModel enh3 = this.getItemModel(bki.getItem());
        return (enh3 == null) ? this.modelManager.getMissingModel() : enh3;
    }
    
    @Nullable
    public BakedModel getItemModel(final Item bke) {
        return (BakedModel)this.shapesCache.get(getIndex(bke));
    }
    
    private static int getIndex(final Item bke) {
        return Item.getId(bke);
    }
    
    public void register(final Item bke, final ModelResourceLocation enn) {
        this.shapes.put(getIndex(bke), enn);
    }
    
    public ModelManager getModelManager() {
        return this.modelManager;
    }
    
    public void rebuildCache() {
        this.shapesCache.clear();
        for (final Map.Entry<Integer, ModelResourceLocation> entry3 : this.shapes.entrySet()) {
            this.shapesCache.put(Integer.valueOf(entry3.getKey()), this.modelManager.getModel(entry3.getValue()));
        }
    }
}
