The chance is a double between 0.0 and 1.0, where 0.0 is 0% and 1.0 is 100%.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final double
The chance value of something happens.private static final Pattern
The pattern used to parse a chance.static final Chance
A constant value for a chance of 1.0.private static final Random
The random number generator used to generate the chance.static final Chance
A constant value for a chance of 0.0.
This is the default chance. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
Chance
(double chance) Constructs a new chance with the given chance.
The chance will be clamped between 0.0 and 1.0. -
Method Summary
Modifier and TypeMethodDescriptionboolean
int
hashCode()
boolean
isFalse()
Checks if the chance is always false.boolean
isTrue()
Checks if the chance is always true.static @NotNull Chance
of
(double chance) Creates a new chance with the given chance.static @NotNull Chance
Parses a chance from a string.boolean
result()
Gets the result of the chance.static void
setSeed
(long seed) Sets the seed of the random number generator.toString()
-
Field Details
-
CHANCE_PATTERN
The pattern used to parse a chance.
The chance can be either 1.0, or 0. followed by at least one digit.
The chance must be followed by a percent sign.
A dot or a comma can be used as a decimal separator.
-
RNG
The random number generator used to generate the chance. -
ZERO
A constant value for a chance of 0.0.
This is the default chance. -
ONE
A constant value for a chance of 1.0. -
chance
private final double chanceThe chance value of something happens.
-
-
Constructor Details
-
Chance
protected Chance(double chance) Constructs a new chance with the given chance.
The chance will be clamped between 0.0 and 1.0.- Parameters:
chance
- The chance value as a double
-
-
Method Details
-
of
Creates a new chance with the given chance.
If the chance is 0.0 or below,
ZERO
will be returned.
If the chance is 1.0 or above,ONE
will be returned.
- Parameters:
chance
- The chance as a double- Returns:
- The created chance
-
parse
Parses a chance from a string.
The chance can be either 1.0, or 0. followed by at least one digit.
The chance must be followed by a percent sign.
A dot or a comma can be used as a decimal separator.
If the given string to parse is null or empty,
ZERO
will be returned.
If the given string does not match the pattern (CHANCE_PATTERN
),ZERO
will be returned.
- Parameters:
chance
- The chance string to parse- Returns:
- The parsed chance or
ZERO
-
setSeed
public static void setSeed(long seed) Sets the seed of the random number generator.- Parameters:
seed
- The seed
-
isTrue
public boolean isTrue()Checks if the chance is always true.- Returns:
- True if the chance is 1.0 or above, otherwise false
-
isFalse
public boolean isFalse()Checks if the chance is always false.- Returns:
- True if the chance is 0.0 or below, otherwise false
-
result
public boolean result()Gets the result of the chance.
If
isTrue()
this will always return true.
IfisFalse()
this will always return false.
Otherwise, true if the random number generator returns a number below the chance.
- Returns:
- The result of the chance
-
equals
-
hashCode
public int hashCode() -
toString
-