Class BattleRandomSource

java.lang.Object
com.pixelmonmod.pixelmon.battles.controller.BattleRandomSource
All Implemented Interfaces:
net.minecraft.util.RandomSource

public class BattleRandomSource extends Object implements net.minecraft.util.RandomSource
An implementation of 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 Type
    Method
    Description
    Forks this BattleRandomSource into two, returning a new BattleRandomSource with the same implementation of RandomSource 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 internal RandomSource.
    final <T> T
    Gets a random element from a given array.
    <T> T
    Gets a random element from a given collection.
    <T> T
    Gets a random element from a given list.
    int
    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
    Gets the initial seed used by this BattleRandomSource.
    make(LongFunction<net.minecraft.util.RandomSource> random)
    Creates a new BattleRandomSource instance using a supplied implementation of RandomSource, with a new unique initial seed.
    static long
    Creates a new unique seed based on how Random performs this step.
    makeWithSeed(long seed, LongFunction<net.minecraft.util.RandomSource> random)
    Creates a new BattleRandomSource instance using a supplied implementation of RandomSource, with a given initial seed.
    Creates a new BattleRandomSource instance using Mojang's implementation of Xoroshiro128++, with a new unique initial 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 internal RandomSource.
    double
    Gets the next double value from the internal RandomSource.
    float
    Gets the next float value from the internal RandomSource.
    float
    nextFloat(float min, float max)
    Gets the next float value from the internal RandomSource which is between min and max.
    double
    Gets the next Gaussian double value from the internal RandomSource.
    int
    Gets the next integer value from the internal RandomSource.
    int
    nextInt(int bound)
    Gets the next integer value from the internal RandomSource up to a given bound.
    int
    nextInt(int min, int max)
    Gets the next integer value from the internal RandomSource which is between min and max inclusive.
    long
    Gets the next long value from the internal RandomSource.
    boolean
    Gets a boolean which is true 1/8 of the time from the internal RandomSource.
    boolean
    Gets a boolean which is true 1/4 of the time from the internal RandomSource.
    boolean
    Gets a boolean which is true 1/16 of the time from the internal RandomSource.
    boolean
    Gets a boolean which is true 1/3 of the time from the internal RandomSource.
    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
    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 internal RandomSource.
    <T> T
    Gets and removes a random element from a given collection.
    <T> T
    Gets and removes a random element from a given list.
    void
    Resets the internal RandomSource 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 internal RandomSource 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

      public static BattleRandomSource makeXoroshiro()
      Creates a new BattleRandomSource instance using Mojang's implementation of Xoroshiro128++, with a new unique initial seed.
      Returns:
      The new BattleRandomSource instance.
    • makeXoroshiroWithSeed

      public static BattleRandomSource makeXoroshiroWithSeed(long seed)
      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

      public static BattleRandomSource make(LongFunction<net.minecraft.util.RandomSource> random)
      Creates a new BattleRandomSource instance using a supplied implementation of RandomSource, with a new unique initial seed.
      Parameters:
      random - Function of seed to RandomSource 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 of RandomSource, with a given initial seed.
      Parameters:
      seed - The seed to use.
      random - Function of seed to RandomSource to be called to use.
      Returns:
      The new BattleRandomSource instance.
    • makeUniqueSeed

      public static long makeUniqueSeed()
      Creates a new unique seed based on how Random performs this step.
      Returns:
      A new unique seed.
    • fork

      public BattleRandomSource fork()
      Forks this BattleRandomSource into two, returning a new BattleRandomSource with the same implementation of RandomSource with a new seed created by this original BattleRandomSource.
      Specified by:
      fork in interface net.minecraft.util.RandomSource
      Returns:
      The forked BattleRandomSource.
    • forkPositional

      public net.minecraft.world.level.levelgen.PositionalRandomFactory forkPositional()
      Specified by:
      forkPositional in interface net.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 the RandomSource, unless the given RandomSource's implementation of setSeed does.
      Specified by:
      setSeed in interface net.minecraft.util.RandomSource
      Parameters:
      seed - The seed to use.
    • nextInt

      public int nextInt()
      Gets the next integer value from the internal RandomSource.
      Specified by:
      nextInt in interface net.minecraft.util.RandomSource
      Returns:
      The next integer.
    • nextInt

      public int nextInt(int bound)
      Gets the next integer value from the internal RandomSource 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 interface net.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 internal RandomSource.
      Specified by:
      nextLong in interface net.minecraft.util.RandomSource
      Returns:
      The next long.
    • nextBoolean

      public boolean nextBoolean()
      Gets the next boolean value from the internal RandomSource.
      Specified by:
      nextBoolean in interface net.minecraft.util.RandomSource
      Returns:
      The next boolean.
    • nextOneThirdChance

      public boolean nextOneThirdChance()
      Gets a boolean which is true 1/3 of the time from the internal RandomSource.
      Returns:
      The next 1/3 chance boolean.
    • nextOneQuarterChance

      public boolean nextOneQuarterChance()
      Gets a boolean which is true 1/4 of the time from the internal RandomSource.
      Returns:
      The next 1/4 chance boolean.
    • nextOneEighthChance

      public boolean nextOneEighthChance()
      Gets a boolean which is true 1/8 of the time from the internal RandomSource.
      Returns:
      The next 1/8 chance boolean.
    • nextOneSixteenthChance

      public boolean nextOneSixteenthChance()
      Gets a boolean which is true 1/16 of the time from the internal RandomSource.
      Returns:
      The next 1/16 chance boolean.
    • nextFloat

      public float nextFloat()
      Gets the next float value from the internal RandomSource.
      Specified by:
      nextFloat in interface net.minecraft.util.RandomSource
      Returns:
      The next float.
    • nextDouble

      public double nextDouble()
      Gets the next double value from the internal RandomSource.
      Specified by:
      nextDouble in interface net.minecraft.util.RandomSource
      Returns:
      The next double.
    • nextGaussian

      public double nextGaussian()
      Gets the next Gaussian double value from the internal RandomSource.
      Specified by:
      nextGaussian in interface net.minecraft.util.RandomSource
      Returns:
      The next Gaussian double.
    • getRandomElementExcluding

      @SafeVarargs public final <T> T getRandomElementExcluding(T[] elements, T... excluded)
      Gets a random element from the given array, excluding given specific elements, using the internal RandomSource.
      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 internal RandomSource which is between min and max inclusive.
      Specified by:
      nextInt in interface net.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 internal RandomSource 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

      public <T> T getRandomElementFromList(List<T> list)
      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

      @SafeVarargs public final <T> T getRandomElementFromArray(T... array)
      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

      public <T> T removeRandomElementFromList(List<T> list)
      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

      public <T> T getRandomElementFromCollection(Collection<T> collection)
      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

      public <T> T removeRandomElementFromCollection(Collection<T> collection)
      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 internal RandomSource.
      Returns:
      The next boolean.
    • getRandomIndexFromWeights

      public 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.
      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 internal RandomSource 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 internal RandomSource 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.