Guest User

Untitled

a guest
Aug 1st, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.29 KB | None | 0 0
  1. public class CustomSort implements Comparator<IBlockState> {
  2.     @Override
  3.     public int compare(IBlockState arg0, IBlockState arg1) {
  4.         return arg0.getBlock().toString().compareTo(arg1.getBlock().toString()); // Sort in alphebetical order.
  5.     }
  6. }
  7.  
  8. public class Generate implements IWorldGenerator {
  9.     int Min = 0;
  10.     int Max = 128;
  11.     int Range = Max-Min+1;
  12.     @Override
  13.     public void generate(Random random, int chunkX, int chunkZ, World world, IChunkGenerator chunkGenerator,
  14.             IChunkProvider chunkProvider) {
  15.         // TODO Auto-generated method stub
  16.         for (int X = 0; X < 16; X++)
  17.         {
  18.             for (int Z = 0; Z < 16; Z++)
  19.             {
  20.                 IBlockState[] Main = new IBlockState[Range]; // Main array. (Sorry if I say table on accident anywhere, used to Lua)
  21.                 List<IBlockState> B = new ArrayList<IBlockState>(); // Array for sorting blocks, so we leave lava, water, and air where they are.
  22.                 for (int Y = Min; Y < Max; Y++) // Min and max were originally to test because I thought it would take hours to generate.
  23.                 {
  24.                     Main[Y-Min] = chunkProvider.getLoadedChunk(chunkX, chunkZ).getBlockState(X, Y, Z); // Add the block to the array.
  25.                 }
  26.                 int Pos = 0;
  27.                 for (int I = 0; I < Range; I++) // Add blocks that are not water, lava, or air to the sorting array.
  28.                 {
  29.                     // This next like is some ugly hard-coding, but I really am not up to doing anything about it for the time being.
  30.                     if (Main[I] != null && Main[I].getBlock() != Blocks.AIR && Main[I].getBlock() != Blocks.WATER && Main[I].getBlock() != Blocks.LAVA)
  31.                     {
  32.                         B.add(Main[I]);
  33.                         Pos++; // Increment the position only after we know that it is a valid sortable block.
  34.                     }
  35.                 }
  36.                 Pos = 0;
  37.                 Collections.sort(B, new CustomSort()); // Sort it with the function above.
  38.                 for (int I = 0; I < Range; I++) // This loop puts the stuff back in the array Main.
  39.                 {
  40.                     // This preserves the structure and shape of liquids and air.
  41.                     if (Main[I] != null && Main[I].getBlock() != Blocks.AIR && Main[I].getBlock() != Blocks.WATER && Main[I].getBlock() != Blocks.LAVA) {
  42.                         Main[I] = B.get(Pos);
  43.                         Pos++;
  44.                     }
  45.                 }
  46.                 for (int Y = Min; Y < Max; Y++)
  47.                 {
  48.                     chunkProvider.getLoadedChunk(chunkX, chunkZ).setBlockState(new BlockPos(X, Y, Z), Main[Y-Min]); // Main contains IBlockStates, making this easy.
  49.                 }
  50.             }
  51.         }
  52.     }  
  53. }
Advertisement
Add Comment
Please, Sign In to add comment