Cancelling Drop Event not working correctly?

Discussion in 'Plugin Development' started by PerezHD, Dec 7, 2014.

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

    PerezHD

    Hey guys, I am attempting to stop pickaxes from dropping on my server, yet when I try and make the event, no errors at all, so I load it in-game but it still lets be drop me pickaxe?!

    CODE:
    Code:java
    1. ItemStack pickaxe = new ItemStack(Material.DIAMOND_PICKAXE);
    2.  
    3. @EventHandler
    4. public void onPickaxeAttemptDrop(PlayerDropItemEvent e){
    5. Player p = e.getPlayer();
    6. if (p.getItemInHand().equals(Material.DIAMOND_PICKAXE)){
    7. p.sendMessage(ChatColor.RED + "You cannot drop your pickaxe for safety purposes.");
    8. p.sendMessage(ChatColor.RED + "To give away your pickaxe, put it in a chest, and break the chest.");
    9. e.setCancelled(true);
    10. }
    11. }
     
  2. Offline

    teej107

    Code:java
    1. if (p.getItemInHand().equals(Material.DIAMOND_PICKAXE))

    You are doing if ItemStack equals Material.
    Now answer this question. Since when does an ItemStack ever equal a Material?
     
    mine-care likes this.
  3. Offline

    PerezHD

    I've tried both ways, putting in pickaxe inside (), then I tried DIAMOND_PICKAXE. It let's me drop it
     
  4. Offline

    tommyhoogstra

    Going on from what teej107 said, check the type of the item in the players hand.
     
  5. Offline

    mine-care

    teej107 means that you're comparing a itemstack with material
    It's like saying "if a apple is exactly the same with an orange, hen do stuff" what you need to do is "if p.getItemInahand().getatype().equals(material); and so on..
    Also add a null check since item in hand may turn null and cause you problems
     
    teej107 likes this.
  6. Offline

    mythbusterma

    PerezHD

    No. Reread his answer. You didn't even respond to this, this isn't a bracket mismatch.
     
    teej107 likes this.
  7. Offline

    teej107

    I would appreciate it if you answer my question.
     
  8. Offline

    PerezHD

    Idk why not, I did as you asked, it still lets the pickaxe be droppable

    CODE:
    Code:java
    1. ItemStack pickaxe = new ItemStack(Material.DIAMOND_PICKAXE);
    2.  
    3. @EventHandler
    4. public void onPickaxeAttemptDrop(PlayerDropItemEvent e){
    5. if ((!isTool(e.getPlayer().getItemInHand().getType().name())) || (e.getPlayer().getItemInHand() == null) || (e.getPlayer().getItemInHand().getType() == Material.AIR)) {
    6. return;
    7. }
    8. Player p = e.getPlayer();
    9. if (p.getItemInHand().getType().equals(pickaxe)){
    10. p.sendMessage(ChatColor.RED + "You cannot drop your pickaxe for safety purposes.");
    11. p.sendMessage(ChatColor.RED + "To give away your pickaxe, put it in a chest, and break the chest.");
    12. e.setCancelled(true);
    13. }
    14. }


    I honestly don't know, listen I am not huge in coding as most of dev's on here expect people to be. You have to remember that everyone makes mistakes, and that maybe some are here to learn. Which is me.

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

    97WaterPolo

    PerezHD
    So going back to what was said earlier... Apples are NOT the same things as oranges.
    Apple != Orange
    Therefore
    ItemStack != Material

    When you use p.getItemInHand() that returns an ItemStack. Before you were comparing it to Material.DIAMOND_PICKAXE, that would never return true.

    Now you are using p.getItemInHand().getType() which returns a Material but then you are comparing it to a new ItemStack(ItemStack pickaxe = new ItemStack(Material.DIAMOND_PICKAXE);) meaning that it would never return true.

    In order for it to be true, they have to be of the same type. Yes you are right everyone makes mistakes, but there is a point where you have to learn something that is at the very core of any programming language. I tried my best to explain what you can do to fix it, hopefully you can figure it out.
     
    Avygeil likes this.
  10. Offline

    teej107

    *facepalm*
    Now you are doing if Material equals ItemStack.
    Do you see your error now?
     
  11. PerezHD
    You need to get the type of the item, not just the item, Try this! ;)

    Code:java
    1. @EventHandler
    2. public void onPickaxeAttemptDrop(PlayerDropItemEvent e) {
    3. if (e.getPlayer.getItemInHand().getType() == Material.(material)
    4. {
    5. //Do stuff here
    6. }
    7. }
     
Thread Status:
Not open for further replies.

Share This Page