Solved Prevent players putting items in off hand slot.

Discussion in 'Spigot Plugin Development' started by Borrachondo, May 29, 2017.

  1. Hi,

    I'm trying to deny putting certain items into off hand slot. Already denied PlayerSwapHandItemsEvent. But players still can open inventory and place that items manually in off hand slot.

    Tried to cancel InventoryClickEvent in slot 40, but that only cancels when you try to take something from off hand slot.

    Help please.
     
  2. I mean the InventoryClickEvent should work. Maybe your checks are wrong. Show some relevant code.
     
  3. Code (Text):
    @EventHandler
        public void onInventoryClick(InventoryClickEvent event) {
            if(event.getCurrentItem() != null) {

                if(event.getCurrentItem().getType() != null) {

                    if(event.getCurrentItem().getType() == Material.DIRT) {

                        if(event.getSlot() == 40) {
                            event.setCancelled(true);
                        }

                    }

                }

            }
        }
    Here you are
     
  4. I mean, the current item can be null shouldn't it? As you're not clicking an item, but an empty slot with an item?

    I believe that you would want to check if the getCursor() != null as this is the item being currently moved around. So just replace getCurrentItem (which is the item being clicked) with getCursor (which is the item being moved to the new slot). Also, id look around for another way to check if it's put in the off hand, maybe getSlotType? Idk.

    Also I don't think getType can possibly be null, so I'd just remove that check
     
    • Like Like x 1
  5. Thanks it work now. But we have another problem now. I thought other threads were false but there is really a bug. When you spam click in the off hand slot the item just change to the off hand slot. And it's not a client bug because the item actually works perfectly. What do we do now?
     
  6. Report it as a bug, wait for it to get fixed, update Spigot.
     
    • Like Like x 1
  7. I'm not sure if I understand. Could you provide an example?
     
  8. Yes, sorry for my bad english.

    It doesn't leave me to put items in off hand. But, if I spam click, yes. So I spam click an item until bug and then I can use it without any problem in off hand.
     
  9. I have seen this myself. I'm not sure if it is a bug with Spigot but it has been around for a while. I believe you can still do the same with a fishing rod on an entity and move them even if you cancel the hooked event if you spam the rod enough.

    Why not run a delayed task to detect the slot it has been moved to see if anything did actually get moved to it then remove it?
     
    • Like Like x 1
  10. I hate that type of solutions... but if there is no other I will :/
     
  11. It's not a bug, you're dragging slightly, which is enough to cause the InventoryDragEvent to be fired instead.
     
  12. I don't understand. Sorry, can you please explain better?
     
  13. You know how when you click and hold the click, dragging items across slots, the items spread out? That works on a single slot as well. You need to handle the event for it.
     
    • Like Like x 1
  14. Ok you're right. Thought it was a bug sorry. Now works for me. Thanks!
     
  15. Know thread is solved, but can't they just deal with InventoryInteractEvent instead? As both drag event and click event extends this event. Guess that would solve stuff as you can cancel both at same time.

    EDIT: Nvm, event doesn't actually provide enough information.