Solved Block location

Discussion in 'Spigot Plugin Development' started by astaspasta, Apr 20, 2017.

  1. Hello i am trying to find a way to test if a (quartz block for example) is 2 blocks away from the player...
    I know how to test a block if its right next to them but i don't know how to test if the block is 2 blocks away from him...

    I have this so far...
    Code (Text):
                        if(player.getLocation().getBlock().getRelative(BlockFace.DOWN).getType() != Material.getMaterial(getConfig().getString("Portal1.block"))||
                                player.getLocation().getBlock().getRelative(BlockFace.EAST).getType() != Material.getMaterial(getConfig().getString("Portal1.block"))||
                                player.getLocation().getBlock().getRelative(BlockFace.WEST).getType() != Material.getMaterial(getConfig().getString("Portal1.block"))||
                                player.getLocation().getBlock().getRelative(BlockFace.NORTH).getType() != Material.getMaterial(getConfig().getString("Portal1.block"))||player.getLocation().getBlock().getRelative(BlockFace.SOUTH).getType() != Material.getMaterial(getConfig().getString("Portal1.block"))) {
                                    } else {
                                        player.sendMessage(getConfig().getString("Portal1.teleportmsg"));
                                       World p = Bukkit.getWorld(getConfig().getString("Portal1.world"));
                                       player.teleport(new Location(p, -232, 51, -8.5));
                                           
                                                   }
    And its what i told you blocks right next to him from all directions... but I want the block to be 2 blocks away from him...
    Please help...
     
  2. You could check blocks in a radius of 2 and then check if the type equals what you need
    like this:

    Code (Text):


    int radius = X;

    List<Material> materialsNearby = new ArrayList<Material>();

    for (int x = location.getBlockX() - r; x <= location.getBlockX() +r; x++) {
      for(int y = (location.getBlockY() - radius); y < (location.getBlockY() + radius); y++){
        for (int z = location.getBlockZ() - r; z <= location.getBlockZ() +r; z++) {
            double distance = (location.getBlockX() - x) * (location.getBlockX() - x) + (location.getBlockZ() - z) * (location.getBlockZ()- z) + ((location.getBlockY() - y) * (location.getBlockY() - y));

           if(distance < radius * radius){
               Location location = new Location(loc.getWorld(), x, y, z);
               materialsNearby.add(location.getBlock().getType());
           }

         }
      }
    }
     
    Now you just check if the materialsNearby contains your type that is needed.
     
  3. @Phloxz
    I am still a bit noob... so.... don't hate...


    I don't want the block to be in specific cordinates or in a random location inside a players radius i am looking on how to test if the block is exactly 2 blocks south from the player... I know its my fault because i didn't explained that well... because my english...... yeah...
     
  4. Ok? So? Get the block whose location is two units south of the player's location? Check it's type? Yikes.
     
  5. @Scarsz
    That's what i am looking for...
     
  6. Ok. Go look for it then. Asking aimlessly on Spigot about how to do some of the simplest shit with block manipulation without even trying to do it yourself or looking at the documentation is pathetic.
     
  7. Senmori

    Senmori Retired Resource Staff
    Retired Patron

    You were on the right track though. Have a look through the BlockFace enum. Then have a look at all the getRelative methods inside of Block.

    That should get you what you want.
     
  8. no worries, hmm, its a bit complicated so i spoonfeed you with a working code
    Code (Text):



    public static boolean existBlock(Player player,Material type,double radius,BlockFace direction){

        Location location = player.getLocation();
       
        double radiusSquared = radius * radius;

        for(double x = -radius; x <= radius; x++) {
            for (double y = -radius; y <= radius; y++) {
                for (double z = -radius; z <= radius; z++) {

                    if ((x * x) + (y * y) + (z * z) <= radiusSquared) {

                        Location loc = new Location(location.getWorld(), player.getLocation().getBlockX() + x, player.getLocation().getBlockY() + y, player.getLocation().getBlockZ() + z);

                        if (loc.getBlock().getType() == type) {

                            float dir = (float) Math.toDegrees(Math.atan2(player.getLocation().getBlockZ() - loc.getBlock().getZ(), loc.getBlock().getX() - player.getLocation().getBlockX()));

                            dir = dir % 360;

                            if (dir < 0) {
                                dir += 360;
                            }

                            dir = Math.round(dir / 45);

                            BlockFace face = null;

                            switch ((int) dir) {

                                case 0:
                                    face = BlockFace.WEST;
                                    break;
                                case 1:
                                    face = BlockFace.NORTH_WEST;
                                    break;
                                case 2:
                                    face = BlockFace.NORTH;
                                    break;
                                case 3:
                                    face = BlockFace.NORTH_EAST;
                                    break;
                                case 4:
                                    face = BlockFace.EAST;
                                    break;
                                case 5:
                                    face = BlockFace.SOUTH_EAST;
                                    break;
                                case 6:
                                    face = BlockFace.SOUTH;
                                    break;
                                case 7:
                                    face = BlockFace.SOUTH_WEST;
                                    break;

                            }

                            if (direction.equals(face)) {
                                return true;
                            }

                        }

                    }
                }
            }
        }

        return false;

    }


     
    You may ask how do i use this?

    It simply returns whether a player has a block nearby in the right radius or not.

    Just call this:

    Code (Text):


    boolean exist = existBlock(yourPlayer,Material.QUARTZ_BLOCK,2,BlockFace.SOUTH);

     
    Edit: Changed the code so it works faster
     
    #9 Phloxz, Apr 20, 2017
    Last edited: Apr 20, 2017
    • Like Like x 1
    • Optimistic Optimistic x 1
  9. Wished people'd spoonfeed me like that :D
     
    • Funny Funny x 1
    • Friendly Friendly x 1
  10. OMG i am so freaking stupid....
    I spent like 2 days searching for the simplest thing ever....
    ....
    Anyway thank you @Phloxz ....