Class AbstractMainHand

java.lang.Object
com.kamikazejam.kamicommon.nms.abstraction.mainhand.AbstractMainHand

public abstract class AbstractMainHand extends Object
Abstract class providing version-specific implementations for main hand and off-hand item management across different Minecraft versions.

This abstraction handles the differences in dual-wielding mechanics that were introduced in Minecraft 1.9. It provides consistent methods for accessing and modifying items in both hands, with proper version compatibility for servers that don't support off-hand functionality.

For versions prior to 1.9, off-hand methods may throw UnsupportedOperationException or return null values as appropriate, since dual-wielding was not available in those versions.

  • Constructor Details

    • AbstractMainHand

      public AbstractMainHand()
  • Method Details

    • getItemInMainHand

      @Nullable public final @Nullable ItemStack getItemInMainHand(@NotNull @NotNull Player player)
      Retrieves the item in the player's main hand.

      This convenience method extracts the item from the player's main hand by accessing their inventory. It provides a direct way to get the main hand item without manually accessing the inventory.

      Parameters:
      player - the Player to get the main hand item from
      Returns:
      the ItemStack in the main hand, or null if empty
    • getItemInMainHand

      @Nullable public abstract @Nullable ItemStack getItemInMainHand(@NotNull @NotNull PlayerInventory inventory)
      Retrieves the item in the main hand from the specified inventory.

      This method provides version-specific access to the main hand slot in the player's inventory. The implementation handles differences in how main hand items are stored across Minecraft versions.

      Parameters:
      inventory - the PlayerInventory to get the main hand item from
      Returns:
      the ItemStack in the main hand, or null if empty
    • getItemInOffHand

      @Nullable public final @Nullable ItemStack getItemInOffHand(@NotNull @NotNull Player player)
      Retrieves the item in the player's off-hand.

      This convenience method extracts the item from the player's off-hand by accessing their inventory. For versions prior to 1.9, this may return null as off-hand functionality was not available.

      Parameters:
      player - the Player to get the off-hand item from
      Returns:
      the ItemStack in the off-hand, or null if empty or not supported
    • getItemInOffHand

      @Nullable public abstract @Nullable ItemStack getItemInOffHand(@NotNull @NotNull PlayerInventory inventory)
      Retrieves the item in the off-hand from the specified inventory.

      This method provides version-specific access to the off-hand slot in the player's inventory. For versions prior to 1.9, this method may return null as dual-wielding was not supported.

      Parameters:
      inventory - the PlayerInventory to get the off-hand item from
      Returns:
      the ItemStack in the off-hand, or null if empty or not supported
    • isOffHand

      public abstract boolean isOffHand(@NotNull @NotNull PlayerInteractEntityEvent event)
      Determines if a player interaction event was performed with the off-hand.

      This method checks whether the specified interaction event was triggered using the player's off-hand rather than their main hand. For versions prior to 1.9, this will typically return false as only main hand interactions were possible.

      Parameters:
      event - the PlayerInteractEntityEvent to check
      Returns:
      true if the interaction was performed with the off-hand, false if it was performed with the main hand or off-hand is not supported
    • setItemInMainHand

      public abstract void setItemInMainHand(@NotNull @NotNull Player player, @Nullable @Nullable ItemStack item)
      Sets the item in the player's main hand.

      This method modifies the item held in the player's main hand slot. The implementation handles version-specific differences in how main hand items are stored and accessed.

      Parameters:
      player - the Player to modify
      item - the ItemStack to place in the main hand, or null to clear the slot
    • setItemInOffHand

      public abstract void setItemInOffHand(@NotNull @NotNull Player player, @Nullable @Nullable ItemStack item) throws UnsupportedOperationException
      Sets the item in the player's off-hand.

      This method modifies the item held in the player's off-hand slot. For versions prior to 1.9, this method will throw an UnsupportedOperationException as dual-wielding was not available.

      Parameters:
      player - the Player to modify
      item - the ItemStack to place in the off-hand, or null to clear the slot
      Throws:
      UnsupportedOperationException - if the server version does not support off-hand functionality