Solved If Player Drops a certain block, cancel?

Discussion in 'Plugin Development' started by Mycrowut, Jul 30, 2013.

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

    Mycrowut

    Hello all, Im trying to prevent a player from dropping a certain item but when its dropped I want to give it back to them, so they don't lose it.

    I'm using the PlayerDropItemEvent and if I use the following code:
    Code:java
    1. if(event.getItemDrop() == Item.EMERALD){


    I cannot select "EMERALD_ORE" from Item because it isn't listed.
    I don't want to use the ItemSpawnEvent because I cannot return the player's name even though if I could it would work perfectly. Any ideas?

    Thanks
    -Mycro
     
  2. Offline

    Polaris29

    Code:java
    1.  
    2. if (event.getItemDrop().getItemStack().getType() == Material.EMERALD_ORE) {
    3. event.setCancelled(true);
    4. }
    5.  
     
  3. Offline

    ZachBora

    You should be using event.getItemDrop().getType() == Material.EMERALD_ORE

    Edit: dang got ninja'd :/
    Edit2: and I forgot .getItemStack()
     
  4. Offline

    soulofw0lf

    .equals not ==
    if (event.getDropItem().getItemStack().getType().equals(Material.EMERALD_ORE) _{
    event.setCancelled(true);
    }
     
  5. Offline

    ZachBora

  6. Offline

    metalhedd


    For enum's, it actually doesn't make a difference, and I believe == is the "preferred" method (read: hypothetically faster). personally I just use .equals() everywhere so that I never have to worry about it, and am less likely to ever accidentally use a == in the wrong place.
     
  7. Offline

    Mycrowut

    Polaris29
    When I paste the following code, I have an error underlining "getDropItem" that says "Add cast to event"

    Code:java
    1. @EventHandler(priority = EventPriority.LOWEST)
    2. public void onPlayerDropItemEvent(PlayerDropItemEvent event) {
    3. if (event.getDropItem().getItemStack().getType() == Material.EMERALD_ORE) {
    4. event.setCancelled(true);
    5. }
    6. event.getPlayer().sendMessage("used 3");
    7. Player player = event.getPlayer();
    8. player.sendMessage(""+event.getItemDrop().toString());
    9.  
    10. }
    11.  
     
  8. Offline

    soulofw0lf

    @ZachBora @metalhedd when comparing properties of object you should always use .equals please don't tell people it's ok to use == even if it's something you do, it can lead to a lot of problems that most people on here aren't experienced enough to trouble shoot or even realize why it's not working.
     
  9. Offline

    Polaris29

    Mycrowut That was just a mistake on my part on switching the words item and drop. Use getItemDrop() instead
     
    Mycrowut likes this.
  10. Offline

    Rocoty

    soulofw0lf Enumeration constants are, as the name implies, constants. Which means MyEnum.FOO will only ever be one instance and thus using == is okay, because you'd want to be checking if it is the same instance, and not necessarily check the properties. Preferably and intuitively, performance-wise, checking with == should be the way to go.

    On top of all that, take a look at this: http://grepcode.com/file/repository.../lang/Enum.java#Enum.equals(java.lang.Object) using equals on enum types (unless it is overridden and changed), would only be like going a longer route to finish...the exact same finish. Pluss == would be null-safe
     
  11. Offline

    soulofw0lf

    Rocoty to compare one enum to another you are correct. however as far as "performance wise" there is no difference at all. and in the instance of things like getType().equals(Material.*) you are comparing a property of an object, and any time you are comparing properties of objects you should use .equals even though the property you are checking is a constant it is still considered good practice to use .equals (especially for people who are not experienced with java that won't necessarily understand the difference between when it's ok and when it's not) since .equals is perfectly safe to be used and does not hamper performance at all AND will help you with debugging since it's not null safe, on these forums where 90% of the questions are by people who do not understand java at all, please never tell people to use == when comparing object properties as previously stated.
     
  12. Offline

    ZachBora

    soulofw0lf An error I often did was using == with strings :S. Using .equals would reduce errors. But I still use == often because of readability.
     
  13. Offline

    soulofw0lf

    we should really stop hijacking this thread now :) sorry about that op.
     
  14. Offline

    Mycrowut

    Polaris29 Figured that eventually :p thank you.
     
Thread Status:
Not open for further replies.

Share This Page