Record Class ItemStack

java.lang.Object
java.lang.Record
fr.fidorial.item.ItemStack
Record Components:
id - the item identifier, e.g. minecraft:diamond_sword
count - how many; 0 or less means empty
components - the patch over the item's defaults
All Implemented Interfaces:
DataComponentHolder, HoverEventSource<HoverEvent.ShowItem>, Translatable

public record ItemStack(Key id, int count, DataComponentMap components) extends Record implements DataComponentHolder, Translatable, HoverEventSource<HoverEvent.ShowItem>
An item identifier, a count, and a patch over that item's default components.
Since:
0.1.0
  • Field Details

    • EMPTY

      public static final ItemStack EMPTY
      The absence of an item. Slots hold this rather than null.
  • Constructor Details

    • ItemStack

      public ItemStack(Key id, int count, DataComponentMap components)
      Creates an instance of a ItemStack record class.
      Parameters:
      id - the value for the id record component
      count - the value for the count record component
      components - the value for the components record component
    • ItemStack

      public ItemStack(Key id, int count)
      Parameters:
      id - the item identifier
      count - how many
  • Method Details

    • of

      public static ItemStack of(Key key, int count)
      Parameters:
      key - the item identifier
      count - how many
      Returns:
      a stack with no components patched
    • of

      public static ItemStack of(Key key)
      Parameters:
      key - the item identifier
      Returns:
      a stack of one, with no components patched
    • builder

      public static ItemStack.Builder builder(Key key)
      Parameters:
      key - the item identifier
      Returns:
      a builder for a stack of one
    • toBuilder

      public ItemStack.Builder toBuilder()
      Returns:
      a builder pre-populated with this stack
    • editor

      public DataComponentEditor editor()
      Hands back an editor over this stack's components. Changing it does not change this stack; feed it back through withComponents(DataComponentEditor) when you are done, or use edit(Consumer) to do both at once.
      Returns:
      a fresh editor
      Since:
      0.1.0
    • withComponents

      public ItemStack withComponents(DataComponentEditor editor)
      Parameters:
      editor - the editor whose components to take
      Returns:
      a new stack carrying those components, or this one when nothing changed
      Since:
      0.1.0
    • edit

      public ItemStack edit(Consumer<DataComponentEditor> editor)
      Reads an editor, hands it to editor, and returns the resulting stack.
      Parameters:
      editor - given an editor over this stack's components
      Returns:
      a new stack, or this one when nothing changed
      Since:
      0.1.0
    • with

      public <T> ItemStack with(DataComponentType<T> type, T value)
      Type Parameters:
      T - the component's value type
      Parameters:
      type - the component to set
      value - the value
      Returns:
      a new stack
    • without

      public ItemStack without(DataComponentType<?> type)
      Removes the item's default for a component. See DataComponentMap.without(DataComponentType) for why this differs from reset(DataComponentType).
      Parameters:
      type - the component to remove
      Returns:
      a new stack
    • reset

      public ItemStack reset(DataComponentType<?> type)
      Parameters:
      type - the component to stop patching
      Returns:
      a new stack
    • isEmpty

      public boolean isEmpty()
      Returns:
      true when this stack holds nothing
    • isSimilar

      public boolean isSimilar(@Nullable ItemStack other)
      Whether two stacks would merge into one slot — same item, same components, count ignored.
      Parameters:
      other - the stack to compare against, may be null
      Returns:
      true when the two are stackable together
    • withCount

      public ItemStack withCount(int newCount)
      Parameters:
      newCount - the new count
      Returns:
      a new stack, or this one when the count is unchanged
    • plus

      public ItemStack plus(int delta)
      Parameters:
      delta - how much to add to the count; may be negative
      Returns:
      a new stack, clamped at 0
    • asHoverEvent

    • translationKey

      public String translationKey()
      Specified by:
      translationKey in interface Translatable
    • toString

      public String toString()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. Reference components are compared with Objects::equals(Object,Object); primitive components are compared with the compare method from their corresponding wrapper classes.
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • id

      public Key id()
      Returns the value of the id record component.
      Returns:
      the value of the id record component
    • count

      public int count()
      Returns the value of the count record component.
      Returns:
      the value of the count record component
    • components

      public DataComponentMap components()
      Returns the value of the components record component.
      Specified by:
      components in interface DataComponentHolder
      Returns:
      the value of the components record component