How does placing item in second hand work with BlockPlaceEvent#getItemInHand?

Discussion in 'Plugin Development' started by Tecno_Wizard, Jul 4, 2016.

Thread Status:
Not open for further replies.
  1. Offline

    Tecno_Wizard

    Title says a lot of it.

    I'm looking for a way to get the itemstack of a block that is about to be placed with the BlockPlaceEvent#getItemInHand function. I believe getItemInHand will accomplish this, but I'm not sure how this works with the second hand implemented with 1.10. Does that change to the itemstack in the placing hand, or always the primary?

    I tried it myself but I couldn't really get an answer.

    EDIT: It appears after testing it more I am right, but I still cant find any documentation on it.
     
    Last edited: Jul 4, 2016
  2. Offline

    mc_myster

    @Tecno_Wizard To place a block the player has to have the block in their right hand anyway don't they? i just tried placing a block from my left hand and it didn't work, i had to swap slots which made me place with right hand which is does work as BlockPlaceEvent.
     
  3. Offline

    Lordloss

    Btw for documentation, look into the Inventory class, it now gots methods for getting the MainHand, and getting the OffHand.
     
  4. Offline

    SuperSniper

    getItemInHand is deprecated in builds higher than 1.8, but it always returns an instance of getItemInMainHand(), so you'd need to use getItemInOffHand() now.
     
  5. Offline

    Tecno_Wizard

    Nope. Try it with a torch.

    That's not applicable here. I'm talking about the BlockPlaceEvent#getItemInHand

    Odd. I was using the Bukkit 1.10.2-SNAPSHOT from the spigot maven repo and it is not deprecated there.

    In all, it does appear that it does return the item in the hand that placed, but the lack of documentation of it inside the event is confusing. I might send a request to the spigot team to clarify that.
     
  6. Offline

    Lordloss

    You can listen to the playerInteractEvent at the same time, this gives you the hand which was used.
     
  7. @Tecno_Wizard I've done some testing and it seems getItemInHand will always return the item placed whether it is in the main hand or the off hand that will always return the placed item. You can also check if it is the item in the main hand by doing the boolean check
    Code:
    boolean b = (e.getItemInHand().equals(e.getPlayer().getInventory().getItemInMainHand()));
    I don't think there is any proper documentation explaining this.
     
  8. Offline

    I Al Istannen

    @bwfcwalshy @Tecno_Wizard
    The blockplace method seems to be called based on the Item used to interact. So it should always return the item responsible for the block place.
    At least this is what I get out of this: ItemStack.patch and some things about the ItemStack.a method.
     
  9. Offline

    mc_myster

Thread Status:
Not open for further replies.

Share This Page