-
Notifications
You must be signed in to change notification settings - Fork 754
/
SlimeTreeGen.java
183 lines (163 loc) · 7.4 KB
/
SlimeTreeGen.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
package tconstruct.worldgen;
import java.util.Random;
import net.minecraft.block.Block;
import net.minecraft.init.Blocks;
import net.minecraft.world.World;
import net.minecraft.world.gen.feature.WorldGenerator;
import net.minecraftforge.common.util.ForgeDirection;
import tconstruct.common.TRepo;
public class SlimeTreeGen extends WorldGenerator
{
public final int minTreeHeight;
public final int treeHeightRange;
public final int metaWood;
public final int metaLeaves;
public final boolean seekHeight;
public final boolean notify;
public SlimeTreeGen(boolean notify, int treeHeight, int treeRange, int woodMeta, int leavesMeta)
{
super(notify);
this.notify = notify;
this.minTreeHeight = treeHeight;
this.treeHeightRange = treeRange;
this.metaWood = woodMeta;
this.metaLeaves = leavesMeta;
this.seekHeight = !notify;
}
@Override
public boolean generate (World world, Random random, int xPos, int yPos, int zPos)
{
int height = random.nextInt(this.treeHeightRange) + this.minTreeHeight;
boolean flag = true;
if (seekHeight)
{
yPos = findGround(world, xPos, yPos, zPos);
if (yPos == -1)
return false;
}
if (yPos >= 1 && yPos + height + 1 <= 256)
{
Block soil = world.getBlock(xPos, yPos - 1, zPos);
boolean isSoil = (soil != null && soil.canSustainPlant(world, xPos, yPos - 1, zPos, ForgeDirection.UP, TRepo.slimeSapling));
if (isSoil)
{
//TODO Fix this for 1.7
//if (!checkClear(world, xPos, yPos, zPos, height))
// return false;
soil.onPlantGrow(world, xPos, yPos - 1, zPos, xPos, yPos, zPos);
placeCanopy(world, random, xPos, yPos, zPos, height);
placeTrunk(world, xPos, yPos, zPos, height);
return true;
}
}
return false;
}
boolean checkClear (World world, int x, int y, int z, int treeHeight)
{
for (int yPos = 0; yPos < treeHeight + 1; yPos++)
{
int range = 1;
if (yPos == 0)
range = 0;
else if (yPos >= treeHeight - 1)
range = 2;
for (int xPos = range; xPos <= range; xPos++)
{
for (int zPos = range; zPos <= range; zPos++)
{
Block blockID = world.getBlock(x + xPos, y + yPos, z + zPos);
if (blockID != null && blockID != TRepo.slimeSapling && !blockID.isLeaves(world, x + xPos, y + yPos, z + zPos))
return false;
}
}
}
return true;
}
int findGround (World world, int x, int y, int z)
{
int ret = -1;
int height = y;
do
{
Block heightID = world.getBlock(x, height, z);
if ((heightID == TRepo.craftedSoil || heightID == TRepo.slimeGrass) && !world.getBlock(x, height + 1, z).isOpaqueCube())
{
ret = height + 1;
break;
}
height--;
} while (height > 0);
return ret;
}
void placeCanopy (World world, Random random, int xPos, int yPos, int zPos, int height)
{
for (int i = 0; i < 4; i++)
{
placeDiamondLayer(world, xPos, yPos + height - i, zPos, i + 1);
}
this.setBlockAndMetadata(world, xPos + 4, yPos + height - 3, zPos, Blocks.air, 0);
this.setBlockAndMetadata(world, xPos - 4, yPos + height - 3, zPos, Blocks.air, 0);
this.setBlockAndMetadata(world, xPos, yPos + height - 3, zPos + 4, Blocks.air, 0);
this.setBlockAndMetadata(world, xPos, yPos + height - 3, zPos - 4, Blocks.air, 0);
this.setBlockAndMetadata(world, xPos + 1, yPos + height - 3, zPos + 1, Blocks.air, 0);
this.setBlockAndMetadata(world, xPos + 1, yPos + height - 3, zPos - 1, Blocks.air, 0);
this.setBlockAndMetadata(world, xPos - 1, yPos + height - 3, zPos + 1, Blocks.air, 0);
this.setBlockAndMetadata(world, xPos - 1, yPos + height - 3, zPos - 1, Blocks.air, 0);
//Drippers
this.setBlockAndMetadata(world, xPos + 3, yPos + height - 4, zPos, TRepo.slimeLeaves, this.metaLeaves);
this.setBlockAndMetadata(world, xPos - 3, yPos + height - 4, zPos, TRepo.slimeLeaves, this.metaLeaves);
this.setBlockAndMetadata(world, xPos, yPos + height - 4, zPos - 3, TRepo.slimeLeaves, this.metaLeaves);
this.setBlockAndMetadata(world, xPos, yPos + height - 4, zPos + 3, TRepo.slimeLeaves, this.metaLeaves);
this.setBlockAndMetadata(world, xPos + 2, yPos + height - 4, zPos + 2, TRepo.slimeLeaves, this.metaLeaves);
this.setBlockAndMetadata(world, xPos + 2, yPos + height - 4, zPos - 2, TRepo.slimeLeaves, this.metaLeaves);
this.setBlockAndMetadata(world, xPos - 2, yPos + height - 4, zPos + 2, TRepo.slimeLeaves, this.metaLeaves);
this.setBlockAndMetadata(world, xPos - 2, yPos + height - 4, zPos - 2, TRepo.slimeLeaves, this.metaLeaves);
this.setBlockAndMetadata(world, xPos + 3, yPos + height - 5, zPos, TRepo.slimeLeaves, this.metaLeaves);
this.setBlockAndMetadata(world, xPos - 3, yPos + height - 5, zPos, TRepo.slimeLeaves, this.metaLeaves);
this.setBlockAndMetadata(world, xPos, yPos + height - 5, zPos - 3, TRepo.slimeLeaves, this.metaLeaves);
this.setBlockAndMetadata(world, xPos, yPos + height - 5, zPos + 3, TRepo.slimeLeaves, this.metaLeaves);
this.setBlockAndMetadata(world, xPos + 2, yPos + height - 5, zPos + 2, TRepo.slimeLeaves, this.metaLeaves);
this.setBlockAndMetadata(world, xPos + 2, yPos + height - 5, zPos - 2, TRepo.slimeLeaves, this.metaLeaves);
this.setBlockAndMetadata(world, xPos - 2, yPos + height - 5, zPos + 2, TRepo.slimeLeaves, this.metaLeaves);
this.setBlockAndMetadata(world, xPos - 2, yPos + height - 5, zPos - 2, TRepo.slimeLeaves, this.metaLeaves);
}
void placeDiamondLayer (World world, int xPos, int yPos, int zPos, int range)
{
for (int x = -range; x <= range; x++)
{
for (int z = -range; z <= range; z++)
{
if (Math.abs(x) + Math.abs(z) <= range)
{
this.setBlockAndMetadata(world, x + xPos, yPos, z + zPos, TRepo.slimeLeaves, this.metaLeaves);
}
}
}
}
void placeTrunk (World world, int xPos, int yPos, int zPos, int height)
{
for (int localHeight = 0; localHeight < height; ++localHeight)
{
Block blockID = world.getBlock(xPos, yPos + localHeight, zPos);
if (blockID == Blocks.air || blockID == null || blockID.isLeaves(world, xPos, yPos + localHeight, zPos))
{
this.setBlockAndMetadata(world, xPos, yPos + localHeight, zPos, TRepo.slimeGel, this.metaWood);
}
}
}
protected void setBlockAndMetadata (World world, int x, int y, int z, Block blockID, int blockMeta)
{
Block block = world.getBlock(x, y, z);
if (block == null || block.canPlaceBlockAt(world, x, y, z))
{
if (this.notify)
{
world.setBlock(x, y, z, blockID, blockMeta, 3);
}
else
{
world.setBlock(x, y, z, blockID, blockMeta, 2);
}
}
}
}