Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rewrite rubber trees #2738

Merged
merged 1 commit into from
Feb 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
package gregtech.api.worldgen.generator;

import gregtech.common.ConfigHolder;
import gregtech.common.worldgen.WorldGenRubberTree;

import net.minecraft.init.Biomes;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraft.world.biome.Biome;
import net.minecraft.world.chunk.Chunk;
import net.minecraft.world.chunk.IChunkProvider;
import net.minecraft.world.gen.IChunkGenerator;
import net.minecraftforge.common.BiomeDictionary;
import net.minecraftforge.fml.common.IWorldGenerator;

import java.util.Random;
Expand All @@ -30,12 +22,6 @@ public void generate(Random random, int chunkX, int chunkZ, World world, IChunkG
int selfGridX = Math.floorDiv(chunkX, GRID_SIZE_X);
int selfGridZ = Math.floorDiv(chunkZ, GRID_SIZE_Z);
generateInternal(world, selfGridX, selfGridZ, chunkX, chunkZ, random);

long rubberTreeSeed = random.nextLong();
if (!ConfigHolder.worldgen.disableRubberTreeGeneration) {
generateRubberTree(random, rubberTreeSeed, chunkProvider.provideChunk(chunkX, chunkZ),
ConfigHolder.worldgen.rubberTreeRateIncrease);
}
}

private static void generateInternal(World world, int selfGridX, int selfGridZ, int chunkX, int chunkZ,
Expand All @@ -50,36 +36,4 @@ private static void generateInternal(World world, int selfGridX, int selfGridZ,
}
}
}

private static void generateRubberTree(Random random, long seed, Chunk chunk, double baseScale) {
random.setSeed(seed);
Biome[] biomes = new Biome[4];
BlockPos.MutableBlockPos pos = new BlockPos.MutableBlockPos();
int seaLevel = chunk.getWorld().getSeaLevel();
for (int i = 0; i < 4; i++) {
int x = chunk.x * 16 + 8 + (i & 0x1) * 15;
int z = chunk.z * 16 + 8 + ((i & 0x2) >>> 1) * 15;
biomes[i] = chunk.getWorld().getBiomeProvider().getBiome(pos.setPos(x, seaLevel, z), Biomes.PLAINS);
}
int rubberTrees = 0;
for (Biome biome : biomes) {
if (biome != null) {
if (BiomeDictionary.hasType(biome, BiomeDictionary.Type.SWAMP))
rubberTrees += random.nextInt(10) + 5;
if (BiomeDictionary.hasType(biome, BiomeDictionary.Type.FOREST) ||
BiomeDictionary.hasType(biome, BiomeDictionary.Type.JUNGLE))
rubberTrees += random.nextInt(5) + 1;
}
}
rubberTrees = (int) Math.round(rubberTrees * baseScale);
rubberTrees /= 2;
if (rubberTrees > 0 && random.nextInt(100) < rubberTrees) {
for (int j = 0; j < rubberTrees; j++) {
pos.setPos(chunk.x * 16 + random.nextInt(16), seaLevel, chunk.z * 16 + random.nextInt(16));
if (!WorldGenRubberTree.WORLD_GEN_INSTANCE.generateImpl(chunk.getWorld(), random, pos)) {
rubberTrees -= 3;
}
}
}
}
}
43 changes: 28 additions & 15 deletions src/main/java/gregtech/common/blocks/wood/BlockRubberSapling.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
package gregtech.common.blocks.wood;

import gregtech.common.creativetab.GTCreativeTabs;
import gregtech.common.worldgen.WorldGenRubberTree;
import gregtech.worldgen.impl.WorldGenRubberTree;

import net.minecraft.block.BlockBush;
import net.minecraft.block.IGrowable;
import net.minecraft.block.SoundType;
import net.minecraft.block.state.BlockStateContainer;
import net.minecraft.block.state.IBlockState;
import net.minecraft.init.Blocks;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraftforge.common.EnumPlantType;
import net.minecraftforge.event.terraingen.TerrainGen;

import org.jetbrains.annotations.NotNull;

Expand All @@ -22,7 +24,7 @@

