forked from CloudburstMC/Nukkit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlockGrindstone.java
More file actions
149 lines (124 loc) · 3.83 KB
/
BlockGrindstone.java
File metadata and controls
149 lines (124 loc) · 3.83 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
package cn.nukkit.block;
import cn.nukkit.Player;
import cn.nukkit.inventory.GrindstoneInventory;
import cn.nukkit.item.Item;
import cn.nukkit.item.ItemBlock;
import cn.nukkit.math.BlockFace;
import cn.nukkit.utils.BlockColor;
import cn.nukkit.utils.Faceable;
public class BlockGrindstone extends BlockTransparentMeta implements Faceable {
public static final int TYPE_ATTACHMENT_STANDING = 0;
public static final int TYPE_ATTACHMENT_HANGING = 1;
public static final int TYPE_ATTACHMENT_SIDE = 2;
public static final int TYPE_ATTACHMENT_MULTIPLE = 3;
public BlockGrindstone() {
this(0);
}
public BlockGrindstone(int meta) {
super(meta);
}
@Override
public String getName() {
return "Grindstone";
}
@Override
public int getId() {
return GRINDSTONE;
}
@Override
public boolean canHarvestWithHand() {
return false;
}
@Override
public BlockColor getColor() {
return BlockColor.IRON_BLOCK_COLOR;
}
@Override
public double getHardness() {
return 2;
}
@Override
public double getResistance() {
return 6;
}
@Override
public BlockFace getBlockFace() {
return BlockFace.fromHorizontalIndex(getDamage() & 0b11);
}
public void setBlockFace(BlockFace face) {
if (face.getHorizontalIndex() == -1) {
return;
}
setDamage(getDamage() & (DATA_MASK ^ 0b11) | face.getHorizontalIndex());
}
@Override
public Item toItem() {
return new ItemBlock(Block.get(GRINDSTONE));
}
@Override
public boolean canBePushed() {
return false;
}
@Override
public boolean place(Item item, Block block, Block target, BlockFace face, double fx, double fy, double fz, Player player) {
if (block.getId() != AIR && block.canBeReplaced()) {
face = BlockFace.UP;
}
switch (face) {
case UP:
this.setAttachmentType(TYPE_ATTACHMENT_STANDING);
this.setBlockFace(player.getDirection().getOpposite());
break;
case DOWN:
this.setAttachmentType(TYPE_ATTACHMENT_HANGING);
this.setBlockFace(player.getDirection().getOpposite());
break;
default:
this.setBlockFace(face);
this.setAttachmentType(TYPE_ATTACHMENT_SIDE);
}
if (!this.checkSupport()) {
return false;
}
return super.place(item, block, target, face, fx, fy, fz, player);
}
public int getAttachmentType() {
return (getDamage() & 0b1100) >> 2 & 0b11;
}
public void setAttachmentType(int attachmentType) {
attachmentType = attachmentType & 0b11;
setDamage(getDamage() & (DATA_MASK ^ 0b1100) | (attachmentType << 2));
}
private boolean checkSupport() {
switch (this.getAttachmentType()) {
case TYPE_ATTACHMENT_STANDING:
if (down().getId() != AIR) {
return true;
}
break;
case TYPE_ATTACHMENT_HANGING:
if (up().getId() != AIR) {
return true;
}
break;
case TYPE_ATTACHMENT_SIDE:
BlockFace blockFace = getBlockFace();
if (getSide(blockFace.getOpposite()).getId() != AIR) {
return true;
}
break;
}
return false;
}
@Override
public boolean canBeActivated() {
return true;
}
@Override
public boolean onActivate(Item item, Player player) {
if (player != null) {
player.addWindow(new GrindstoneInventory(player.getUIInventory(), this), Player.GRINDSTONE_WINDOW_ID);
}
return true;
}
}