forked from CloudburstMC/Nukkit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlockChorusFlower.java
More file actions
259 lines (227 loc) · 8.96 KB
/
BlockChorusFlower.java
File metadata and controls
259 lines (227 loc) · 8.96 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
package cn.nukkit.block;
import cn.nukkit.Player;
import cn.nukkit.Server;
import cn.nukkit.entity.Entity;
import cn.nukkit.entity.item.EntityFirework;
import cn.nukkit.entity.projectile.EntityArrow;
import cn.nukkit.entity.projectile.EntityEgg;
import cn.nukkit.entity.projectile.EntitySnowball;
import cn.nukkit.entity.projectile.EntityThrownTrident;
import cn.nukkit.event.block.BlockGrowEvent;
import cn.nukkit.item.Item;
import cn.nukkit.item.ItemBlock;
import cn.nukkit.item.ItemTool;
import cn.nukkit.level.Level;
import cn.nukkit.math.BlockFace;
import cn.nukkit.network.protocol.LevelSoundEventPacket;
import cn.nukkit.utils.BlockColor;
import java.util.concurrent.ThreadLocalRandom;
public class BlockChorusFlower extends BlockTransparentMeta {
// Version 7a3d8a5
public BlockChorusFlower() {
super(0);
}
public BlockChorusFlower(int meta) {
super(meta);
}
@Override
public int getId() {
return CHORUS_FLOWER;
}
@Override
public String getName() {
return "Chorus Flower";
}
@Override
public double getHardness() {
return 0.4;
}
@Override
public double getResistance() {
return 0.4;
}
@Override
public int getToolType() {
return ItemTool.TYPE_AXE;
}
private boolean isPositionValid() {
// Chorus flowers must be above end stone or chorus plant, or be above air and horizontally adjacent to exactly one chorus plant.
// If these conditions are not met, the block breaks without dropping anything.
Block down = down();
if (down.getId() == CHORUS_PLANT || down.getId() == END_STONE) {
return true;
}
if (down.getId() != AIR) {
return false;
}
boolean foundPlant = false;
for (BlockFace face : BlockFace.Plane.HORIZONTAL) {
Block side = getSide(face);
if (side.getId() == CHORUS_PLANT) {
if (foundPlant) {
return false;
}
foundPlant = true;
}
}
return foundPlant;
}
@Override
public int onUpdate(int type) {
if (type == Level.BLOCK_UPDATE_NORMAL) {
if (!isPositionValid()) {
this.getLevel().scheduleUpdate(this, 1);
return type;
}
} else if (type == Level.BLOCK_UPDATE_SCHEDULED) {
this.getLevel().useBreakOn(this, null, null, true);
return type;
} else if (type == Level.BLOCK_UPDATE_RANDOM) {
// Check limit
Block up;
if (this.y < level.getMaxBlockY() && (up = this.up()).getId() == AIR) {
if (!isFullyAged()) {
boolean growUp = false; // Grow upward?
boolean ground = false; // Is on the ground directly?
Block down = this.down();
if (down.getId() == AIR || down.getId() == END_STONE) {
growUp = true;
} else if (down.getId() == CHORUS_PLANT) {
int height = 1;
for (int y = 2; y < 6; y++) {
Block downY = this.down(y);
if (downY.getId() == CHORUS_PLANT) {
height++;
} else {
if (downY.getId() == END_STONE) {
ground = true;
}
break;
}
}
if (height < 2 || height <= ThreadLocalRandom.current().nextInt(ground ? 5 : 4)) {
growUp = true;
}
}
// Grow Upward
if (growUp && up.up().getId() == AIR && isHorizontalAir(up)) {
BlockChorusFlower block = (BlockChorusFlower) this.clone();
block.y = this.y + 1;
BlockGrowEvent ev = new BlockGrowEvent(this, block);
Server.getInstance().getPluginManager().callEvent(ev);
if (!ev.isCancelled()) {
this.getLevel().setBlock(this, Block.get(CHORUS_PLANT));
this.getLevel().setBlock(block, ev.getNewState());
this.level.addLevelSoundEvent(this, LevelSoundEventPacket.SOUND_CHORUSGROW);
} else {
return Level.BLOCK_UPDATE_RANDOM;
}
// Grow Horizontally
} else if (!isFullyAged()) {
ThreadLocalRandom random = ThreadLocalRandom.current();
for (int i = 0; i < random.nextInt(ground ? 5 : 4); i++) {
BlockFace face = BlockFace.Plane.HORIZONTAL.random();
Block check = this.getSide(face);
if (check.getId() == AIR && check.down().getId() == AIR && isHorizontalAirExcept(check, face.getOpposite())) {
BlockChorusFlower block = (BlockChorusFlower) this.clone();
block.x = check.x;
block.y = check.y;
block.z = check.z;
block.setAge(getAge() + 1);
BlockGrowEvent ev = new BlockGrowEvent(this, block);
Server.getInstance().getPluginManager().callEvent(ev);
if (!ev.isCancelled()) {
this.getLevel().setBlock(this, Block.get(CHORUS_PLANT));
this.getLevel().setBlock(block, ev.getNewState());
this.level.addLevelSoundEvent(this, LevelSoundEventPacket.SOUND_CHORUSGROW);
} else {
return Level.BLOCK_UPDATE_RANDOM;
}
}
}
// Death
} else {
BlockChorusFlower block = (BlockChorusFlower) this.clone();
block.setAge(getMaxAge());
BlockGrowEvent ev = new BlockGrowEvent(this, block);
Server.getInstance().getPluginManager().callEvent(ev);
if (!ev.isCancelled()) {
this.getLevel().setBlock(block, ev.getNewState());
this.level.addLevelSoundEvent(this, LevelSoundEventPacket.SOUND_CHORUSDEATH);
} else {
return Level.BLOCK_UPDATE_RANDOM;
}
}
}
} else {
return Level.BLOCK_UPDATE_RANDOM;
}
}
return 0;
}
@Override
public boolean place(Item item, Block block, Block target, BlockFace face, double fx, double fy, double fz, Player player) {
if (!isPositionValid()) {
return false;
}
return super.place(item, block, target, face, fx, fy, fz, player);
}
@Override
public Item[] getDrops(Item item) {
return new Item[]{this.toItem()};
}
@Override
public Item toItem() {
return new ItemBlock(Block.get(this.getId(), 0), 0);
}
@Override
public boolean hasEntityCollision() {
return true;
}
@Override
public void onEntityCollide(Entity entity) {
int e = entity.getNetworkId();
if (e == EntityArrow.NETWORK_ID || e == EntityThrownTrident.NETWORK_ID || e == EntityFirework.NETWORK_ID || e == EntitySnowball.NETWORK_ID || e == EntityEgg.NETWORK_ID || e == 85 || e == 94 || e == 79 || e == 89) {
entity.close();
this.getLevel().useBreakOn(this);
}
}
public int getMaxAge() {
return 5;
}
public int getAge() {
return getDamage();
}
public void setAge(int age) {
this.setDamage(age);
}
public boolean isFullyAged() {
return getAge() >= getMaxAge();
}
private boolean isHorizontalAir(Block block) {
for (BlockFace face : BlockFace.Plane.HORIZONTAL) {
if (block.getSide(face).getId() != AIR) {
return false;
}
}
return true;
}
private boolean isHorizontalAirExcept(Block block, BlockFace except) {
for (BlockFace face : BlockFace.Plane.HORIZONTAL) {
if (face != except) {
if (block.getSide(face).getId() != AIR) {
return false;
}
}
}
return true;
}
@Override
public boolean breakWhenPushed() {
return true;
}
@Override
public BlockColor getColor() {
return BlockColor.PURPLE_BLOCK_COLOR;
}
}