Class BattleRandomSource
- All Implemented Interfaces:
net.minecraft.util.RandomSource
RandomSource
for use by Pixelmon battles.
Constructed via a selection of factory methods, this implementation is a wrapper around another implementation of
RandomSource, which is what is actually used to provide randomness. Both provided and created seeds used to seed the
random implementation are uniquifed and shuffled using the same techniques used by Random
.
The purpose of this class is to allow battle recreation, as the seed used by the randomizer is extractable back out of this class. Starting a battle with the same seed and same battle state will result in the exact same battle occurring, which is useful for both testing and plugin developers. Use cases for developers could be, for example, verification of battle results, or recreation of battles after an error or server fault.
-
Field Summary
Fields inherited from interface net.minecraft.util.RandomSource
GAUSSIAN_SPREAD_FACTOR
-
Method Summary
Modifier and TypeMethodDescriptionfork()
Forks this BattleRandomSource into two, returning a new BattleRandomSource with the same implementation ofRandomSource
with a new seed created by this original BattleRandomSource.net.minecraft.world.level.levelgen.PositionalRandomFactory
int[]
getRandomDistinctNumbersBetween
(int min, int max, int numElements) Gets an array of random distinct integers between min and max inclusive.final <T> T
getRandomElementExcluding
(T[] elements, T... excluded) Gets a random element from the given array, excluding given specific elements, using the internalRandomSource
.final <T> T
getRandomElementFromArray
(T... array) Gets a random element from a given array.<T> T
getRandomElementFromCollection
(Collection<T> collection) Gets a random element from a given collection.<T> T
getRandomElementFromList
(List<T> list) Gets a random element from a given list.int
getRandomIndexFromWeights
(List<Integer> weights) Gets a random index of an element of the given list of integers, where the integer values are the weights of returning that element's index.long
getSeed()
Gets the initial seed used by this BattleRandomSource.static BattleRandomSource
make
(LongFunction<net.minecraft.util.RandomSource> random) Creates a new BattleRandomSource instance using a supplied implementation ofRandomSource
, with a new unique initial seed.static long
Creates a new unique seed based on howRandom
performs this step.static BattleRandomSource
makeWithSeed
(long seed, LongFunction<net.minecraft.util.RandomSource> random) Creates a new BattleRandomSource instance using a supplied implementation ofRandomSource
, with a given initial seed.static BattleRandomSource
Creates a new BattleRandomSource instance using Mojang's implementation of Xoroshiro128++, with a new unique initial seed.static BattleRandomSource
makeXoroshiroWithSeed
(long seed) Creates a new BattleRandomSource instance using Mojang's implementation of Xoroshiro128++, with a given initial seed.boolean
Gets the next boolean value from the internalRandomSource
.double
Gets the next double value from the internalRandomSource
.float
Gets the next float value from the internalRandomSource
.float
nextFloat
(float min, float max) Gets the next float value from the internalRandomSource
which is between min and max.double
Gets the next Gaussian double value from the internalRandomSource
.int
nextInt()
Gets the next integer value from the internalRandomSource
.int
nextInt
(int bound) Gets the next integer value from the internalRandomSource
up to a given bound.int
nextInt
(int min, int max) Gets the next integer value from the internalRandomSource
which is between min and max inclusive.long
nextLong()
Gets the next long value from the internalRandomSource
.boolean
Gets a boolean which is true 1/8 of the time from the internalRandomSource
.boolean
Gets a boolean which is true 1/4 of the time from the internalRandomSource
.boolean
Gets a boolean which is true 1/16 of the time from the internalRandomSource
.boolean
Gets a boolean which is true 1/3 of the time from the internalRandomSource
.boolean
nextPercentageChance
(double chance) Gets a boolean with a chance to be true based on the given percentage, where 0.0 is 0% and 1.0 is 100%.boolean
nextPercentageChance
(float chance) Gets a boolean with a chance to be true based on the given percentage, where 0F is 0% and 1F is 100%.boolean
nextPercentageChance
(int chance) Gets a boolean with a chance to be true based on the given percentage, where 0 is 0% and 100 is 100%.boolean
Gets the next boolean value from the internalRandomSource
.<T> T
removeRandomElementFromCollection
(Collection<T> collection) Gets and removes a random element from a given collection.<T> T
removeRandomElementFromList
(List<T> list) Gets and removes a random element from a given list.void
Resets the internalRandomSource
state by constructing a new instance of the RandomSource, and setting its seed to the same initial seed.void
setRandom
(net.minecraft.util.RandomSource random) Directly sets the internalRandomSource
to a given instance.void
setSeed
(long seed) Sets the seed of this BattleRandomSource.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface net.minecraft.util.RandomSource
consumeCount, nextIntBetweenInclusive, triangle
-
Method Details
-
makeXoroshiro
Creates a new BattleRandomSource instance using Mojang's implementation of Xoroshiro128++, with a new unique initial seed.- Returns:
- The new BattleRandomSource instance.
-
makeXoroshiroWithSeed
Creates a new BattleRandomSource instance using Mojang's implementation of Xoroshiro128++, with a given initial seed.- Parameters:
seed
- The seed to use.- Returns:
- The new BattleRandomSource instance.
-
make
Creates a new BattleRandomSource instance using a supplied implementation ofRandomSource
, with a new unique initial seed.- Parameters:
random
- Function of seed toRandomSource
to be called to use.- Returns:
- The new BattleRandomSource instance.
-
makeWithSeed
public static BattleRandomSource makeWithSeed(long seed, LongFunction<net.minecraft.util.RandomSource> random) Creates a new BattleRandomSource instance using a supplied implementation ofRandomSource
, with a given initial seed.- Parameters:
seed
- The seed to use.random
- Function of seed toRandomSource
to be called to use.- Returns:
- The new BattleRandomSource instance.
-
makeUniqueSeed
public static long makeUniqueSeed()Creates a new unique seed based on howRandom
performs this step.- Returns:
- A new unique seed.
-
fork
Forks this BattleRandomSource into two, returning a new BattleRandomSource with the same implementation ofRandomSource
with a new seed created by this original BattleRandomSource.- Specified by:
fork
in interfacenet.minecraft.util.RandomSource
- Returns:
- The forked BattleRandomSource.
-
forkPositional
public net.minecraft.world.level.levelgen.PositionalRandomFactory forkPositional()- Specified by:
forkPositional
in interfacenet.minecraft.util.RandomSource
- Throws:
UnsupportedOperationException
- Not implemented.
-
setSeed
public void setSeed(long seed) Sets the seed of this BattleRandomSource. This does not modify the internal state of theRandomSource
, unless the given RandomSource's implementation of setSeed does.- Specified by:
setSeed
in interfacenet.minecraft.util.RandomSource
- Parameters:
seed
- The seed to use.
-
nextInt
public int nextInt()Gets the next integer value from the internalRandomSource
.- Specified by:
nextInt
in interfacenet.minecraft.util.RandomSource
- Returns:
- The next integer.
-
nextInt
public int nextInt(int bound) Gets the next integer value from the internalRandomSource
up to a given bound. Whether this value can be negative, or whether this is exclusive/inclusive is dependent on the RandomSource in use.- Specified by:
nextInt
in interfacenet.minecraft.util.RandomSource
- Parameters:
bound
- Upper bound of the returned integer.- Returns:
- The next integer.
-
nextLong
public long nextLong()Gets the next long value from the internalRandomSource
.- Specified by:
nextLong
in interfacenet.minecraft.util.RandomSource
- Returns:
- The next long.
-
nextBoolean
public boolean nextBoolean()Gets the next boolean value from the internalRandomSource
.- Specified by:
nextBoolean
in interfacenet.minecraft.util.RandomSource
- Returns:
- The next boolean.
-
nextOneThirdChance
public boolean nextOneThirdChance()Gets a boolean which is true 1/3 of the time from the internalRandomSource
.- Returns:
- The next 1/3 chance boolean.
-
nextOneQuarterChance
public boolean nextOneQuarterChance()Gets a boolean which is true 1/4 of the time from the internalRandomSource
.- Returns:
- The next 1/4 chance boolean.
-
nextOneEighthChance
public boolean nextOneEighthChance()Gets a boolean which is true 1/8 of the time from the internalRandomSource
.- Returns:
- The next 1/8 chance boolean.
-
nextOneSixteenthChance
public boolean nextOneSixteenthChance()Gets a boolean which is true 1/16 of the time from the internalRandomSource
.- Returns:
- The next 1/16 chance boolean.
-
nextFloat
public float nextFloat()Gets the next float value from the internalRandomSource
.- Specified by:
nextFloat
in interfacenet.minecraft.util.RandomSource
- Returns:
- The next float.
-
nextDouble
public double nextDouble()Gets the next double value from the internalRandomSource
.- Specified by:
nextDouble
in interfacenet.minecraft.util.RandomSource
- Returns:
- The next double.
-
nextGaussian
public double nextGaussian()Gets the next Gaussian double value from the internalRandomSource
.- Specified by:
nextGaussian
in interfacenet.minecraft.util.RandomSource
- Returns:
- The next Gaussian double.
-
getRandomElementExcluding
Gets a random element from the given array, excluding given specific elements, using the internalRandomSource
.- Type Parameters:
T
- The type of the elements of the array.- Parameters:
elements
- The array to select from.excluded
- The elements to exclude from being chosen.- Returns:
- The chosen element, or null if no such element is possible.
-
nextInt
public int nextInt(int min, int max) Gets the next integer value from the internalRandomSource
which is between min and max inclusive.- Specified by:
nextInt
in interfacenet.minecraft.util.RandomSource
- Parameters:
min
- The lower bound of the integer value.max
- The upper bound of the integer value.- Returns:
- The next bounded int.
-
nextFloat
public float nextFloat(float min, float max) Gets the next float value from the internalRandomSource
which is between min and max.- Parameters:
min
- The lower bound of the float value.max
- The upper bound of the float value.- Returns:
- The next bounded float.
-
getRandomDistinctNumbersBetween
public int[] getRandomDistinctNumbersBetween(int min, int max, int numElements) Gets an array of random distinct integers between min and max inclusive.- Parameters:
min
- The lower bound of the integer values.max
- The upper bound of the integer values.numElements
- How many integers to return.- Returns:
- The array of distinct bounded integers.
-
getRandomElementFromList
Gets a random element from a given list.- Type Parameters:
T
- The type of the elements in the list.- Parameters:
list
- The list to select from.- Returns:
- A random element from the given list, or null if the given list is empty.
-
getRandomElementFromArray
Gets a random element from a given array.- Type Parameters:
T
- The type of the elements in the array.- Parameters:
array
- The array to select from.- Returns:
- A random element from the given array, or null if the given array is empty or null.
-
removeRandomElementFromList
Gets and removes a random element from a given list.- Type Parameters:
T
- The type of the elements in the list.- Parameters:
list
- The list to remove from.- Returns:
- A random element from the given list, which is now no longer in the list, or null if the given list is empty.
-
getRandomElementFromCollection
Gets a random element from a given collection.- Type Parameters:
T
- The type of the elements in the collection.- Parameters:
collection
- The collection to select from.- Returns:
- A random element from the given collection, or null if the given collection is empty.
-
removeRandomElementFromCollection
Gets and removes a random element from a given collection.- Type Parameters:
T
- The type of the elements in the collection.- Parameters:
collection
- The collection to remove from.- Returns:
- A random element from the given collection, which is now no longer in the collection, or null if the given collection is empty.
-
nextPercentageChance
public boolean nextPercentageChance(double chance) Gets a boolean with a chance to be true based on the given percentage, where 0.0 is 0% and 1.0 is 100%.- Parameters:
chance
- The percentage chance of returning true as a double.- Returns:
- The boolean result.
-
nextPercentageChance
public boolean nextPercentageChance(float chance) Gets a boolean with a chance to be true based on the given percentage, where 0F is 0% and 1F is 100%.- Parameters:
chance
- The percentage chance of returning true as a float.- Returns:
- The boolean result.
-
nextPercentageChance
public boolean nextPercentageChance(int chance) Gets a boolean with a chance to be true based on the given percentage, where 0 is 0% and 100 is 100%.- Parameters:
chance
- The percentage chance of returning true as an integer.- Returns:
- The boolean result.
-
nextRandomChance
public boolean nextRandomChance()Gets the next boolean value from the internalRandomSource
.- Returns:
- The next boolean.
-
getRandomIndexFromWeights
Gets a random index of an element of the given list of integers, where the integer values are the weights of returning that element's index.- Parameters:
weights
- The list of weights to select from.- Returns:
- The index of the selected weight, or -1 if no selection is possible for the given list.
-
resetState
public void resetState()Resets the internalRandomSource
state by constructing a new instance of the RandomSource, and setting its seed to the same initial seed. -
setRandom
public void setRandom(net.minecraft.util.RandomSource random) Directly sets the internalRandomSource
to a given instance.- Parameters:
random
- The new RandomSource to use.
-
getSeed
public long getSeed()Gets the initial seed used by this BattleRandomSource.- Returns:
- The initial seed.
-