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 ItemStack
getItemInMainHand
(@NotNull Player player) Retrieves the item in the player's main hand.abstract @Nullable ItemStack
getItemInMainHand
(@NotNull PlayerInventory inventory) Retrieves the item in the main hand from the specified inventory.final @Nullable ItemStack
getItemInOffHand
(@NotNull Player player) Retrieves the item in the player's off-hand.abstract @Nullable ItemStack
getItemInOffHand
(@NotNull PlayerInventory inventory) Retrieves the item in the off-hand from the specified inventory.abstract boolean
isOffHand
(@NotNull PlayerInteractEntityEvent event) Determines if a player interaction event was performed with the off-hand.abstract void
setItemInMainHand
(@NotNull Player player, @Nullable ItemStack item) Sets the item in the player's main hand.abstract void
setItemInOffHand
(@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
- thePlayerInventory
to get the main hand item from- Returns:
- the
ItemStack
in the main hand, ornull
if 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
null
as 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
null
as dual-wielding was not supported.- Parameters:
inventory
- thePlayerInventory
to get the off-hand item from- Returns:
- the
ItemStack
in the off-hand, ornull
if 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
false
as only main hand interactions were possible.- Parameters:
event
- thePlayerInteractEntityEvent
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.
-
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
- thePlayer
to modifyitem
- theItemStack
to place in the off-hand, ornull
to clear the slot- Throws:
UnsupportedOperationException
- if the server version does not support off-hand functionality
-