Flat square

Discussion in 'Plugin Development' started by Deleted user, Aug 31, 2012.

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

    Deleted user

    I need to generate a flat (5x5) square of lets say grass? How would I do this..?
     
  2. Offline

    sternmin8or

    relative to a player?
     
  3. Offline

    Deleted user

    At a specified location, for example:

    Location l = Bukkit.getServer().getWorld("world").getSpawnLocation();
     
  4. Offline

    WarmakerT

  5. just pseudo code:
    Code:
    for(int x = 0; x < 5; x++) {
        for(int z = 0; z < 5; z++) {
            Location l = new Location(x, spawn.getY(), z, world);
            world.getBlockAt(l).setType(Material.DIRT);
        }
    }
    something like that, you just need to figure out where to get the y coordinate and the world from, but this is how to do it i think:)
     
  6. Offline

    sternmin8or

    Code:java
    1.  
    2. y = l.getY()
    3. for(int x = l.getX()-2;i<l.getX()+3;x++){
    4. for(int z = l.getZ()-2;i<l.getZ()-3;z++){
    5. getServer().getWorld().getBlockAt(x,y,z).setType(Material.GRASS)
    6. }
    7. }
    8.  

    edit: too slow, but be careful about declaring variables inside loops, it takes up a lot of resources. I think my way is the most efficient (but i could be wrong)
     
  7. Offline

    Deleted user

    Considering getBlockAt requires int's I had to change getY / getZ to getBlockY/getBlockZ and what exactly is 'i' suppose to represent?

    Code:java
    1.  
    2. int y = l.getBlockY();
    3. for (int x = l.getBlockX() - 2; i < l.getX() + 3; x++) {
    4. for (int z = l.getBlockZ() - 2; i < l.getZ() - 3; z++) {
    5. Bukkit.getServer().getWorld("world").getBlockAt(x, y, z).setType(Material.NETHER_BRICK);
    6. }
    7. }
    8.  
     
  8. Offline

    sternmin8or

    oops, the is are supposed to be x and z respectively (sorry habit). You dont use getY(), x,y, and z are allready integers defined by the for loops
     
  9. Offline

    Deleted user

    I really can't understand what your saying with your grammar.
     
  10. Offline

    sternmin8or

    The "i"s are supposed to be x and z respectively (sorry, it is a habit of mine). You do not in fact need to use getY() because "x","y", and "z" are all integers defined in the "for" loops.

    Edit: ignore the second sentence, I read your post wrong.
     
  11. Offline

    Deleted user

    Okay so...

    Code:java
    1.  
    2. int y = l.getBlockY();
    3. for (int x = l.getBlockX() - 2; l.getBlockX() < l.getX() + 3; x++) {
    4. for (int z = l.getBlockZ() - 2; l.getBlockZ() < l.getZ() - 3; z++) {
    5. Bukkit.getServer().getWorld("world").getBlockAt(x, y, z).setType(Material.NETHER_BRICK);
    6. }
    7. }
    8.  
     
  12. Offline

    sternmin8or

    no, just x and z, not l.getBlockX(). Also, change all get to getBlock.
     
  13. Offline

    Deleted user

    So:

    Code:
    int y = l.getBlockY();
    for (int x = l.getBlockX() - 2; x < l.getX() + 3; x++) {
    for (int z = l.getBlockZ() - 2; z < l.getZ() - 3; z++) {
    Bukkit.getServer().getWorld("world").getBlockAt(x, y, z).setType(Material.NETHER_BRICK);
    }
    }
    
     
  14. Offline

    sternmin8or

    lol almost, sorry about all the miscommunication. Change : l.getX() to l.getBlockX() and l.getZ() to l.getBlockZ()
     
  15. Offline

    Deleted user

    It's alright I appriciate the help, anyways this:

    Code:
    int y = l.getBlockY();
    for (int x = l.getBlockX() - 2; x < l.getBlockX() + 3; x++) {
    for (int z = l.getBlockZ() - 2; z < l.getBlockZ() - 3; z++) {
    Bukkit.getServer().getWorld("world").getBlockAt(x, y, z).setType(Material.NETHER_BRICK);
    }
    }
    
    Doesn't do anything..
     
  16. Offline

    sternmin8or

    Where do you have it?
     
  17. Offline

    Deleted user

    In a public void. When I call upon the void everything else in the void happens just not that.
     
  18. Offline

    sternmin8or

    are you sure you are looking at the right area. Where is your location l?
     
  19. Offline

    Deleted user

    It's

    Bukkit.getServer().getWorld("world).getSpawnLocation();
     
  20. Offline

    sternmin8or

    and you dont have any sort of spawn location mod?
     
  21. Offline

    calebbfmv

     
  22. Offline

    Deleted user

    Bump - still don't got this figured out.
     
  23. Offline

    calebbfmv

    Make sure you have the have registered it in the onEnable, and are there any errors in the console?
     
  24. Offline

    kyle1320

    I realize you guys have this all figured out, just thought I'd post some code from a mod I made which allows the size to be configurable and whatnot, it spawns a platform underneath the player but this can obviously be changed to anywhere:
    Code:
    Location loc = player.getLocation();
    Integer width = (**width** / 2) * 2 + 1; // rounds the width up to the closest odd number
    Material mat = **material**;
    Block block = loc.getBlock().getRelative(0, -1, 0); // block under the player
    Block[] newBlocks = getPlatform(width, block); // every block in their platform
     
    for (Block b : newBlocks) {
        b.setType(mat); // set each block in the platform to the material
    }
     
    public Block[] getPlatform(Integer width, Block center) {
     
        Block corner = center.getRelative(width / 2, 0, width / 2); //corner block of their platform
        Block[] newBlocks = new Block[(int)Math.pow(width, 2)]; // width squared
     
        for (int i=0; i<width; i++) {
            for (int j=0; j<width; j++) {
                newBlocks[i * width + j] = corner.getRelative(-i, 0, -j);
            }
        }
     
        return newBlocks;
    }
     
Thread Status:
Not open for further replies.

Share This Page