Solved how to check blocks around player

Discussion in 'Plugin Development' started by crzytlp, Jul 21, 2014.

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

    crzytlp

    Hi! i was wondering how i can check blocks around player. Like this photo...

    I just asking how can i . i dont want the code . Thanks for helping...

    2014-07-21_15.23.07.png
     
  2. Offline

    xmarinusx

    You can do this by using a for loop. If you want to check only the x and z direction use 2 for loops. Start at one corner and keep adding the x and z coördinate to that corner location (location.add();)
     
  3. Offline

    Traks

    Simply get the player's location and then:
    • Add 3 to its X-coordinate
    • or subtract 3 from its X-coordinate
    • or add 3 to its Z-coordinate
    • or subtract 3 from its Z-coordinate
     
  4. Offline

    Phantom_64

    crzytlp
    You can use:
    Code:java
    1. loc.add(x, y, z);
    Or:
    Code:java
    1. loc.subtract(x, y, z);
    These methods return locations whose co-ordinates are greater then or lesser than the co-ordinates of loc by the amount specified.

    EDIT: Much easier than the methods specified above this post.
     
  5. Offline

    xmarinusx

    Traks
    It's indeed possible to use that way, but it could get pretty messy when you're going to check a lot of blocks. Using a for loop you'll keep it more clear.
     
  6. You could first get the block of the player:
    Code:
    Block pBlock = player.getLocation().getBlock();
    
    Then you could use the getRelative() method to get the ones to the right, left, infront, behind.
    Code:
    Block northBlock = pBlock.getRelative(BlockFace.NORTH);
    Block southBlock = pBlock.getRelative(BlockFace.SOUTH);
    Block westBlock = pBlock.getRelative(BlockFace.WEST);
    Block eastBlock = pBlock.getRelative(BlockFace.EAST);
    
    If you want to add spacings (e.g. 1 block gap), you could have a for loop.
    Code:
    Block northBlock = pBlock.getRelative(BlockFace.NORTH);
    Block southBlock = pBlock.getRelative(BlockFace.SOUTH);
    Block westBlock = pBlock.getRelative(BlockFace.WEST);
    Block eastBlock = pBlock.getRelative(BlockFace.EAST);
    int spacing = 1;
    for (int i = 0; i < spacing; i++) {
        northBlock = northBlock.getRelative(BlockFace.NORTH);
        southBlock = southBlock.getRelative(BlockFace.SOUTH);
        westBlock = westBlock.getRelative(BlockFace.WEST);
        eastBlock = eastBlock.getRelative(BlockFace.EAST);
    }
    
    Or... you could just add <gap> to pBlock.getLocation() each time, add then use .getBlock().getRelative().
     
    TheMintyMate likes this.
  7. Offline

    Traks

    xmarinusx In the OP he only asked how to check those 4 blocks though. Utilising for-loops would be excessive in my opinion and would disguise what blocks you actually want to obtain... But yes you're correct, if he wants to check all blocks between two points in 3-D space, he shouldn't definitely use a loop to iterate over them.
     
    xmarinusx likes this.
  8. Offline

    xmarinusx

    He wants the blocks that are further away, using a for loop would be easier then in my opinion.
     
  9. Offline

    Necrodoom

    Er, no, it wont.
    Its like saying that using a loop to do 5 times add 1 to an int is simpler than int + 5.
    Using location.add works fine here.
     
    KingFaris11 likes this.
  10. True, but look at the bottom of my code.
     
  11. Offline

    xmarinusx

    KingFaris11 Necrodoom
    "Hi! i was wondering how i can check blocks around player. Like this photo..." is probably what confused me :confused:
    If only those 4 blocks need to be checked I wouldn't use a for loop indeed ;)
     
  12. Offline

    crzytlp

    Thanks for all reply :)


    blocks can be more :) not just 4
    ---------------------------
    yeah i will use for loop but i dont know which methods i have to use .i tried this but isnt work...

    Code:java
    1.  
    2. ...
    3. if( player.getLocation().getBlock().getRelative(BlockFace.NORTH).getMaterial == Material.LAPIS_BLOCK )
    4. {
    5. //codes
    6. }
    7. ...
    8.  
     
    KingFaris11 likes this.
  13. Offline

    xmarinusx

    Replace '.getMaterial' with '.getType()' and it should work.
     
  14. Offline

    crzytlp

    Code:java
    1. @EventHandler
    2. public void onPlayerRitual(PlayerDropItemEvent event)
    3. {
    4. Player player = event.getPlayer();
    5. Location loc = player.getLocation();
    6.  
    7. loc = loc.add(3, 0, 3);
    8. player.sendMessage("Drop event work");//For debug
    9. for(int i=0; i < 7 ; i++)
    10. {
    11. for(int j =0; j < 7 ; j++ )
    12. if( loc.getBlock().getType() == Material.LAPIS_BLOCK )
    13. {
    14. player.sendMessage("for loop work");//For debug
    15.  
    16. loc = loc.add(0,0,1); // Just debug for z coordinate
    17. }
    18. }
    19. }


    Where is wrong ? its not say "for loop work". And not work. This is litte testing code. If this work i will write other pieces

    xmarinusx Thanks for getTytpe() KingFaris11 Traks
     
  15. Offline

    stonar96

    crzytlp Does it say "Drop event work"? Mabe it doesn't say "for loop work" because there is no LAPIS_BLOCK. And I would compare materials with .equals();
     
  16. Offline

    Necrodoom

    stonar96 Comparing enum works fine with ==, as well as being null safe.
     
  17. Offline

    stonar96

    Necrodoom ok, I said I would. But you are right ;)
     
  18. Offline

    crzytlp


    Yes you are right :)) its need be wool:) and some changes in code :) İts perfect now

    Code:java
    1. @EventHandler
    2. public void onPlayerRitual(PlayerDropItemEvent event)
    3. {
    4. Player player = event.getPlayer();
    5. Location loc = player.getLocation();
    6. int temp = 0;
    7.  
    8. loc = loc.add(3, 0, 3);
    9. player.sendMessage("Drop event work");//For debug
    10. player.sendMessage(loc.getX() + " " + loc.getZ());
    11.  
    12. for(int i=0; i < 7 ; i++)
    13. {
    14. for(int j =0; j < 7 ; j++ )
    15. {
    16. if( loc.getBlock().getType() == Material.WOOL )
    17. {
    18. player.sendMessage(i + " " + j);//For debug
    19. temp++;
    20. }
    21.  
    22. loc = loc.add(0,0,-1);
    23. }
    24.  
    25. loc = loc.add(-1,0,7);
    26. }
    27.  
    28. player.sendMessage("Blocks :" + temp);//For debug
    29. temp = 0;
    30. }
     
Thread Status:
Not open for further replies.

Share This Page