Skip to content

Commit

Permalink
Generate petrified trees earlier
Browse files Browse the repository at this point in the history
  • Loading branch information
unascribed committed Apr 27, 2017
1 parent 01de0d9 commit b0fa4ad
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 32 deletions.
18 changes: 10 additions & 8 deletions src/main/java/com/elytradev/glasshearts/GlassHearts.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import io.github.elytra.concrete.NetworkContext;
import io.github.elytra.concrete.invoker.Invoker;
import io.github.elytra.concrete.invoker.Invokers;

import com.elytradev.glasshearts.block.BlockFluidLifeforce;
import com.elytradev.glasshearts.block.BlockGlassHeart;
import com.elytradev.glasshearts.block.BlockOre;
Expand Down Expand Up @@ -95,7 +96,7 @@
import net.minecraftforge.event.entity.living.LivingHurtEvent;
import net.minecraftforge.event.entity.player.PlayerEvent;
import net.minecraftforge.event.entity.player.PlayerInteractEvent.RightClickItem;
import net.minecraftforge.event.terraingen.PopulateChunkEvent;
import net.minecraftforge.event.terraingen.ChunkGeneratorEvent;
import net.minecraftforge.event.world.ExplosionEvent;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidRegistry;
Expand Down Expand Up @@ -398,6 +399,14 @@ public void onPostInit(FMLPostInitializationEvent e) {
proxy.onPostInit();
}

@SubscribeEvent
public void onPopulate(ChunkGeneratorEvent.ReplaceBiomeBlocks e) {
if (e.getWorld().provider.isSurfaceWorld() && configGeneratePetrifiedTrees) {
// This is DEFINITELY not a hack. Nope. Not at all.
GeneratePetrifiedTree.generate(e.getWorld().getSeed()+(e.getX()^e.getZ()), e.getPrimer(), e.getGen());
}
}

@SubscribeEvent(priority=EventPriority.LOWEST)
public void onLogIn(PlayerLoggedInEvent e) {
PlayerHandler ph = new PlayerHandler(e.player);
Expand Down Expand Up @@ -450,13 +459,6 @@ public void onHeal(LivingHealEvent e) {
}
}

@SubscribeEvent(priority=EventPriority.HIGHEST)
public void onChunkPopulate(PopulateChunkEvent.Pre e) {
if (configGeneratePetrifiedTrees) {
new GeneratePetrifiedTree().generate(e.getRand(), e.getChunkX(), e.getChunkZ(), e.getWorld(), e.getGen(), e.getWorld().getChunkProvider());
}
}

