verify if exist a block on certain region

Discussion in 'Spigot Plugin Development' started by JoKillerPT, Jun 10, 2018.

  1. how i do a thing to verify if exist any sponge for example on a certain reginon, for example check in radius of 21x21x21 exist any sponge, i dont need the cords of him only verify if exist
     
  2. First, get the location of the player
    Code (Text):
    Location loc = player.getLocation();
    Next, loop through all blocks in a 21x21x21 radius
    Code (Text):
    for(int i = 0; i <= 21; i++) {
        Location l = new Location(player.getWorld(), loc.getX() + i, loc.getY() + i, loc.getZ() + i);
        Location m =  new Location(player.getWorld(), loc.getX() - i, loc.getY() - i, loc.getZ() - i);

        if(l.getBlock().getType() == Material.SPONGE) do something + continue;
        if(m.getBlock().getType() == Material.SPONGE) do something + continue;
    }
    Full disclosure, the code probably isn't word for word correct, just my general idea first time reading it.
     
  3. that i want cant place two sponges on the same region
     
  4. If you don't want people to be able to place two sponges in the same 21x21x21 region, where i said "do something + continue;" make a counter, to count how many sponges there are. Then if it's already = 1 don't let them place another.
     
  5. Ah I realise now. You need a triple nested forloop. Fun.
    Code (Java):
    for(int x = 0; x <= 21; x++) {
        for(int y = 0; y <= 21; y++) {
            for(int z = 0; z <= 21; z++) {
                Location l = new Location(player.getWorld(), loc.getX() + x, loc.getY() + y, loc.getZ() + z);
                Location m = new Location(player.getWorld(), loc.getX() - x, loc.getY() - y, loc.getZ() - z);

                if(l.getBlock() etc etc etc
            }
        }
    }
     
  6. its not working him getting the message and not a sponge block near me


    Code (Java):
    for(int x = 0; x <= 21; x++) {
                    for(int y = 0; y <= 21; y++) {
                        for(int z = 0; z <= 21; z++) {
                            Location l = new Location(p1.getWorld(), loc.getX() + x, loc.getY() + y, loc.getZ() + z);
                            Location m = new Location(p1.getWorld(), loc.getX() - x, loc.getY() - y, loc.getZ() - z);

                            if(l.getBlock().getType() == Material.SPONGE) {
                                event.setCancelled(true);
                                p1.sendMessage("test");
                            }
                           
                            if(m.getBlock().getType() == Material.SPONGE) {
                                event.setCancelled(true);
                                p1.sendMessage("test");
                            }
                        }
                    }
                }
            }
     
  7. i think to p1 need to be a block