Advertisement
Guest User

Untitled

a guest
Mar 18th, 2021
548
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.73 KB | None | 0 0
  1. public class ExampleSurfaceBuilder extends SurfaceBuilder<TernarySurfaceConfig> {
  2.     public ExampleSurfaceBuilder(Codec<TernarySurfaceConfig> codec) {
  3.         super(codec);
  4.     }
  5.  
  6.     @Override
  7.     public void generate(Random random, Chunk chunk, Biome biome, int x, int z, int terrainHeight, double noise, BlockState defaultBlock, BlockState defaultFluid, int seaLevel, long seed, TernarySurfaceConfig config) {
  8.         this.buildSurface(random, chunk, biome, x, z, terrainHeight, noise, defaultBlock, defaultFluid, config.getTopMaterial(), config.getUnderMaterial(), config.getUnderwaterMaterial(), seaLevel);
  9.     }
  10.  
  11.     protected void buildSurface(Random random, Chunk chunk, Biome biome, int x, int z, int terrainHeight, double noise, BlockState defaultBlock, BlockState defaultFluid, BlockState topBlock, BlockState middleBlock, BlockState underwaterBlock, int seaLevel) {
  12.         BlockPos.Mutable mutable = new BlockPos.Mutable();
  13.         int depth = -1; // Will be used to know how deep we are in solid blocks so we know when to stop placing middleBlock
  14.         int scaledNoise = (int)(noise / 3.0D + 3.0D + random.nextDouble() * 0.25D);
  15.  
  16.         // Start at top land and loop downward
  17.         for(int y = terrainHeight; y >= 0; --y) {
  18.  
  19.             // Get the block in the world (Overworld will always give Air, Water, or Stone)
  20.             mutable.set(x, y, z);
  21.             BlockState currentBlockInWorld = chunk.getBlockState(mutable);
  22.  
  23.             // Reset the depth counter as we are not in land anymore
  24.             if (currentBlockInWorld.isAir()) {
  25.                 depth = -1;
  26.             }
  27.  
  28.             // Else if we are at liquid, we can use this to swap the default fluid in your biome
  29.             else if (!currentBlockInWorld.getFluidState().isEmpty()) {
  30.                 chunk.setBlockState(mutable,defaultFluid, false);
  31.             }
  32.  
  33.             // We are in solid land now. Skip Bedrock as we shouldn't replace that
  34.             else if (currentBlockInWorld.getBlock != Blocks.BEDROCK) {
  35.                 // -1 depth means we are switching from air to solid land. Place the surface block now
  36.                 if (depth == -1) {
  37.                     // Signal that depth is now on the surface so we can start placing middle blocks when moving down next loop.
  38.                     depth = 0;
  39.  
  40.                     // The typical normal dry surface of the biome.
  41.                     if(y >= seaLevel - 1){
  42.                         chunk.setBlockState(mutable, topBlock, false);
  43.                     }
  44.                     // Places middle block when starting to go under sealevel.
  45.                     // Think of this as the top block of the bottom of shallow lakes in your biome.
  46.                     else if(y >= seaLevel - scaledNoise - 7){
  47.                         chunk.setBlockState(mutable, middleBlock, false);
  48.                     }
  49.                     // Places the underwater block when really deep under sealevel instead.
  50.                     // This is like the top block of the sea floor.
  51.                     else{
  52.                         chunk.setBlockState(mutable, underwaterBlock, false);
  53.                     }
  54.                 }
  55.                 // Place block only when under surface and down to as deep as the scaledNoise says to go.
  56.                 else if (depth <= scaledNoise) {
  57.                     // Increment depth to keep track of how deep we have gone
  58.                     depth++;
  59.                     chunk.setBlockState(mutable, middleBlock, false);
  60.                 }
  61.                 // Place the default block if not placing top, middle, or underwater block anymore.
  62.                 else {
  63.                     chunk.setBlockState(mutable, defaultBlock, false);
  64.                 }
  65.             }
  66.         }
  67.     }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement