001package net.minecraft.util;
002
003import java.util.Random;
004import net.minecraft.inventory.IInventory;
005import net.minecraft.item.ItemStack;
006import net.minecraft.tileentity.TileEntityDispenser;
007import net.minecraftforge.common.ChestGenHooks;
008import net.minecraftforge.common.DungeonHooks;
009import cpw.mods.fml.common.FMLLog;
010
011public class WeightedRandomChestContent extends WeightedRandomItem
012{
013    /** The Item/Block ID to generate in the Chest. */
014    public ItemStack theItemId = null;
015
016    /** The minimum chance of item generating. */
017    public int theMinimumChanceToGenerateItem;
018
019    /** The maximum chance of item generating. */
020    public int theMaximumChanceToGenerateItem;
021
022    public WeightedRandomChestContent(int par1, int par2, int par3, int par4, int par5)
023    {
024        super(par5);
025        this.theItemId = new ItemStack(par1, 1, par2);
026        this.theMinimumChanceToGenerateItem = par3;
027        this.theMaximumChanceToGenerateItem = par4;
028    }
029
030    public WeightedRandomChestContent(ItemStack par1ItemStack, int par2, int par3, int par4)
031    {
032        super(par4);
033        this.theItemId = par1ItemStack;
034        this.theMinimumChanceToGenerateItem = par2;
035        this.theMaximumChanceToGenerateItem = par3;
036    }
037
038    /**
039     * Generates the Chest contents.
040     */
041    public static void generateChestContents(Random par0Random, WeightedRandomChestContent[] par1ArrayOfWeightedRandomChestContent, IInventory par2IInventory, int par3)
042    {
043        for (int j = 0; j < par3; ++j)
044        {
045            WeightedRandomChestContent weightedrandomchestcontent = (WeightedRandomChestContent)WeightedRandom.getRandomItem(par0Random, par1ArrayOfWeightedRandomChestContent);
046            int k = weightedrandomchestcontent.theMinimumChanceToGenerateItem + par0Random.nextInt(weightedrandomchestcontent.theMaximumChanceToGenerateItem - weightedrandomchestcontent.theMinimumChanceToGenerateItem + 1);
047
048            ItemStack[] stacks = ChestGenHooks.generateStacks(par0Random, weightedrandomchestcontent.theItemId, weightedrandomchestcontent.theMinimumChanceToGenerateItem, weightedrandomchestcontent.theMaximumChanceToGenerateItem);
049
050            for (ItemStack item : stacks)
051            {
052                par2IInventory.setInventorySlotContents(par0Random.nextInt(par2IInventory.getSizeInventory()), item);
053            }
054        }
055    }
056
057    /**
058     * Generates the Dispenser contents.
059     */
060    public static void generateDispenserContents(Random par0Random, WeightedRandomChestContent[] par1ArrayOfWeightedRandomChestContent, TileEntityDispenser par2TileEntityDispenser, int par3)
061    {
062        for (int j = 0; j < par3; ++j)
063        {
064            WeightedRandomChestContent weightedrandomchestcontent = (WeightedRandomChestContent)WeightedRandom.getRandomItem(par0Random, par1ArrayOfWeightedRandomChestContent);
065            ItemStack[] stacks = ChestGenHooks.generateStacks(par0Random, weightedrandomchestcontent.theItemId, weightedrandomchestcontent.theMinimumChanceToGenerateItem, weightedrandomchestcontent.theMaximumChanceToGenerateItem);
066
067            for (ItemStack item : stacks)
068            {
069                par2TileEntityDispenser.setInventorySlotContents(par0Random.nextInt(par2TileEntityDispenser.getSizeInventory()), item);
070            }
071        }
072    }
073
074    public static WeightedRandomChestContent[] func_92080_a(WeightedRandomChestContent[] par0ArrayOfWeightedRandomChestContent, WeightedRandomChestContent ... par1ArrayOfWeightedRandomChestContent)
075    {
076        WeightedRandomChestContent[] aweightedrandomchestcontent1 = new WeightedRandomChestContent[par0ArrayOfWeightedRandomChestContent.length + par1ArrayOfWeightedRandomChestContent.length];
077        int i = 0;
078
079        for (int j = 0; j < par0ArrayOfWeightedRandomChestContent.length; ++j)
080        {
081            aweightedrandomchestcontent1[i++] = par0ArrayOfWeightedRandomChestContent[j];
082        }
083
084        WeightedRandomChestContent[] aweightedrandomchestcontent2 = par1ArrayOfWeightedRandomChestContent;
085        int k = par1ArrayOfWeightedRandomChestContent.length;
086
087        for (int l = 0; l < k; ++l)
088        {
089            WeightedRandomChestContent weightedrandomchestcontent1 = aweightedrandomchestcontent2[l];
090            aweightedrandomchestcontent1[i++] = weightedrandomchestcontent1;
091        }
092
093        return aweightedrandomchestcontent1;
094    }
095
096    // -- Forge hooks
097    /**
098     * Allow a mod to submit a custom implementation that can delegate item stack generation beyond simple stack lookup
099     *
100     * @param random The current random for generation
101     * @param newInventory The inventory being generated (do not populate it, but you can refer to it)
102     * @return An array of {@link ItemStack} to put into the chest
103     */
104    protected ItemStack[] generateChestContent(Random random, IInventory newInventory)
105    {
106        return ChestGenHooks.generateStacks(random, theItemId, theMinimumChanceToGenerateItem, theMaximumChanceToGenerateItem);
107    }
108
109}