Generates ores in ChunkGenerator

Blocks cannot be set in populators so no ores were being generated. So I
moved ore generstion into the chunk generator.

WIP: Also, all entities (except Shulkers) are dying instantly because blocks are not being set to air in the entity populator. This needs fixing still.
This commit is contained in:
tastybento 2019-02-01 16:49:46 -08:00
parent 25bdaea000
commit 0d73188e6d
4 changed files with 210 additions and 378 deletions

1
.gitignore vendored
View File

@ -21,3 +21,4 @@
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
/target/

View File

@ -1,16 +1,22 @@
package world.bentobox.caveblock.generators;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.World.Environment;
import org.bukkit.generator.BlockPopulator;
import org.bukkit.generator.ChunkGenerator;
import java.util.*;
import world.bentobox.bentobox.util.Pair;
import world.bentobox.caveblock.CaveBlock;
import world.bentobox.caveblock.Settings;
import world.bentobox.caveblock.generators.populators.EntitiesPopulator;
import world.bentobox.caveblock.generators.populators.MaterialPopulator;
/**
@ -21,190 +27,223 @@ import world.bentobox.caveblock.generators.populators.MaterialPopulator;
*/
public class ChunkGeneratorWorld extends ChunkGenerator
{
// ---------------------------------------------------------------------
// Section: Constructor
// ---------------------------------------------------------------------
// ---------------------------------------------------------------------
// Section: Constructor
// ---------------------------------------------------------------------
/**
* @param addon - CaveBlock object
*/
public ChunkGeneratorWorld(CaveBlock addon)
{
super();
this.addon = addon;
this.settings = addon.getSettings();
this.blockPopulators = new ArrayList<>(1);
this.blockPopulators.add(new EntitiesPopulator(this.addon));
// Set up chances
chances = new HashMap<>();
// Normal
chances.put(Environment.NORMAL, new Chances(this.getMaterialMap(this.settings.getNormalBlocks()), this.settings.getNormalMainBlock()));
// Nether
chances.put(Environment.NETHER, new Chances(this.getMaterialMap(this.settings.getNetherBlocks()), this.settings.getNetherMainBlock()));
// End
chances.put(Environment.THE_END, new Chances(this.getMaterialMap(this.settings.getEndBlocks()), this.settings.getEndMainBlock()));
// Other settings
worldHeight = this.settings.getWorldDepth() - 1;
}
/**
* @param addon - CaveBlock object
*/
public ChunkGeneratorWorld(CaveBlock addon)
{
super();
this.addon = addon;
this.settings = addon.getSettings();
this.blockPopulators = new ArrayList<>(1);
this.blockPopulators.add(new MaterialPopulator(this.addon));
this.blockPopulators.add(new EntitiesPopulator(this.addon));
}
// ---------------------------------------------------------------------
// Section: Methods
// ---------------------------------------------------------------------
// ---------------------------------------------------------------------
// Section: Methods
// ---------------------------------------------------------------------
/**
* This method sets if given coordinates can be set as spawn location
*/
@Override
public boolean canSpawn(World world, int x, int z)
{
return true;
}
/**
* This method sets if given coordinates can be set as spawn location
*/
@Override
public boolean canSpawn(World world, int x, int z)
{
return true;
}
/**
* This method generates given chunk.
* @param world World where chunk must be generated.
* @param random Random that allows define object randomness.
* @param chunkX Chunk X coordinate.
* @param chunkZ Chunk Z coordinate.
* @param biomeGrid BiomeGrid that contains biomes.
* @return new ChunkData for given chunk.
*/
@Override
public ChunkData generateChunkData(World world,
Random random,
int chunkX,
int chunkZ,
ChunkGenerator.BiomeGrid biomeGrid)
{
ChunkData result = this.createChunkData(world);
/**
* This method generates given chunk.
* @param world World where chunk must be generated.
* @param random Random that allows define object randomness.
* @param chunkX Chunk X coordinate.
* @param chunkZ Chunk Z coordinate.
* @param biomeGrid BiomeGrid that contains biomes.
* @return new ChunkData for given chunk.
*/
@Override
public ChunkData generateChunkData(World world,
Random random,
int chunkX,
int chunkZ,
ChunkGenerator.BiomeGrid biomeGrid)
{
ChunkData result = this.createChunkData(world);
int maxHeight = Math.min(worldHeight, world.getMaxHeight());
// Fill all blocks
result.setRegion(0, 1, 0,
16, maxHeight, 16,
chances.get(world.getEnvironment()).mainMaterial);
// Populate chunk with necessary information
if (world.getEnvironment().equals(World.Environment.NETHER))
{
this.populateNetherChunk(result);
}
else if (world.getEnvironment().equals(World.Environment.THE_END))
{
this.populateTheEndChunk(result);
}
else
{
this.populateOverWorldChunk(result, biomeGrid);
}
// Generate ground and ceiling.
result.setRegion(0, 0, 0,
16, 1, 16,
this.settings.isNormalFloor() ? Material.BEDROCK : chances.get(world.getEnvironment()).mainMaterial);
result.setRegion(0, worldHeight - 1, 0,
16, maxHeight, 16,
this.settings.isNormalRoof() ? Material.BEDROCK : chances.get(world.getEnvironment()).mainMaterial);
return result;
}
// Set biome
for (int x = 0; x < 16; x++)
{
for (int z = 0; z < 16; z++)
{
biomeGrid.setBiome(x, z, this.settings.getDefaultBiome());
}
}
// Create ores
generateOres(world, random, result, maxHeight);
return result;
}
private void generateOres(World world, Random random, ChunkData result, int maxHeight)
{
for (Map.Entry<Material, Pair<Integer, Integer>> entry : chances.get(world.getEnvironment()).materialChanceMap.entrySet())
{
for (int subY = 1; subY < maxHeight; subY += 16)
{
if (random.nextInt(100) < entry.getValue().x)
{
int x = random.nextInt(15);
int z = random.nextInt(15);
int y = Math.min(maxHeight - 2, subY + random.nextInt(15));
for (int packSize = 0; packSize < entry.getValue().z; packSize++)
{
result.setBlock(x,y,z,entry.getKey());
// The direction chooser
switch (random.nextInt(5))
{
case 0:
x = Math.min(15, x + 1);
break;
case 1:
y = Math.min(maxHeight - 2, y + 1);
break;
case 2:
z = Math.min(15, z + 1);
break;
case 3:
x = Math.max(0, x - 1);
break;
case 4:
y = Math.max(1, y - 1);
break;
case 5:
z = Math.max(0, z - 1);
break;
}
}
}
}
}
}
/**
* This method populates The End world chunk data.
* @param chunkData ChunkData that must be populated.
*/
private void populateTheEndChunk(ChunkData chunkData)
{
// because everything starts at 0 and ends at 255
final int worldHeight = this.settings.getWorldDepth();
// Fill all blocks
chunkData.setRegion(0, 1, 0,
16, worldHeight - 1, 16,
this.settings.getEndMainBlock());
/**
* This method set world block populators.
* @param world World where this must apply.
* @return List with block populators.
*/
@Override
public List<BlockPopulator> getDefaultPopulators(final World world)
{
return this.blockPopulators;
}
// Generate ground and ceiling.
chunkData.setRegion(0, 0, 0,
16, 1, 16,
this.settings.isEndFloor() ? Material.BEDROCK : this.settings.getEndMainBlock());
chunkData.setRegion(0, worldHeight - 1, 0,
16, worldHeight, 16,
this.settings.isEndRoof() ? Material.BEDROCK : this.settings.getEndMainBlock());
}
/**
* This method returns material frequently and pack size map.
* @param objectList List with objects that contains data.
* @return Map that contains material, its rarity and pack size.
*/
private Map<Material, Pair<Integer, Integer>> getMaterialMap(List<String> objectList)
{
Map<Material, Pair<Integer, Integer>> materialMap = new HashMap<>(objectList.size());
// wrong material object.
objectList.stream().
filter(object -> object.startsWith("MATERIAL")).
map(object -> object.split(":")).
filter(splitString -> splitString.length == 4).
forEach(splitString -> {
Material material = Material.getMaterial(splitString[1]);
if (material != null)
{
materialMap.put(material,
new Pair<>(Integer.parseInt(splitString[2]), Integer.parseInt(splitString[3])));
}
});
return materialMap;
}
// ---------------------------------------------------------------------
// Section: Variables
// ---------------------------------------------------------------------
/**
* This method populates nether world chunk data.
* @param chunkData ChunkData that must be populated.
*/
private void populateNetherChunk(ChunkData chunkData)
{
// because everything starts at 0 and ends at 255
final int worldHeight = this.settings.getWorldDepth();
/**
* CaveBlock addon.
*/
private CaveBlock addon;
// Fill all blocks
chunkData.setRegion(0, 1, 0,
16, worldHeight - 1, 16,
this.settings.getNetherMainBlock());
/**
* Addon settings.
*/
private Settings settings;
// Generate ground and ceiling.
chunkData.setRegion(0, 0, 0,
16, 1, 16,
this.settings.isNetherFloor() ? Material.BEDROCK : this.settings.getNetherMainBlock());
chunkData.setRegion(0, worldHeight - 1, 0,
16, worldHeight, 16,
this.settings.isNetherRoof() ? Material.BEDROCK : this.settings.getNetherMainBlock());
}
private Map<Environment, Chances> chances;
private int worldHeight;
/**
* This method populates Over world chunk data.
* @param chunkData ChunkData that must be populated.
* @param biomeGrid BiomeGrid for this chunk.
*/
private void populateOverWorldChunk(ChunkData chunkData, BiomeGrid biomeGrid)
{
// because everything starts at 0 and ends at 255
final int worldHeight = this.settings.getWorldDepth();
/**
* This list contains block populators that will be applied after chunk is generated.
*/
private List<BlockPopulator> blockPopulators;
// Fill all blocks
chunkData.setRegion(0, 1, 0,
16, worldHeight - 1, 16,
this.settings.getNormalMainBlock());
// Private class
/**
* Chances class to store chances for environments and main material
*
*/
private class Chances {
final Map<Material, Pair<Integer, Integer>> materialChanceMap;
final Material mainMaterial;
// Generate ground and ceiling.
chunkData.setRegion(0, 0, 0,
16, 1, 16,
this.settings.isNormalFloor() ? Material.BEDROCK : this.settings.getNormalMainBlock());
chunkData.setRegion(0, worldHeight - 1, 0,
16, worldHeight, 16,
this.settings.isNormalRoof() ? Material.BEDROCK : this.settings.getNormalMainBlock());
// Set biome
for (int x = 0; x < 16; x++)
{
for (int z = 0; z < 16; z++)
{
biomeGrid.setBiome(x, z, this.settings.getDefaultBiome());
}
}
}
/**
* This method set world block populators.
* @param world World where this must apply.
* @return List with block populators.
*/
@Override
public List<BlockPopulator> getDefaultPopulators(final World world)
{
return this.blockPopulators;
}
// ---------------------------------------------------------------------
// Section: Variables
// ---------------------------------------------------------------------
/**
* CaveBlock addon.
*/
private CaveBlock addon;
/**
* Addon settings.
*/
private Settings settings;
/**
* This list contains block populators that will be applied after chunk is generated.
*/
private List<BlockPopulator> blockPopulators;
/**
* @param materialChanceMap
* @param mainMaterial
*/
public Chances(Map<Material, Pair<Integer, Integer>> materialChanceMap, Material mainMaterial) {
this.materialChanceMap = materialChanceMap;
this.mainMaterial = mainMaterial;
}
}
}

