forked from CloudburstMC/Nukkit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlockButton.java
More file actions
158 lines (128 loc) · 4.28 KB
/
BlockButton.java
File metadata and controls
158 lines (128 loc) · 4.28 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
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.network.protocol.LevelSoundEventPacket;
import cn.nukkit.utils.Faceable;
/**
* Created by CreeperFace on 27. 11. 2016.
*/
public abstract class BlockButton extends BlockFlowable implements Faceable {
public BlockButton() {
this(0);
}
public BlockButton(int meta) {
super(meta);
}
@Override
public double getResistance() {
return 2.5;
}
@Override
public double getHardness() {
return 0.5;
}
@Override
public boolean place(Item item, Block block, Block target, BlockFace face, double fx, double fy, double fz, Player player) {
this.setDamage(face.getIndex());
if (!isSupportValid(this.getSide(this.getFacing().getOpposite()))) {
return false;
}
this.getLevel().setBlock(this, this, true, true);
return true;
}
@Override
public boolean canBeActivated() {
return true;
}
@Override
public boolean onActivate(Item item, Player player) {
if (this.isActivated()) {
return false;
}
this.level.getServer().getPluginManager().callEvent(new BlockRedstoneEvent(this, 0, 15));
this.setDamage(this.getDamage() ^ 0x08);
this.level.setBlock(this, this, true, true);
this.level.addLevelSoundEvent(this, LevelSoundEventPacket.SOUND_POWER_ON);
this.level.scheduleUpdate(this, 30);
level.updateAroundRedstone(this, null);
level.updateAroundRedstone(getSideVec(getFacing().getOpposite()), null);
return true;
}
@Override
public int onUpdate(int type) {
if (type == Level.BLOCK_UPDATE_NORMAL) {
if (!isSupportValid(this.getSide(this.getFacing().getOpposite()))) {
this.level.useBreakOn(this, Item.get(Item.WOODEN_PICKAXE));
return Level.BLOCK_UPDATE_NORMAL;
}
} else if (type == Level.BLOCK_UPDATE_SCHEDULED) {
if (this.isActivated()) {
this.level.getServer().getPluginManager().callEvent(new BlockRedstoneEvent(this, 15, 0));
this.setDamage(this.getDamage() ^ 0x08);
this.level.setBlock(this, this, true, true);
this.level.addLevelSoundEvent(this, LevelSoundEventPacket.SOUND_POWER_OFF);
level.updateAroundRedstone(this, null);
level.updateAroundRedstone(getSideVec(getFacing().getOpposite()), null);
}
return Level.BLOCK_UPDATE_SCHEDULED;
}
return 0;
}
private boolean isSupportValid(Block block) {
if (!block.isTransparent()) {
return true;
}
if (this.getFacing() == BlockFace.UP) {
return Block.canStayOnFullSolid(block);
}
return Block.canConnectToFullSolid(block);
}
public boolean isActivated() {
return ((this.getDamage() & 0x08) == 0x08);
}
@Override
public boolean isPowerSource() {
return true;
}
public int getWeakPower(BlockFace side) {
return isActivated() ? 15 : 0;
}
public int getStrongPower(BlockFace side) {
return !isActivated() ? 0 : (getFacing() == side ? 15 : 0);
}
public BlockFace getFacing() {
int side = isActivated() ? getDamage() ^ 0x08 : getDamage();
return BlockFace.fromIndex(side);
}
@Override
public boolean onBreak(Item item) {
if (isActivated()) {
this.level.getServer().getPluginManager().callEvent(new BlockRedstoneEvent(this, 15, 0));
}
return super.onBreak(item);
}
@Override
public Item toItem() {
return new ItemBlock(Block.get(this.getId(), 0), 0);
}
@Override
public BlockFace getBlockFace() {
return BlockFace.fromHorizontalIndex(this.getDamage() & 0x7);
}
@Override
public WaterloggingType getWaterloggingType() {
return WaterloggingType.WHEN_PLACED_IN_WATER;
}
@Override
public boolean canBeFlowedInto() {
return false;
}
@Override
public boolean breakWhenPushed() {
return true;
}
}