forked from CloudburstMC/Nukkit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlockTripWireHook.java
More file actions
258 lines (210 loc) · 7.63 KB
/
BlockTripWireHook.java
File metadata and controls
258 lines (210 loc) · 7.63 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
package cn.nukkit.block;
import cn.nukkit.Player;
import cn.nukkit.event.block.BlockRedstoneEvent;
import cn.nukkit.item.Item;
import cn.nukkit.item.ItemBlock;
import cn.nukkit.level.Level;
import cn.nukkit.math.BlockFace;
import cn.nukkit.math.Vector3;
import cn.nukkit.network.protocol.LevelSoundEventPacket;
/**
* @author CreeperFace
*/
public class BlockTripWireHook extends BlockFlowable {
public BlockTripWireHook() {
this(0);
}
public BlockTripWireHook(int meta) {
super(meta);
}
@Override
public String getName() {
return "Tripwire Hook";
}
@Override
public int getId() {
return TRIPWIRE_HOOK;
}
public BlockFace getFacing() {
return BlockFace.fromHorizontalIndex(getDamage() & 0b11);
}
@Override
public int onUpdate(int type) {
if (type == Level.BLOCK_UPDATE_NORMAL) {
if (!isSupportValid(this.getSide(this.getFacing().getOpposite()))) {
this.level.useBreakOn(this);
}
return type;
} else if (type == Level.BLOCK_UPDATE_SCHEDULED) {
this.calculateState(false, true, -1, null);
return type;
}
return 0;
}
@Override
public boolean place(Item item, Block block, Block target, BlockFace face, double fx, double fy, double fz, Player player) {
if (face == BlockFace.DOWN || face == BlockFace.UP) {
return false;
}
if (face.getAxis().isHorizontal()) {
this.setFace(face);
}
if (!isSupportValid(this.getSide(this.getFacing().getOpposite()))) {
return false;
}
this.level.setBlock(this, this);
if (player != null) {
this.calculateState(false, false, -1, null);
}
return true;
}
private static boolean isSupportValid(Block block) {
return !block.isTransparent() || Block.canConnectToFullSolid(block);
}
@Override
public boolean onBreak(Item item) {
super.onBreak(item);
boolean attached = isAttached();
boolean powered = isPowered();
if (attached || powered) {
this.calculateState(true, false, -1, null);
}
if (powered) {
this.level.updateAroundRedstone(this, null);
this.level.updateAroundRedstone(this.getSideVec(getFacing().getOpposite()), null);
}
return true;
}
public void calculateState(boolean onBreak, boolean updateAround, int pos, Block block) {
BlockFace facing = getFacing();
boolean attached = isAttached();
boolean powered = isPowered();
boolean canConnect = !onBreak;
boolean nextPowered = false;
int distance = 0;
Block[] blocks = new Block[42];
for (int i = 1; i < 42; ++i) {
Block b = this.getSide(facing, i);
if (b instanceof BlockTripWireHook) {
if (((BlockTripWireHook) b).getFacing() == facing.getOpposite()) {
distance = i;
}
break;
}
if (b.getId() != Block.TRIPWIRE && i != pos) {
blocks[i] = null;
canConnect = false;
} else {
if (i == pos) {
b = block != null ? block : b;
}
if (b instanceof BlockTripWire) {
boolean disarmed = !((BlockTripWire) b).isDisarmed();
boolean wirePowered = ((BlockTripWire) b).isPowered();
nextPowered |= disarmed && wirePowered;
if (i == pos) {
this.level.scheduleUpdate(this, 10);
canConnect &= disarmed;
}
}
blocks[i] = b;
}
}
canConnect = canConnect & distance > 1;
nextPowered = nextPowered & canConnect;
BlockTripWireHook hook = (BlockTripWireHook) Block.get(TRIPWIRE_HOOK);
hook.setAttached(canConnect);
hook.setPowered(nextPowered);
if (distance > 0) {
Vector3 vec = this.getSideVec(facing, distance);
BlockFace face = facing.getOpposite();
hook.setFace(face);
this.level.setBlock(vec, hook, true, true);
this.level.updateAroundRedstone(vec, null);
this.level.updateAroundRedstone(vec.getSideVec(face.getOpposite()), null);
this.addSound(vec, canConnect, nextPowered, attached, powered);
}
this.addSound(this, canConnect, nextPowered, attached, powered);
if (!onBreak) {
hook.setFace(facing);
this.level.setBlock(this, hook, true, true);
if (updateAround) {
this.level.updateAroundRedstone(this, null);
this.level.updateAroundRedstone(this.getSideVec(facing.getOpposite()), null);
}
}
if (attached != canConnect) {
for (int i = 1; i < distance; i++) {
Vector3 vc = this.getSideVec(facing, i);
block = blocks[i];
if (block != null && this.level.getBlockIdAt(vc.getFloorX(), vc.getFloorY(), vc.getFloorZ()) != Block.AIR) {
if (canConnect ^ ((block.getDamage() & 0x04) > 0)) {
block.setDamage(block.getDamage() ^ 0x04);
}
this.level.setBlock(vc, block, true, true);
}
}
}
}
private void addSound(Vector3 pos, boolean canConnect, boolean nextPowered, boolean attached, boolean powered) {
if (nextPowered && !powered) {
this.level.addLevelSoundEvent(pos, LevelSoundEventPacket.SOUND_POWER_ON);
this.level.getServer().getPluginManager().callEvent(new BlockRedstoneEvent(this, 0, 15));
} else if (!nextPowered && powered) {
this.level.addLevelSoundEvent(pos, LevelSoundEventPacket.SOUND_POWER_OFF);
this.level.getServer().getPluginManager().callEvent(new BlockRedstoneEvent(this, 15, 0));
} else if (canConnect && !attached) {
this.level.addLevelSoundEvent(pos, LevelSoundEventPacket.SOUND_ATTACH);
} else if (!canConnect && attached) {
this.level.addLevelSoundEvent(pos, LevelSoundEventPacket.SOUND_DETACH);
}
}
public boolean isAttached() {
return (getDamage() & 0x04) > 0;
}
public boolean isPowered() {
return (this.getDamage() & 0x08) > 0;
}
public void setPowered(boolean value) {
if (value ^ this.isPowered()) {
this.setDamage(this.getDamage() ^ 0x08);
}
}
public void setAttached(boolean value) {
if (value ^ this.isAttached()) {
this.setDamage(this.getDamage() ^ 0x04);
}
}
public void setFace(BlockFace face) {
this.setDamage(this.getDamage() - this.getDamage() % 4);
this.setDamage(this.getDamage() | face.getHorizontalIndex());
}
@Override
public boolean isPowerSource() {
return true;
}
@Override
public int getWeakPower(BlockFace face) {
return isPowered() ? 15 : 0;
}
@Override
public int getStrongPower(BlockFace side) {
return !isPowered() ? 0 : getFacing() == side ? 15 : 0;
}
@Override
public Item toItem() {
return new ItemBlock(Block.get(this.getId(), 0), 0);
}
@Override
public WaterloggingType getWaterloggingType() {
return WaterloggingType.FLOW_INTO_BLOCK;
}
@Override
public boolean canBeFlowedInto() {
return false;
}
@Override
public boolean breakWhenPushed() {
return true;
}
}