BlockPlaceEvent for server?

Discussion in 'Plugin Development' started by justin_393, Aug 27, 2014.

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

    justin_393

    I'm creating a plugin that when you click with a stick it will place 2 portals in front of you (All done) all I need to know now is, how do you check if the server placed the blocks? Because I need to add the blocks to a Hashmap so I can remove the blocks later, but BlockPlaceEvent only calls if a player places a block.

    So does anyone know?
     
  2. Offline

    FerusGrim

    When you specify what blocks to add and where, can't you just add them to a hashmap then?
     
  3. Offline

    LCastr0

    When you right-click with the stick, just add a line of code after you place the portals in the world, adding them to the hashmap.
     
  4. Offline

    justin_393

    LCastr0 FerusGrim
    Ok, I've done that, but now it only removes the block you clicked, yet i need it to remove the blocks above it. Here's my code.
    Code:java
    1. event.getPlayer();
    2. Location loc = event.getClickedBlock().getLocation();
    3. final Block block = loc.getBlock();
    4.  
    5. loc.setY(loc.getY() + 1);
    6. loc.getBlock().setType(Material.PORTAL);
    7. loc.setY(loc.getY() + 1);
    8. loc.getBlock().setType(Material.PORTAL);
    9.  
    10.  
    11. portal.add(block);
    12.  
    13. new BukkitRunnable() {
    14. public void run() {
    15. if(portal.contains(block)) {
    16. block.breakNaturally();
    17. }
    18. }
    19. }.runTaskLater(core, core.getConfig().getInt("BlockRemover"));
     
  5. Offline

    BillyGalbreath

    Code:java
    1.  
    2. event.getPlayer();
    3. Location loc = event.getClickedBlock().getLocation();
    4.  
    5. loc.add(0, 1, 0);
    6. final Block block1 = loc.getBlock();
    7. block1.setType(Material.PORTAL);
    8.  
    9. loc.add(0, 1, 0);
    10. final Block block2 = loc.getBlock();
    11. block2.setType(Material.PORTAL);
    12.  
    13.  
    14. portal.add(block1);
    15. portal.add(block2);
    16.  
    17. new BukkitRunnable() {
    18. public void run() {
    19. if(portal.contains(block)) {
    20. block1.breakNaturally();
    21. block2.breakNaturally();
    22. }
    23. }
    24. }.runTaskLater(core, core.getConfig().getInt("BlockRemover"));
    25.  


    Also, here's how I would do it so you dont have to set things final. (just a rough draft, not tested, but gives you an idea)

    Code:java
    1.  
    2.  
    3. // Your listener class
    4.  
    5. Location loc = event.getClickedBlock().getLocation();
    6.  
    7. loc.add(0, 1, 0);
    8. Block block1 = loc.getBlock();
    9. block1.setType(Material.PORTAL);
    10.  
    11. loc.add(0, 1, 0);
    12. Block block2 = loc.getBlock();
    13. block2.setType(Material.PORTAL);
    14.  
    15. portal.add(block1);
    16. portal.add(block2);
    17.  
    18. new BreakBlocks(block1, block2).runTaskLater(core, core.getConfig().getInt("BlockRemover"));
    19.  
    20.  
    21.  
    22. //BreakBlocks.java
    23.  
    24. public class BreakBlocks implements BukkitRunnable {
    25. private Block block1;
    26. private Block block2;
    27.  
    28. public BreakBlocks(Block block1, Block block2) {
    29. this.block1 = block1;
    30. this.block2 = block2;
    31. }
    32.  
    33. @Override
    34. public void run() {
    35. block1.breakNaturally();
    36. block2.breakNaturally();
    37. }
    38. }
    39.  


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

    justin_393

    BillyGalbreath
    Still does the exact same thing

    Code:java
    1. event.getPlayer();
    2. Location loc = event.getClickedBlock().getLocation();
    3. final Block block = loc.getBlock();
    4.  
    5. loc.add(0, 1, 0);
    6. final Block block1 = loc.getBlock();
    7. block1.setType(Material.PORTAL);
    8.  
    9. loc.add(0, 1, 0);
    10. final Block block2 = loc.getBlock();
    11. block2.setType(Material.PORTAL);
    12.  
    13. portal.add(block1);
    14. portal.add(block2);
    15.  
    16. new BukkitRunnable() {
    17. public void run() {
    18. if (portal.contains(block1)
    19. || portal.contains(block2)) {
    20. block.breakNaturally();
    21. }
    22. }
    23. }.runTaskLater(core, core.getConfig()
    24. .getInt("BlockRemover"));


    Just realized that I need to change || to &&, but either way it still doesn't work.

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

    BillyGalbreath

    Thats because you are breaking block, not block1 and block2. ;)
     
  8. Offline

    justin_393

  9. Offline

    BillyGalbreath

    :D

    Also might want to check out what I said in post #6 about how to avoid making your variables final. I think its cleaner, anyway. :p
     
    justin_393 likes this.
Thread Status:
Not open for further replies.

Share This Page