Contents
Class PrefabWeights
java.lang.Object
com.hypixel.hytale.server.core.prefab.PrefabWeights
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
FieldsModifier and TypeFieldDescriptionstatic final charstatic final Codec<PrefabWeights> static final doublestatic final charstatic final PrefabWeights -
Constructor Summary
ConstructorsConstructorDescriptionConstruct a new mutable PrefabWeights instance using a default backing map and an initial total weight of 0.0. -
Method Summary
Modifier and TypeMethodDescriptionentrySet()<T> TGet a weighted random value from the elements array.<T> TGet a weighted random value from the elements array.doubleGet 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.doubleGet the weight value for a prefab.static PrefabWeightsParses a comma-separated name-to-weight mapping String into a corresponding PrefabWeights instance.voidremoveWeight(String prefab) Remove the weight value for a specific file.voidsetDefaultWeight(double defaultWeight) Set the default weight used when a mapping does not exist for a given prefab name.voidSet the weight value for a specific prefab file.intsize()Returns the number of prefab name to weight mappings contained in this PrefabWeights.toString()
-
Field Details
-
MAP_SUPPLIER
-
MAP_CODEC
-
CODEC
-
NONE
-
DEFAULT_WEIGHT
public static final double DEFAULT_WEIGHT- See Also:
-
DELIMITER_CHAR
public static final char DELIMITER_CHAR- See Also:
-
ASSIGNMENT_CHAR
public static final char ASSIGNMENT_CHAR- See Also:
-
-
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
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
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 theNONEinstance.
-
removeWeight
Remove the weight value for a specific file.- Parameters:
prefab- The name of the prefab file (without file extension).- Throws:
UnsupportedOperationException- If called on theNONEinstance.
-
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
Returns a string representation of the prefab name to weight mappings held within this PrefabWeights instance.- Returns:
- The prefab weight mappings String.
-
toString
-
parse
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
-