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 eca) {
        this.shapes = (Int2ObjectMap<ModelResourceLocation>)new Int2ObjectOpenHashMap(256);
        this.shapesCache = (Int2ObjectMap<BakedModel>)new Int2ObjectOpenHashMap(256);
        this.modelManager = eca;
    }
    
    public TextureAtlasSprite getParticleIcon(final ItemLike bjv) {
        return this.getParticleIcon(new ItemStack(bjv));
    }
    
    public TextureAtlasSprite getParticleIcon(final ItemStack ben) {
        final BakedModel ebv3 = this.getItemModel(ben);
        if (ebv3 == this.modelManager.getMissingModel() && ben.getItem() instanceof BlockItem) {
            return this.modelManager.getBlockModelShaper().getParticleIcon(((BlockItem)ben.getItem()).getBlock().defaultBlockState());
        }
        return ebv3.getParticleIcon();
    }
    
    public BakedModel getItemModel(final ItemStack ben) {
        final BakedModel ebv3 = this.getItemModel(ben.getItem());
        return (ebv3 == null) ? this.modelManager.getMissingModel() : ebv3;
    }
    
    @Nullable
    public BakedModel getItemModel(final Item bei) {
        return (BakedModel)this.shapesCache.get(getIndex(bei));
    }
    
    private static int getIndex(final Item bei) {
        return Item.getId(bei);
    }
    
    public void register(final Item bei, final ModelResourceLocation ecb) {
        this.shapes.put(getIndex(bei), ecb);
    }
    
    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()));
        }
    }
}
