Schematic Based World Generator

Discussion in 'Plugin Development' started by md_5, Jun 28, 2012.

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

    md_5

    Well guys I have an issue, somebody has tasked me with the goal of creating a world generator that takes a 4*4 chunk (exactly sized) schematic and tile it all over the world.

    The issue is I have never done world generation and have no idea how to do this. The only progress I have made so far is creating the actual schematic and using:
    Code:
    CuboidClipBoard schematic = SchematicFormat.MCEDIT.load(new File(Plugin.instance.getDataFolder(), "Plots.schematic"));
    To load it.

    Help would really be appreciated.

    codename_B

    EDIT:

    Code:java
    1. package com.md_5.levelup.plots;
    2.  
    3. import com.sk89q.worldedit.BlockVector;
    4. import com.sk89q.worldedit.CuboidClipboard;
    5. import com.sk89q.worldedit.blocks.BaseBlock;
    6. import com.sk89q.worldedit.data.DataException;
    7. import com.sk89q.worldedit.schematic.SchematicFormat;
    8. import java.io.File;
    9. import java.io.IOException;
    10. import java.util.Random;
    11. import org.bukkit.World;
    12. import org.bukkit.generator.ChunkGenerator;
    13.  
    14. public class PlotGenerator extends ChunkGenerator {
    15.  
    16. private final CuboidClipboard schematic;
    17.  
    18. public PlotGenerator() throws DataException, IOException {
    19. schematic = SchematicFormat.MCEDIT.load(new File(Plugin.instance.getDataFolder(), "Plots.schematic"));
    20. }
    21.  
    22. @Override
    23. public byte[][] generateBlockSections(World world, Random random, int cx, int cz, BiomeGrid biomes) {
    24. byte[][] result = new byte[world.getMaxHeight() / 16][];
    25. //
    26. int xRegion;
    27. if (cx % 2 == 0) {
    28. xRegion = 0;
    29. } else {
    30. xRegion = 1;
    31. }
    32. //
    33. int zRegion;
    34. if (cz % 2 == 0) {
    35. zRegion = 0;
    36. } else {
    37. zRegion = 1;
    38. }
    39. //
    40. for (int x = 0; x < 16; x++) {
    41. int xBlock = (xRegion * 16) + x;
    42. //
    43. for (int z = 0; z < 16; z++) {
    44. //
    45. int zBlock = (zRegion * 16) + z;
    46. //
    47. for (int y = 0; y < world.getMaxHeight(); y++) {
    48. try {
    49. BaseBlock block = schematic.getPoint(new BlockVector(xBlock, y, zBlock));
    50. setBlock(result, x, y, z, (byte) block.getType());
    51. //
    52. }
    53. }
    54. }
    55. }
    56. return result;
    57. }
    58.  
    59. private void setBlock(byte[][] result, int x, int y, int z, byte blkid) {
    60. if (result[y >> 4] == null) {
    61. result[y >> 4] = new byte[4096];
    62. }
    63. result[y >> 4][((y & 0xF) << 8) | (z << 4) | x] = blkid;
    64. }
    65. }
    66.  


    The above seems to generate part of the plots, ie 2/4 chunks are correct, I think.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 26, 2016
  2. as looking to the ode you posted, it generate schunks at an area of 2X2 chunks, but at the forum thread, you said 4X4, what is correct?
     
  3. Offline

    md_5

    [​IMG]

    Odd, my checkerboard test works just fine.

    Sorry meant 2*2
     
  4. you are ignoring a IndexOutOfBound exception, mostly, these indicate bugs, can you verify if its correct?
     
  5. Offline

    md_5

    Sorry I derped, I do mean 4*4. Wonder how to fix..

    Code:java
    1. package com.md_5.levelup.plots;
    2.  
    3. import com.sk89q.worldedit.BlockVector;
    4. import com.sk89q.worldedit.CuboidClipboard;
    5. import com.sk89q.worldedit.blocks.BaseBlock;
    6. import com.sk89q.worldedit.data.DataException;
    7. import com.sk89q.worldedit.schematic.SchematicFormat;
    8. import java.io.File;
    9. import java.io.IOException;
    10. import java.util.Random;
    11. import org.bukkit.World;
    12. import org.bukkit.block.Biome;
    13. import org.bukkit.generator.ChunkGenerator;
    14.  
    15. public class PlotGenerator extends ChunkGenerator {
    16.  
    17. private final CuboidClipboard schematic;
    18.  
    19. public PlotGenerator() throws DataException, IOException {
    20. schematic = SchematicFormat.MCEDIT.load(new File(Plugin.instance.getDataFolder(), "Plots.schematic"));
    21. }
    22.  
    23. @Override
    24. public byte[][] generateBlockSections(World world, Random random, int cx, int cz, BiomeGrid biomes) {
    25. //
    26. byte[][] result = new byte[world.getMaxHeight() / 16][];
    27. //
    28. int xRegion = cx % 4;
    29. //
    30. int zRegion = cz % 4;
    31. //
    32. for (int x = 0; x < 16; x++) {
    33. int xBlock = (xRegion * 16) + x;
    34. //
    35. for (int z = 0; z < 16; z++) {
    36. //
    37. int zBlock = (zRegion * 16) + z;
    38. //
    39. for (int y = 0; y < world.getMaxHeight(); y++) {
    40. //
    41. try {
    42. BaseBlock block = schematic.getPoint(new BlockVector(xBlock, y, zBlock));
    43. setBlock(result, x, y, z, (byte) block.getType());
    44. //
    45. }
    46. //
    47. biomes.setBiome(x, z, Biome.PLAINS);
    48. }
    49. }
    50. }
    51. return result;
    52. }
    53.  
    54. private void setBlock(byte[][] result, int x, int y, int z, byte blkid) {
    55. if (result[y >> 4] == null) {
    56. result[y >> 4] = new byte[4096];
    57. }
    58. result[y >> 4][((y & 0xF) << 8) | (z << 4) | x] = blkid;
    59. }
    60. }
    61.  


    Done, I just do xPos = cx % 4;

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 26, 2016
Thread Status:
Not open for further replies.

Share This Page