forked from CloudburstMC/Nukkit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlockCampfire.java
More file actions
256 lines (212 loc) · 7.79 KB
/
BlockCampfire.java
File metadata and controls
256 lines (212 loc) · 7.79 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
package cn.nukkit.block;
import cn.nukkit.Player;
import cn.nukkit.blockentity.BlockEntity;
import cn.nukkit.blockentity.BlockEntityCampfire;
import cn.nukkit.entity.Entity;
import cn.nukkit.event.entity.EntityDamageByBlockEvent;
import cn.nukkit.event.entity.EntityDamageEvent;
import cn.nukkit.inventory.CampfireInventory;
import cn.nukkit.inventory.CampfireRecipe;
import cn.nukkit.inventory.ContainerInventory;
import cn.nukkit.item.*;
import cn.nukkit.item.enchantment.Enchantment;
import cn.nukkit.level.Level;
import cn.nukkit.math.BlockFace;
import cn.nukkit.nbt.tag.CompoundTag;
import cn.nukkit.nbt.tag.Tag;
import cn.nukkit.network.protocol.LevelSoundEventPacket;
import cn.nukkit.utils.BlockColor;
import cn.nukkit.utils.Faceable;
import java.util.Map;
import java.util.concurrent.ThreadLocalRandom;
public class BlockCampfire extends BlockTransparentMeta implements Faceable {
public BlockCampfire() {
this(0);
}
public BlockCampfire(int meta) {
super(meta);
}
@Override
public String getName() {
return "Campfire";
}
@Override
public int getId() {
return CAMPFIRE_BLOCK;
}
@Override
public int getLightLevel() {
return isExtinguished() ? 0 : 15;
}
@Override
public double getResistance() {
return 2;
}
@Override
public double getHardness() {
return 2;
}
@Override
public int getToolType() {
return ItemTool.TYPE_AXE;
}
@Override
public Item[] getDrops(Item item) {
return new Item[] {Item.get(ItemID.COAL, 0, 1 + ThreadLocalRandom.current().nextInt(1))};
}
@Override
public boolean canSilkTouch() {
return true;
}
@Override
public WaterloggingType getWaterloggingType() {
return WaterloggingType.WHEN_PLACED_IN_WATER;
}
@Override
public boolean place(Item item, Block block, Block target, BlockFace face, double fx, double fy, double fz, Player player) {
if (this.down().getId() == CAMPFIRE_BLOCK) {
return false;
}
this.setDamage(player != null ? player.getDirection().getOpposite().getHorizontalIndex() : 0);
Block layer1 = block.getLevelBlock(BlockLayer.WATERLOGGED);
boolean defaultLayerCheck = (block instanceof BlockWater && block.getDamage() == 0 || block.getDamage() >= 8) || block instanceof BlockIceFrosted;
boolean layer1Check = (layer1 instanceof BlockWater && layer1.getDamage() == 0 || layer1.getDamage() >= 8) || layer1 instanceof BlockIceFrosted;
if (defaultLayerCheck || layer1Check) {
this.setExtinguished(true);
this.getLevel().addLevelSoundEvent(this, LevelSoundEventPacket.SOUND_FIZZ);
this.level.setBlock(this, BlockLayer.WATERLOGGED, defaultLayerCheck ? block : layer1, false, false);
} else {
this.level.setBlock(this, BlockLayer.WATERLOGGED, Block.get(Block.AIR), false, false);
}
this.getLevel().setBlock(this, this, true, true);
this.createBlockEntity(item);
return true;
}
private BlockEntityCampfire createBlockEntity(Item item) {
CompoundTag nbt = new CompoundTag()
.putString("id", BlockEntity.CAMPFIRE)
.putInt("x", (int) this.x)
.putInt("y", (int) this.y)
.putInt("z", (int) this.z);
if (item.hasCustomBlockData()) {
Map<String, Tag> customData = item.getCustomBlockData().getTags();
for (Map.Entry<String, Tag> tag : customData.entrySet()) {
nbt.put(tag.getKey(), tag.getValue());
}
}
return (BlockEntityCampfire) BlockEntity.createBlockEntity(BlockEntity.CAMPFIRE, this.getChunk(), nbt);
}
@Override
public boolean hasEntityCollision() {
return true;
}
@Override
public void onEntityCollide(Entity entity) {
if (!this.isExtinguished() && !entity.isSneaking()) {
entity.attack(new EntityDamageByBlockEvent(this, entity, EntityDamageEvent.DamageCause.FIRE, this instanceof BlockCampfireSoul ? 2 : 1));
}
}
@Override
public boolean canBeActivated() {
return true;
}
@Override
public int onUpdate(int type) {
if (type == Level.BLOCK_UPDATE_NORMAL) {
if (!this.isExtinguished()) {
Block layer1 = this.getLevelBlock(BlockLayer.WATERLOGGED);
if (layer1 instanceof BlockWater || layer1 instanceof BlockIceFrosted) {
this.setExtinguished(true);
this.level.setBlock(this, this, true, true);
this.getLevel().addLevelSoundEvent(this, LevelSoundEventPacket.SOUND_FIZZ);
}
}
return type;
}
return 0;
}
@Override
public boolean onActivate(Item item, Player player) {
if (item.getId() == BlockID.AIR || item.getCount() <= 0) {
return false;
}
BlockEntity entity = this.level.getBlockEntity(this);
if (!(entity instanceof BlockEntityCampfire)) {
return false;
}
boolean itemUsed = false;
if (item.isShovel() && !this.isExtinguished()) {
this.setExtinguished(true);
this.level.setBlock(this, this, true, true);
this.getLevel().addLevelSoundEvent(this, LevelSoundEventPacket.SOUND_FIZZ);
itemUsed = true;
} else if (item.getId() == ItemID.FLINT_AND_STEEL || item.hasEnchantment(Enchantment.ID_FIRE_ASPECT)) {
item.useOn(this);
this.setExtinguished(false);
this.level.setBlock(this, this, true, true);
entity.scheduleUpdate();
level.addLevelSoundEvent(this, LevelSoundEventPacket.SOUND_IGNITE);
itemUsed = true;
}
BlockEntityCampfire campfire = (BlockEntityCampfire) entity;
Item cloned = item.clone();
cloned.setCount(1);
CampfireInventory inventory = campfire.getInventory();
if (inventory.canAddItem(cloned)) {
CampfireRecipe recipe = this.level.getServer().getCraftingManager().matchCampfireRecipe(cloned);
if (recipe != null) {
inventory.addItem(cloned);
item.setCount(item.getCount() - 1);
return true;
}
}
return itemUsed;
}
@Override
public double getMaxY() {
return y + 0.5;
}
@Override
public BlockColor getColor() {
return BlockColor.SPRUCE_BLOCK_COLOR;
}
public boolean isExtinguished() {
return (this.getDamage() & 0x4) == 0x4;
}
public void setExtinguished(boolean extinguished) {
this.setDamage((this.getDamage() & 0x3) | (extinguished? 0x4 : 0x0));
}
@Override
public BlockFace getBlockFace() {
return BlockFace.fromHorizontalIndex(getDamage() & 0x3);
}
public void setBlockFace(BlockFace face) {
if (face == BlockFace.UP || face == BlockFace.DOWN) {
return;
}
this.setDamage((this.getDamage() & 0x4) | face.getHorizontalIndex());
}
@Override
public Item toItem() {
return Item.get(ItemID.CAMPFIRE);
}
public boolean hasComparatorInputOverride() {
return true;
}
@Override
public int getComparatorInputOverride() {
BlockEntity blockEntity = this.level.getBlockEntity(this);
if (blockEntity instanceof BlockEntityCampfire) {
return ContainerInventory.calculateRedstone(((BlockEntityCampfire) blockEntity).getInventory());
}
return super.getComparatorInputOverride();
}
@Override
public boolean breakWhenPushed() {
return true;
}
@Override
public boolean canBePushed() {
return false; // prevent item loss issue with pistons until a working implementation
}
}