Class InventoryUtils

java.lang.Object
com.hypixel.hytale.server.core.inventory.InventoryUtils

public class InventoryUtils extends Object
Static utility methods for reading from and mutating entity inventory state via the ECS component model.

All methods operate on InventoryComponent subtypes attached to an entity reference and require a ComponentAccessor for component lookup.

See Also:
  • Constructor Details

    • InventoryUtils

      public InventoryUtils()
  • Method Details

    • getContainerForItemPickup

      @Nonnull public static ItemContainer getContainerForItemPickup(@Nonnull Ref<EntityStore> ref, @Nonnull Item item, PlayerSettings playerSettings, @Nonnull ComponentAccessor<EntityStore> componentAccessor)
      Returns the appropriate ItemContainer to place item into when it is picked up, using the player's configured pickup preferences. Delegates to getContainerForItemPickup(Ref, Item, PlayerSettings, PickupLocation, ComponentAccessor) with no override location.
      Parameters:
      ref - The entity reference of the associated entity.
      item - The item being picked up.
      playerSettings - The player's inventory pickup preferences.
      componentAccessor - The ComponentAccessor for component access.
      Returns:
      the container the item should be placed into.
    • getContainerForItemPickup

      @Nonnull public static ItemContainer getContainerForItemPickup(@Nonnull Ref<EntityStore> ref, @Nonnull Item item, PlayerSettings playerSettings, @Nullable PickupLocation overridePickupLocation, @Nonnull ComponentAccessor<EntityStore> componentAccessor)
      Returns the appropriate ItemContainer to place item into when it is picked up. If overridePickupLocation is non-null it is used directly; otherwise the container is chosen based on the item's type and the player's configured pickup preferences.
      Parameters:
      ref - The entity reference of the associated entity.
      item - The item being picked up.
      playerSettings - The player's inventory pickup preferences.
      overridePickupLocation - If non-null, bypasses per-item-type routing and uses this location directly.
      componentAccessor - The ComponentAccessor for component access.
      Returns:
      the container the item should be placed into.
    • smartMoveItem

      public static void smartMoveItem(@Nonnull Ref<EntityStore> ref, int fromSectionId, int fromSlotId, int quantity, @Nonnull SmartMoveType moveType, PlayerSettings settings, @Nonnull ComponentAccessor<EntityStore> accessor)
      Performs a smart move of the item at fromSectionId/fromSlotId according to moveType. The behaviour varies by move type:
      • SmartMoveType.EquipOrMergeStack — tries to equip the item as armor first; if that fails, merges the stack into any open windows and then into the full inventory.
      • SmartMoveType.PutInHotbarOrWindow — moves items from windows to inventory, and from inventory sections to the opposing section (hotbar ↔ storage), trying open windows first for player entities.
      • SmartMoveType.PutInHotbarOrBackpack — routes the item to the backpack when it originates outside the backpack, otherwise uses normal pickup priority.
      Parameters:
      ref - The entity reference of the associated entity.
      fromSectionId - The section ID of the container the item is being moved from.
      fromSlotId - The slot index within that section.
      quantity - The quantity to move.
      moveType - The smart-move strategy to apply.
      settings - The player's inventory settings, used for pickup priority routing.
      accessor - The ComponentAccessor for component access.
    • moveItem

      public static void moveItem(@Nonnull Ref<EntityStore> ref, int fromSectionId, int fromSlotId, int quantity, int toSectionId, int toSlotId, @Nonnull ComponentAccessor<EntityStore> componentAccessor)
      Moves quantity of an item from one inventory section and slot to another. For player entities, if the move involves the currently active hotbar slot, a SmartMoveType.EquipOrMergeStack interaction chain is fired first so that item-swap interactions can run before the move completes. The move is skipped silently if either section is not found on the entity.
      Parameters:
      ref - The entity reference of the associated entity.
      fromSectionId - The section ID to move the item from.
      fromSlotId - The slot index within the source section.
      quantity - The quantity to move.
      toSectionId - The section ID to move the item to.
      toSlotId - The slot index within the destination section.
      componentAccessor - The ComponentAccessor for component access.
    • prepareAbilityCoreRemoval

      public static boolean prepareAbilityCoreRemoval(@Nonnull Ref<EntityStore> ref, @Nonnull ItemContainer fromContainer, int fromSlotId, @Nonnull ComponentAccessor<EntityStore> componentAccessor)
      Enforces ability-line consistency when a line's Core rune leaves the ability slots without a replacement (quick-move, drop). The line's supports are rehomed first (rune bag, then storage, then hotbar). When they cannot all be rehomed the removal is vetoed, and the ability slots plus any window backed by them resync the client's prediction.
      Returns:
      true when the removal may proceed.
    • moveItemFromCheckToInventory

      @Nonnull public static MoveTransaction<ItemStackTransaction> moveItemFromCheckToInventory(@Nonnull Ref<EntityStore> ref, @Nonnull ItemStack itemStack, @Nonnull ItemContainer targetContainer, short fromSlotId, int quantity, PlayerSettings settings, @Nonnull ComponentAccessor<EntityStore> componentAccessor)
      Moves quantity of the item at fromSlotId in targetContainer into the container determined by getContainerForItemPickup(Ref, Item, PlayerSettings, ComponentAccessor) for the item's type.
      Parameters:
      ref - The entity reference of the associated entity.
      itemStack - The item stack being moved, used to determine the destination container.
      targetContainer - The container the item is currently in.
      fromSlotId - The slot index within targetContainer to move from.
      quantity - The quantity to move.
      settings - The player's inventory settings, used for pickup priority routing.
      componentAccessor - The ComponentAccessor for component access.
      Returns:
      the MoveTransaction describing the result of the move.
    • takeAll

      @Nullable public static ListTransaction<MoveTransaction<ItemStackTransaction>> takeAll(@Nonnull Ref<EntityStore> ref, int inventorySectionId, PlayerSettings settings, @Nonnull ComponentAccessor<EntityStore> componentAccessor)
      Moves all items from the container identified by inventorySectionId into the entity's inventory, respecting per-item-type pickup priority via settings. Returns null if the section is not found on the entity.
      Parameters:
      ref - The entity reference of the associated entity.
      inventorySectionId - The section ID of the container to take items from.
      settings - The player's inventory settings, used for pickup priority routing.
      componentAccessor - The ComponentAccessor for component access.
      Returns:
      a transaction listing each individual move, or null if the section was not found.
    • takeAllWithPriority

      @Nonnull public static ListTransaction<MoveTransaction<ItemStackTransaction>> takeAllWithPriority(@Nonnull Ref<EntityStore> ref, ItemContainer fromContainer, PlayerSettings settings, @Nonnull ComponentAccessor<EntityStore> componentAccessor)
      Moves all items from fromContainer into the entity's inventory, respecting per-item-type pickup priority via settings.
      Parameters:
      ref - The entity reference of the associated entity.
      fromContainer - The container whose items should be moved.
      settings - The player's inventory settings, used for pickup priority routing.
      componentAccessor - The ComponentAccessor for component access.
      Returns:
      a transaction listing each individual move.
    • putAll

      @Nullable public static ListTransaction<MoveTransaction<ItemStackTransaction>> putAll(@Nonnull Ref<EntityStore> ref, int inventorySectionId, @Nonnull ComponentAccessor<EntityStore> componentAccessor)
      Moves all items from the entity's storage inventory into the container identified by inventorySectionId. Returns null if the target section or the InventoryComponent.Storage component is not found on the entity.
      Parameters:
      ref - The entity reference of the associated entity.
      inventorySectionId - The section ID of the container to move items into.
      componentAccessor - The ComponentAccessor for component access.
      Returns:
      a transaction listing each individual move, or null if a required component was not found.
    • quickStack

      @Nullable public static ListTransaction<MoveTransaction<ItemStackTransaction>> quickStack(@Nonnull Ref<EntityStore> ref, int inventorySectionId, @Nonnull ComponentAccessor<EntityStore> componentAccessor)
      Moves items from the hotbar (then storage) into the container identified by inventorySectionId, merging stacks where possible. Returns null if the target section is not found on the entity.
      Parameters:
      ref - The entity reference of the associated entity.
      inventorySectionId - The section ID of the container to quick-stack items into.
      componentAccessor - The ComponentAccessor for component access.
      Returns:
      a transaction listing each individual move, or null if the section was not found.
    • getSectionById

      @Nullable public static ItemContainer getSectionById(@Nonnull Ref<EntityStore> ref, int id, @Nonnull ComponentAccessor<EntityStore> componentAccessor)
      Returns the ItemContainer for the inventory section identified by id on the entity. Negative IDs map to built-in inventory sections (see InventoryComponent.HOTBAR_SECTION_ID and related constants); non-negative IDs are treated as open window IDs and are only resolved for player entities.
      Parameters:
      ref - The entity reference of the associated entity.
      id - The section ID to look up.
      componentAccessor - The ComponentAccessor for component access.
      Returns:
      the ItemContainer for the section, or null if not found.
    • containsBrokenItem

      public static boolean containsBrokenItem(@Nonnull Ref<EntityStore> ref, @Nonnull ComponentAccessor<EntityStore> accessor)
      Determines whether the given entity has a broken item in their inventory.
      Parameters:
      ref - The entity reference of the entity to check.
      accessor - The ComponentAccessor for component access.
      Returns:
      true if the entity has a broken item in their inventory, false otherwise.
    • dropAllItemStacks

      @Nonnull public static List<ItemStack> dropAllItemStacks(@Nonnull Ref<EntityStore> ref, @Nonnull ComponentAccessor<EntityStore> accessor)
      Drops all item stacks from every inventory section on the entity (storage, armor, hotbar, utility, and backpack).
      Parameters:
      ref - The entity reference.
      accessor - The ComponentAccessor for component access.
      Returns:
      a list of all dropped ItemStacks.
    • clear

      public static void clear(@Nonnull Ref<EntityStore> ref, @Nonnull ComponentAccessor<EntityStore> accessor)
      Clears all item stacks from every inventory section on the entity (storage, armor, hotbar, utility, and backpack).
      Parameters:
      ref - The entity reference.
      accessor - The ComponentAccessor for component access.
    • sortStorage

      public static void sortStorage(@Nonnull Ref<EntityStore> ref, @Nonnull ComponentAccessor<EntityStore> accessor)
      Sorts the storage inventory by item type.
      Parameters:
      ref - The entity reference.
      accessor - The ComponentAccessor for component access.
    • getActiveSlot

      public static byte getActiveSlot(@Nonnull Ref<EntityStore> ref, int inventorySectionId, @Nonnull ComponentAccessor<EntityStore> componentAccessor)
      Fetches the active slot for the specified inventory section.
      Parameters:
      ref - The entity reference of the associated entity.
      inventorySectionId - The section ID of the inventory (must be InventoryComponent.HOTBAR_SECTION_ID, InventoryComponent.UTILITY_SECTION_ID, or InventoryComponent.TOOLS_SECTION_ID).
      componentAccessor - The ComponentAccessor for component access.
      Returns:
      the active slot index, or InventoryComponent.INACTIVE_SLOT_INDEX if the corresponding component is not present on the entity.
      Throws:
      IllegalArgumentException - if inventorySectionId does not support an active slot.
    • setActiveSlot

      @Deprecated public static void setActiveSlot(@Nonnull Ref<EntityStore> ref, int inventorySectionId, byte slot, @Nonnull ComponentAccessor<EntityStore> componentAccessor)
      Sets the active slot for the specified inventory section and fires a InventorySetActiveSlotEvent so registered systems can react (e.g. clearing tool state, recalculating entity stats).
      Parameters:
      ref - The entity reference of the associated entity.
      inventorySectionId - The ID of the inventory section to set the active slot for (e.g. InventoryComponent.HOTBAR_SECTION_ID).
      slot - The slot index to set as active.
      componentAccessor - The ComponentAccessor for component access.
      Throws:
      IllegalArgumentException - if inventorySectionId does not support an active slot.
    • createEquipmentUpdate

      @Nonnull public static EquipmentUpdate createEquipmentUpdate(@Nonnull Ref<EntityStore> ref, @Nonnull ComponentAccessor<EntityStore> componentAccessor, @Nullable PlayerSettings playerSettings, @Nullable InventoryComponent.Armor armorComponent, @Nullable InventoryComponent.Utility utilityComponent)
      Creates an EquipmentUpdate based on the components from the provided entity.
      Parameters:
      ref - The entity reference of the entity that we are creating the packet for.
      componentAccessor - The ComponentAccessor<EntityStore> for component access.
      playerSettings - The player settings component, or null if the entity doesn't have one.
      armorComponent - The InventoryComponent.Armor component, or null if the entity doesn't have one.
      utilityComponent - The InventoryComponent.Utility component, or null if the entity doesn't have one.
      Returns:
      the constructed EquipmentUpdate packet.