Stop players from adding an item to a gui

Discussion in 'Spigot Plugin Development' started by dentoffe, Jun 16, 2016.

  1. Hi,
    So players can add items to a gui (the empty slots) but I have no idea of how I cancel something like that? It has nothing to do with a click (I know how to cancel that so they can't take them out). But how do I cancel it from them adding an item.
    I tried this but it didn't work.
    Code (Text):
    @EventHandler
         public void onAdditem(InventoryMoveItemEvent e) {
             if (!e.getInitiator().getName().equalsIgnoreCase(inv.getName())) return;
             else {
                 e.setCancelled(true);
             }
             
         }
     
  2. Choco

    Moderator

    InventoryClickEvent instead :p
     
  3. Based on the documentation for ImventoryMoveItemEvent, it doesn't look like this is the event you want to use. I would have a closer look at InventoryClickEvent and InventoryDragEvent. You can have full control over standard (i.e. not hoppers) inventory input/output if you listen to these two events. Neglecting to account for InventoryDragEvent will result in players being able to drag items into places they shouldn't.
     
    • Useful Useful x 1
  4. But what are they clicking? They just do shift + the item and it moves to the inventory ;)
     
  5. That includes clicking on the item kid
     
    • Funny Funny x 2
  6. Yeah but from the players inventory right? So if I cancel that they can't move items in their inventory anymore I want it so they are only not able to add it to their gui not cancel it to move items in their inventory
     
  7. For everyone reading this thread though, you still have to use InventoryDragEvent as well as InventoryClickEvent. The click event will handle 99% of cases where players can put items into the GUI, however, when I have made GUIs in the past, I always find that players are still able to put items in the GUI by dragging them over the GUI. The drag event should fix this issue.
     
    • Agree Agree x 2
  8. It depends if you cancel it instantly or not, with a normal GUI system the InventoryClickEvent is more than enough.
     
    • Agree Agree x 1
  9. Not if they drag items over the GUI though. InventoryClickEvent doesn't get fired in this case.
     
    • Agree Agree x 1
  10. Trust me, if you instantly cancel it, nothing will happen, you can test my API where i make it, it has been tested for thousands of players on my server, nothing happened.
     
  11. What do you mean by cancelling instantly? I've always had issues with this so now I'm curious.
     
  12. Just do the InventoryHolder check and cancel. Not checking any kind of clicks or that kind of stuff. When i started to make my API i had that problem with people draging items, if you instead cancel the event when you check for the InventoryHolder nothing will happen.

    This is my events class.
    Code (Java):

    @EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
    public void onClick(InventoryClickEvent event) {
        if(event.getCurrentItem() == null || event.getCurrentItem().getType() == Material.AIR) {
            return;
        }

        if(event.getInventory().getHolder() instanceof CustomHolder) {
            event.setCancelled(true);

            CustomHolder customHolder = (CustomHolder) event.getInventory().getHolder();
            CustomInventory customInventory = customHolder.getCustomInventory();

            Icon icon = customInventory.getIcon(event.getRawSlot());
            if(icon == null) {
                return;
            }

            Player player = (Player) event.getWhoClicked();

            if(icon.getType() == ClickType.BOTH_CLICKS || (icon.getType() == ClickType.LEFT_CLICK && event.isLeftClick())
                    || (icon.getType() == ClickType.RIGHT_CLICK && event.isRightClick())) {

                if(!icon.havePermission() || icon.getPermission().hasPermission(player)) {
                    for (ClickAction clickAction : icon.getClickActions()) {
                        clickAction.execute(player);
                    }
                } else {
                    player.sendMessage(icon.getNoPermissionMessage());
                }
            }
        }
    }
     
     

  13. Hmmmm, maybe I just need to play around with it some more. No matter what I seemed to do I couldn't get the drag to stop with inventory click. I've never done anything with custom inventory holders and I haven't tried changing the event priority. Maybe one of those is what's making your method work.
     
  14. Yeah, I'm curious about this too. Exact same problem.

    Would a player's mouse type matter at all? Just a thought. Perhaps some mice "click" before they drag (hence the event cancel) whereas others do not? It seems illogical to me that cancelling ClickEvent would also cancel DragEvent, since the latter is not a subclass.
     
  15. Yea, an issue I kept having when testing is, if I click on the edge of an inventory (not on a slot, but not outside the InventoryView) and then drag across, players could still add items.
     
  16. To Drag the item you first have to click over an item, that's why nothing will happen.
     
  17. I mean if they take an item from their inventory and then try to drag it into the GUI inventory. I wouldn't want the player to not have access to their inventory while they are in a GUI.
     
  18. Yeah, my plugin prevents that too, if you want the player to move items you will have to change some stuff. Like only canceling the Click if the Click is on the GUI inventory and cancel the drag stuff too and some more stuff.
     
  19. There's another really pesky one where the player will click on just one slot, but because the player moves their mouse a little, it registers as a single slot drag event and isn't cancelled. Took me a whole morning to find that.