From b0fa4ad05c548ffaa75c64f1e36bf857b1818b35 Mon Sep 17 00:00:00 2001 From: William Thompson Date: Thu, 27 Apr 2017 03:05:33 -0400 Subject: [PATCH] Generate petrified trees earlier --- .../elytradev/glasshearts/GlassHearts.java | 18 +++--- .../world/GeneratePetrifiedTree.java | 62 ++++++++++++------- 2 files changed, 48 insertions(+), 32 deletions(-) diff --git a/src/main/java/com/elytradev/glasshearts/GlassHearts.java b/src/main/java/com/elytradev/glasshearts/GlassHearts.java index 6cf0783..f3f95be 100644 --- a/src/main/java/com/elytradev/glasshearts/GlassHearts.java +++ b/src/main/java/com/elytradev/glasshearts/GlassHearts.java @@ -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; @@ -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; @@ -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); @@ -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) { diff --git a/src/main/java/com/elytradev/glasshearts/world/GeneratePetrifiedTree.java b/src/main/java/com/elytradev/glasshearts/world/GeneratePetrifiedTree.java index 26f7bec..cd8b3c2 100644 --- a/src/main/java/com/elytradev/glasshearts/world/GeneratePetrifiedTree.java +++ b/src/main/java/com/elytradev/glasshearts/world/GeneratePetrifiedTree.java @@ -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 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)]; @@ -41,9 +55,9 @@ 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; @@ -51,9 +65,9 @@ public void generate(Random random, int chunkX, int chunkZ, World world, IChunkG 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; @@ -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); @@ -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; @@ -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;