mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2025-07-18 10:32:30 +00:00
Move trees to separate populator
This commit is contained in:
parent
21d606853a
commit
8b95e86fe2
@ -15,6 +15,7 @@ import com.dfsek.terra.population.FloraPopulator;
|
||||
import com.dfsek.terra.population.OrePopulator;
|
||||
import com.dfsek.terra.population.SnowPopulator;
|
||||
import com.dfsek.terra.population.StructurePopulator;
|
||||
import com.dfsek.terra.population.TreePopulator;
|
||||
import com.dfsek.terra.util.DataUtil;
|
||||
import org.bukkit.Chunk;
|
||||
import org.bukkit.Material;
|
||||
@ -56,6 +57,7 @@ public class TerraChunkGenerator extends GaeaChunkGenerator {
|
||||
public TerraChunkGenerator(ConfigPack c) {
|
||||
super(ChunkInterpolator.InterpolationType.TRILINEAR);
|
||||
this.configPack = c;
|
||||
popMan.attach(new TreePopulator());
|
||||
popMan.attach(new FloraPopulator());
|
||||
popMan.attach(new SnowPopulator());
|
||||
}
|
||||
|
@ -1,64 +1,27 @@
|
||||
package com.dfsek.terra.population;
|
||||
|
||||
import com.dfsek.terra.Terra;
|
||||
import com.dfsek.terra.TerraProfiler;
|
||||
import com.dfsek.terra.TerraWorld;
|
||||
import com.dfsek.terra.biome.UserDefinedBiome;
|
||||
import com.dfsek.terra.biome.grid.TerraBiomeGrid;
|
||||
import com.dfsek.terra.config.genconfig.biome.BiomeConfig;
|
||||
import com.dfsek.terra.config.genconfig.biome.BiomeFloraConfig;
|
||||
import com.dfsek.terra.event.TreeGenerateEvent;
|
||||
import net.jafama.FastMath;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Chunk;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.block.Block;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.polydev.gaea.generation.GenerationPhase;
|
||||
import org.polydev.gaea.math.Range;
|
||||
import org.polydev.gaea.population.GaeaBlockPopulator;
|
||||
import org.polydev.gaea.profiler.ProfileFuture;
|
||||
import org.polydev.gaea.tree.Tree;
|
||||
import org.polydev.gaea.util.GlueList;
|
||||
import org.polydev.gaea.world.Flora;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
|
||||
/**
|
||||
* Populates Flora and Trees
|
||||
*/
|
||||
public class FloraPopulator extends GaeaBlockPopulator {
|
||||
private static boolean doTrees(@NotNull UserDefinedBiome biome, TerraWorld world, @NotNull Random random, @NotNull Chunk chunk, int x, int z) {
|
||||
for(Block block : getValidTreeSpawnsAt(chunk, x, z, new Range(0, 254))) {
|
||||
Tree tree = biome.getDecorator().getTrees().get(random);
|
||||
Range range = biome.getConfig().getTreeRange(tree);
|
||||
if(!range.isInRange(block.getY())) continue;
|
||||
try {
|
||||
Location l = block.getLocation();
|
||||
TreeGenerateEvent event = new TreeGenerateEvent(world, l, tree);
|
||||
Bukkit.getPluginManager().callEvent(event);
|
||||
if(!event.isCancelled()) tree.plant(l, random, Terra.getInstance());
|
||||
} catch(NullPointerException ignore) {
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static List<Block> getValidTreeSpawnsAt(Chunk chunk, int x, int z, Range check) {
|
||||
List<Block> blocks = new GlueList<>();
|
||||
for(int y : check) {
|
||||
if(chunk.getBlock(x, y, z).getType().isSolid() && chunk.getBlock(x, y + 1, z).getType().isAir()) {
|
||||
blocks.add(chunk.getBlock(x, y + 1, z));
|
||||
}
|
||||
}
|
||||
return blocks;
|
||||
}
|
||||
|
||||
private static int offset(Random r, int i) {
|
||||
return FastMath.min(FastMath.max(i + r.nextInt(3) - 1, 0), 15);
|
||||
}
|
||||
|
||||
@SuppressWarnings("try")
|
||||
@Override
|
||||
@ -72,14 +35,6 @@ public class FloraPopulator extends GaeaBlockPopulator {
|
||||
for(int x = 0; x < 16; x++) {
|
||||
for(int z = 0; z < 16; z++) {
|
||||
UserDefinedBiome biome = (UserDefinedBiome) grid.getBiome((chunk.getX() << 4) + x, (chunk.getZ() << 4) + z, GenerationPhase.POPULATE);
|
||||
if((x & 1) == 0 && (z & 1) == 0) {
|
||||
int treeChance = biome.getDecorator().getTreeDensity();
|
||||
if(random.nextInt(1000) < treeChance) {
|
||||
int xt = offset(random, x);
|
||||
int zt = offset(random, z);
|
||||
if(doTrees(biome, tw, random, chunk, xt, zt)) continue;
|
||||
}
|
||||
}
|
||||
if(biome.getDecorator().getFloraChance() <= 0) continue;
|
||||
try {
|
||||
BiomeConfig c = biome.getConfig();
|
||||
|
71
src/main/java/com/dfsek/terra/population/TreePopulator.java
Normal file
71
src/main/java/com/dfsek/terra/population/TreePopulator.java
Normal file
@ -0,0 +1,71 @@
|
||||
package com.dfsek.terra.population;
|
||||
|
||||
import com.dfsek.terra.Terra;
|
||||
import com.dfsek.terra.TerraWorld;
|
||||
import com.dfsek.terra.biome.UserDefinedBiome;
|
||||
import com.dfsek.terra.biome.grid.TerraBiomeGrid;
|
||||
import com.dfsek.terra.event.TreeGenerateEvent;
|
||||
import net.jafama.FastMath;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Chunk;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.block.Block;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.polydev.gaea.generation.GenerationPhase;
|
||||
import org.polydev.gaea.math.Range;
|
||||
import org.polydev.gaea.population.GaeaBlockPopulator;
|
||||
import org.polydev.gaea.tree.Tree;
|
||||
import org.polydev.gaea.util.GlueList;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Random;
|
||||
|
||||
public class TreePopulator extends GaeaBlockPopulator {
|
||||
private static void doTrees(@NotNull UserDefinedBiome biome, TerraWorld world, @NotNull Random random, @NotNull Chunk chunk, int x, int z) {
|
||||
for(Block block : getValidTreeSpawnsAt(chunk, x, z, new Range(0, 254))) {
|
||||
Tree tree = biome.getDecorator().getTrees().get(random);
|
||||
Range range = biome.getConfig().getTreeRange(tree);
|
||||
if(!range.isInRange(block.getY())) continue;
|
||||
try {
|
||||
Location l = block.getLocation();
|
||||
TreeGenerateEvent event = new TreeGenerateEvent(world, l, tree);
|
||||
Bukkit.getPluginManager().callEvent(event);
|
||||
if(!event.isCancelled()) tree.plant(l, random, Terra.getInstance());
|
||||
} catch(NullPointerException ignore) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static List<Block> getValidTreeSpawnsAt(Chunk chunk, int x, int z, Range check) {
|
||||
List<Block> blocks = new GlueList<>();
|
||||
for(int y : check) {
|
||||
if(chunk.getBlock(x, y, z).getType().isSolid() && chunk.getBlock(x, y + 1, z).getType().isAir()) {
|
||||
blocks.add(chunk.getBlock(x, y + 1, z));
|
||||
}
|
||||
}
|
||||
return blocks;
|
||||
}
|
||||
|
||||
private static int offset(Random r, int i) {
|
||||
return FastMath.min(FastMath.max(i + r.nextInt(3) - 1, 0), 15);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void populate(@NotNull World world, @NotNull Random random, @NotNull Chunk chunk) {
|
||||
TerraWorld tw = TerraWorld.getWorld(world);
|
||||
if(!tw.isSafe()) return;
|
||||
TerraBiomeGrid grid = tw.getGrid();
|
||||
for(int x = 0; x < 16; x += 2) {
|
||||
for(int z = 0; z < 16; z += 2) {
|
||||
UserDefinedBiome biome = (UserDefinedBiome) grid.getBiome((chunk.getX() << 4) + x, (chunk.getZ() << 4) + z, GenerationPhase.POPULATE);
|
||||
int treeChance = biome.getDecorator().getTreeDensity();
|
||||
if(random.nextInt(1000) < treeChance) {
|
||||
int xt = offset(random, x);
|
||||
int zt = offset(random, z);
|
||||
doTrees(biome, tw, random, chunk, xt, zt);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user