View File

@ -240,10 +240,12 @@ public class EntitiesPopulator extends BlockPopulator
if (otherBlock.getY() < block.getY())
{
addon.log("DEBUG: spawning " + entity.toString() + " at " + otherBlock.getLocation());
world.spawnEntity(otherBlock.getLocation(), entity);
}
else
{
addon.log("DEBUG: spawning " + entity.toString() + " at " + block.getLocation());
world.spawnEntity(block.getLocation(), entity);
}
}
@ -252,6 +254,7 @@ public class EntitiesPopulator extends BlockPopulator
{
block.setType(Material.CAVE_AIR);
world.spawnEntity(block.getLocation(), entity);
addon.log("DEBUG: spawning " + entity.toString() + " at " + block.getLocation());
}
}
}

View File

@ -1,211 +0,0 @@
package world.bentobox.caveblock.generators.populators;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import org.bukkit.Chunk;
import org.bukkit.Material;
import org.bukkit.World;
import org.bukkit.World.Environment;
import org.bukkit.block.Block;
import org.bukkit.generator.BlockPopulator;
import world.bentobox.bentobox.util.Pair;
import world.bentobox.caveblock.CaveBlock;
import world.bentobox.caveblock.Settings;
/**
* This class allows to fill given chunk with necessary blocks.
*/
public class MaterialPopulator extends BlockPopulator
{
private Map<Environment, Chances> chances;
private final int generationTry;
private int worldHeight;
/**
* This is default constructor
* @param addon CaveBlock addon.
*/
public MaterialPopulator(CaveBlock addon)
{
this.addon = addon;
this.settings = addon.getSettings();
// Set up chances
chances = new HashMap<>();
// Normal
chances.put(Environment.NORMAL, new Chances(this.getMaterialMap(this.settings.getNormalBlocks()), this.settings.getNormalMainBlock()));
// Nether
chances.put(Environment.NETHER, new Chances(this.getMaterialMap(this.settings.getNetherBlocks()), this.settings.getNetherMainBlock()));
// End
chances.put(Environment.THE_END, new Chances(this.getMaterialMap(this.settings.getEndBlocks()), this.settings.getEndMainBlock()));
// Other settings
generationTry = this.settings.getNumberOfBlockGenerationTries();
worldHeight = this.settings.getWorldDepth() - 1;
}
/**
* This method populates chunk with blocks.
* @param world World where population must be.
* @param random Randomness
* @param chunk Chunk were populator operates.
*/
@Override
public void populate(World world, Random random, Chunk chunk)
{
for (Map.Entry<Material, Pair<Integer, Integer>> entry : chances.get(world.getEnvironment()).materialChanceMap.entrySet())
{
for (int subY = 0; subY < worldHeight; subY += 16)
{
for (int tries = 0; tries < generationTry; tries++)
{
if (random.nextInt(100) < entry.getValue().x)
{
int x = random.nextInt(15);
int z = random.nextInt(15);
int y = Math.min(worldHeight - 2, subY + random.nextInt(15));
Block block = chunk.getBlock(x, y, z);
if (block.getType().equals(chances.get(world.getEnvironment()).mainMaterial) &&
this.isValidBlock(world, block, x, z))
{
int packSize = random.nextInt(entry.getValue().z);
boolean continuePlacing = true;
while (continuePlacing)
{
if (!block.getType().equals(entry.getKey()))
{
block.setType(entry.getKey());
packSize--;
}
// The direction chooser
switch (random.nextInt(5))
{
case 0:
x = Math.min(15, x + 1);
break;
case 1:
y = Math.min(worldHeight - 2, y + 1);
break;
case 2:
z = Math.min(15, z + 1);
break;
case 3:
x = Math.max(0, x - 1);
break;
case 4:
y = Math.max(1, y - 1);
break;
case 5:
z = Math.max(0, z - 1);
break;
}
block = chunk.getBlock(x, y, z);
continuePlacing = this.isValidBlock(world, block, x, z) &&
packSize > 0 &&
(block.getType().equals(chances.get(world.getEnvironment()).mainMaterial) ||
block.getType().equals(entry.getKey()));
}
}
}
}
}
}
}
/**
* This method checks if all chunks around given block is generated.
* @param world World in which block is located
* @param block Block that must be checked.
* @param x Block x-index in chunk
* @param z Block z-index in chunk
* @return true, if all chunks around given block are generated.
*/
private boolean isValidBlock(World world, Block block, int x, int z)
{
return x > 0 && x < 15 && z > 0 && z < 15 ||
world.isChunkGenerated(block.getX() + 1, block.getZ()) &&
world.isChunkGenerated(block.getX() - 1, block.getZ()) &&
world.isChunkGenerated(block.getX(), block.getZ() - 1) &&
world.isChunkGenerated(block.getX(), block.getZ() + 1);
}
/**
* This method returns material frequently and pack size map.
* @param objectList List with objects that contains data.
* @return Map that contains material, its rarity and pack size.
*/
private Map<Material, Pair<Integer, Integer>> getMaterialMap(List<String> objectList)
{
Map<Material, Pair<Integer, Integer>> materialMap = new HashMap<>(objectList.size());
// wrong material object.
objectList.stream().
filter(object -> object.startsWith("MATERIAL")).
map(object -> object.split(":")).
filter(splitString -> splitString.length == 4).
forEach(splitString -> {
Material material = Material.getMaterial(splitString[1]);
if (material != null)
{
materialMap.put(material,
new Pair<>(Integer.parseInt(splitString[2]), Integer.parseInt(splitString[3])));
}
});
return materialMap;
}
// ---------------------------------------------------------------------
// Section: Variables
// ---------------------------------------------------------------------
/**
* CaveBlock addon.
*/
private CaveBlock addon;
/**
* CaveBlock settings.
*/
private Settings settings;
/**
* Chances class to store chances for environments and main material
*
*/
private class Chances {
final Map<Material, Pair<Integer, Integer>> materialChanceMap;
final Material mainMaterial;
/**
* @param materialChanceMap
* @param mainMaterial
*/
public Chances(Map<Material, Pair<Integer, Integer>> materialChanceMap, Material mainMaterial) {
this.materialChanceMap = materialChanceMap;
this.mainMaterial = mainMaterial;
}
}
}