Maxlego08 icon

CavePopulor

Maxlego08 | PRO | 06/22/19 10:19:39 AM UTC | 0 ⭐ | 392 👁️ | Never ⏰ | []
Java |

3.47 KB

|

None

|

0 👍

/

0 👎

package fr.maxlego08.generation.popular;
 
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
 
import org.bukkit.Chunk;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.generator.BlockPopulator;
 
import fr.maxlego08.generation.utils.Position;
 
public class CavePopulor extends BlockPopulator {
    
    private void generateCave(World world, Random random, Chunk chunk, int x, int y, int z){
        
        List<Position> positions = new ArrayList<Position>();
        
         int r = random.nextInt(120) + 50;
         for(int a = 0; a != r; a++){
             
             int r2 = random.nextInt(5);
             int r3 = random.nextInt(20);
             
             if (r2 == 0){           
                 if (r3 <= 2) x--; 
                 if (r3 <= 6) x++; 
                 if (r3 <= 9) y--; 
                 if (r3 <= 12) y++; 
                 if (r3 <= 15) z--; 
                 if (r3 <= 20) z++;                  
             }           
             if (r2 == 1){           
                 if (r3 <= 2) y--; 
                 if (r3 <= 6) z++; 
                 if (r3 <= 9) x--; 
                 if (r3 <= 12) y++; 
                 if (r3 <= 15) x--; 
                 if (r3 <= 20) z++;                  
             }
             if (r2 == 2){           
                 if (r3 <= 2) y--; 
                 if (r3 <= 6) z++; 
                 if (r3 <= 9) z--; 
                 if (r3 <= 12) y++; 
                 if (r3 <= 15) x--; 
                 if (r3 <= 20) x++;                  
             }
             if (r2 == 3){           
                 if (r3 <= 2) x--; 
                 if (r3 <= 6) x++; 
                 if (r3 <= 9) z--; 
                 if (r3 <= 12) z++; 
                 if (r3 <= 15) y--; 
                 if (r3 <= 20) y++;                  
             }
             if (r2 >= 4){           
                 if (r3 <= 2) y--; 
                 if (r3 <= 6) z++; 
                 if (r3 <= 9) y--; 
                 if (r3 <= 12) y++; 
                 if (r3 <= 15) y--; 
                 if (r3 <= 20) x++;                  
             }
             positions.add(new Position(x, y, z));
         }
         
         positions.forEach(position -> {
             getPostions(world, random, chunk, position.getPosX(), position.getPosY(), position.getPosZ()).forEach(localPostion -> {
                 Block block = world.getBlockAt(localPostion.getPosX(), localPostion.getPosY(), localPostion.getPosZ());
                 if (!block.isEmpty() && !block.isLiquid() && !block.getType().equals(Material.BEDROCK)){
                     block.setType(Material.AIR);
                 }
             });
         });
         
    }
    
    @SuppressWarnings("deprecation")
    private List<Position> getPostions(World world, Random random, Chunk chunk, int x, int y, int z){
        List<Position> positions = new ArrayList<Position>();
        
        int a = 0;
        int b = random.nextInt(200) + 20;
        while(a <= b){
            
            int k = random.nextInt(50);
            if (k == 0) y++;
            else if (k == 5) y++;
            else if (random.nextBoolean()){
                if (random.nextBoolean()) x++;
                else z++;
            }else if (random.nextBoolean()) x--;
            else z--;
            
            if (world.getBlockTypeIdAt(x, y, z) != 0){
                int c = 1 + random.nextInt(3);
                int d = c * c + 1;
                for(int x2 = -c; x2 <= c; x2++){
                    for(int y2 = -c; y2 <= c; y2++){
                        for(int z2 = -c; z2 <= c; z2++){
                            if (world.getBlockTypeIdAt(x2, y2, z2) != 0) a++;
                            else{
                                
                                positions.add(new Position(x + x2, y + y2, z + z2));
                                
                            }
                        }
                    }
                }
            }else a++;
            
        }
        return positions;
    }
    
    public void populate(World world, Random random, Chunk chunk) {
        
        int a = random.nextInt(5000);
        int b = random.nextInt(5000);
        
        if (a > b){
            
            int x = 4 + random.nextInt(8) + chunk.getX() * 16;
            int z = 4 + random.nextInt(8) + chunk.getX() * 16;
            int y = world.getHighestBlockYAt(x, z);
            if (y <= 16) y = 32;
            int newY = random.nextInt(y);
            generateCave(world, random, chunk, x, newY, z);
            
        }
        
        
        
    }
 
}

Comments