Skip to content

Commit 0c8dcf0

Browse files
authored
rewrite rubber trees (#2738)
1 parent 012de01 commit 0c8dcf0

File tree

5 files changed

+229
-168
lines changed

5 files changed

+229
-168
lines changed
Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,8 @@
11
package gregtech.api.worldgen.generator;
22

3-
import gregtech.common.ConfigHolder;
4-
import gregtech.common.worldgen.WorldGenRubberTree;
5-
6-
import net.minecraft.init.Biomes;
7-
import net.minecraft.util.math.BlockPos;
83
import net.minecraft.world.World;
9-
import net.minecraft.world.biome.Biome;
10-
import net.minecraft.world.chunk.Chunk;
114
import net.minecraft.world.chunk.IChunkProvider;
125
import net.minecraft.world.gen.IChunkGenerator;
13-
import net.minecraftforge.common.BiomeDictionary;
146
import net.minecraftforge.fml.common.IWorldGenerator;
157

168
import java.util.Random;
@@ -30,12 +22,6 @@ public void generate(Random random, int chunkX, int chunkZ, World world, IChunkG
3022
int selfGridX = Math.floorDiv(chunkX, GRID_SIZE_X);
3123
int selfGridZ = Math.floorDiv(chunkZ, GRID_SIZE_Z);
3224
generateInternal(world, selfGridX, selfGridZ, chunkX, chunkZ, random);
33-
34-
long rubberTreeSeed = random.nextLong();
35-
if (!ConfigHolder.worldgen.disableRubberTreeGeneration) {
36-
generateRubberTree(random, rubberTreeSeed, chunkProvider.provideChunk(chunkX, chunkZ),
37-
ConfigHolder.worldgen.rubberTreeRateIncrease);
38-
}
3925
}
4026

4127
private static void generateInternal(World world, int selfGridX, int selfGridZ, int chunkX, int chunkZ,
@@ -50,36 +36,4 @@ private static void generateInternal(World world, int selfGridX, int selfGridZ,
5036
}
5137
}
5238
}
53-
54-
private static void generateRubberTree(Random random, long seed, Chunk chunk, double baseScale) {
55-
random.setSeed(seed);
56-
Biome[] biomes = new Biome[4];
57-
BlockPos.MutableBlockPos pos = new BlockPos.MutableBlockPos();
58-
int seaLevel = chunk.getWorld().getSeaLevel();
59-
for (int i = 0; i < 4; i++) {
60-
int x = chunk.x * 16 + 8 + (i & 0x1) * 15;
61-
int z = chunk.z * 16 + 8 + ((i & 0x2) >>> 1) * 15;
62-
biomes[i] = chunk.getWorld().getBiomeProvider().getBiome(pos.setPos(x, seaLevel, z), Biomes.PLAINS);
63-
}
64-
int rubberTrees = 0;
65-
for (Biome biome : biomes) {
66-
if (biome != null) {
67-
if (BiomeDictionary.hasType(biome, BiomeDictionary.Type.SWAMP))
68-
rubberTrees += random.nextInt(10) + 5;
69-
if (BiomeDictionary.hasType(biome, BiomeDictionary.Type.FOREST) ||
70-
BiomeDictionary.hasType(biome, BiomeDictionary.Type.JUNGLE))
71-
rubberTrees += random.nextInt(5) + 1;
72-
}
73-
}
74-
rubberTrees = (int) Math.round(rubberTrees * baseScale);
75-
rubberTrees /= 2;
76-
if (rubberTrees > 0 && random.nextInt(100) < rubberTrees) {
77-
for (int j = 0; j < rubberTrees; j++) {
78-
pos.setPos(chunk.x * 16 + random.nextInt(16), seaLevel, chunk.z * 16 + random.nextInt(16));
79-
if (!WorldGenRubberTree.WORLD_GEN_INSTANCE.generateImpl(chunk.getWorld(), random, pos)) {
80-
rubberTrees -= 3;
81-
}
82-
}
83-
}
84-
}
8539
}

src/main/java/gregtech/common/blocks/wood/BlockRubberSapling.java

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
11
package gregtech.common.blocks.wood;
22

33
import gregtech.common.creativetab.GTCreativeTabs;
4-
import gregtech.common.worldgen.WorldGenRubberTree;
4+
import gregtech.worldgen.impl.WorldGenRubberTree;
55

