summaryrefslogtreecommitdiff
path: root/src/main/java/darkknight/jewelrycraft/random/WeightedRandomItem.java
blob: 351046bc16ec807ff4bb2e3d5ca9b789f3046097 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package darkknight.jewelrycraft.random;

import java.util.Random;

import net.minecraft.item.ItemStack;
import net.minecraft.util.WeightedRandom;

public class WeightedRandomItem extends WeightedRandom.Item {
	private final ItemStack item;
	private int maxMeta, minMeta, minItem, maxItem;

	public WeightedRandomItem(ItemStack item, int weight) {
		super(weight);
		this.item = item;
		this.minItem = 1;
		this.maxItem = 1;
		this.maxMeta = 0;
		this.minMeta = 0;
	}

	public WeightedRandomItem(ItemStack item, int maxMetadata, int weight) {
		this(item, weight);
		this.maxMeta = maxMetadata;
	}

	public WeightedRandomItem(ItemStack item, int weight, int minItem, int maxItem) {
		this(item, weight);
		this.minItem = minItem;
		this.maxItem = maxItem;
	}

	public WeightedRandomItem setMaxMetadata(int meta) {
		this.maxMeta = meta;
		return this;
	}

	public WeightedRandomItem setMinMetadata(int meta) {
		this.minMeta = meta;
		return this;
	}

	public WeightedRandomItem setMinItem(int min) {
		this.minItem = min;
		return this;
	}

	public WeightedRandomItem setMaxItem(int max) {
		this.maxItem = max;
		return this;
	}

	public ItemStack getItem(Random random) {
		ItemStack itemstack = this.item.copy();
		if (maxMeta > 0)
			itemstack.setItemDamage(minMeta + random.nextInt(maxMeta - minMeta));
		if (maxItem > 1)
			itemstack.stackSize = this.minItem + random.nextInt(this.maxItem - this.minItem + 1);
		return itemstack;
	}
}