Cancelling PlayerPickUpItemEvent

Discussion in 'Plugin Development' started by MrSpyMan, Jan 1, 2015.

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

    MrSpyMan

    I am trying to make it so when you try to pickup Purple Dye it will be cancelled. I don't know why this won't work because I am comparing the Item from the event to an itemstack and they are both item stacks.

    Code:
    @EventHandler
        public void onPickUpItem(PlayerPickupItemEvent event) {
            if(event.getItem().equals(new ItemStack(Material.INK_SACK, (short) 5))) {
                event.setCancelled(true);
            }
        }
     
  2. @MrSpyMan Are you registering the event in your onEnable?
     
  3. Offline

    teej107

    Rather than creating a new ItemStack, just compare the event ItemStack's material and durability.
     
    moo3oo3oo3 likes this.
  4. Offline

    MrSpyMan

    @CodePlaysMinecraft It is registered onEnable.


    @teej107 Do you mean this?

    Code:
    if(event.getItem().equals(Material.INK_SACK, (short) 5)) {
     
  5. Offline

    teej107

    @MrSpyMan No, I do not, but try it anyway.
     
  6. Offline

    MrSpyMan

    @teej107 Can't do that, saids

    "The method equals(Object) in the type Object is not applicable for the arguments (Material, short)"
     
  7. Offline

    teej107

    @MrSpyMan Well do what is says then. Btw it's better to compare primitives and enums with ==.
     
  8. @MrSpyMan
    Use integer instead of short
    and u`re only cancellating the event when there`re 5 droped ink sacks
     
  9. Offline

    moo3oo3oo3

    Code:
    @EventHandler
        public void onPickUpItem(PlayerPickupItemEvent event) {
            ItemStack item = event.getItem().getItemStack();
            if(item.getType() == Material.INK_SACK) {
                if (item.getDurability() == 5) {
                    event.setCancelled(true);
                }
            }
        }
     
  10. @moo3oo3oo3 You should really use ".equals()" instead of "==" because it's less buggier. Other than that your coding looks fine to me.
     
  11. Offline

    teej107

    It's not buggy. You need to know how and when to use it.
     
Thread Status:
Not open for further replies.

Share This Page