forked from CloudburstMC/Nukkit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlockSapling.java
More file actions
283 lines (246 loc) · 9.87 KB
/
BlockSapling.java
File metadata and controls
283 lines (246 loc) · 9.87 KB
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package cn.nukkit.block;
import cn.nukkit.Player;
import cn.nukkit.event.level.StructureGrowEvent;
import cn.nukkit.item.Item;
import cn.nukkit.item.ItemDye;
import cn.nukkit.level.ChunkManager;
import cn.nukkit.level.Level;
import cn.nukkit.level.ListChunkManager;
import cn.nukkit.level.generator.object.BasicGenerator;
import cn.nukkit.level.generator.object.tree.*;
import cn.nukkit.level.particle.BoneMealParticle;
import cn.nukkit.math.BlockFace;
import cn.nukkit.math.NukkitRandom;
import cn.nukkit.math.Vector2;
import cn.nukkit.math.Vector3;
import cn.nukkit.utils.BlockColor;
import cn.nukkit.utils.Utils;
import java.util.concurrent.ThreadLocalRandom;
/**
* @author Angelic47
* Nukkit Project
*/
public class BlockSapling extends BlockFlowable {
public static final int OAK = 0;
public static final int SPRUCE = 1;
public static final int BIRCH = 2;
public static final int JUNGLE = 3;
public static final int ACACIA = 4;
public static final int DARK_OAK = 5;
public static final int BIRCH_TALL = 10;
public BlockSapling() {
this(0);
}
public BlockSapling(int meta) {
super(meta);
}
@Override
public int getId() {
return SAPLING;
}
private static final String[] NAMES = {
"Oak Sapling",
"Spruce Sapling",
"Birch Sapling",
"Jungle Sapling",
"Acacia Sapling",
"Dark Oak Sapling",
"",
""
};
@Override
public String getName() {
return NAMES[this.getDamage() & 0x07];
}
@Override
public boolean place(Item item, Block block, Block target, BlockFace face, double fx, double fy, double fz, Player player) {
if (!(this instanceof BlockMangrovePropagule) &&
(block instanceof BlockWater || block.level.isBlockWaterloggedAt(block.getChunk(), (int) block.x, (int) block.y, (int) block.z))) {
return false;
}
Block down = this.down();
int id = down.getId();
if (id == Block.GRASS || id == Block.DIRT || id == Block.FARMLAND || id == Block.PODZOL || id == MYCELIUM || id == MOSS_BLOCK || id == MUD) {
this.getLevel().setBlock(block, this, true, true);
return true;
}
return false;
}
@Override
public boolean canBeActivated() {
return true;
}
public boolean onActivate(Item item, Player player) {
if (item.getId() == Item.DYE && item.getDamage() == ItemDye.BONE_MEAL) {
if (player != null && !player.isCreative()) {
item.count--;
}
this.level.addParticle(new BoneMealParticle(this));
if (ThreadLocalRandom.current().nextFloat() >= 0.45) {
return true;
}
return growTreeHere();
}
return false;
}
protected boolean growTreeHere() {
BasicGenerator generator = null;
boolean bigTree = false;
Vector3 vector3 = new Vector3(this.x, this.y - 1, this.z);
int woodType = this.getDamage() & 0x7;
switch (woodType) {
case JUNGLE:
Vector2 vector2;
if ((vector2 = this.findSaplings(JUNGLE)) != null) {
vector3 = this.add(vector2.getFloorX(), 0, vector2.getFloorY());
generator = new ObjectJungleBigTree(10, 20, Block.get(BlockID.WOOD, BlockWood.JUNGLE), Block.get(BlockID.LEAVES, BlockLeaves.JUNGLE));
bigTree = true;
}
if (!bigTree) {
generator = new NewJungleTree(4, 7);
vector3 = this.add(0, 0, 0);
}
break;
case ACACIA:
generator = new ObjectSavannaTree();
vector3 = this.add(0, 0, 0);
break;
case DARK_OAK:
if ((vector2 = this.findSaplings(DARK_OAK)) != null) {
vector3 = this.add(vector2.getFloorX(), 0, vector2.getFloorY());
generator = new ObjectDarkOakTree();
bigTree = true;
}
if (!bigTree) {
return false;
}
break;
case SPRUCE:
if ((vector2 = this.findSaplings(SPRUCE)) != null) {
vector3 = this.add(vector2.getFloorX(), 0, vector2.getFloorY());
generator = new HugeTreesGenerator(0, 0, null, null) {
@Override
public boolean generate(ChunkManager level, NukkitRandom rand, Vector3 position) {
ObjectBigSpruceTree object = new ObjectBigSpruceTree(0.25f, 4);
if (!this.ensureGrowable(level, position, object.getTreeHeight())) {
return false;
}
object.placeObject(level, position.getFloorX(), position.getFloorY(), position.getFloorZ(), rand);
return true;
}
};
bigTree = true;
}
if (bigTree) {
break;
}
default:
ListChunkManager chunkManager = new ListChunkManager(this.level);
ObjectTree.growTree(chunkManager, this.getFloorX(), this.getFloorY(), this.getFloorZ(), new NukkitRandom(), woodType);
StructureGrowEvent ev = new StructureGrowEvent(this, chunkManager.getBlocks());
this.level.getServer().getPluginManager().callEvent(ev);
if (ev.isCancelled()) {
return false;
}
for (Block block : ev.getBlockList()) {
this.level.setBlock(block, block);
}
return true;
}
if (bigTree) {
this.level.setBlock(vector3, Block.get(AIR), true, true);
this.level.setBlock(vector3.add(1, 0, 0), Block.get(AIR), true, true);
this.level.setBlock(vector3.add(0, 0, 1), Block.get(AIR), true, true);
this.level.setBlock(vector3.add(1, 0, 1), Block.get(AIR), true, true);
} else {
this.level.setBlock(this, Block.get(AIR), true, true);
}
ListChunkManager chunkManager = new ListChunkManager(this.level);
boolean success = generator.generate(chunkManager, new NukkitRandom(), vector3);
StructureGrowEvent ev = new StructureGrowEvent(this, chunkManager.getBlocks());
this.level.getServer().getPluginManager().callEvent(ev);
if (ev.isCancelled() || !success) {
if (bigTree) {
this.level.setBlock(vector3, this, true, false);
this.level.setBlock(vector3.add(1, 0, 0), this, true, false);
this.level.setBlock(vector3.add(0, 0, 1), this, true, false);
this.level.setBlock(vector3.add(1, 0, 1), this, true, false);
} else {
this.level.setBlock(this, this, true, false);
}
return false;
}
for (Block block : ev.getBlockList()) {
this.level.setBlock(block, block);
}
return true;
}
public int onUpdate(int type) {
if (type == Level.BLOCK_UPDATE_NORMAL) {
if (this.down().isTransparent()) {
this.getLevel().useBreakOn(this);
return Level.BLOCK_UPDATE_NORMAL;
}
} else if (type == Level.BLOCK_UPDATE_RANDOM) { //Growth
if (Utils.rand(1, 7) == 1) {
if ((this.getDamage() & 0x08) == 0x08) {
growTreeHere();
} else {
this.setDamage(this.getDamage() | 0x08);
this.getLevel().setBlock(this, this, true);
return Level.BLOCK_UPDATE_RANDOM;
}
} else {
return Level.BLOCK_UPDATE_RANDOM;
}
}
return 1;
}
public boolean isSameType(Vector3 pos, int type) {
Block block = this.level.getBlock(pos);
return block.getId() == this.getId() && (block.getDamage() & 0x07) == (type & 0x07);
}
@Override
public Item toItem() {
return Item.get(BlockID.SAPLING, this.getDamage() & 0x7);
}
@Override
public BlockColor getColor() {
return BlockColor.FOLIAGE_BLOCK_COLOR;
}
@Override
public boolean breakWhenPushed() {
return true;
}
private static final Vector2[][] VALID_SAPLINGS = new Vector2[4][4];
static {
VALID_SAPLINGS[0] = new Vector2[]{new Vector2(0, 0), new Vector2(1, 0), new Vector2(0, 1), new Vector2(1, 1)};
VALID_SAPLINGS[1] = new Vector2[]{new Vector2(0, 0), new Vector2(-1, 0), new Vector2(0, -1), new Vector2(-1, -1)};
VALID_SAPLINGS[2] = new Vector2[]{new Vector2(0, 0), new Vector2(1, 0), new Vector2(0, -1), new Vector2(1, -1)};
VALID_SAPLINGS[3] = new Vector2[]{new Vector2(0, 0), new Vector2(-1, 0), new Vector2(0, 1), new Vector2(-1, 1)};
}
private Vector2 findSaplings(int type) {
for (Vector2[] validVectors : VALID_SAPLINGS) {
boolean found = true;
for (Vector2 vector2 : validVectors) {
if (!this.isSameType(this.add(vector2.x, 0, vector2.y), type)) {
found = false;
}
}
if (found) {
int lowestX = 0;
int lowestZ = 0;
for (Vector2 vector2 : validVectors) {
if (vector2.getFloorX() < lowestX) {
lowestX = vector2.getFloorX();
}
if (vector2.getFloorY() < lowestZ) {
lowestZ = vector2.getFloorY();
}
}
return new Vector2(lowestX, lowestZ);
}
}
return null;
}
}