Class AbstractMainHand
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionfinal @Nullable ItemStackgetItemInMainHand(@NotNull Player player) Retrieves the item in the player's main hand.abstract @Nullable ItemStackgetItemInMainHand(@NotNull PlayerInventory inventory) Retrieves the item in the main hand from the specified inventory.final @Nullable ItemStackgetItemInOffHand(@NotNull Player player) Retrieves the item in the player's off-hand.abstract @Nullable ItemStackgetItemInOffHand(@NotNull PlayerInventory inventory) Retrieves the item in the off-hand from the specified inventory.abstract booleanisOffHand(@NotNull PlayerInteractEntityEvent event) Determines if a player interaction event was performed with the off-hand.abstract voidsetItemInMainHand(@NotNull Player player, @Nullable ItemStack item) Sets the item in the player's main hand.abstract voidsetItemInOffHand(@NotNull Player player, @Nullable ItemStack item) Sets the item in the player's off-hand.
-
Constructor Details
-
AbstractMainHand
public AbstractMainHand()
-
-
Method Details
-
getItemInMainHand
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.
-
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- thePlayerInventoryto get the main hand item from- Returns:
- the
ItemStackin the main hand, ornullif empty
-
getItemInOffHand
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
nullas off-hand functionality was not available. -
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
nullas dual-wielding was not supported.- Parameters:
inventory- thePlayerInventoryto get the off-hand item from- Returns:
- the
ItemStackin the off-hand, ornullif empty or not supported
-
isOffHand
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
falseas only main hand interactions were possible.- Parameters:
event- thePlayerInteractEntityEventto check- Returns:
trueif the interaction was performed with the off-hand,falseif 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.
-
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
UnsupportedOperationExceptionas dual-wielding was not available.- Parameters:
player- thePlayerto modifyitem- theItemStackto place in the off-hand, ornullto clear the slot- Throws:
UnsupportedOperationException- if the server version does not support off-hand functionality
-