public class ItemItemWithChance extends Item {
private ArrayList<DropsItem> list_drops = new ArrayList<DropsItem>();
public ItemItemWithChance() {
super();
setCreativeTab(CreativeTabs.tabMisc);
addItemWithChance(50F, Items.stick);
addItemWithChance(35F, new ItemStack(Items.coal, 1, 1));
addItemWithChance(15F, new ItemStack(Items.coal, 1, 0));
addItemWithChance(25F, Items.beef);
addItemWithChance(5F, Items.diamond);
addItemWithChance(1F, Items.nether_wart);
addItemWithChance(0.001F, Items.nether_star);
/*
addItemWithChance(30F, Items.diamond);
addItemWithChance(70F, Items.nether_wart);
*/
}
@Override
public ItemStack onItemRightClick(ItemStack is, World world, EntityPlayer player) {
if (!world.isRemote) {
float p = world.rand.nextFloat() * getChanceMax();
ItemStack item_drop = null;
if (list_drops != null)
for (int i = 0; i < list_drops.size(); ++i) {
if (list_drops.get(i) != null) {
if (list_drops.get(i).getChanceA() <= p && list_drops.get(i).getChanceB() > p) {
item_drop = list_drops.get(i).getItem().copy();
//System.out.println("onItemRightClick - " + p + ", " + item_drop);
break;
}
}
}
if (item_drop != null) {
player.func_146097_a(item_drop, false, false);
//player.inventory.addItemStackToInventory(item_drop);
--is.stackSize;
}
}
return is;
}
public void addItemWithChance(float chance, Object item) {
float a = 0F;
if (list_drops != null && list_drops.size() > 0) {
float b = -1F;
for (int i = 0; i < list_drops.size(); ++i) {
if (list_drops.get(i) != null) {
if (b < list_drops.get(i).getChanceB()) b = list_drops.get(i).getChanceB();
else continue;
} else continue;
}
if (b != -1F) a = b;
}
list_drops.add(new DropsItem(a, a + chance, item));
}
public float getChanceMax() {
float a = 0F;
if (list_drops != null && list_drops.size() > 0) {
float b = -1;
for (int i = 0; i < list_drops.size(); ++i) {
if (list_drops.get(i) != null) {
if (b < list_drops.get(i).getChanceB()) b = list_drops.get(i).getChanceB();
else continue;
} else continue;
}
if (b != -1) a = b;
}
return a;
}
private static class DropsItem {
private ItemStack item;
private float chanceA;
private float chanceB;
private DropsItem(float chanceA, float chanceB, Object item) {
if (item == null) return;
if (item != null) {
if (item instanceof Block) this.item = new ItemStack((Block)item);
else if (item instanceof Item) this.item = new ItemStack((Item)item);
else if (item instanceof ItemStack) this.item = (ItemStack)item;
//this.item = item;
}
this.chanceA = chanceA;
this.chanceB = chanceB;
}
public ItemStack getItem() {
return item;
}
public float getChanceA() {
return chanceA;
}
public float getChanceB() {
return chanceB;
}
}
}