Interface ItemRandomizer<T>
- Type Parameters:
T- the type of item this random item model returns
public interface ItemRandomizer<T>
ItemRandomizer provides a way to randomly choose an item based on a weight value.
For instances use the following factory functions:
Item one = new Item();
Item two = new Item();
Item three = new Item();
ItemRandomizer<Object> randomizer = ItemRandomizer.randomizer(Arrays.asList(one, two, three));
randomizer.setWeight(one, 10);
randomizer.setWeight(two, 60);
randomizer.setWeight(three, 30);
//10% chance of getting 'one', 60% chance of getting 'two' and 30% chance of getting 'three'.
Item random = randomizer.get().orElse(null);
randomizer(Collection)-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic interfaceWraps an item for usage in the ItemRandomizer. -
Method Summary
Modifier and TypeMethodDescriptionget()Fetches a random item from this model based on the item weights.items()static <T> ItemRandomizer<T> randomizer(Collection<ItemRandomizer.RandomItem<T>> items) Instantiates a newItemRandomizer.
-
Method Details
-
items
Collection<ItemRandomizer.RandomItem<T>> items()- Returns:
- the items in this model.
-
weight
- Returns:
- the
Valuecontrolling the weight
-
enabled
- Parameters:
item- the item- Returns:
- the item enabled state
-
get
Fetches a random item from this model based on the item weights.- Returns:
- a randomly chosen item or an empty
Optionalin case no item is enabled or the total weights are zero
-
randomizer
Instantiates a newItemRandomizer.- Type Parameters:
T- the item type- Parameters:
items- the items to randomize- Returns:
- a new
ItemRandomizer
-