package net.minecraft.util;

import java.util.Random;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntityDispenser;
import net.minecraftforge.common.ChestGenHooks;

public class WeightedRandomChestContent extends WeightedRandom.Item
{
	public ItemStack theItemId;
	public int theMinimumChanceToGenerateItem;
	public int theMaximumChanceToGenerateItem;
	private static final String __OBFID = "CL_00001505";

	public WeightedRandomChestContent(Item p_i45311_1_, int p_i45311_2_, int p_i45311_3_, int p_i45311_4_, int p_i45311_5_)
	{
		super(p_i45311_5_);
		this.theItemId = new ItemStack(p_i45311_1_, 1, p_i45311_2_);
		this.theMinimumChanceToGenerateItem = p_i45311_3_;
		this.theMaximumChanceToGenerateItem = p_i45311_4_;
	}

	public WeightedRandomChestContent(ItemStack par1ItemStack, int par2, int par3, int par4)
	{
		super(par4);
		this.theItemId = par1ItemStack;
		this.theMinimumChanceToGenerateItem = par2;
		this.theMaximumChanceToGenerateItem = par3;
	}

	public static void generateChestContents(Random par0Random, WeightedRandomChestContent[] par1ArrayOfWeightedRandomChestContent, IInventory par2IInventory, int par3)
	{
		for (int j = 0; j < par3; ++j)
		{
			WeightedRandomChestContent weightedrandomchestcontent = (WeightedRandomChestContent)WeightedRandom.getRandomItem(par0Random, par1ArrayOfWeightedRandomChestContent);
			ItemStack[] stacks = weightedrandomchestcontent.generateChestContent(par0Random, par2IInventory);

			for (ItemStack item : stacks)
			{
				par2IInventory.setInventorySlotContents(par0Random.nextInt(par2IInventory.getSizeInventory()), item);
			}
		}
	}

	public static void generateDispenserContents(Random p_150706_0_, WeightedRandomChestContent[] p_150706_1_, TileEntityDispenser p_150706_2_, int p_150706_3_)
	{
		for (int j = 0; j < p_150706_3_; ++j)
		{
			WeightedRandomChestContent weightedrandomchestcontent = (WeightedRandomChestContent)WeightedRandom.getRandomItem(p_150706_0_, p_150706_1_);
			int k = weightedrandomchestcontent.theMinimumChanceToGenerateItem + p_150706_0_.nextInt(weightedrandomchestcontent.theMaximumChanceToGenerateItem - weightedrandomchestcontent.theMinimumChanceToGenerateItem + 1);
			ItemStack[] stacks = weightedrandomchestcontent.generateChestContent(p_150706_0_, p_150706_2_);
			for (ItemStack item : stacks)
			{
				p_150706_2_.setInventorySlotContents(p_150706_0_.nextInt(p_150706_2_.getSizeInventory()), item);
			}
		}
	}

	public static WeightedRandomChestContent[] func_92080_a(WeightedRandomChestContent[] par0ArrayOfWeightedRandomChestContent, WeightedRandomChestContent ... par1ArrayOfWeightedRandomChestContent)
	{
		WeightedRandomChestContent[] aweightedrandomchestcontent1 = new WeightedRandomChestContent[par0ArrayOfWeightedRandomChestContent.length + par1ArrayOfWeightedRandomChestContent.length];
		int i = 0;

		for (int j = 0; j < par0ArrayOfWeightedRandomChestContent.length; ++j)
		{
			aweightedrandomchestcontent1[i++] = par0ArrayOfWeightedRandomChestContent[j];
		}

		WeightedRandomChestContent[] aweightedrandomchestcontent2 = par1ArrayOfWeightedRandomChestContent;
		int k = par1ArrayOfWeightedRandomChestContent.length;

		for (int l = 0; l < k; ++l)
		{
			WeightedRandomChestContent weightedrandomchestcontent1 = aweightedrandomchestcontent2[l];
			aweightedrandomchestcontent1[i++] = weightedrandomchestcontent1;
		}

		return aweightedrandomchestcontent1;
	}

	// -- Forge hooks
	/**
	 * Allow a mod to submit a custom implementation that can delegate item stack generation beyond simple stack lookup
	 *
	 * @param random The current random for generation
	 * @param newInventory The inventory being generated (do not populate it, but you can refer to it)
	 * @return An array of {@link ItemStack} to put into the chest
	 */
	protected ItemStack[] generateChestContent(Random random, IInventory newInventory)
	{
		return ChestGenHooks.generateStacks(random, theItemId, theMinimumChanceToGenerateItem, theMaximumChanceToGenerateItem);
	}
}