PlayerInteractEvent Bugging out

Discussion in 'Plugin Development' started by iWareWolf, May 30, 2014.

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

    iWareWolf

    I'm using this code on my player interact event but its not working.

    Code:java
    1. if (event.getAction().equals(Action.RIGHT_CLICK_AIR) || event.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {


    This code should allow for both air and block but the event only activates when its a block right click.
     
  2. Offline

    badboysteee98

    Need more information a little like

    What's not working?
    What's the error?

    Then we can help you more :)

    Probably need some more code aswell
     
  3. Offline

    iWareWolf

    badboysteee98

    Code:java
    1. @EventHandler
    2. public void onPlayerInteract(PlayerInteractEvent event) {
    3. for (String arena : core.arenaManager.activeArenas) {
    4. if (core.arenaManager.getDeadPlayers.get(arena).contains(event.getPlayer())) {
    5. event.setCancelled(true);
    6. }
    7. }
    8. if (event.isCancelled() == false) {
    9. if (event.getPlayer().getInventory().getItemInHand() != null) {
    10. if (event.getPlayer().getInventory().getItemInHand().hasItemMeta() != false) {
    11. if (event.getPlayer().getInventory().getItemInHand().getItemMeta().hasDisplayName() != false) {
    12.  
    13. if (event.getPlayer().getInventory().getItemInHand().getItemMeta().getDisplayName().equals(ChatColor.AQUA + "" + ChatColor.BOLD + "" + ChatColor.UNDERLINE + "R870")) {
    14. if (event.getPlayer().getInventory().getItemInHand().getDurability() < 55) {
    15. if (event.getAction().equals(Action.LEFT_CLICK_AIR) || event.getAction().equals(Action.LEFT_CLICK_BLOCK)) {
    16. short durability = event.getPlayer().getInventory().getItemInHand().getDurability();
    17. durability += 5;
    18. event.getPlayer().getInventory().getItemInHand().setDurability(durability);
    19. event.getPlayer().getWorld().playSound(event.getPlayer().getLocation(), Sound.EXPLODE, 1, 2.5f);
    20. Snowball snowball = event.getPlayer().throwSnowball();
    21. Snowball snowball1 = event.getPlayer().throwSnowball();
    22. snowball1.setVelocity(snowball1.getVelocity().add(new Vector(.1, .1, 0)));
    23. Snowball snowball2 = event.getPlayer().throwSnowball();
    24. snowball2.setVelocity(snowball2.getVelocity().add(new Vector(0, .1, .1)));
    25. Snowball snowball3 = event.getPlayer().throwSnowball();
    26. snowball3.setVelocity(snowball3.getVelocity().subtract(new Vector(0, .1, .1)));
    27. Snowball snowball4 = event.getPlayer().throwSnowball();
    28. snowball4.setVelocity(snowball4.getVelocity().subtract(new Vector(.1, .1, 0)));
    29. }
    30. } else {
    31. event.getPlayer().sendMessage(ChatColor.GRAY + "[" + ChatColor.DARK_GRAY + "Crafted" + ChatColor.DARK_RED
    32. + "Fever" + ChatColor.GRAY + "] " + ChatColor.RED + " NO AMMO");
    33. event.getPlayer().playSound(event.getPlayer().getLocation(), Sound.ANVIL_BREAK, 1F, 1F);
    34. }
    35. }
    36. if (event.getPlayer().getInventory().getItemInHand().getItemMeta().getDisplayName().equals(ChatColor.AQUA + "" + ChatColor.BOLD + "" + ChatColor.UNDERLINE + "P90")) {
    37. if (event.getPlayer().getInventory().getItemInHand().getDurability() < 62) {
    38. if (event.getAction().equals(Action.RIGHT_CLICK_AIR) || event.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
    39. event.getPlayer().getWorld().playSound(event.getPlayer().getLocation(), Sound.EXPLODE, 1, 2.5f);
    40. short durability = event.getPlayer().getInventory().getItemInHand().getDurability();
    41. durability += 2;
    42. event.getPlayer().getInventory().getItemInHand().setDurability(durability);
    43. Snowball snowball = event.getPlayer().throwSnowball();
    44. int rand = new Random().nextInt(5) + 1;
    45. if (rand == 1) {
    46. snowball.setVelocity(event.getPlayer().getLocation().getDirection().add(new Vector(.1, .1, 0)));
    47. }
    48. if (rand == 2) {
    49. snowball.setVelocity(event.getPlayer().getLocation().getDirection().add(new Vector(0, .1, .1)));
    50. }
    51. if (rand == 3) {
    52. snowball.setVelocity(event.getPlayer().getLocation().getDirection().subtract(new Vector(0, .1, .1)));
    53. }
    54. if (rand == 4) {
    55. snowball.setVelocity(event.getPlayer().getLocation().getDirection().subtract(new Vector(.1, .1, 0)));
    56. }
    57. }
    58. }
    59. }
    60. if (event.getPlayer().getInventory().getItemInHand().getItemMeta().getDisplayName().equals(ChatColor.DARK_RED + "" + ChatColor.BOLD + "" + ChatColor.UNDERLINE + "Flamethrower")) {
    61. if (event.getPlayer().getInventory().getItemInHand().getDurability() < 1556) {
    62. if (event.getAction().equals(Action.RIGHT_CLICK_AIR) || event.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
    63. event.getPlayer().getWorld().playSound(event.getPlayer().getLocation(), Sound.BLAZE_BREATH, 1L, 1L);
    64. short durability = event.getPlayer().getInventory().getItemInHand().getDurability();
    65. durability += 5;
    66. event.getPlayer().getInventory().getItemInHand().setDurability(durability);
    67. Item item = event.getPlayer().getWorld().dropItem(event.getPlayer().getLocation().add(0, 1, 0), new ItemStack(Material.FIRE));
    68. item.setVelocity(event.getPlayer().getLocation().getDirection().add(new Vector(.1, .1, 0)));
    69. Item item1 = event.getPlayer().getWorld().dropItem(event.getPlayer().getLocation().add(0, 1, 0), new ItemStack(Material.FIRE));
    70. item1.setVelocity(event.getPlayer().getLocation().getDirection().add(new Vector(0, .1, .1)));
    71. Item item2 = event.getPlayer().getWorld().dropItem(event.getPlayer().getLocation().add(0, 1, 0), new ItemStack(Material.FIRE));
    72. item2.setVelocity(event.getPlayer().getLocation().getDirection().subtract(new Vector(0, .1, .1)));
    73. Item item3 = event.getPlayer().getWorld().dropItem(event.getPlayer().getLocation().add(0, 1, 0), new ItemStack(Material.FIRE));
    74. item3.setVelocity(event.getPlayer().getLocation().getDirection().subtract(new Vector(.1, .1, 0)));
    75. Item item4 = event.getPlayer().getWorld().dropItem(event.getPlayer().getLocation().add(0, 1, 0), new ItemStack(Material.FIRE));
    76. item4.setVelocity(event.getPlayer().getLocation().getDirection());
    77. }
    78. }
    79. }
    80. }
    81. }
    82. }
    83. }
    84. }
    85.  
     
  4. Offline

    badboysteee98

    iWareWolf Okay no what error is it giving you?
    Any stack trace or is it not working inside Eclipse?
     
  5. Offline

    iWareWolf

    badboysteee98

    There is no stacktrace. It is working but only one half. The desired code fires only when player interacts the block when the code clearly shows the desired code should fire also when player interacts with air.
     
  6. Offline

    badboysteee98

    I haven't changed nothing just removed equals with == same thing I think it just makes the code look better

    Also maybe try debugging the code or removing the left click block and see if it works with just air

    Although I don't know that much about Bukkit API yet :)
     
  7. Offline

    iWareWolf

  8. Offline

    iWareWolf

  9. Offline

    WinX64

    When you interact with air, your event will be cancelled by default, thus not executing part of your code.
     
  10. Offline

    CoderMusgrove

    You don't need to check for if the event is/isn't cancelled. This will heavily be affected by other plugins.
     
  11. Offline

    iiHeroo


    There's no need to even put event.isCancalled() == false, all you'd need is !event.isCancelled()
     
  12. Offline

    iWareWolf

  13. Offline

    iiHeroo

    iWareWolf
    It's the same thing, but it's more code efficient.
     
  14. Offline

    L33m4n123

  15. Offline

    WinX64

  16. Offline

    L33m4n123

    Ah of course. My Bad. Somehow misunderstood your canceled by default as not getting called :/
     
Thread Status:
Not open for further replies.

Share This Page