Class PrefabWeights

java.lang.Object
com.hypixel.hytale.server.core.prefab.PrefabWeights

public class PrefabWeights extends Object
Contains a mapping of prefab names to weights used to determine the probability of a given prefab being selected for placement from a collection of prefabs.

Prefab names are expected to match the filename of the prefab without suffix and file extension. Weight values are represented by positive decimal number values inclusive of 0.0.

Once initialized via get(T[], Function, Random) any further changes to the weight mappings via the set/remove methods will not be reflected in the get functionality.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final char
     
    static final Codec<PrefabWeights>
     
    static final double
     
    static final char
     
    static final Codec<it.unimi.dsi.fastutil.objects.Object2DoubleMap<String>>
     
    static final Supplier<it.unimi.dsi.fastutil.objects.Object2DoubleMap<String>>
     
    static final PrefabWeights
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    Construct a new mutable PrefabWeights instance using a default backing map and an initial total weight of 0.0.
  • Method Summary

    Modifier and Type
    Method
    Description
    Set<it.unimi.dsi.fastutil.objects.Object2DoubleMap.Entry<String>>
     
    <T> T
    get(T[] elements, Function<T,String> nameFunc, double value)
    Get a weighted random value from the elements array.
    <T> T
    get(T[] elements, Function<T,String> nameFunc, Random random)
    Get a weighted random value from the elements array.
    double
    Get the default weight used when a mapping does not exist for a given prefab name.
    Returns a string representation of the prefab name to weight mappings held within this PrefabWeights instance.
    double
    getWeight(String prefab)
    Get the weight value for a prefab.
    parse(String mappingString)
    Parses a comma-separated name-to-weight mapping String into a corresponding PrefabWeights instance.
    void
    Remove the weight value for a specific file.
    void
    setDefaultWeight(double defaultWeight)
    Set the default weight used when a mapping does not exist for a given prefab name.
    void
    setWeight(String prefab, double weight)
    Set the weight value for a specific prefab file.
    int
    Returns the number of prefab name to weight mappings contained in this PrefabWeights.
     

    Methods inherited from class Object

    equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Field Details

  • Constructor Details

    • PrefabWeights

      public PrefabWeights()
      Construct a new mutable PrefabWeights instance using a default backing map and an initial total weight of 0.0.
  • Method Details

    • size

      public int size()
      Returns the number of prefab name to weight mappings contained in this PrefabWeights.
      Returns:
      The number of prefab to weight mappings.
    • get

      @Nullable public <T> T get(@Nonnull T[] elements, @Nonnull Function<T,String> nameFunc, @Nonnull Random random)
      Get a weighted random value from the elements array.
      Type Parameters:
      T - The type of the array element.
      Parameters:
      elements - The array of elements to pick from.
      nameFunc - A function that maps an element to a name string.
      random - A number generator used to produce a random 'value' to select the next entry with.
      Returns:
      The element in the array that corresponds to the generated random 'value' using the weights configured in this instance to alter the probability of any one entry being selected. May return null if the provided value is less that zero or the provided elements array does not match the length of the one originally used to initialize the weights.
    • get

      @Nullable public <T> T get(@Nonnull T[] elements, @Nonnull Function<T,String> nameFunc, double value)
      Get a weighted random value from the elements array.
      Type Parameters:
      T - The type of the array element.
      Parameters:
      elements - The array of elements to pick from.
      nameFunc - A function that maps an element to a name string.
      value - A random value used to pick from the elements array (expected in the range 0.0-1.0)
      Returns:
      The element in the array that corresponds to the provided 'value' using the weights configured in this instance to alter the probability of any one entry being selected. May return null if the provided value is less that zero or the provided elements array does not match the length of the one originally used to initialize the weights.
    • getWeight

      public double getWeight(String prefab)
      Get the weight value for a prefab.
      Parameters:
      prefab - The name of the prefab to look up the weight for.
      Returns:
      The configured weight of the prefab or the default value of 1.0 if the prefab name is not contained in this PrefabWeights instance.
    • setWeight

      public void setWeight(String prefab, double weight)
      Set the weight value for a specific prefab file.
      Parameters:
      prefab - The name of the prefab file (without file extension).
      weight - The weight value for the prefab (expected greater than or equal to zero).
      Throws:
      IllegalArgumentException - If the provided weight value is less than zero.
      UnsupportedOperationException - If called on the NONE instance.
    • removeWeight

      public void removeWeight(String prefab)
      Remove the weight value for a specific file.
      Parameters:
      prefab - The name of the prefab file (without file extension).
      Throws:
      UnsupportedOperationException - If called on the NONE instance.
    • getDefaultWeight

      public double getDefaultWeight()
      Get the default weight used when a mapping does not exist for a given prefab name.
      Returns:
      The default weight.
    • setDefaultWeight

      public void setDefaultWeight(double defaultWeight)
      Set the default weight used when a mapping does not exist for a given prefab name.
      Parameters:
      defaultWeight - The default weight. Must be greater than or equal to 0.0
    • getMappingString

      @Nonnull public String getMappingString()
      Returns a string representation of the prefab name to weight mappings held within this PrefabWeights instance.
      Returns:
      The prefab weight mappings String.
    • toString

      @Nonnull public String toString()
      Overrides:
      toString in class Object
    • parse

      @Nonnull public static PrefabWeights parse(@Nonnull String mappingString)
      Parses a comma-separated name-to-weight mapping String into a corresponding PrefabWeights instance.

      A single mapping consists of a prefab file name (without extension) and double number value separated by an equals character. For example: "Prefab_001=1.0". Multiple mappings can be defined by separating each mapping with a comma character. For example: "Prefab_001=1.0, Prefab_002=2.0". Note: Whitespace around the prefab name or weight value is trimmed and ignored.

      Parameters:
      mappingString - The prefab mappings String.
      Returns:
      A PrefabWeights instance containing the prefab name to weight mapping represented by the input mapping String.
      Throws:
      NumberFormatException - If a weight value is encountered that cannot be parsed as a double.
    • entrySet

      public Set<it.unimi.dsi.fastutil.objects.Object2DoubleMap.Entry<String>> entrySet()