PlayerDropItemEvent -> No Item in Hand, Event not working.

Discussion in 'Plugin Development' started by Dealyise, Aug 8, 2014.

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

    Dealyise

    So, I got a plugin, which checks if a player is around a specific location. If it's true, then it checks the cooldown. When there's no cooldown, it shall give the player access to another location.
    I just found out when there's no Item in Hand, that this Event is not working. I was wondering how I could check if there's nothing in Hand. But it assumes that this event wouldn't do it.
    Is there any chance to check that? Because this is extremly important for me.

    greetz
     
  2. Offline

    dsouzamatt

    What, something like this?
    Code:java
    1. if(player.getItemInHand()!=null)
    2. {
    3. //your code
    4. }

    You haven't been too clear in what you're asking. Please post your code if possible.
     
  3. Offline

    Dealyise

    This isn't working, tested it already and there's no response.
    Simple: When a player presses Q, it checks through PlayerItemDropEvent if hes in a specific range of one location. If so, check if he has a cooldown. If not -> Give him access to another location. Otherwise he can't join the location.
    When pressing Q without having an Item in Hand, the Event doesn't work and I wonder how you could check that because that what you've posted doesn't work, I tried it many times.
     
  4. Offline

    dsouzamatt

    Dealyise It may be given as air rather than just null. Try this:
    Code:java
    1. if(player.getItemInHand().getType() != Material.AIR)
    2. {
    3. //your code
    4. }

    I still recommend you post your code here, and any errors that may be appearing in the console.

    (On a side note, please tahg me in future or I may not reply to your messages.)
     
  5. Offline

    Dealyise

    dsouzamatt
    This isn't working. I checked if its Air, still no response. I dont know, maybe it's a bug or something.
    Code:java
    1. @EventHandler
    2. public void onPlayerDropItem(PlayerDropItemEvent e){
    3. boolean status = sysFunc.checkLoginQueue(e.getPlayer());
    4. if(status == true){
    5. e.setCancelled(true);
    6. return;
    7. }
    8. Player p = e.getPlayer();
    9. File folder = new File("plugins/System/Locations/");
    10. File[] listOfFiles = folder.listFiles();
    11. for(File file : listOfFiles){
    12. FileConfiguration cfg = YamlConfiguration.loadConfiguration(file);
    13. int x = cfg.getInt("Location.x");
    14. int y = cfg.getInt("Location.y");
    15. int z = cfg.getInt("Location.z");
    16. int x2 = x + 1;
    17. int y2 = y;
    18. int z2 = z + 1;
    19. int x3 = x - 1;
    20. int y3 = y;
    21. int z3 = z - 1;
    22. int xx = cfg.getInt("Location.x2");
    23. int yy = cfg.getInt("Location.y2");
    24. int zz = cfg.getInt("Location.z2");
    25. int x22 = xx + 1;
    26. int y22 = yy;
    27. int z22 = zz + 1;
    28. int x33 = xx - 1;
    29. int y33 = yy;
    30. int z33 = zz - 1;
    31. Location loc = p.getLocation();
    32. Location loc2 = new Location(p.getWorld(), x2, y2, z2);
    33. Location loc3 = new Location(p.getWorld(), x3, y3, z3);
    34. Location loc4 = new Location(p.getWorld(), x22, y22, z22);
    35. Location loc5 = new Location(p.getWorld(), x33, y33, z33);
    36. if(teamFunc.isInLocation(loc, loc2, loc3)){
    37. if(LocCooldown.checkCooldown(p).equalsIgnoreCase("Nein")){
    38.  
    39. sysFunc.msg(p, "Item in Hand: " + p.getItemInHand());
    40. if(p.getItemInHand() == null){
    41. sysFunc.msg(p, "funzt");
    42. }
    43.  
    44. e.setCancelled(true);
    45. Location pLoc = new Location(p.getWorld(), cfg.getInt("Location.x2"), cfg.getInt("Location.y2"), cfg.getInt("Location.z2"));
    46. p.teleport(pLoc);
    47. LocCooldown.activateCooldown(p);
    48. sysFunc.msg(p, "Du hast den Laden betreten.");
    49. }else{
    50. e.setCancelled(true);
    51. sysFunc.msg(p, "Du kannst den Laden erst in §6" + LocCooldown.checkCooldown(p) + "§e betreten!");
    52. }
    53. }else if(teamFunc.isInLocation(loc, loc4, loc5)){
    54. if(LocCooldown.checkCooldown(p).equalsIgnoreCase("Nein")){
    55. e.setCancelled(true);
    56. Location pLoc = new Location(p.getWorld(), cfg.getInt("Location.x"), cfg.getInt("Location.y"), cfg.getInt("Location.z"));
    57. p.teleport(pLoc);
    58. LocCooldown.activateCooldown(p);
    59. sysFunc.msg(p, "Du hast den Laden verlassen.");
    60. }else{
    61. e.setCancelled(true);
    62. sysFunc.msg(p, "Du kannst den Laden erst in §6" + LocCooldown.checkCooldown(p) + "§e verlassen!");
    63. }
    64. }
    65.  
    66. }
    67.  
    68. }


    I still think that the source is not needed, but here you go, so you can hopefully imagine what the source does.
    It just works if you got an Item in your hand, it doesnt work if you don't have an Item in your hand.
     
  6. Offline

    dsouzamatt

    Dealyise It's good that you've posted the code; I think I can see where the problem is.

    At line 39 it appears that you're trying to concatenate a String ("Item in Hand) to an ItemStack (p.getItemInHand()). Your null check is also on the line after you have executed the above code, making it somewhat pointless. Try this instead:
    Code:java
    1. if(p.getItemInHand() != null)
    2. {
    3. sysFunc.msg(p, "Item in Hand: " + p.getItemInHand().getType().name());
    4. }
    5. else
    6. {
    7. sysFunc.msg(p, "funzt");
    8. }
     
  7. Offline

    Dealyise

  8. Offline

    fireblast709

    Dealyise try again in survival. Creative is known to be buggy.
     
  9. Offline

    Dealyise

  10. Offline

    fireblast709

    Dealyise post the latest code. If it matches the last posted snippet, null != air
     
  11. Offline

    Dealyise

    fireblast709
    Code:java
    1. @EventHandler
    2. public void onPlayerDropItem(PlayerDropItemEvent e){
    3. boolean status = sysFunc.checkLoginQueue(e.getPlayer());
    4. if(status == true){
    5. e.setCancelled(true);
    6. return;
    7. }
    8. Player p = e.getPlayer();
    9. File folder = new File("plugins/System/Locations/");
    10. File[] listOfFiles = folder.listFiles();
    11. for(File file : listOfFiles){
    12. FileConfiguration cfg = YamlConfiguration.loadConfiguration(file);
    13. int x = cfg.getInt("Location.x");
    14. int y = cfg.getInt("Location.y");
    15. int z = cfg.getInt("Location.z");
    16. int x2 = x + 1;
    17. int y2 = y;
    18. int z2 = z + 1;
    19. int x3 = x - 1;
    20. int y3 = y;
    21. int z3 = z - 1;
    22. int xx = cfg.getInt("Location.x2");
    23. int yy = cfg.getInt("Location.y2");
    24. int zz = cfg.getInt("Location.z2");
    25. int x22 = xx + 1;
    26. int y22 = yy;
    27. int z22 = zz + 1;
    28. int x33 = xx - 1;
    29. int y33 = yy;
    30. int z33 = zz - 1;
    31. Location loc = p.getLocation();
    32. Location loc2 = new Location(p.getWorld(), x2, y2, z2);
    33. Location loc3 = new Location(p.getWorld(), x3, y3, z3);
    34. Location loc4 = new Location(p.getWorld(), x22, y22, z22);
    35. Location loc5 = new Location(p.getWorld(), x33, y33, z33);
    36. if(teamFunc.isInLocation(loc, loc2, loc3)){
    37. if(LocCooldown.checkCooldown(p).equalsIgnoreCase("Nein")){
    38.  
    39. if(p.getItemInHand() != null)
    40. {
    41. sysFunc.msg(p, "Item in Hand: " + p.getItemInHand().getType().name());
    42. }
    43. else
    44. {
    45. sysFunc.msg(p, "funzt");
    46. }
    47.  
    48. e.setCancelled(true);
    49. Location pLoc = new Location(p.getWorld(), cfg.getInt("Location.x2"), cfg.getInt("Location.y2"), cfg.getInt("Location.z2"));
    50. p.teleport(pLoc);
    51. LocCooldown.activateCooldown(p);
    52. sysFunc.msg(p, "Du hast den Laden betreten.");
    53. }else{
    54. e.setCancelled(true);
    55. sysFunc.msg(p, "Du kannst den Laden erst in §6" + LocCooldown.checkCooldown(p) + "§e betreten!");
    56. }
    57. }else if(teamFunc.isInLocation(loc, loc4, loc5)){
    58. if(LocCooldown.checkCooldown(p).equalsIgnoreCase("Nein")){
    59. e.setCancelled(true);
    60. Location pLoc = new Location(p.getWorld(), cfg.getInt("Location.x"), cfg.getInt("Location.y"), cfg.getInt("Location.z"));
    61. p.teleport(pLoc);
    62. LocCooldown.activateCooldown(p);
    63. sysFunc.msg(p, "Du hast den Laden verlassen.");
    64. }else{
    65. e.setCancelled(true);
    66. sysFunc.msg(p, "Du kannst den Laden erst in §6" + LocCooldown.checkCooldown(p) + "§e verlassen!");
    67. }
    68. }
    69.  
    70. }
    71.  
    72. }
     
  12. Offline

    fireblast709

    Dealyise due to a lacking return statement, the code below will always be run ;). Check if getItemInHand() == null || getItemInHand().getType() == Material.AIR, then optionally cancel and return.
     
    Dealyise likes this.
  13. Offline

    Dealyise

    fireblast709
    http://gyazo.com/99733a10c2ef6759bca6244ff658eee2
    This is what I get now after entering. Still doesn't work without an Item, when I hold an Item, it returns AIR as the holding material for any reason. I think it's a bug in Bukkit API bro.

    Code:java
    1. @EventHandler
    2. public void onPlayerDropItem(PlayerDropItemEvent e){
    3. boolean status = sysFunc.checkLoginQueue(e.getPlayer());
    4. if(status == true){
    5. e.setCancelled(true);
    6. return;
    7. }
    8. Player p = e.getPlayer();
    9. File folder = new File("plugins/System/Locations/");
    10. File[] listOfFiles = folder.listFiles();
    11. for(File file : listOfFiles){
    12. FileConfiguration cfg = YamlConfiguration.loadConfiguration(file);
    13. int x = cfg.getInt("Location.x");
    14. int y = cfg.getInt("Location.y");
    15. int z = cfg.getInt("Location.z");
    16. int x2 = x + 1;
    17. int y2 = y;
    18. int z2 = z + 1;
    19. int x3 = x - 1;
    20. int y3 = y;
    21. int z3 = z - 1;
    22. int xx = cfg.getInt("Location.x2");
    23. int yy = cfg.getInt("Location.y2");
    24. int zz = cfg.getInt("Location.z2");
    25. int x22 = xx + 1;
    26. int y22 = yy;
    27. int z22 = zz + 1;
    28. int x33 = xx - 1;
    29. int y33 = yy;
    30. int z33 = zz - 1;
    31. Location loc = p.getLocation();
    32. Location loc2 = new Location(p.getWorld(), x2, y2, z2);
    33. Location loc3 = new Location(p.getWorld(), x3, y3, z3);
    34. Location loc4 = new Location(p.getWorld(), x22, y22, z22);
    35. Location loc5 = new Location(p.getWorld(), x33, y33, z33);
    36. if(teamFunc.isInLocation(loc, loc2, loc3)){
    37. if(LocCooldown.checkCooldown(p).equalsIgnoreCase("Nein")){
    38.  
    39. if(p.getItemInHand() == null || p.getItemInHand().getType() == Material.AIR)
    40. {
    41. sysFunc.msg(p, "funzt");
    42. }
    43. else
    44. {
    45. sysFunc.msg(p, "Item in Hand: " + p.getItemInHand().getType().name());
    46. }
    47.  
    48. e.setCancelled(true);
    49. Location pLoc = new Location(p.getWorld(), cfg.getInt("Location.x2"), cfg.getInt("Location.y2"), cfg.getInt("Location.z2"));
    50. p.teleport(pLoc);
    51. LocCooldown.activateCooldown(p);
    52. sysFunc.msg(p, "Du hast den Laden betreten.");
    53. }else{
    54. e.setCancelled(true);
    55. sysFunc.msg(p, "Du kannst den Laden erst in §6" + LocCooldown.checkCooldown(p) + "§e betreten!");
    56. }
    57. }else if(teamFunc.isInLocation(loc, loc4, loc5)){
    58. if(LocCooldown.checkCooldown(p).equalsIgnoreCase("Nein")){
    59. e.setCancelled(true);
    60. Location pLoc = new Location(p.getWorld(), cfg.getInt("Location.x"), cfg.getInt("Location.y"), cfg.getInt("Location.z"));
    61. p.teleport(pLoc);
    62. LocCooldown.activateCooldown(p);
    63. sysFunc.msg(p, "Du hast den Laden verlassen.");
    64. }else{
    65. e.setCancelled(true);
    66. sysFunc.msg(p, "Du kannst den Laden erst in §6" + LocCooldown.checkCooldown(p) + "§e verlassen!");
    67. }
    68. }
    69.  
    70. }
    71.  
    72. }
     
  14. Offline

    fireblast709

    Dealyise being a complete fool for forgetting Bukkit's derp there. When the drop event is fired, the item is already dropped (hence you don't hold anything, air). When you cancel the event, the item is removed from the world and placed in the first free slot in your inventory, which is the same as the original slot.

    To fix this, get the ItemStack of the Item dropped instead of the held item
     
    Dealyise likes this.
  15. Offline

    Dealyise

    fireblast709
    Code:java
    1. @EventHandler
    2. public void onPlayerDropItem(PlayerDropItemEvent e){
    3. boolean status = sysFunc.checkLoginQueue(e.getPlayer());
    4. if(status == true){
    5. e.setCancelled(true);
    6. return;
    7. }
    8. Player p = e.getPlayer();
    9. File folder = new File("plugins/System/Locations/");
    10. File[] listOfFiles = folder.listFiles();
    11. for(File file : listOfFiles){
    12. FileConfiguration cfg = YamlConfiguration.loadConfiguration(file);
    13. int x = cfg.getInt("Location.x");
    14. int y = cfg.getInt("Location.y");
    15. int z = cfg.getInt("Location.z");
    16. int x2 = x + 1;
    17. int y2 = y;
    18. int z2 = z + 1;
    19. int x3 = x - 1;
    20. int y3 = y;
    21. int z3 = z - 1;
    22. int xx = cfg.getInt("Location.x2");
    23. int yy = cfg.getInt("Location.y2");
    24. int zz = cfg.getInt("Location.z2");
    25. int x22 = xx + 1;
    26. int y22 = yy;
    27. int z22 = zz + 1;
    28. int x33 = xx - 1;
    29. int y33 = yy;
    30. int z33 = zz - 1;
    31. Location loc = p.getLocation();
    32. Location loc2 = new Location(p.getWorld(), x2, y2, z2);
    33. Location loc3 = new Location(p.getWorld(), x3, y3, z3);
    34. Location loc4 = new Location(p.getWorld(), x22, y22, z22);
    35. Location loc5 = new Location(p.getWorld(), x33, y33, z33);
    36. if(teamFunc.isInLocation(loc, loc2, loc3)){
    37. if(LocCooldown.checkCooldown(p).equalsIgnoreCase("Nein")){
    38.  
    39. if(e.getItemDrop().getItemStack().getType() == null)
    40. {
    41. e.setCancelled(true);
    42. Location pLoc = new Location(p.getWorld(), cfg.getInt("Location.x2"), cfg.getInt("Location.y2"), cfg.getInt("Location.z2"));
    43. p.teleport(pLoc);
    44. LocCooldown.activateCooldown(p);
    45. sysFunc.msg(p, "Du hast den Laden betreten.");
    46. }
    47. else
    48. {
    49. e.setCancelled(true);
    50. Location pLoc = new Location(p.getWorld(), cfg.getInt("Location.x2"), cfg.getInt("Location.y2"), cfg.getInt("Location.z2"));
    51. p.teleport(pLoc);
    52. LocCooldown.activateCooldown(p);
    53. sysFunc.msg(p, "Du hast den Laden betreten.");
    54. sysFunc.msg(p, "Item in Hand: " + e.getItemDrop().getItemStack().getType().name());
    55. }
    56.  
    57.  
    58. }else{
    59. e.setCancelled(true);
    60. sysFunc.msg(p, "Du kannst den Laden erst in §6" + LocCooldown.checkCooldown(p) + "§e betreten!");
    61. }
    62. }else if(teamFunc.isInLocation(loc, loc4, loc5)){
    63. if(LocCooldown.checkCooldown(p).equalsIgnoreCase("Nein")){
    64. e.setCancelled(true);
    65. Location pLoc = new Location(p.getWorld(), cfg.getInt("Location.x"), cfg.getInt("Location.y"), cfg.getInt("Location.z"));
    66. p.teleport(pLoc);
    67. LocCooldown.activateCooldown(p);
    68. sysFunc.msg(p, "Du hast den Laden verlassen.");
    69. }else{
    70. e.setCancelled(true);
    71. sysFunc.msg(p, "Du kannst den Laden erst in §6" + LocCooldown.checkCooldown(p) + "§e verlassen!");
    72. }
    73. }
    74.  
    75. }
    76.  
    77. }


    Still the same problem. When I held no item, it doesn't work. It won't call the event. Is there any other event or solution to fix that????
     
  16. Offline

    fireblast709

    Dealyise sadly no. You cannot drop nothing ;)
     
    Dealyise likes this.
  17. Offline

    Dealyise

    fireblast709 thats crap ... the event should be also called when no item is in hand.... this would be awesome and very useful for me...

    I need to do it then with commands.... -.-
     
  18. Offline

    mrgreen33gamer

    fireblast709 is right. You cannot do that. The event is casting AN item that got dropped. Not null. It doesn't check for air or null items.
     
  19. Offline

    Dealyise

    mrgreen33gamer
    Like I said, it would be awesome if it would return null or air. Would be really useful for me.... But, since it's not given, I have to do it via. commands. Thanks anyways guys.
    fireblast709
     
Thread Status:
Not open for further replies.

Share This Page