Solved Replace block?

Discussion in 'Plugin Development' started by CptnPummeluff, May 11, 2014.

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

    CptnPummeluff

    Heya people, I need some help to fix an issue. I currently try that if a player stands 2 blocks away from a glass block, that the glass is replaced with air and after 2 sec's the glass is back.
    Probably it doesn't work. There's no error at the console. I hope u can help me.
    The current code:
    Code:java
    1. @EventHandler
    2. public void homo(PlayerMoveEvent e){
    3. Player p = e.getPlayer();
    4. Location loc = p.getLocation();
    5. Block b = loc.subtract(2.0, 2.0, 2.0).getBlock();
    6.  
    7. if(loc.subtract(2.0, 2.0, 2.0).getBlock().getType().equals(Material.GLASS)){
    8. b.setType(Material.AIR);
    9. }
    10. }


    thx
     
  2. Offline

    ever_x

    Instead of loc being p.getLocation(), it would be p.getLocation().getBlock().getLocation(). This seems pointless, as they both return a location, but p.getLocation().getBlock().getLocation() will reset the location to the exact block location, rather than one with a load of decimal points after it. You can use a double for loop to loop through all the block locations and see if they are a glass block:
    Code:java
    1.  
    2. for (int i = -2; i < 2; i++)
    3. {
    4. for (int j = -2; j < 2; j++)
    5. {
    6. Location blockLoc = new Location(loc.getWorld(), loc.getX().add(i, 0, 0), loc.getY(), loc.getZ().add(0, 0, j);
    7. if (blockLoc.getBlock().getType().equals(Material.GLASS))
    8. {
    9. //Do code here
    10. }
    11. }
    12. }
    13.  

    Haven't tested it, but give it a shot :)
     
  3. Offline

    CptnPummeluff

  4. Offline

    dentych

    CptnPummeluff
    When you use loc.getX(), it will return a number of the type double. Then you proceed to try to run a .add(int,int,int) method, which is not a method of double. That causes the error.
     
    CptnPummeluff likes this.
  5. Offline

    ever_x

    new code :)
    Code:java
    1.  
    2. for (double i = -2; i < 2; i++)
    3. {
    4. for (double j = -2; j < 2; j++)
    5. {
    6. Location blockLoc = new Location(loc.getWorld(), loc.getX() + i, loc.getY(), loc.getZ() + j);
    7. if (blockLoc.getBlock().getType().equals(Material.GLASS))
    8. {
    9. //Do code here
    10. }
    11. }
    12. }
    13.  
     
    CptnPummeluff likes this.
  6. Offline

    CptnPummeluff

  7. Offline

    ever_x

    Glad I could help :)
     
Thread Status:
Not open for further replies.

Share This Page