@SubscribeEvent
public void onExplode(ExplosionEvent.Detonate e) {
if (e.getExplosion().getExplosivePlacedBy() instanceof EntityCreeper) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,47 @@
import com.elytradev.glasshearts.GlassHearts;
import com.elytradev.glasshearts.block.BlockPetrifiedLog;

import io.github.elytra.concrete.accessor.Accessor;
import io.github.elytra.concrete.accessor.Accessors;
import net.minecraft.block.BlockLog.EnumAxis;
import net.minecraft.block.state.IBlockState;
import net.minecraft.init.Blocks;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraft.world.biome.Biome;
import net.minecraft.world.chunk.ChunkPrimer;
import net.minecraft.world.chunk.IChunkGenerator;
import net.minecraft.world.chunk.IChunkProvider;
import net.minecraft.world.gen.ChunkProviderOverworld;
import net.minecraftforge.common.BiomeDictionary;
import net.minecraftforge.common.BiomeDictionary.Type;
import net.minecraftforge.fml.common.IWorldGenerator;

public class GeneratePetrifiedTree implements IWorldGenerator {
public class GeneratePetrifiedTree {

@Override
public void generate(Random random, int chunkX, int chunkZ, World world, IChunkGenerator chunkGenerator, IChunkProvider chunkProvider) {
if (!world.provider.isSurfaceWorld()) return;
// This is the OPPOSITE of recommended advice for populators.
private static final Accessor<Biome[]> biomesForGeneration = Accessors.findField(ChunkProviderOverworld.class, "field_185981_C", "biomesForGeneration", "D");

private static Random random = new Random(0);

public static void generate(long seed, ChunkPrimer primer, IChunkGenerator gen) {
if (!(gen instanceof ChunkProviderOverworld)) return;

// But, the generator below should never leave chunk bounds, and this
// lets us dodge already-generated trees.
int x = (chunkX*16)+16;
int z = (chunkZ*16)+16;
int y = world.getHeight(x, z);
Biome b = world.getBiome(new BlockPos(x, y, z));
random.setSeed(seed);

int x = 8;
int z = 8;
int y = 128;

Biome[] biomes = biomesForGeneration.get(gen);

Biome b = biomes[z + (x*16)];
if (b != null && BiomeDictionary.isBiomeOfType(b, Type.FOREST)) {
if (random.nextInt(5) == 0) {
while (true) {
if (y <= 0) return;
IBlockState ibs = primer.getBlockState(x, y, z);
if (ibs.getBlock() != Blocks.AIR) {
y++;
break;
}
y--;
}
IBlockState brownMush = Blocks.BROWN_MUSHROOM.getDefaultState();
IBlockState redMush = Blocks.RED_MUSHROOM.getDefaultState();
EnumAxis axis = EnumAxis.values()[random.nextInt(3)];
Expand All @@ -41,19 +55,19 @@ public void generate(Random random, int chunkX, int chunkZ, World world, IChunkG
case X: {
x -= length/2;
for (int i = 0; i < length; i++) {
world.setBlockState(new BlockPos(x+i, y, z), ibs);
primer.setBlockState(x+i, y, z, ibs);
if (random.nextInt(3) == 0) {
world.setBlockState(new BlockPos(x+i, y+1, z), random.nextBoolean() ? brownMush : redMush);
primer.setBlockState(x+i, y+1, z, random.nextBoolean() ? brownMush : redMush);
}
}
break;
}
case Z: {
z -= length/2;
for (int i = 0; i < length; i++) {
world.setBlockState(new BlockPos(x, y, z+i), ibs);
primer.setBlockState(x, y, z+i, ibs);
if (random.nextInt(3) == 0) {
world.setBlockState(new BlockPos(x, y+1, z+i), random.nextBoolean() ? brownMush : redMush);
primer.setBlockState(x, y+1, z+i, random.nextBoolean() ? brownMush : redMush);
}
}
break;
Expand All @@ -62,7 +76,7 @@ public void generate(Random random, int chunkX, int chunkZ, World world, IChunkG
int up = random.nextInt(3)+1;
length -= up;
for (int i = 0; i < up; i++) {
world.setBlockState(new BlockPos(x, y+i, z), ibs);
primer.setBlockState(x, y+i, z, ibs);
}
EnumAxis axis2 = random.nextBoolean() ? EnumAxis.X : EnumAxis.Z;
IBlockState ibs2 = GlassHearts.inst.PETRIFIED_LOG.getDefaultState().withProperty(BlockPetrifiedLog.LOG_AXIS, axis2);
Expand All @@ -75,9 +89,9 @@ public void generate(Random random, int chunkX, int chunkZ, World world, IChunkG
x += 2;
}
for (int i = 0; i < length; i++) {
world.setBlockState(new BlockPos(x+i, y, z), ibs2);
primer.setBlockState(x+i, y, z, ibs2);
if (random.nextInt(3) == 0) {
world.setBlockState(new BlockPos(x+i, y+1, z), random.nextBoolean() ? brownMush : redMush);
primer.setBlockState(x+i, y+1, z, random.nextBoolean() ? brownMush : redMush);
}
}
break;
Expand All @@ -88,9 +102,9 @@ public void generate(Random random, int chunkX, int chunkZ, World world, IChunkG
z += 2;
}
for (int i = 0; i < length; i++) {
world.setBlockState(new BlockPos(x, y, z+i), ibs2);
primer.setBlockState(x, y, z+i, ibs2);
if (random.nextInt(3) == 0) {
world.setBlockState(new BlockPos(x, y+1, z+i), random.nextBoolean() ? brownMush : redMush);
primer.setBlockState(x, y+1, z+i, random.nextBoolean() ? brownMush : redMush);
}
}
break;
Expand Down

0 comments on commit b0fa4ad

Please sign in to comment.