Class RecipeKey<T>

java.lang.Object
dev.latvian.mods.kubejs.recipe.RecipeKey<T>
Type Parameters:
T - The type of element held by this key's component

public final class RecipeKey<T> extends Object
Represents a typed data key in a recipe schema. This refers to both the literal "key" used in the recipe JSON and to a "holder" that can be used to lookup and set data in the recipe itself. It is also used by recipe constructors in order to determine the order and types of arguments, as well as whether those arguments are optional or not.

A key is always associated with a RecipeComponent which represents the type of data that it holds.

Each recipe key has set of names that it may be called by in JSON, as well as a functionNames name used in autogenerated methods and a primary name used for serialization.

A key may also be optional, which means that its value may not always be present in JSON and will also not be serialized or written to the value map unless it is declared as alwaysWrite.

By default, each key will have a "builder" method generated for scripts using the first functionNames name. You can disable this by setting noFunctions().

Finally, some types of components such as items or fluids may perform validation to ensure that they aren't holding empty data.

See Also:
  • Field Details

    • component

      public final RecipeComponent<T> component
    • typeInfo

      public final dev.latvian.mods.rhino.type.TypeInfo typeInfo
    • codec

      public final com.mojang.serialization.Codec<T> codec
    • name

      public final String name
    • role

      public final ComponentRole role
    • names

      public final SequencedSet<String> names
    • optional

      public RecipeOptional<T> optional
    • excluded

      public boolean excluded
    • functionNames

      public List<String> functionNames
    • alwaysWrite

      public boolean alwaysWrite
  • Constructor Details

  • Method Details

    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • optional

      public RecipeKey<T> optional(T value)
      Marks this key as optional, meaning that it can be omitted when deserializing recipes from JSON and will not be serialized unless it is explicitly set.
      Parameters:
      value - The default value of this key; note that the instance supplied here will be used directly and across multiple recipes, so make sure to only pass immutable objects!
      See Also:
    • optional

      public RecipeKey<T> optional(RecipeOptional<T> value)
      Marks this key as optional, meaning that it can be omitted when deserializing recipes from JSON and will not be serialized unless it is explicitly set.
      Parameters:
      value - The default value of this key; unlike in optional(Object), the value will be computed at recipe initialization time, which makes it safe to pass mutable objects here.
      See Also:
    • defaultOptional

      public RecipeKey<T> defaultOptional()
      Marks this key as optional, with a default value of null.
      See Also:
    • optional

      public boolean optional()
      Returns true if this key has a defined optional value
      Returns:
      Whether this key is optional
    • alt

      public RecipeKey<T> alt(String name)
      Adds an alternate name for this key in JSON
      Parameters:
      name - Another name this key may be called by
    • alt

      public RecipeKey<T> alt(String... names)
      Adds multiple alternate names for this key in JSON
      Parameters:
      names - A list of names this key may be called by
    • exclude

      public RecipeKey<T> exclude()
      Excludes this key from auto-generated constructors. Requires optional() value to also be set.

      This method does nothing if a custom constructor has been set.

    • noFunctions

      public RecipeKey<T> noFunctions()
      Disables the generation of builder functions for this key.
    • functionNames

      public RecipeKey<T> functionNames(List<String> names)
      Sets a list of names that are used to auto-generate builder functions in JS, e.g. .xp(value). The first one of these names will be the preferred one that documentation should recommend.
    • functionNames

      public RecipeKey<T> functionNames(String... names)
      Sets a list of names that are used to auto-generate builder functions in JS, e.g. .xp(value). The first one of these names will be the preferred one that documentation should recommend.
    • alwaysWrite

      public RecipeKey<T> alwaysWrite()
      Set this in order to always write optional keys, even if their value hasn't changed.

      This can also be used to always populate the value map with a default value for an optional key.

    • getValidFunctionNames

      public List<String> getValidFunctionNames()
    • getPrimaryFunctionName

      public String getPrimaryFunctionName()
    • toJson

      public com.google.gson.JsonObject toJson(RecipeSchemaStorage storage, RecipeSchemaType type, com.mojang.serialization.DynamicOps<com.google.gson.JsonElement> ops)