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