66
import net.minecraft.block.BlockBush;
77
import net.minecraft.block.IGrowable;
88
import net.minecraft.block.SoundType;
99
import net.minecraft.block.state.BlockStateContainer;
1010
import net.minecraft.block.state.IBlockState;
11+
import net.minecraft.init.Blocks;
1112
import net.minecraft.util.math.AxisAlignedBB;
1213
import net.minecraft.util.math.BlockPos;
1314
import net.minecraft.world.IBlockAccess;
1415
import net.minecraft.world.World;
1516
import net.minecraftforge.common.EnumPlantType;
17+
import net.minecraftforge.event.terraingen.TerrainGen;
1618

1719
import org.jetbrains.annotations.NotNull;
1820

@@ -22,7 +24,7 @@
2224

2325
public class BlockRubberSapling extends BlockBush implements IGrowable {
2426

25-
protected static final AxisAlignedBB SAPLING_AABB = new AxisAlignedBB(0.1, 0.0D, 0.1, 0.9, 0.8, 0.9);
27+
private static final AxisAlignedBB SAPLING_AABB = new AxisAlignedBB(0.1, 0.0D, 0.1, 0.9, 0.8, 0.9);
2628

2729
public BlockRubberSapling() {
2830
this.setDefaultState(this.blockState.getBaseState()
@@ -40,13 +42,21 @@ protected BlockStateContainer createBlockState() {
4042
}
4143

4244
@Override
43-
public void updateTick(World worldIn, @NotNull BlockPos pos, @NotNull IBlockState state, @NotNull Random rand) {
45+
public void updateTick(@NotNull World worldIn, @NotNull BlockPos pos, @NotNull IBlockState state,
46+
@NotNull Random rand) {
4447
if (!worldIn.isRemote) {
4548
super.updateTick(worldIn, pos, state, rand);
46-
if (!worldIn.isAreaLoaded(pos, 1))
49+
if (!worldIn.isAreaLoaded(pos, 1)) {
4750
return;
51+
}
52+
53+
// longer than the vanilla growth requirement
4854
if (worldIn.getLightFromNeighbors(pos.up()) >= 9 && rand.nextInt(30) == 0) {
49-
this.grow(worldIn, rand, pos, state);
55+
if (state.getValue(STAGE) == 0) {
56+
worldIn.setBlockState(pos, state.cycleProperty(STAGE), 4);
57+
} else {
58+
this.grow(worldIn, rand, pos, state);
59+
}
5060
}
5161
}
5262
}
@@ -59,17 +69,14 @@ public IBlockState getStateFromMeta(int meta) {
5969
}
6070

6171
@Override
62-
public int getMetaFromState(IBlockState state) {
63-
int i = 0;
64-
i |= state.getValue(STAGE) << 3;
65-
return i;
72+
public int getMetaFromState(@NotNull IBlockState state) {
73+
return state.getValue(STAGE) << 3;
6674
}
6775

68-
@NotNull
6976
@Override
7077
@SuppressWarnings("deprecation")
71-
public AxisAlignedBB getBoundingBox(@NotNull IBlockState state, @NotNull IBlockAccess source,
72-
@NotNull BlockPos pos) {
78+
public @NotNull AxisAlignedBB getBoundingBox(@NotNull IBlockState state, @NotNull IBlockAccess source,
79+
@NotNull BlockPos pos) {
7380
return SAPLING_AABB;
7481
}
7582

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

94101
@Override
95102
public void grow(@NotNull World worldIn, @NotNull Random rand, @NotNull BlockPos pos, @NotNull IBlockState state) {
96-
WorldGenRubberTree.TREE_GROW_INSTANCE.grow(worldIn, pos, rand);
103+
if (!TerrainGen.saplingGrowTree(worldIn, rand, pos)) {
104+
return;
105+
}
106+
107+
worldIn.setBlockState(pos, Blocks.AIR.getDefaultState(), 4);
108+
if (!WorldGenRubberTree.INSTANCE_NOTIFY.generate(worldIn, rand, pos)) {
109+
worldIn.setBlockState(pos, state, 4);
110+
}
97111
}
98112

99113
@Override
100-
@NotNull
101-
public EnumPlantType getPlantType(@NotNull IBlockAccess world, @NotNull BlockPos pos) {
114+
public @NotNull EnumPlantType getPlantType(@NotNull IBlockAccess world, @NotNull BlockPos pos) {
102115
return EnumPlantType.Plains;
103116
}
104117
}

src/main/java/gregtech/common/worldgen/WorldGenRubberTree.java

Lines changed: 0 additions & 107 deletions
This file was deleted.

src/main/java/gregtech/worldgen/WorldgenModule.java

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,14 @@
55
import gregtech.common.ConfigHolder;
66
import gregtech.modules.BaseGregTechModule;
77
import gregtech.modules.GregTechModules;
8+
import gregtech.worldgen.impl.WorldGenRubberTree;
89

10+
import net.minecraft.util.math.BlockPos;
11+
import net.minecraft.util.math.ChunkPos;
12+
import net.minecraft.world.World;
13+
import net.minecraft.world.biome.Biome;
14+
import net.minecraftforge.common.BiomeDictionary;
15+
import net.minecraftforge.event.terraingen.DecorateBiomeEvent;
916
import net.minecraftforge.event.terraingen.OreGenEvent;
1017
import net.minecraftforge.fml.common.eventhandler.Event;
1118
import net.minecraftforge.fml.common.eventhandler.EventPriority;
@@ -18,6 +25,7 @@
1825
import java.util.Collections;
1926
import java.util.EnumSet;
2027
import java.util.List;
28+
import java.util.Random;
2129
import java.util.Set;
2230

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

50+
@Override
51+
public @NotNull List<Class<?>> getTerrainGenBusSubscribers() {
52+
return Collections.singletonList(WorldgenModule.class);
53+
}
54+
4255
@Override
4356
public @NotNull List<Class<?>> getOreGenBusSubscribers() {
4457
return Collections.singletonList(WorldgenModule.class);
@@ -50,4 +63,66 @@ public static void onGenerateMineable(@NotNull OreGenEvent.GenerateMinable event
5063
event.setResult(Event.Result.DENY);
5164
}
5265
}
66+
67+
@SubscribeEvent
68+
public static void onBiomeDecorate(@NotNull DecorateBiomeEvent.Decorate event) {
69+
if (event.getType() == DecorateBiomeEvent.Decorate.EventType.TREE) {
70+
if (ConfigHolder.worldgen.disableRubberTreeGeneration) {
71+
return;
72+
}
73+
74+
// replaces regular tree generation with occasional rubber trees
75+
if (generateRubberTrees(event.getWorld(), event.getChunkPos(), event.getRand())) {
76+
event.setResult(Event.Result.DENY);
77+
}
78+
}
79+
}
80+
81+
/**
82+
* Generates a rubber trees
83+
*
84+
* @param world the world in which the tree should be placed
85+
* @param chunkPos the position of the chunk the tree should be generated at
86+
* @param random the random number generator to use
87+
* @return if trees were generated
88+
*/
89+
private static boolean generateRubberTrees(@NotNull World world, @NotNull ChunkPos chunkPos,
90+
@NotNull Random random) {
91+
BlockPos.MutableBlockPos pos = new BlockPos.MutableBlockPos();
92+
93+
final int chunkX = chunkPos.x * 16;
94+
final int chunkZ = chunkPos.z * 16;
95+
pos.setPos(chunkX + 16, 0, chunkZ + 16);
96+
Biome biome = world.getBiome(pos);
97+
98+
int amount = 0;
99+
if (BiomeDictionary.hasType(biome, BiomeDictionary.Type.SWAMP)) {
100+
amount += random.nextInt(10) + 5;
101+
}
102+
if (BiomeDictionary.hasType(biome, BiomeDictionary.Type.FOREST) ||
103+
BiomeDictionary.hasType(biome, BiomeDictionary.Type.JUNGLE)) {
104+
amount += random.nextInt(5) + 1;
105+
}
106+
107+
amount = (int) (amount * ConfigHolder.worldgen.rubberTreeRateIncrease / 2);
108+
if (amount > 0 && random.nextInt(100) < amount) {
109+
boolean generated = false;
110+
for (int i = 0; i < amount; i++) {
111+
int x = chunkX + random.nextInt(16) + 8;
112+
int z = chunkZ + random.nextInt(16) + 8;
113+
int y = world.getHeight(x, z);
114+
pos.setPos(x, y, z);
115+
116+
WorldGenRubberTree.INSTANCE.setDecorationDefaults();
117+
if (WorldGenRubberTree.INSTANCE.generate(world, random, pos)) {
118+
WorldGenRubberTree.INSTANCE.generateSaplings(world, random, pos);
119+
generated = true;
120+
}
121+
}
122+
123+
return generated;
124+
}
125+
126+
return false;
127+
}
53128
}

0 commit comments

Comments
 (0)