Maxlego08

CavePopulor

Jun 22nd, 2019
393
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.47 KB | None | 0 0
  1. package fr.maxlego08.generation.popular;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.Random;
  6.  
  7. import org.bukkit.Chunk;
  8. import org.bukkit.Material;
  9. import org.bukkit.World;
  10. import org.bukkit.block.Block;
  11. import org.bukkit.generator.BlockPopulator;
  12.  
  13. import fr.maxlego08.generation.utils.Position;
  14.  
  15. public class CavePopulor extends BlockPopulator {
  16.    
  17.     private void generateCave(World world, Random random, Chunk chunk, int x, int y, int z){
  18.        
  19.         List<Position> positions = new ArrayList<Position>();
  20.        
  21.          int r = random.nextInt(120) + 50;
  22.          for(int a = 0; a != r; a++){
  23.              
  24.              int r2 = random.nextInt(5);
  25.              int r3 = random.nextInt(20);
  26.              
  27.              if (r2 == 0){           
  28.                  if (r3 <= 2) x--;
  29.                  if (r3 <= 6) x++;
  30.                  if (r3 <= 9) y--;
  31.                  if (r3 <= 12) y++;
  32.                  if (r3 <= 15) z--;
  33.                  if (r3 <= 20) z++;                  
  34.              }           
  35.              if (r2 == 1){           
  36.                  if (r3 <= 2) y--;
  37.                  if (r3 <= 6) z++;
  38.                  if (r3 <= 9) x--;
  39.                  if (r3 <= 12) y++;
  40.                  if (r3 <= 15) x--;
  41.                  if (r3 <= 20) z++;                  
  42.              }
  43.              if (r2 == 2){           
  44.                  if (r3 <= 2) y--;
  45.                  if (r3 <= 6) z++;
  46.                  if (r3 <= 9) z--;
  47.                  if (r3 <= 12) y++;
  48.                  if (r3 <= 15) x--;
  49.                  if (r3 <= 20) x++;                  
  50.              }
  51.              if (r2 == 3){           
  52.                  if (r3 <= 2) x--;
  53.                  if (r3 <= 6) x++;
  54.                  if (r3 <= 9) z--;
  55.                  if (r3 <= 12) z++;
  56.                  if (r3 <= 15) y--;
  57.                  if (r3 <= 20) y++;                  
  58.              }
  59.              if (r2 >= 4){           
  60.                  if (r3 <= 2) y--;
  61.                  if (r3 <= 6) z++;
  62.                  if (r3 <= 9) y--;
  63.                  if (r3 <= 12) y++;
  64.                  if (r3 <= 15) y--;
  65.                  if (r3 <= 20) x++;                  
  66.              }
  67.              positions.add(new Position(x, y, z));
  68.          }
  69.          
  70.          positions.forEach(position -> {
  71.              getPostions(world, random, chunk, position.getPosX(), position.getPosY(), position.getPosZ()).forEach(localPostion -> {
  72.                  Block block = world.getBlockAt(localPostion.getPosX(), localPostion.getPosY(), localPostion.getPosZ());
  73.                  if (!block.isEmpty() && !block.isLiquid() && !block.getType().equals(Material.BEDROCK)){
  74.                      block.setType(Material.AIR);
  75.                  }
  76.              });
  77.          });
  78.          
  79.     }
  80.    
  81.     @SuppressWarnings("deprecation")
  82.     private List<Position> getPostions(World world, Random random, Chunk chunk, int x, int y, int z){
  83.         List<Position> positions = new ArrayList<Position>();
  84.        
  85.         int a = 0;
  86.         int b = random.nextInt(200) + 20;
  87.         while(a <= b){
  88.            
  89.             int k = random.nextInt(50);
  90.             if (k == 0) y++;
  91.             else if (k == 5) y++;
  92.             else if (random.nextBoolean()){
  93.                 if (random.nextBoolean()) x++;
  94.                 else z++;
  95.             }else if (random.nextBoolean()) x--;
  96.             else z--;
  97.            
  98.             if (world.getBlockTypeIdAt(x, y, z) != 0){
  99.                 int c = 1 + random.nextInt(3);
  100.                 int d = c * c + 1;
  101.                 for(int x2 = -c; x2 <= c; x2++){
  102.                     for(int y2 = -c; y2 <= c; y2++){
  103.                         for(int z2 = -c; z2 <= c; z2++){
  104.                             if (world.getBlockTypeIdAt(x2, y2, z2) != 0) a++;
  105.                             else{
  106.                                
  107.                                 positions.add(new Position(x + x2, y + y2, z + z2));
  108.                                
  109.                             }
  110.                         }
  111.                     }
  112.                 }
  113.             }else a++;
  114.            
  115.         }
  116.         return positions;
  117.     }
  118.    
  119.     public void populate(World world, Random random, Chunk chunk) {
  120.        
  121.         int a = random.nextInt(5000);
  122.         int b = random.nextInt(5000);
  123.        
  124.         if (a > b){
  125.            
  126.             int x = 4 + random.nextInt(8) + chunk.getX() * 16;
  127.             int z = 4 + random.nextInt(8) + chunk.getX() * 16;
  128.             int y = world.getHighestBlockYAt(x, z);
  129.             if (y <= 16) y = 32;
  130.             int newY = random.nextInt(y);
  131.             generateCave(world, random, chunk, x, newY, z);
  132.            
  133.         }
  134.        
  135.        
  136.        
  137.     }
  138.  
  139. }
Advertisement
Add Comment
Please, Sign In to add comment