Skip to content

Commit a24bc08

Browse files
authored
[2.0] Entity fixes (#1231)
* Entity and BlockEntity Serializer fixes * Chest and Bed fixes * Remove Log annotation and fix package name inclusion * bump protocol lib version * Barrel updates * Beacon fixes * Furnace fixes * revert saveClientData change since items do not need to be sent in NBT * Enchantment Table fixes * Add Convenience methods for `setBlockDataAt` which accepts `Vector3i` position * Furnace Fixes * Fix build from Porky's lib change * fix implements clause
1 parent 5c55824 commit a24bc08

26 files changed

Lines changed: 132 additions & 102 deletions

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@
175175
<dependency>
176176
<groupId>com.nukkitx.protocol</groupId>
177177
<artifactId>bedrock-v389</artifactId>
178-
<version>2.5.1</version>
178+
<version>2.5.2</version>
179179
</dependency>
180180
<dependency>
181181
<groupId>com.nukkitx</groupId>

src/main/java/cn/nukkit/block/BlockBarrel.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ public BlockFace getBlockFace() {
102102

103103
public void setBlockFace(BlockFace face) {
104104
setMeta((getMeta() & 0x8) | (face.getIndex() & 0x7));
105+
getLevel().setBlockDataAt(this.getX(), this.getY(), this.getZ(), this.getLayer(), getMeta());
105106
}
106107

107108
public boolean isOpen() {
@@ -110,6 +111,7 @@ public boolean isOpen() {
110111

111112
public void setOpen(boolean open) {
112113
setMeta((getMeta() & 0x7) | (open ? 0x8 : 0x0));
114+
getLevel().setBlockDataAt(this.getX(), this.getY(), this.getZ(), this.getLayer(), getMeta());
113115
}
114116

115117
@Override

src/main/java/cn/nukkit/block/BlockBeacon.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,8 @@ public boolean canBeActivated() {
5353
public boolean onActivate(Item item, Player player) {
5454
if (player != null) {
5555
BlockEntity t = this.getLevel().getBlockEntity(this.getPosition());
56-
if (t instanceof Beacon) {
56+
if (!(t instanceof Beacon)) {
5757
t.close();
58-
} else {
5958
BlockEntityRegistry.get().newEntity(BEACON, this.getChunk(), this.getPosition());
6059
}
6160

src/main/java/cn/nukkit/block/BlockBed.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,11 @@ public boolean onActivate(Item item, Player player) {
111111
@Override
112112
public boolean place(Item item, Block block, Block target, BlockFace face, Vector3f clickPos, Player player) {
113113
Block down = this.down();
114-
if (!down.isTransparent()) {
115-
Block next = this.getSide(player.getDirection());
114+
if (!down.isTransparent() || down instanceof BlockSlab) {
115+
Block next = this.getSide(player.getHorizontalFacing());
116116
Block downNext = next.down();
117117

118-
if (next.canBeReplaced() && !downNext.isTransparent()) {
118+
if (next.canBeReplaced() && (!downNext.isTransparent() || downNext instanceof BlockSlab)) {
119119
int meta = player.getDirection().getHorizontalIndex();
120120

121121
this.getLevel().setBlock(block.getPosition(), Block.get(this.getId(), meta), true, true);

src/main/java/cn/nukkit/block/BlockChest.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,6 @@ public boolean place(Item item, Block block, Block target, BlockFace face, Vecto
118118

119119
if (chest != null) {
120120
chest.pairWith(chest1);
121-
chest1.pairWith(chest);
122121
}
123122

124123
return true;

src/main/java/cn/nukkit/block/BlockFurnace.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package cn.nukkit.block;
22

3+
import cn.nukkit.blockentity.BlockEntityType;
4+
import cn.nukkit.blockentity.Furnace;
35
import cn.nukkit.utils.Identifier;
46

57
/**
@@ -8,8 +10,8 @@
810
*/
911
public class BlockFurnace extends BlockFurnaceBurning {
1012

11-
public BlockFurnace(Identifier id) {
12-
super(id);
13+
protected BlockFurnace(Identifier id, BlockEntityType<? extends Furnace> furnace) {
14+
super(id, furnace);
1315
}
1416

1517
@Override

src/main/java/cn/nukkit/block/BlockFurnaceBurning.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cn.nukkit.block;
22

33
import cn.nukkit.blockentity.BlockEntity;
4+
import cn.nukkit.blockentity.BlockEntityType;
45
import cn.nukkit.blockentity.BlockEntityTypes;
56
import cn.nukkit.blockentity.Furnace;
67
import cn.nukkit.inventory.ContainerInventory;
@@ -20,9 +21,15 @@
2021
* Nukkit Project
2122
*/
2223
public class BlockFurnaceBurning extends BlockSolid implements Faceable {
24+
private BlockEntityType<? extends Furnace> furnaceEntity;
2325

24-
public BlockFurnaceBurning(Identifier id) {
26+
protected BlockFurnaceBurning(Identifier id, BlockEntityType<? extends Furnace> entity) {
2527
super(id);
28+
this.furnaceEntity = entity;
29+
}
30+
31+
public static BlockFactory factory (BlockEntityType<? extends Furnace> furnaceEntity) {
32+
return id -> new BlockFurnaceBurning(id, furnaceEntity);
2633
}
2734

2835
@Override
@@ -56,7 +63,7 @@ public boolean place(Item item, Block block, Block target, BlockFace face, Vecto
5663
this.setMeta(faces[player != null ? player.getDirection().getHorizontalIndex() : 0]);
5764
this.getLevel().setBlock(block.getPosition(), this, true, true);
5865

59-
Furnace furnace = BlockEntityRegistry.get().newEntity(BlockEntityTypes.FURNACE, this.getChunk(), this.getPosition());
66+
Furnace furnace = BlockEntityRegistry.get().newEntity(furnaceEntity, this.getChunk(), this.getPosition());
6067
furnace.loadAdditionalData(item.getTag());
6168
if (item.hasCustomName()) {
6269
furnace.setCustomName(item.getCustomName());
@@ -79,7 +86,7 @@ public boolean onActivate(Item item, Player player) {
7986
if (blockEntity instanceof Furnace) {
8087
furnace = (Furnace) blockEntity;
8188
} else {
82-
furnace = BlockEntityRegistry.get().newEntity(BlockEntityTypes.FURNACE, this.getChunk(), this.getPosition());
89+
furnace = BlockEntityRegistry.get().newEntity(furnaceEntity, this.getChunk(), this.getPosition());
8390
}
8491

8592
player.addWindow(furnace.getInventory());
@@ -128,4 +135,5 @@ public boolean canHarvestWithHand() {
128135
public BlockFace getBlockFace() {
129136
return BlockFace.fromHorizontalIndex(this.getMeta() & 0x7);
130137
}
138+
131139
}

src/main/java/cn/nukkit/blockentity/impl/BeaconBlockEntity.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,6 @@ public BeaconBlockEntity(BlockEntityType<?> type, Chunk chunk, Vector3i position
3232
super(type, chunk, position);
3333
}
3434

35-
@Override
36-
protected void init() {
37-
scheduleUpdate();
38-
39-
super.init();
40-
}
41-
4235
@Override
4336
public void loadAdditionalData(CompoundTag tag) {
4437
super.loadAdditionalData(tag);
@@ -49,6 +42,7 @@ public void loadAdditionalData(CompoundTag tag) {
4942

5043
@Override
5144
public void saveAdditionalData(CompoundTagBuilder tag) {
45+
super.saveAdditionalData(tag);
5246
tag.intTag("primary", this.getPrimaryEffect());
5347
tag.intTag("secondary", this.getSecondaryEffect());
5448
}
@@ -107,9 +101,9 @@ public boolean onUpdate() {
107101

108102
//If secondary is selected as the primary too, apply 2 amplification
109103
if (getSecondaryEffect() == getPrimaryEffect()) {
110-
e.setAmplifier(2);
111-
} else {
112104
e.setAmplifier(1);
105+
} else {
106+
e.setAmplifier(0);
113107
}
114108

115109
//Hide particles
@@ -128,7 +122,7 @@ public boolean onUpdate() {
128122
e.setDuration(duration * 20);
129123

130124
//Regen I
131-
e.setAmplifier(1);
125+
e.setAmplifier(0);
132126

133127
//Hide particles
134128
e.setVisible(false);
@@ -158,7 +152,7 @@ private boolean hasSkyAccess() {
158152
}
159153

160154
private int calculatePowerLevel() {
161-
int tileX = this.getPosition().getZ();
155+
int tileX = this.getPosition().getX();
162156
int tileY = this.getPosition().getY();
163157
int tileZ = this.getPosition().getZ();
164158

@@ -215,6 +209,12 @@ public boolean updateCompoundTag(CompoundTag nbt, Player player) {
215209
BeaconInventory inv = (BeaconInventory) player.getWindowById(ContainerIds.BEACON);
216210

217211
inv.clear(0);
212+
this.scheduleUpdate();
213+
return true;
214+
}
215+
216+
@Override
217+
public boolean isSpawnable() {
218218
return true;
219219
}
220220
}

src/main/java/cn/nukkit/blockentity/impl/BlastFurnaceBlockEntity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
public class BlastFurnaceBlockEntity extends FurnaceBlockEntity implements BlastFurnace {
1313

1414
public BlastFurnaceBlockEntity(BlockEntityType<?> type, Chunk chunk, Vector3i position) {
15-
super(type, chunk, position, InventoryType.SMOKER);
15+
super(type, chunk, position, InventoryType.BLAST_FURNACE);
1616
}
1717

1818
@Override

src/main/java/cn/nukkit/blockentity/impl/ChestBlockEntity.java

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,16 @@ public void loadAdditionalData(CompoundTag tag) {
5555
tag.listenForBoolean("Findable", this::setFindable);
5656
}
5757

58+
@Override
59+
protected void saveClientData(CompoundTagBuilder tag) {
60+
super.saveClientData(tag);
61+
if (this.pairPosition != null && this.pairlead) {
62+
tag.intTag("pairx", this.pairPosition.getX());
63+
tag.intTag("pairz", this.pairPosition.getZ());
64+
tag.booleanTag("pairlead", this.pairlead);
65+
}
66+
}
67+
5868
@Override
5969
public void saveAdditionalData(CompoundTagBuilder tag) {
6070
super.saveAdditionalData(tag);
@@ -64,16 +74,10 @@ public void saveAdditionalData(CompoundTagBuilder tag) {
6474
items.add(ItemUtils.serializeItem(entry.getValue(), entry.getKey()));
6575
}
6676
tag.listTag("Items", CompoundTag.class, items);
67-
68-
if (this.pairPosition != null) {
69-
tag.intTag("pairx", this.pairPosition.getX());
70-
tag.intTag("pairz", this.pairPosition.getZ());
71-
}
72-
tag.booleanTag("pairlead", this.pairlead);
7377
tag.booleanTag("Findable", this.findable);
7478
}
7579

76-
private void setPairlead(boolean pairlead) {
80+
public void setPairlead(boolean pairlead) {
7781
this.pairlead = pairlead;
7882
}
7983

@@ -149,9 +153,10 @@ protected void checkPairing() {
149153
}
150154
}
151155
} else {
152-
if (this.getLevel().isChunkLoaded(this.pairPosition)) {
156+
if (this.pairPosition != null && this.getLevel().isChunkLoaded(this.pairPosition)) {
153157
this.doubleInventory = null;
154158
this.pairPosition = null;
159+
this.pairlead = false;
155160
}
156161
}
157162
}
@@ -177,6 +182,7 @@ public boolean pairWith(Chest chest) {
177182
}
178183

179184
this.createPair((ChestBlockEntity) chest);
185+
this.pairlead = true;
180186

181187
chest.spawnToAll();
182188
this.spawnToAll();
@@ -205,6 +211,7 @@ public boolean unpair() {
205211
if (chest != null) {
206212
chest.pairPosition = null;
207213
chest.doubleInventory = null;
214+
chest.pairlead = false;
208215
chest.checkPairing();
209216
chest.spawnToAll();
210217
}
@@ -217,4 +224,23 @@ public boolean unpair() {
217224
public boolean isSpawnable() {
218225
return true;
219226
}
227+
228+
@Override
229+
public boolean onUpdate() {
230+
super.onUpdate();
231+
if (this.pairlead) {
232+
if (this.pairPosition != null) {
233+
ChestBlockEntity other = getPair();
234+
if (other == null) {
235+
return true;
236+
}
237+
if (!other.isPaired()) {
238+
other.pairPosition = this.getPosition();
239+
checkPairing();
240+
other.checkPairing();
241+
}
242+
}
243+
}
244+
return false;
245+
}
220246
}

0 commit comments

Comments
 (0)