Class WeightedChance<E>
java.lang.Object
me.sciguymjm.uberenchant.api.utils.WeightedChance<E>
- Type Parameters:
E- - The type of result (Can be anything)
Simple utility class for weighted chances
Example Usage:
Example Usage:
WeightedChance<Boolean> wc = new WeightedChance<Boolean>(); // Create new WeightedChance for booleans
wc.add(0.9, false); // False 90% of the time
wc.add(0.1, true); // True 10% of the time
System.out.println(wc.next()); // Print the result
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final recordA Simple record for use it storing values. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanAdd a new possible selection with a percent chance.booleanChecks if this WeightedChance contains the specified result or not.doubleGets the weight of the specified result.next()Gets the next possible result.booleanRemove possible selections if the result exists.booleanremove(Predicate<WeightedChance.WeightedEntry<E>> predicate) Remove possible selections based on a predicate.
-
Constructor Details
-
WeightedChance
public WeightedChance()
-
-
Method Details
-
add
Add a new possible selection with a percent chance.- Parameters:
weight- The chance (Between 0.0 and 1.0, 1.0 being 100% chance)result- The result- Returns:
- True if it was successfully added
-
remove
Remove possible selections if the result exists.- Parameters:
result- The result to remove- Returns:
- True if any were removed
-
remove
Remove possible selections based on a predicate.- Parameters:
predicate- The predicate to use- Returns:
- True if any were removed
-
next
Gets the next possible result.- Returns:
- The next result
-
getWeight
Gets the weight of the specified result.- Parameters:
result- The result to get the weight for- Returns:
- The weight or 0 if not found
-
contains
Checks if this WeightedChance contains the specified result or not.- Parameters:
result- the result to check for- Returns:
- True if this WeightedChance contains the result
-