Skip to content

Commit

Permalink
Fix Slab duplicate when placed on the top of a block (#339)
Browse files Browse the repository at this point in the history
  • Loading branch information
angga7togk authored Nov 14, 2024
1 parent 304934f commit a0d9b4e
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 31 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
*.class
.vscode
.vscode/*
target
target/*
build
Expand Down
69 changes: 46 additions & 23 deletions src/main/java/cn/nukkit/block/BlockSlab.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,56 +26,74 @@ public BlockSlab(int meta, int doubleSlab) {

@Override
protected AxisAlignedBB recalculateBoundingBox() {
if ((this.getDamage() & SLAB_TOP_BIT) > 0) {
return new SimpleAxisAlignedBB(this.x, this.y + 0.5, this.z, this.x + 1, this.y + 1, this.z + 1);
if (this.hasTopBit()) {
return new SimpleAxisAlignedBB(
this.x,
this.y + 0.5,
this.z,
this.x + 1,
this.y + 1,
this.z + 1
);
} else {
return new SimpleAxisAlignedBB(this.x, this.y, this.z, this.x + 1, this.y + 0.5, this.z + 1);
return new SimpleAxisAlignedBB(
this.x,
this.y,
this.z,
this.x + 1,
this.y + 0.5,
this.z + 1
);
}
}

public String getSlabName() {
return "";
}

@Override
public double getHardness() {
return 2;
public String getName() {
return (this.hasTopBit()? "Upper " : "") + this.getSlabName() + " Slab";
}

@Override
public double getResistance() {
return getToolType() < ItemTool.TYPE_AXE ? 30 : 15;
public double getHardness() {
return 3;
}

@Override
public WaterloggingType getWaterloggingType() {
return WaterloggingType.WHEN_PLACED_IN_WATER;
public double getResistance() {
return getToolType() < ItemTool.TYPE_AXE ? 30 : 15;
}

@Override
public boolean place(Item item, Block block, Block target, BlockFace face, double fx, double fy, double fz, Player player) {
this.setDamage(this.getDamage() & SLAB_BLOCK_TYPE_BIT);
if (face == BlockFace.DOWN) {
if (target instanceof BlockSlab && (target.getDamage() & SLAB_TOP_BIT) == SLAB_TOP_BIT && (target.getDamage() & SLAB_BLOCK_TYPE_BIT) == (this.getDamage() & SLAB_BLOCK_TYPE_BIT)) {
if (target instanceof BlockSlab && ((BlockSlab) target).doubleSlab == this.doubleSlab && ((BlockSlab) target).hasTopBit() && (target.getDamage() & SLAB_BLOCK_TYPE_BIT) == (this.getDamage() & SLAB_BLOCK_TYPE_BIT)) {
this.getLevel().setBlock(target, Block.get(doubleSlab, this.getDamage()), true);

return true;
} else if (block instanceof BlockSlab && (block.getDamage() & SLAB_BLOCK_TYPE_BIT) == (this.getDamage() & SLAB_BLOCK_TYPE_BIT)) {
} else if (block instanceof BlockSlab && ((BlockSlab) block).doubleSlab == this.doubleSlab && (block.getDamage() & SLAB_BLOCK_TYPE_BIT) == (this.getDamage() & SLAB_BLOCK_TYPE_BIT)) {
this.getLevel().setBlock(block, Block.get(doubleSlab, this.getDamage()), true);

return true;
} else {
this.setDamage(this.getDamage() | SLAB_TOP_BIT);
this.setTopBit(true);
}
} else if (face == BlockFace.UP) {
if (target instanceof BlockSlab && (target.getDamage() & SLAB_TOP_BIT) == 0 && (target.getDamage() & SLAB_BLOCK_TYPE_BIT) == (this.getDamage() & SLAB_BLOCK_TYPE_BIT)) {
if (target instanceof BlockSlab && ((BlockSlab) target).doubleSlab == this.doubleSlab && !((BlockSlab) target).hasTopBit() && (target.getDamage() & SLAB_BLOCK_TYPE_BIT) == (this.getDamage() & SLAB_BLOCK_TYPE_BIT)) {
this.getLevel().setBlock(target, Block.get(doubleSlab, this.getDamage()), true);

return true;
} else if (block instanceof BlockSlab && (block.getDamage() & SLAB_BLOCK_TYPE_BIT) == (this.getDamage() & SLAB_BLOCK_TYPE_BIT)) {
} else if (block instanceof BlockSlab && ((BlockSlab) block).doubleSlab == this.doubleSlab && (block.getDamage() & SLAB_BLOCK_TYPE_BIT) == (this.getDamage() & SLAB_BLOCK_TYPE_BIT)) {
this.getLevel().setBlock(block, Block.get(doubleSlab, this.getDamage()), true);

return true;
}
//TODO: check for collision
} else {
if (block instanceof BlockSlab) {
if (block instanceof BlockSlab && ((BlockSlab) block).doubleSlab == this.doubleSlab) {
if ((block.getDamage() & SLAB_BLOCK_TYPE_BIT) == (this.getDamage() & SLAB_BLOCK_TYPE_BIT)) {
this.getLevel().setBlock(block, Block.get(doubleSlab, this.getDamage()), true);

Expand All @@ -85,34 +103,39 @@ public boolean place(Item item, Block block, Block target, BlockFace face, doubl
return false;
} else {
if (fy > 0.5) {
this.setDamage(this.getDamage() | SLAB_TOP_BIT);
this.setTopBit(true);
}
}
}

if (block instanceof BlockSlab && (target.getDamage() & SLAB_BLOCK_TYPE_BIT) != (this.getDamage() & SLAB_BLOCK_TYPE_BIT)) {
if (block instanceof BlockSlab && ((BlockSlab) block).doubleSlab == this.doubleSlab && (target.getDamage() & SLAB_BLOCK_TYPE_BIT) != (this.getDamage() & SLAB_BLOCK_TYPE_BIT)) {
return false;
}
this.getLevel().setBlock(block, this, true, true);

this.getLevel().setBlock(this, this, true, true);
return true;
}

public boolean hasTopBit() {
return (this.getDamage() & 0x08) > 0;
return (this.getDamage() & SLAB_TOP_BIT) > 0;
}

public void setTopBit(boolean topBit) {
if (topBit) {
this.setDamage(this.getDamage() | 0x08);
this.setDamage(this.getDamage() | SLAB_TOP_BIT);
} else {
this.setDamage(this.getDamage() & 0x07);
this.setDamage(this.getDamage() & SLAB_BLOCK_TYPE_BIT);
}
}

@Override
public Item toItem() {
int damage = this.getDamage() & 0x07;
int damage = this.getDamage() & SLAB_BLOCK_TYPE_BIT;
return new ItemBlock(Block.get(this.getId(), damage), damage);
}
}

@Override
public WaterloggingType getWaterloggingType() {
return WaterloggingType.WHEN_PLACED_IN_WATER;
}
}
8 changes: 0 additions & 8 deletions src/main/java/cn/nukkit/level/Level.java
Original file line number Diff line number Diff line change
Expand Up @@ -2600,14 +2600,6 @@ public Item useItemOn(Vector3 vector, Item item, BlockFace face, float fx, float
Block target = this.getBlock(vector);
Block block = target.getSide(face);

//针对半砖特判
if (target instanceof BlockSlab
&& (face == BlockFace.UP || face == BlockFace.DOWN)
&& item.getId() == target.getId()
&& (item.getDamage() & BlockSlab.SLAB_BLOCK_TYPE_BIT) == (target.getDamage() & BlockSlab.SLAB_BLOCK_TYPE_BIT)) {
block = target;
}

if (!isYInRange(block.getFloorY())) {
return null;
}
Expand Down

0 comments on commit a0d9b4e

Please sign in to comment.