/
FusewoodTreeGenerator.java
209 lines (171 loc) · 6.51 KB
/
FusewoodTreeGenerator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package com.progwml6.natura.world.worldgen.trees.nether;
import java.util.Random;
import com.progwml6.natura.nether.NaturaNether;
import com.progwml6.natura.world.worldgen.trees.BaseTreeGenerator;
import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState;
import net.minecraft.init.Blocks;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraft.world.chunk.IChunkGenerator;
import net.minecraft.world.chunk.IChunkProvider;
public class FusewoodTreeGenerator extends BaseTreeGenerator
{
public final int minTreeHeight;
public final IBlockState log;
public final IBlockState leaves;
public final boolean seekHeight;
public FusewoodTreeGenerator(int treeHeight, IBlockState log, IBlockState leaves, boolean seekHeight)
{
this.minTreeHeight = treeHeight;
this.log = log;
this.leaves = leaves;
this.seekHeight = seekHeight;
}
public FusewoodTreeGenerator(int treeHeight, IBlockState log, IBlockState leaves)
{
this(treeHeight, log, leaves, true);
}
BlockPos findGround(World world, BlockPos pos)
{
boolean foundGround = false;
int height = pos.getY();
BlockPos position = new BlockPos(pos.getX(), height, pos.getZ());
do
{
position = position.down();
Block underBlock = world.getBlockState(position).getBlock();
if (underBlock == Blocks.NETHERRACK || underBlock == Blocks.SOUL_SAND || underBlock == NaturaNether.netherTaintedSoil || height < 0)
{
foundGround = true;
}
}
while (!foundGround);
return position.up();
}
@Override
public void generate(Random random, int chunkX, int chunkZ, World world, IChunkGenerator chunkGenerator, IChunkProvider chunkProvider)
{
}
@Override
public void generateTree(Random random, World world, BlockPos pos)
{
int height = random.nextInt(3) + this.minTreeHeight;
if (height < 4)
{
height = 4;
}
if (this.seekHeight)
{
pos = this.findGround(world, pos);
if (pos.getY() < 0)
{
return;
}
}
int yPos = pos.getY();
if (yPos >= 1 && yPos + height + 1 <= 256)
{
if (!this.checkClear(world, pos.getX(), yPos, pos.getY(), height))
{
return;
}
IBlockState state = world.getBlockState(pos.down());
Block soil = state.getBlock();
boolean isSoil = (soil != null && soil.canSustainPlant(state, world, pos.down(), EnumFacing.UP, NaturaNether.netherSapling) || soil == Blocks.NETHERRACK);
if (isSoil && yPos < 256 - height - 1)
{
soil.onPlantGrow(state, world, pos.down(), pos);
this.placeCanopy(world, random, pos, height);
this.placeTrunk(world, pos, height);
}
}
}
boolean checkClear(World world, int xPos, int yPos, int zPos, int treeHeight)
{
boolean flag = true;
for (int y = yPos; y <= yPos + 1 + treeHeight; ++y)
{
int range = 1;
if (y == yPos)
{
range = 0;
}
if (y >= yPos + 1 + treeHeight - 2)
{
range = 2;
}
for (int x = xPos - range; x <= xPos + range && flag; ++x)
{
for (int z = zPos - range; z <= zPos + range && flag; ++z)
{
if (y >= 0 && y < 256)
{
BlockPos blockpos = new BlockPos(x, y, z);
IBlockState state = world.getBlockState(blockpos);
Block block = state.getBlock();
if (!block.isAir(state, world, blockpos) && !block.isLeaves(state, world, blockpos) && block != Blocks.NETHERRACK && block != Blocks.SOUL_SAND && block != NaturaNether.netherTaintedSoil && !block.isWood(world, blockpos))
{
flag = false;
}
}
else
{
flag = false;
}
}
}
}
return flag;
}
protected void placeCanopy(World world, Random random, BlockPos pos, int height)
{
for (int y = pos.getY() - 3 + height; y <= pos.getY() + height; ++y)
{
int subract = y - (pos.getY() + height);
int subract2 = 1 - subract / 2;
for (int x = pos.getX() - subract2; x <= pos.getX() + subract2; ++x)
{
int mathX = x - pos.getX();
for (int z = pos.getZ() - subract2; z <= pos.getZ() + subract2; ++z)
{
int mathZ = z - pos.getZ();
if (Math.abs(mathX) != subract2 || Math.abs(mathZ) != subract2 || random.nextInt(2) != 0 && subract != 0)
{
BlockPos blockpos = new BlockPos(x, y, z);
IBlockState state = world.getBlockState(blockpos);
if (state.getBlock() == null || state.getBlock().canBeReplacedByLeaves(state, world, blockpos))
{
world.setBlockState(blockpos, this.leaves, 2);
}
}
}
}
}
}
protected void placeTrunk(World world, BlockPos pos, int height)
{
for (int localHeight = 0; localHeight < height; ++localHeight)
{
BlockPos blockpos = new BlockPos(pos.getX(), pos.getY() + localHeight, pos.getZ());
IBlockState state = world.getBlockState(blockpos);
Block block = state.getBlock();
if (block.isAir(state, world, blockpos) || block == null || block.isLeaves(state, world, blockpos))
{
world.setBlockState(blockpos, this.log, 2);
}
}
/*while (height >= 0)
{
IBlockState state = world.getBlockState(pos);
Block block = state.getBlock();
if (block.isAir(state, world, pos) || block.isReplaceable(world, pos) || block.isLeaves(state, world, pos))
{
this.setBlockAndMetadata(world, pos, this.log);
}
pos = pos.up();
height--;
}*/
}
}