This won't work? (Preventing a player from dropping a certain item)

Discussion in 'Plugin Development' started by AnOrangeGamer, Jul 21, 2014.

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

    AnOrangeGamer

    The title says it all, for some reason I can't get this to work.

    code:
    Code:java
    1. @EventHandler
    2. public void onPlayerDrop(PlayerDropItemEvent e) {
    3. ItemStack phone = new ItemStack(Material.FLINT);
    4. ItemMeta phonem = phone.getItemMeta();
    5. phonem.setDisplayName(ChatColor.GREEN + "phone (Drop to open phone menu)");
    6. phone.setItemMeta(phonem);
    7. if(e.getItemDrop() == phone) {
    8. e.setCancelled(true);
    9. Main.getPhoneMenu().show(e.getPlayer());
    10. }
    11. }


    What am I doing wrong here?

    //edit: found a needless return statement, still doesn't work
     
  2. Offline

    Garris0n

    The "==" operator checks if two instances are the same. The .equals() method (assuming the class overrides it properly) checks if two objects are "equal" to each other in the sense you're looking for.
     
  3. Offline

    AnOrangeGamer

    Garris0n

    I did if(e.getItemDrop().equals(phone)) just now, still doesn't work :/
    Any other suggestions?
     
  4. Offline

    LugnutsK

  5. Offline

    Garris0n

  6. Offline

    stormneo7

    Code:java
    1. if(e.getItemDrop().getItemStack() == phone) {
     
  7. Offline

    AnOrangeGamer

    stormneo7 Garris0n

    I tried getting the ItemStack of the drop, and it didn't work as well.
     
  8. Offline

    Garris0n

    Didn't I just go over this?


    Post the code you used.
     
  9. Offline

    AnOrangeGamer

    Garris0n
    Sorry, was only meant to reply to storm. I used storm's way but replaced == with .equals. Works perfectly. Thanks for your help.
     
    Garris0n likes this.
  10. Offline

    stormneo7

    Garris0n
    I'm comparing an ItemStack with an ItemStack. == and .equal will work the same way.
    As you said...
    "The "==" operator checks if two instances are the same."
    "The .equals() method checks if two objects are "equal" to each other"
     
  11. Offline

    Garris0n

    That quote completely contradicts what you just said.
     
  12. Offline

    stormneo7

    Tell me m8, how so.

    We're comparing an ItemStack vs an ItemStack.
    "The "==" operator checks if two instances are the same." - Yes, they're both ItemStacks.
    "The .equals() method checks if two objects are "equal" to each other" - Well they're both two objects... so...

    Your first response to this thread was that you believed that he should've used .equals instead of == when that, clearly, was not the problem. He was comparing an Item to an Itemstack.
     
  13. Offline

    Necrodoom

    stormneo7
    Incorrect.
    == operator checks if both variables point to the same instance. (Ex: if you were to item1.setX, then do item2.getX, you would get the different X)
    .equals checks if the 2 object's variables hold the same values.
     
  14. Offline

    Garris0n

    ItemStack item1 = new ItemStack(Material.DIRT);
    ItemStack item2 = new ItemStack(Material.DIRT);
    ItemStack item3 = new ItemStack(Material.STONE);
    ItemStack item4 = item1;

    item1.equals(item2) is true, they are the "same", as in they have the same amount, material, data, etc.
    item1 == item2 is false, they are not the same instance. They are different objects, they just have the same attributes.
    item1.equals(item3) is false, they do not have the same material, so they are not "equal" in either sense.
    item1 == item4 is true, as I have assigned both variables to the same instance.

    m8
     
  15. Offline

    negative_codezZ

    Why not just check the type since the beginning, and then set the stuff?
    Code:java
    1. @EventHandler
    2. public void onPlayerDrop(PlayerDropItemEvent e) {
    3. if(e.getItemDrop().getType().equals(Material.FLINT)){
    4. if(e.getItemDrop().getItemMeta().getDisplayName().equals(ChatColor.GREEN + "phone (Drop to open phone menu)") {
    5. e.setCancelled(true);
    6. Main.getPhoneMenu().show(e.getPlayer());
    7. }
    8. }
    9. }
     
  16. Offline

    Garris0n

    Note that the display name can be null. You should check hasDisplayName first.
     
  17. Offline

    artish1

    AnOrangeGamer

    At one point when i was preventing players from dropping items, i have noticed that the inventory does not update... so when you drop it, it goes away, but virtually it is still in your inventory, you just can't see it. So you should probably do player.updateInventory(); after cancelling... this method is deprecated but it still works.
     
  18. Offline

    negative_codezZ

    True, thanks for noting that out. AnOrangeGamer
     
Thread Status:
Not open for further replies.

Share This Page