forked from CloudburstMC/Nukkit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlockCactus.java
More file actions
166 lines (143 loc) · 4.59 KB
/
BlockCactus.java
File metadata and controls
166 lines (143 loc) · 4.59 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
package cn.nukkit.block;
import cn.nukkit.Player;
import cn.nukkit.Server;
import cn.nukkit.entity.Entity;
import cn.nukkit.event.block.BlockGrowEvent;
import cn.nukkit.event.entity.EntityDamageByBlockEvent;
import cn.nukkit.event.entity.EntityDamageEvent.DamageCause;
import cn.nukkit.item.Item;
import cn.nukkit.level.Level;
import cn.nukkit.level.format.FullChunk;
import cn.nukkit.math.AxisAlignedBB;
import cn.nukkit.math.BlockFace;
import cn.nukkit.math.SimpleAxisAlignedBB;
import cn.nukkit.utils.BlockColor;
/**
* @author Nukkit Project Team
*/
public class BlockCactus extends BlockTransparentMeta {
public BlockCactus(int meta) {
super(meta);
}
public BlockCactus() {
this(0);
}
@Override
public int getId() {
return CACTUS;
}
@Override
public double getHardness() {
return 0.4;
}
@Override
public double getResistance() {
return 2;
}
@Override
public boolean hasEntityCollision() {
return true;
}
@Override
public double getMinX() {
return this.x + 0.0625;
}
@Override
public double getMinZ() {
return this.z + 0.0625;
}
@Override
public double getMaxX() {
return this.x + 0.9375;
}
@Override
public double getMaxZ() {
return this.z + 0.9375;
}
@Override
protected AxisAlignedBB recalculateCollisionBoundingBox() {
return new SimpleAxisAlignedBB(x, y, z, x + 1, y + 1, z + 1);
}
@Override
public double getMaxY() {
return this.y + 0.9375;
}
@Override
public void onEntityCollide(Entity entity) {
entity.attack(new EntityDamageByBlockEvent(this, entity, DamageCause.CONTACT, 1));
}
@Override
public int onUpdate(int type) {
if (type == Level.BLOCK_UPDATE_NORMAL) {
Block down = down();
if (down.getId() != SAND && down.getId() != CACTUS) {
this.getLevel().useBreakOn(this);
} else {
for (BlockFace side : BlockFace.Plane.HORIZONTAL) {
if (!getSide(side).canBeFlowedInto()) {
this.getLevel().useBreakOn(this);
}
}
}
} else if (type == Level.BLOCK_UPDATE_RANDOM) {
if (down().getId() != CACTUS) {
if (this.getDamage() == 0x0F) {
FullChunk chunk = this.level.getChunk((int) x >> 4, (int) z >> 4);
for (int y = 1; y < 3; ++y) {
Block b = this.getLevel().getBlock(chunk, (int) this.x, (int) this.y + y, (int) this.z, true);
if (b.getId() == AIR) {
BlockGrowEvent event = new BlockGrowEvent(b, Block.get(CACTUS));
Server.getInstance().getPluginManager().callEvent(event);
if (!event.isCancelled()) {
this.getLevel().setBlock(b, event.getNewState(), true, true);
}
break;
}
}
this.setDamage(0);
} else {
this.setDamage(this.getDamage() + 1);
}
this.level.setBlock((int) this.x, (int) this.y, (int) this.z, BlockLayer.NORMAL, this, false, true, false); // No need to send this to client
}
}
return 0;
}
@Override
public boolean place(Item item, Block block, Block target, BlockFace face, double fx, double fy, double fz, Player player) {
Block down = this.down();
if (down.getId() == SAND || down.getId() == CACTUS) {
Block block0 = north();
Block block1 = south();
Block block2 = west();
Block block3 = east();
if (block0.canBeFlowedInto() && block1.canBeFlowedInto() && block2.canBeFlowedInto() && block3.canBeFlowedInto()) {
this.getLevel().setBlock(this, this, true, true);
return true;
}
}
return false;
}
@Override
public String getName() {
return "Cactus";
}
@Override
public BlockColor getColor() {
return BlockColor.FOLIAGE_BLOCK_COLOR;
}
@Override
public Item[] getDrops(Item item) {
return new Item[]{
Item.get(Item.CACTUS, 0, 1)
};
}
@Override
public WaterloggingType getWaterloggingType() {
return WaterloggingType.WHEN_PLACED_IN_WATER;
}
@Override
public boolean breakWhenPushed() {
return true;
}
}