Solved Cancel Drop Event

Discussion in 'Plugin Development' started by nivek1212, Dec 21, 2014.

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

    nivek1212

    Hey guys,

    I tried to make a Plugin, that makes Items undroppable, but it doesnt work properly. When I press Q, the item goes from slot 9 to slot 1, but it should stay on slot 9. Or when i try to remove the item and add it to slot 9 I have two items, because the first one doesn't get removed.

    Here's my code:
    Code:
    @EventHandler (priority=EventPriority.HIGHEST)
        public void onDrop(PlayerDropItemEvent e) {
            ItemStack GreenDye = new ItemStack(Material.INK_SACK, 1, (short) 10);
            ItemMeta GreenMeta = GreenDye.getItemMeta();
            GreenMeta.setDisplayName(ChatColor.BOLD + "Hide Players");
            GreenDye.setItemMeta(GreenMeta);
           
            ItemStack GrayDye = new ItemStack(Material.INK_SACK, 1, (short) 8);
            ItemMeta GrayMeta = GrayDye.getItemMeta();
            GrayMeta.setDisplayName(ChatColor.BOLD + "Show Players");
            GrayDye.setItemMeta(GrayMeta);
           
            if(e.getItemDrop().getItemStack().getItemMeta().equals(GreenMeta)) {
                e.setCancelled(true);
                e.getPlayer().getInventory().remove(GreenDye);
                e.getPlayer().getInventory().setItem(8, GreenDye);
            } else if(e.getItemDrop().getItemStack().getItemMeta().equals(GrayDye)) {
                e.setCancelled(true);
                e.getPlayer().getInventory().remove(GrayDye);
                e.getPlayer().getInventory().setItem(8, GrayDye);
            }
           
        }
     
  2. Offline

    567legodude

    According to what you posted. This should be what you are looking for. (I haven't tested it though)
    Code:
    @EventHandler
    public void onDrop(PlayerDropItemEvent event) {
        ItemStack is = event.getItemDrop().getItemStack();
        ItemMeta im = is.getItemMeta();
                  
        if (is.equals(new ItemStack(Material.INK_SACK, 1, (short) 10)) || is.equals(new ItemStack(Material.INK_SACK, 1, (short) 8))) {
            if (im.hasDisplayName()) {
                if (ChatColor.stripColor(im.getDisplayName()).equals("Hide Players") || ChatColor.stripColor(im.getDisplayName()).equals("Show Players")) {
                    event.setCancelled(true);
                }
            }
        }
    }
     
    mrgreen33gamer and Europia79 like this.
  3. Offline

    nivek1212

    Now it's droppable again. It wasn't droppable before, but it went from slot 9 to slot 1, but I want it to stay at slot 9
     
  4. Offline

    AdamQpzm

    @nivek1212 Try calling Player#updateInventory() after updating it.
     
  5. Offline

    nivek1212

    Now the Item stacks up to 2 when I relogg and press Q and Stacks infinitly +1 when I press CTRL+Q, but it stays in slot 9.

    Code:
        @SuppressWarnings("deprecation")
        @EventHandler (priority=EventPriority.HIGHEST)
        public void onDrop(PlayerDropItemEvent e) {
            Player p = e.getPlayer();
            ItemStack GreenDye = new ItemStack(Material.INK_SACK, 1, (short) 10);
            ItemMeta GreenMeta = GreenDye.getItemMeta();
            GreenMeta.setDisplayName(ChatColor.BOLD + "Hide Players");
            GreenDye.setItemMeta(GreenMeta);
           
            ItemStack GrayDye = new ItemStack(Material.INK_SACK, 1, (short) 8);
            ItemMeta GrayMeta = GrayDye.getItemMeta();
            GrayMeta.setDisplayName(ChatColor.BOLD + "Show Players");
            GrayDye.setItemMeta(GrayMeta);
           
            if(e.getItemDrop().getItemStack().getItemMeta().equals(GreenMeta)) {
                e.setCancelled(true);
                p.updateInventory();
                p.getInventory().removeItem(GreenDye);
                p.updateInventory();
                p.getInventory().setItem(8, GreenDye);
                p.updateInventory();
                return;
            } else if(e.getItemDrop().getItemStack().getItemMeta().equals(GrayDye)) {
                e.setCancelled(true);
                p.updateInventory();
                p.getInventory().removeItem(GreenDye);
                p.updateInventory();
                p.getInventory().setItem(8, GreenDye);
                p.updateInventory();
                return;
            }
           
        }
     
  6. Offline

    AdamQpzm

    @nivek1212 Are you sure it's actually removing the item? Rather than removing, for example, GreenDye, try removing the ItemStack returned by getItemDrop().getItemStack()
     
  7. Offline

    nivek1212

    Yeah, I'm removing the item.
    1.But when I reload/rejoin and press Q, I get 2 Lime Green Dyes on Slot 9.
    2.When I press Q again, I end up with 1 Lime Green Dye on Slot9.
    3.When I press CTRL+Q, I get 3 Lime Green Dyes and +1 everytime I press CTRL+Q. When I press Q 1. happens, and when I press Q again 2. happens
     
  8. Offline

    AdamQpzm

    Did you try this?
     
  9. Offline

    nivek1212

    Yep, but it still doesn't work:

    Code:
    @SuppressWarnings("deprecation")
        @EventHandler (priority=EventPriority.HIGHEST)
        public void onDrop(PlayerDropItemEvent e) {
            Player p = e.getPlayer();
            ItemStack GreenDye = new ItemStack(Material.INK_SACK, 1, (short) 10);
            ItemMeta GreenMeta = GreenDye.getItemMeta();
            GreenMeta.setDisplayName(ChatColor.BOLD + "Hide Players");
            GreenDye.setItemMeta(GreenMeta);
            ItemStack is = new ItemStack(e.getItemDrop().getItemStack());
          
            ItemStack GrayDye = new ItemStack(Material.INK_SACK, 1, (short) 8);
            ItemMeta GrayMeta = GrayDye.getItemMeta();
            GrayMeta.setDisplayName(ChatColor.BOLD + "Show Players");
            GrayDye.setItemMeta(GrayMeta);
          
            if(e.getItemDrop().getItemStack().getItemMeta().equals(GreenMeta)) {
                e.setCancelled(true);
                p.updateInventory();
                p.getInventory().removeItem(is);
                p.updateInventory();
                p.getInventory().setItem(8, GreenDye);
                p.updateInventory();
                if(GreenDye.getMaxStackSize() == 2) {
                    p.getInventory().setItem(8, GreenDye);
                }
                return;
            } else if(e.getItemDrop().getItemStack().getItemMeta().equals(GrayDye)) {
                e.setCancelled(true);
                p.updateInventory();
                p.getInventory().removeItem(is);
                p.updateInventory();
                p.getInventory().setItem(8, GreenDye);
                p.updateInventory();
                return;
            }
          
        }
    *bump*

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 28, 2016
  10. Offline

    Jaaakee224

    @nivek1212
    Code:
    @SuppressWarnings("deprecation")
        @EventHandler
        public void onInventoryDropEvent(PlayerDropItemEvent e) {
            Player p = (Player)e.getPlayer();
                ItemStack item = e.getItemDrop().getItemStack().clone();
                item.setAmount(p.getInventory().getItemInHand().getAmount() - 1);
                e.getItemDrop().remove();
                p.getInventory().setItem(p.getInventory().getHeldItemSlot(), item);
                p.updateInventory();
            }
     
    nivek1212 likes this.
  11. Offline

    nivek1212

    It worked thanks :)
     
  12. Offline

    Jaaakee224

    Glad I could help.
     
Thread Status:
Not open for further replies.

Share This Page