public class BlockRubberSapling extends BlockBush implements IGrowable {

protected static final AxisAlignedBB SAPLING_AABB = new AxisAlignedBB(0.1, 0.0D, 0.1, 0.9, 0.8, 0.9);
private static final AxisAlignedBB SAPLING_AABB = new AxisAlignedBB(0.1, 0.0D, 0.1, 0.9, 0.8, 0.9);

public BlockRubberSapling() {
this.setDefaultState(this.blockState.getBaseState()
Expand All @@ -40,13 +42,21 @@ protected BlockStateContainer createBlockState() {
}

@Override
public void updateTick(World worldIn, @NotNull BlockPos pos, @NotNull IBlockState state, @NotNull Random rand) {
public void updateTick(@NotNull World worldIn, @NotNull BlockPos pos, @NotNull IBlockState state,
@NotNull Random rand) {
if (!worldIn.isRemote) {
super.updateTick(worldIn, pos, state, rand);
if (!worldIn.isAreaLoaded(pos, 1))
if (!worldIn.isAreaLoaded(pos, 1)) {
return;
}

// longer than the vanilla growth requirement
if (worldIn.getLightFromNeighbors(pos.up()) >= 9 && rand.nextInt(30) == 0) {
this.grow(worldIn, rand, pos, state);
if (state.getValue(STAGE) == 0) {
worldIn.setBlockState(pos, state.cycleProperty(STAGE), 4);
} else {
this.grow(worldIn, rand, pos, state);
}
}
}
}
Expand All @@ -59,17 +69,14 @@ public IBlockState getStateFromMeta(int meta) {
}

@Override
public int getMetaFromState(IBlockState state) {
int i = 0;
i |= state.getValue(STAGE) << 3;
return i;
public int getMetaFromState(@NotNull IBlockState state) {
return state.getValue(STAGE) << 3;
}

@NotNull
@Override
@SuppressWarnings("deprecation")
public AxisAlignedBB getBoundingBox(@NotNull IBlockState state, @NotNull IBlockAccess source,
@NotNull BlockPos pos) {
public @NotNull AxisAlignedBB getBoundingBox(@NotNull IBlockState state, @NotNull IBlockAccess source,
@NotNull BlockPos pos) {
return SAPLING_AABB;
}

Expand All @@ -93,12 +100,18 @@ public boolean canBeReplacedByLeaves(@NotNull IBlockState state, @NotNull IBlock

@Override
public void grow(@NotNull World worldIn, @NotNull Random rand, @NotNull BlockPos pos, @NotNull IBlockState state) {
WorldGenRubberTree.TREE_GROW_INSTANCE.grow(worldIn, pos, rand);
if (!TerrainGen.saplingGrowTree(worldIn, rand, pos)) {
return;
}

worldIn.setBlockState(pos, Blocks.AIR.getDefaultState(), 4);
if (!WorldGenRubberTree.INSTANCE_NOTIFY.generate(worldIn, rand, pos)) {
worldIn.setBlockState(pos, state, 4);
}
}

@Override
@NotNull
public EnumPlantType getPlantType(@NotNull IBlockAccess world, @NotNull BlockPos pos) {
public @NotNull EnumPlantType getPlantType(@NotNull IBlockAccess world, @NotNull BlockPos pos) {
return EnumPlantType.Plains;
}
}
107 changes: 0 additions & 107 deletions src/main/java/gregtech/common/worldgen/WorldGenRubberTree.java

This file was deleted.

75 changes: 75 additions & 0 deletions src/main/java/gregtech/worldgen/WorldgenModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@
import gregtech.common.ConfigHolder;
import gregtech.modules.BaseGregTechModule;
import gregtech.modules.GregTechModules;
import gregtech.worldgen.impl.WorldGenRubberTree;

import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.ChunkPos;
import net.minecraft.world.World;
import net.minecraft.world.biome.Biome;
import net.minecraftforge.common.BiomeDictionary;
import net.minecraftforge.event.terraingen.DecorateBiomeEvent;
import net.minecraftforge.event.terraingen.OreGenEvent;
import net.minecraftforge.fml.common.eventhandler.Event;
import net.minecraftforge.fml.common.eventhandler.EventPriority;
Expand All @@ -18,6 +25,7 @@
import java.util.Collections;
import java.util.EnumSet;
import java.util.List;
import java.util.Random;
import java.util.Set;

import static net.minecraftforge.event.terraingen.OreGenEvent.GenerateMinable.EventType.*;
Expand All @@ -39,6 +47,11 @@ public class WorldgenModule extends BaseGregTechModule {
return LOGGER;
}

@Override
public @NotNull List<Class<?>> getTerrainGenBusSubscribers() {
return Collections.singletonList(WorldgenModule.class);
}

@Override
public @NotNull List<Class<?>> getOreGenBusSubscribers() {
return Collections.singletonList(WorldgenModule.class);
Expand All @@ -50,4 +63,66 @@ public static void onGenerateMineable(@NotNull OreGenEvent.GenerateMinable event
event.setResult(Event.Result.DENY);
}
}

@SubscribeEvent
public static void onBiomeDecorate(@NotNull DecorateBiomeEvent.Decorate event) {
if (event.getType() == DecorateBiomeEvent.Decorate.EventType.TREE) {
if (ConfigHolder.worldgen.disableRubberTreeGeneration) {
return;
}

// replaces regular tree generation with occasional rubber trees
if (generateRubberTrees(event.getWorld(), event.getChunkPos(), event.getRand())) {
event.setResult(Event.Result.DENY);
}
}
}

/**
* Generates a rubber trees
*
* @param world the world in which the tree should be placed
* @param chunkPos the position of the chunk the tree should be generated at
* @param random the random number generator to use
* @return if trees were generated
*/
private static boolean generateRubberTrees(@NotNull World world, @NotNull ChunkPos chunkPos,
@NotNull Random random) {
BlockPos.MutableBlockPos pos = new BlockPos.MutableBlockPos();

final int chunkX = chunkPos.x * 16;
final int chunkZ = chunkPos.z * 16;
pos.setPos(chunkX + 16, 0, chunkZ + 16);
Biome biome = world.getBiome(pos);

int amount = 0;
if (BiomeDictionary.hasType(biome, BiomeDictionary.Type.SWAMP)) {
amount += random.nextInt(10) + 5;
}
if (BiomeDictionary.hasType(biome, BiomeDictionary.Type.FOREST) ||
BiomeDictionary.hasType(biome, BiomeDictionary.Type.JUNGLE)) {
amount += random.nextInt(5) + 1;
}

amount = (int) (amount * ConfigHolder.worldgen.rubberTreeRateIncrease / 2);
if (amount > 0 && random.nextInt(100) < amount) {
boolean generated = false;
for (int i = 0; i < amount; i++) {
int x = chunkX + random.nextInt(16) + 8;
int z = chunkZ + random.nextInt(16) + 8;
int y = world.getHeight(x, z);
pos.setPos(x, y, z);

WorldGenRubberTree.INSTANCE.setDecorationDefaults();
if (WorldGenRubberTree.INSTANCE.generate(world, random, pos)) {
WorldGenRubberTree.INSTANCE.generateSaplings(world, random, pos);
generated = true;
}
}

return generated;
}

return false;
}
}
Loading
Loading