How to prevent drop items inventory GUI?

Discussion in 'Spigot Plugin Development' started by CrystalDev, May 12, 2015.

  1. I have a plugin which show items in an inventory but people can drop.

    Recommendations?


    Help :confused:
     
  2. just cancel the inventoryclickevent, still allows your plugin to detect clicking on items.
     
  3. Work with: ?
    Code (Text):
     
               if(event.isRightClick() || event.isLeftClick()){
                    event.setCancelled(true);
       }
     
  4. Code (Text):

    public void onClick(InventoryClickEvent e) {
       if (e.getInventory().getTitle().equalsIgnoreCase("YourInventory")) {
          e.setCancelled(true);
       }
    }
     
     
  5. Code (Text):

      public void onOnline(InventoryClickEvent event)
      {
        Player[] players = Bukkit.getServer().getOnlinePlayers();
        Player player = (Player)event.getWhoClicked();
        if ((event.getCurrentItem() != null) && (event.getCurrentItem().getType() != Material.AIR) &&
          (event.getInventory().getName().equals("§bCurrent Players §r[" + "§a" + players.length + "§r]")))
        {
          event.setCancelled(true);
          player.updateInventory();
        }
      }
    }
    Not work for me :/
     
  6. Depends what you mean by dropping.
    manually dragging and dropping would be InventoryClickEvent
    or pressing Q would be PlayerDropItemEvent

    Also, with the 'PlayerDropItemEvent' there is a bug where setCancelled(true) dosnt work..
    so here is the code to prevent that,

    Code (Text):
    ItemStack item = e.getItemDrop().getItemStack();
    e.getItemDrop().remove();
    p.getInventory().setItemInHand(item);
    Hope this helped :)
     
  7. When do you create the inventory?
    Use String#contains("§bCurrent Players §r[" + "§a") instead of equals().
     
  8. i wouldn't advise using contains, use this..

    Code (Text):
    if(invVariable != null && event.getInventory().getName().equils(invVariable.getName()))
     
  9. Code (Text):
     
    public void Online(Player player)
      {
        Player[] players = Bukkit.getServer().getOnlinePlayers();
        ItemStack i = new ItemStack(Material.SKULL_ITEM);
        ItemMeta m = i.getItemMeta();
        i.setDurability((short)3);
        Inventory spectate = Bukkit.createInventory(player, 54, "§aPing Online Players §r[" + "§a" + players.length + "§r]");
     
  10. This isnt what he is asking..
    ALL he wants is to stop items from being dropped/ stolen from an inventory,
    I know i shouldn't be feeding him the code, but if i dont he/her is gonna learn the completely wrong thing.

    [EDIT] totally didnt know it was your thread :p may aswell keep this code here for you <3


    Code (Text):
        @EventHandler
        public void onInventoryClick(InventoryClickEvent e) {
            Player p = (Player) e.getWhoClicked();
            ItemStack item = e.getCurrentItem();
         
            if(item == null || item.getType() == null || item.getType() == Material.AIR)
                return;
         
            if(invVariable != null && e.getInventory().getName().equals(invVariable.getName())) {
                e.setCancelled(true);
            }
        }