forked from CloudburstMC/Nukkit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlockLeaves.java
More file actions
228 lines (190 loc) · 6.23 KB
/
BlockLeaves.java
File metadata and controls
228 lines (190 loc) · 6.23 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
package cn.nukkit.block;
import cn.nukkit.Player;
import cn.nukkit.Server;
import cn.nukkit.event.block.LeavesDecayEvent;
import cn.nukkit.item.Item;
import cn.nukkit.item.ItemBlock;
import cn.nukkit.item.ItemTool;
import cn.nukkit.item.enchantment.Enchantment;
import cn.nukkit.level.Level;
import cn.nukkit.math.BlockFace;
import cn.nukkit.utils.BlockColor;
import cn.nukkit.utils.Utils;
import java.util.*;
import java.util.concurrent.ThreadLocalRandom;
/**
* @author Angelic47
* Nukkit Project
*/
public class BlockLeaves extends BlockTransparentMeta {
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 BlockLeaves() {
this(0);
}
public BlockLeaves(int meta) {
super(meta);
}
@Override
public int getId() {
return LEAVES;
}
@Override
public double getHardness() {
return 0.2;
}
@Override
public int getToolType() {
return ItemTool.TYPE_HOE;
}
private static final String[] NAMES = {
"Oak Leaves",
"Spruce Leaves",
"Birch Leaves",
"Jungle Leaves"
};
@Override
public String getName() {
return NAMES[this.getDamage() & 0x03];
}
@Override
public int getBurnChance() {
return 30;
}
@Override
public int getBurnAbility() {
return 60;
}
@Override
public boolean place(Item item, Block block, Block target, BlockFace face, double fx, double fy, double fz, Player player) {
setPersistent(true);
this.getLevel().setBlock(this, this, true, true);
return true;
}
@Override
public Item toItem() {
return new ItemBlock(this, this.getDamage() & 0x3, 1);
}
@Override
public Item[] getDrops(Item item) {
if (item.isShears()) {
return new Item[]{
toItem()
};
} else {
if (item.hasEnchantment(Enchantment.ID_SILK_TOUCH)) {
return new Item[]{this.toItem()};
}
if (this.canDropApple() && ThreadLocalRandom.current().nextInt(200) == 0) {
return new Item[]{
Item.get(Item.APPLE)
};
}
if (ThreadLocalRandom.current().nextInt(20) == 0) {
if (Utils.rand()) {
return new Item[]{
Item.get(Item.STICK, 0, ThreadLocalRandom.current().nextInt(1, 2))
};
} else if ((this.getDamage() & 0x03) != JUNGLE || ThreadLocalRandom.current().nextInt(20) == 0) {
return new Item[]{
this.getSapling()
};
}
}
}
return new Item[0];
}
@Override
public int onUpdate(int type) {
if (type == Level.BLOCK_UPDATE_NORMAL && !isPersistent() && !isCheckDecay()) {
if (this.level.getBlockIdAt((int) this.x, (int) this.y, (int) this.z) != this.getId()) {
return 0;
}
setCheckDecay(true);
getLevel().setBlock((int) this.x, (int) this.y, (int) this.z, BlockLayer.NORMAL, this, false, false, false); // No need to send this to client
return Level.BLOCK_UPDATE_NORMAL;
} else if (type == Level.BLOCK_UPDATE_RANDOM && isCheckDecay() && !isPersistent()) {
LeavesDecayEvent ev = new LeavesDecayEvent(this);
Server.getInstance().getPluginManager().callEvent(ev);
if (ev.isCancelled() || findLog()) {
setCheckDecay(false);
getLevel().setBlock((int) this.x, (int) this.y, (int) this.z, BlockLayer.NORMAL, this, false, false, false); // No need to send this to client
} else {
getLevel().useBreakOn(this);
}
return Level.BLOCK_UPDATE_RANDOM;
}
return 0;
}
public boolean isCheckDecay() {
return (this.getDamage() & 0x08) != 0;
}
public void setCheckDecay(boolean checkDecay) {
if (checkDecay) {
this.setDamage(this.getDamage() | 0x08);
} else {
this.setDamage(this.getDamage() & -9);
}
}
public boolean isPersistent() {
return (this.getDamage() & 0x04) != 0;
}
public void setPersistent(boolean persistent) {
if (persistent) {
this.setDamage(this.getDamage() | 0x04);
} else {
this.setDamage(this.getDamage() & -5);
}
}
@Override
public BlockColor getColor() {
return BlockColor.FOLIAGE_BLOCK_COLOR;
}
@Override
public boolean canSilkTouch() {
return true;
}
protected boolean canDropApple() {
return (this.getDamage() & 0x03) == OAK;
}
protected Item getSapling() {
return Item.get(BlockID.SAPLING, this.getDamage() & 0x03);
}
@Override
public WaterloggingType getWaterloggingType() {
return WaterloggingType.WHEN_PLACED_IN_WATER;
}
@Override
public boolean breakWhenPushed() {
return true;
}
private boolean findLog() {
Set<Block> visited = new HashSet<>();
Queue<Block> queue = new LinkedList<>();
Map<Block, Integer> distance = new HashMap<>();
queue.offer(this);
visited.add(this);
distance.put(this, 0);
while (!queue.isEmpty()) {
Block currentBlock = queue.poll();
int currentDistance = distance.get(currentBlock);
if (currentDistance > 4) {
return false;
}
for (BlockFace face : BlockFace.values()) {
Block nextBlock = currentBlock.getSideIfLoadedOrNull(face); // If side chunk not loaded, do not load or decay
if (nextBlock == null || nextBlock instanceof BlockWood) {
return true;
}
if (nextBlock instanceof BlockLeaves && !visited.contains(nextBlock)) {
queue.offer(nextBlock);
visited.add(nextBlock);
distance.put(nextBlock, currentDistance + 1);
}
}
}
return false;
}
}