See if the clicked block is equal to another clicked block

Discussion in 'Plugin Development' started by PieMan456, Dec 11, 2013.

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

    PieMan456

    Hello Everyone,

    I am making a landmine plugin. I am also making wirecutters that if you click on the landmine with them it takes away the landmine. It saves the exact location of the block where the landmine is so how can I check if where they click is like 1 block away from the saved location?
    Here is where I save location:
    Code:java
    1. Vector vec = e.getPlayer().getLocation().toVector();
    2. Vector dir = e.getPlayer().getLocation().getDirection();
    3. vec = vec.add(dir.multiply(1));
    4. final Location location = vec.toLocation(e.getPlayer().getWorld());
    5. bomb.add(location);
     
  2. Offline

    sgavster

    Code:java
    1. for(BlockFace bf : BlockFace.values()) {
    2. Block[] b = e.getClickedBlock().getLocation().getRelative(bf);
    3. //do something with b
    4. }
     
  3. Offline

    PieMan456

    sgavster
    BlockFace cannot be resolved to a type.
     
  4. Offline

    sgavster

  5. Offline

    PieMan456

    sgavster
    It doesn't allow you to import it is just red and says
     
  6. Offline

    sgavster

  7. Offline

    PieMan456

    sgavster
    That still does not work. Anyone else know how to do this?
     
  8. Offline

    Developing

    PieMan456 Perhaps getting the location of the clicked block and check whether there is a difference in 1 in the landmines's X or Z compared with the clicked block's X or Z ? It might not be as efficient as getting the relative block but it may work though.
     
  9. Offline

    mrZcr4fter

    This is what I thought about from the top of my head (don't judge ;p)
    Code:java
    1. int clickedBlockX = event.getBlock().getX();
    2. int clickedBlockZ = event.getBlock().getZ();
    3. //assume that 'landmine' is a saved Location
    4. int landmineX = landmine.getBlockX();
    5. int landmineZ = landmine.getBlockZ();
    6. if(landmineZ == landmineZ - 1 || landmineZ == landmineZ + 1 || landmineX == landmineX - 1 || landmineX == linemineX + 1){
    7. player.sendMessage(chatColor.RED + "A LANDMINE IS SOMEWHERE NEAR YOU")
     
  10. Offline

    maxben34

    Store the first clicked block in an ArrayList. Then when the second block is clicked, see if the block is Equal to the one In the ArrayList
     
  11. Offline

    PieMan456

    maxben34 mrZcr4fter Developing sgavster
    Ok I am having a problem in the PlayerInteractEvent. The event will not fire. I set it to fire if the player is right clicking a block but event if I do that it does not work. Here is what I have:
    Code:java
    1. if(e.getAction() == Action.RIGHT_CLICK_BLOCK){
    2. System.out.println("Blah");
    3. int x = e.getClickedBlock().getLocation().getBlockX();
    4. int y = e.getClickedBlock().getLocation().getBlockY();
    5. int z = e.getClickedBlock().getLocation().getBlockZ();
    6.  
    7. final Location location = new Location(e.getPlayer().getWorld(), x, y, z);
    8. System.out.println(location);
    9. if (bomb.contains(location)) {
    10. Player p = e.getPlayer();
    11. if (p.getItemInHand().getType() == Material.FLINT_AND_STEEL && p.getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.GREEN + "WireCutters")) {
    12. p.sendMessage(ChatColor.GREEN + "You have successfully defused the bomb!");
    13. p.getInventory().remove(Material.FLINT_AND_STEEL);
    14. Bukkit.getServer().getScheduler().cancelTask(id1);
    15. bomb.remove(location);
    16. return;
    17. }
    18. }
    19. }
     
  12. Offline

    sgavster

    PieMan456 Did you register your events? Do you have @EventHandler?
     
  13. Offline

    PieMan456

    sgavster
    Yes because there are other things in the playerinteractevent and they are working perfectly fin.

    Anyone know why?

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

    JRL1004

  15. Offline

    PieMan456

    JRL1004
    Yeah here you go:
    Code:java
    1. @EventHandler
    2. public void onPlayerInteract(final PlayerInteractEvent e) {
    3. if (e.getAction() == Action.RIGHT_CLICK_BLOCK) {
    4. if (e.getClickedBlock().getType() != Material.SIGN
    5. && e.getClickedBlock().getType() != Material.WALL_SIGN
    6. && e.getClickedBlock().getType() != Material.SIGN_POST)
    7. return;
    8. Sign s = (Sign) e.getClickedBlock().getState();
    9. if (s.getLine(0).equalsIgnoreCase(ChatColor.GREEN + "Eggsplosions")) {
    10. Player p = (Player) e.getPlayer();
    11. openInventory(p);
    12. }
    13. }
    14. if(e.getAction() == Action.RIGHT_CLICK_BLOCK){
    15. System.out.println("Blah");
    16.  
    17. int x = e.getClickedBlock().getLocation().getBlockX();
    18. int y = e.getClickedBlock().getLocation().getBlockY();
    19. int z = e.getClickedBlock().getLocation().getBlockZ();
    20.  
    21. final Location location = new Location(e.getPlayer().getWorld(), x, y, z);
    22.  
    23. System.out.println(location);
    24. if (bomb.contains(location)) {
    25. Player p = e.getPlayer();
    26. if (p.getItemInHand().getType() == Material.FLINT_AND_STEEL && p.getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.GREEN + "WireCutters")) {
    27. p.sendMessage(ChatColor.GREEN + "You have successfully defused the bomb!");
    28. p.getInventory().remove(Material.FLINT_AND_STEEL);
    29. Bukkit.getServer().getScheduler().cancelTask(id1);
    30. bomb.remove(location);
    31. return;
    32. }
    33. }
    34. }
    35. Vector v1 = e.getPlayer().getEyeLocation().getDirection().multiply(2.0);
    36. if (e.getPlayer().getItemInHand().getType() != Material.EGG)
    37. return;
    38. if (e.getAction() == Action.RIGHT_CLICK_AIR) {
    39. if (e.getPlayer().getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.GREEN + "Grenade")) {
    40. e.setCancelled(true);
    41. Egg egg =e.getPlayer().getWorld().spawn(e.getPlayer().getEyeLocation().add(v1.getX(), v1.getY(), v1.getZ()), Egg.class);
    42. egg.setMetadata("grenade", new FixedMetadataValue(this, false));
    43. egg.setVelocity(v1);
    44. bob.add(e.getPlayer().getName());
    45. return;
    46. }
    47. }
    48. if(e.getAction() != Action.RIGHT_CLICK_AIR && e.getAction() != Action.RIGHT_CLICK_BLOCK) return;
    49. if(e.getAction() == Action.RIGHT_CLICK_BLOCK && e.getPlayer().getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.GREEN + "LandMine")){
    50. e.getPlayer().sendMessage(ChatColor.RED + "You must right click the landmine in the air not on a block!");
    51. return;
    52. }
    53. if(e.getAction() == Action.RIGHT_CLICK_BLOCK && e.getPlayer().getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.GREEN + "Bomb")){
    54. e.getPlayer().sendMessage(ChatColor.RED + "You must right click the bomb in the air not on a block!");
    55. return;
    56. }
    57. if (e.getPlayer().getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.GREEN + "LandMine")) {
    58. e.setCancelled(true);
    59. lard.add(e.getPlayer().getName());
    60.  
    61. Egg egg =e.getPlayer().getWorld().spawn(e.getPlayer().getEyeLocation().add(v1.getX(), v1.getY(), v1.getZ()), Egg.class);
    62. egg.setMetadata("bomb", new FixedMetadataValue(this, true));
    63. egg.setVelocity(v1);
    64.  
    65. Vector vec = e.getPlayer().getLocation().toVector();
    66. Vector dir = e.getPlayer().getLocation().getDirection();
    67. vec = vec.add(dir.multiply(1));
    68. final Location location = vec.toLocation(e.getPlayer().getWorld());
    69. mine.add(location);
    70. location.getWorld().playSound(location, Sound.FUSE, 2.0F, 2.0F);
    71. e.getPlayer().sendMessage(ChatColor.GREEN + "Landmine set!");
    72. lala.add(e.getPlayer().getName());
    73. final Entity arrow = location.getWorld().spawnEntity(location, EntityType.ARROW);
    74. arrow.setMetadata("ARROW", new FixedMetadataValue(this, false));
    75. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable(){
    76. public void run(){
    77. lala.remove(e.getPlayer().getName());
    78. }
    79. }, 20*10);
    80. id = Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable(){
    81. public void run(){
    82. for(Entity en : arrow.getNearbyEntities(3, 3, 3)){
    83. if(en instanceof Player){
    84. Player pl = (Player) en;
    85. if(lala.contains(pl.getName())){
    86. return;
    87. } else {
    88. location.getWorld().createExplosion(location.getX(), location.getY(), location.getZ(), 4.0F, false, false);
    89. Bukkit.getScheduler().cancelTask(id);
    90. return;
    91. }
    92. }
    93. }
    94. }
    95. }, 5, 5);
    96. /*
    97.   * Inventory pi = e.getPlayer().getInventory(); pi.remove(new
    98.   * ItemStack(Material.EGG, 1)); Block b = e.getClickedBlock();
    99.   * b.getWorld().playSound(b.getLocation(), Sound.FUSE, 2.0F, 2.0F);
    100.   * mine.add(b.getLocation());
    101.   */
    102. }
    103. if (e.getPlayer().getItemInHand().getItemMeta().getDisplayName()
    104. .equalsIgnoreCase(ChatColor.GREEN + "Bomb")) {
    105. e.setCancelled(true);
    106. doo.add(e.getPlayer().getName());
    107.  
    108. Egg egg =e.getPlayer().getWorld().spawn(e.getPlayer().getEyeLocation().add(v1.getX(), v1.getY(), v1.getZ()), Egg.class);
    109. egg.setMetadata("bomb", new FixedMetadataValue(this, false));
    110. egg.setVelocity(v1);
    111.  
    112. final int x = e.getPlayer().getLocation().getBlockX();
    113. final int y = e.getPlayer().getLocation().getBlockY();
    114. final int z = e.getPlayer().getLocation().getBlockZ();
    115.  
    116. final Location location = new Location(e.getPlayer().getWorld(), x, y, z);
    117. bomb.add(location);
    118. location.getWorld().playSound(location, Sound.FUSE, 2.0F, 2.0F);
    119. /*
    120.   * Inventory pi = e.getPlayer().getInventory();
    121.   * pi.remove(e.getPlayer().getItemInHand()); final Block b =
    122.   * e.getClickedBlock(); bomb.add(b.getLocation());
    123.   */
    124. System.out.println(location);
    125. e.getPlayer().sendMessage(ChatColor.GREEN + "The bomb is going to blow up in 10 seconds!");
    126. id1 = Bukkit.getServer().getScheduler()
    127. .scheduleSyncDelayedTask(this, new Runnable() {
    128. public void run() {
    129. location.getWorld().createExplosion(x, y, z, 4.0F, false, true);
    130. bomb.remove(location);
    131. }
    132. }, 20 * 10);
    133. return;
    134. }
    135. }
     
  16. Offline

    JRL1004

    PieMan456 At the top of your event you have an Action.RIGHT_CLICK_AIR check so Java is going down that part first
    Code:java
    1.  
    2. if (e.getAction() == Action.RIGHT_CLICK_BLOCK) {
    3. if (e.getClickedBlock().getType() != Material.SIGN
    4. && e.getClickedBlock().getType() != Material.WALL_SIGN
    5. && e.getClickedBlock().getType() != Material.SIGN_POST)
    6. return;
    7. Sign s = (Sign) e.getClickedBlock().getState();
    8. if (s.getLine(0).equalsIgnoreCase(ChatColor.GREEN + "Eggsplosions")) {
    9. Player p = (Player) e.getPlayer();
    10. openInventory(p);
    11. }
    12. }

    but since you are nor clicking a sign, it is returning out. This stops that part from firing
     
  17. Offline

    PieMan456

    JRL1004
    But it works for the events below it?
     
  18. Offline

    JRL1004

    PieMan456 None of those ask for Action.RIGHT_CLICK_BLOCK or they are not being returned out. It's hard to explain, I would suggest just having multiple listeners
     
  19. Offline

    PieMan456

    JRL1004
    Ok thanks it works now. I have another question for you if that is ok. I am trying to see if they click a certain location to disarm a landmine except even if I click the location it does not work.
    Here is how I save landmine's location:
    Code:java
    1. if (e.getPlayer().getItemInHand().getItemMeta().getDisplayName()
    2. .equalsIgnoreCase(ChatColor.GREEN + "Bomb")) {
    3. e.setCancelled(true);
    4. doo.add(e.getPlayer().getName());
    5.  
    6. Egg egg =e.getPlayer().getWorld().spawn(e.getPlayer().getEyeLocation().add(v1.getX(), v1.getY(), v1.getZ()), Egg.class);
    7. egg.setMetadata("bomb", new FixedMetadataValue(this, false));
    8. egg.setVelocity(v1);
    9.  
    10. final int x = e.getPlayer().getLocation().getBlockX();
    11. final int y = e.getPlayer().getLocation().getBlockY();
    12. final int z = e.getPlayer().getLocation().getBlockZ();
    13.  
    14. final Location location = new Location(e.getPlayer().getWorld(), x, y, z);
    15. bomb.add(location);
    16. location.getWorld().playSound(location, Sound.FUSE, 2.0F, 2.0F);
    17. /*
    18.   * Inventory pi = e.getPlayer().getInventory();
    19.   * pi.remove(e.getPlayer().getItemInHand()); final Block b =
    20.   * e.getClickedBlock(); bomb.add(b.getLocation());
    21.   */
    22. System.out.println(location);
    23. e.getPlayer().sendMessage(ChatColor.GREEN + "The bomb is going to blow up in 10 seconds!");
    24. id1 = Bukkit.getServer().getScheduler()
    25. .scheduleSyncDelayedTask(this, new Runnable() {
    26. public void run() {
    27. location.getWorld().createExplosion(x, y, z, 4.0F, false, true);
    28. bomb.remove(location);
    29. }
    30. }, 20 * 10);
    31. return;
    32. }
    33. }

    Here is where I see where the player clicks:
    Code:java
    1. if(e.getAction() == Action.RIGHT_CLICK_BLOCK){
    2. System.out.println("Blah");
    3.  
    4. int x = e.getClickedBlock().getLocation().getBlockX();
    5. int y = e.getClickedBlock().getLocation().getBlockY();
    6. int z = e.getClickedBlock().getLocation().getBlockZ();
    7.  
    8. final Location location = new Location(e.getPlayer().getWorld(), x, y, z);
    9.  
    10. System.out.println(location);
    11. if (bomb.contains(location)) {
    12. Player p = e.getPlayer();
    13. if (p.getItemInHand().getType() == Material.FLINT_AND_STEEL && p.getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.GREEN + "WireCutters")) {
    14. p.sendMessage(ChatColor.GREEN + "You have successfully defused the bomb!");
    15. p.getInventory().remove(Material.FLINT_AND_STEEL);
    16. Bukkit.getServer().getScheduler().cancelTask(id1);
    17. bomb.remove(location);
    18. return;
    19. }
    20. }
    21. }
    22. }

    Do you know what I am doing wrong?
     
  20. Offline

    JRL1004

    PieMan456 You are not cancelling the delayed task
     
  21. Offline

    PieMan456

    JRL1004
    Yes I am. But even then it doesn't say the message"You defused the bomb!"
    Code:java
    1. Bukkit.getServer().getScheduler().cancelTask(id1);
     
  22. Offline

    JRL1004

    PieMan456 It could be reassigning the id1 variable. Maybe make a HashMap<Location, Integer> and temporarily store the taskID there using the bomb's location as the key? Otherwise, I am not sure.
     
  23. Offline

    PieMan456

    JRL1004
    I don't want to do that. Anyone else know why this is not working?
     
Thread Status:
Not open for further replies.

Share This Page