summaryrefslogtreecommitdiffstats
path: root/src/Items/ItemSlab.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/Items/ItemSlab.h')
-rw-r--r--src/Items/ItemSlab.h134
1 files changed, 63 insertions, 71 deletions
diff --git a/src/Items/ItemSlab.h b/src/Items/ItemSlab.h
index 944336f7e..467975047 100644
--- a/src/Items/ItemSlab.h
+++ b/src/Items/ItemSlab.h
@@ -14,95 +14,87 @@ class cItemSlabHandler:
public:
- /** Creates a new handler for the specified slab item type.
- Sets the handler to use the specified doubleslab block type for combining self into doubleslabs. */
- cItemSlabHandler(int a_ItemType, BLOCKTYPE a_DoubleSlabBlockType):
- Super(a_ItemType),
- m_DoubleSlabBlockType(a_DoubleSlabBlockType)
+ using Super::Super;
+
+private:
+
+ virtual bool CommitPlacement(cPlayer & a_Player, const cItem & a_HeldItem, const Vector3i a_PlacePosition, const eBlockFace a_ClickedBlockFace, const Vector3i a_CursorPosition) override
{
- }
+ // Confer BlockSlab.h, which we're in cahoots with to make the below logic work.
+
+ // If clicking a slab, combine it into a double-slab:
+ if (cBlockSlabHandler::IsAnySlabType(a_Player.GetWorld()->GetBlock(a_PlacePosition)))
+ {
+ if (!a_Player.PlaceBlock(a_PlacePosition, GetDoubleSlabType(static_cast<BLOCKTYPE>(a_HeldItem.m_ItemType)), static_cast<NIBBLETYPE>(a_HeldItem.m_ItemDamage)))
+ {
+ return false;
+ }
+ a_Player.SendBlocksAround(a_PlacePosition.x, a_PlacePosition.y, a_PlacePosition.z, 2); // (see below)
+ return true;
+ }
+
+ // Set the correct metadata based on player equipped item:
+ if (!a_Player.PlaceBlock(a_PlacePosition, static_cast<BLOCKTYPE>(a_HeldItem.m_ItemType), FaceToMetaData(static_cast<NIBBLETYPE>(a_HeldItem.m_ItemDamage), a_ClickedBlockFace, a_CursorPosition)))
+ {
+ return false;
+ }
+ /* This is a workaround for versions < 1.13, where the client combines a slab in the
+ direction of the clicked block face of a block ignoring build collision, rather than replacing said block.
+ Resend blocks to the client to fix the bug.
+ Ref.: https://forum.cuberite.org/thread-434-post-17388.html#pid17388 */
+ a_Player.SendBlocksAround(a_PlacePosition.x, a_PlacePosition.y, a_PlacePosition.z, 2);
+ return true;
+ }
- // cItemHandler overrides:
- virtual bool OnPlayerPlace(
- cWorld & a_World,
- cPlayer & a_Player,
- const cItem & a_EquippedItem,
- const Vector3i a_ClickedBlockPos,
- eBlockFace a_ClickedBlockFace,
- const Vector3i a_CursorPos
- ) override
+ static NIBBLETYPE FaceToMetaData(const NIBBLETYPE a_BaseMeta, const eBlockFace a_ClickedBlockFace, const Vector3i a_CursorPosition)
{
- // If clicking a slab, try combining it into a double-slab:
- BLOCKTYPE ClickedBlockType;
- NIBBLETYPE ClickedBlockMeta;
- a_World.GetBlockTypeMeta(a_ClickedBlockPos, ClickedBlockType, ClickedBlockMeta);
- if (
- (ClickedBlockType == m_ItemType) && // Placing the same slab material
- ((ClickedBlockMeta & 0x07) == a_EquippedItem.m_ItemDamage) // Placing the same slab sub-kind (and existing slab is single)
- )
+ switch (a_ClickedBlockFace)
{
- if (
- ((a_ClickedBlockFace == BLOCK_FACE_TOP) && ((ClickedBlockMeta & 0x08) == 0)) || // Top side of a bottom-half-slab
- ((a_ClickedBlockFace == BLOCK_FACE_BOTTOM) && ((ClickedBlockMeta & 0x08) != 0)) // Bottom side of a top-half-slab
- )
+ case BLOCK_FACE_TOP:
{
- if (!a_Player.PlaceBlock(a_ClickedBlockPos.x, a_ClickedBlockPos.y, a_ClickedBlockPos.z, m_DoubleSlabBlockType, ClickedBlockMeta & 0x07))
- {
- return false;
- }
- if (a_Player.IsGameModeSurvival())
- {
- a_Player.GetInventory().RemoveOneEquippedItem();
- }
- return true;
+ // Bottom half slab block:
+ return a_BaseMeta & 0x07;
}
- }
-
- // If there's already a slab in the destination, combine it into a double-slab:
- auto PlacePos = AddFaceDirection(a_ClickedBlockPos, a_ClickedBlockFace);
- BLOCKTYPE PlaceBlockType;
- NIBBLETYPE PlaceBlockMeta;
- a_World.GetBlockTypeMeta(PlacePos, PlaceBlockType, PlaceBlockMeta);
- if (
- (PlaceBlockType == m_ItemType) && // Placing the same slab material
- ((PlaceBlockMeta & 0x07) == a_EquippedItem.m_ItemDamage) // Placing the same slab sub-kind (and existing slab is single)
- )
- {
- if (!a_Player.PlaceBlock(PlacePos.x, PlacePos.y, PlacePos.z, m_DoubleSlabBlockType, PlaceBlockMeta & 0x07))
+ case BLOCK_FACE_BOTTOM:
{
- return false;
+ // Top half slab block:
+ return a_BaseMeta | 0x08;
}
- if (a_Player.IsGameModeSurvival())
+ case BLOCK_FACE_EAST:
+ case BLOCK_FACE_NORTH:
+ case BLOCK_FACE_SOUTH:
+ case BLOCK_FACE_WEST:
{
- a_Player.GetInventory().RemoveOneEquippedItem();
+ if (a_CursorPosition.y > 7)
+ {
+ // Cursor at top half of block, place top slab:
+ return a_BaseMeta | 0x08;
+ }
+ else
+ {
+ // Cursor at bottom half of block, place bottom slab:
+ return a_BaseMeta & 0x07;
+ }
}
- return true;
+ default: UNREACHABLE("Unhandled block face");
}
+ }
- // The slabs didn't combine, use the default handler to place the slab:
- bool res = Super::OnPlayerPlace(a_World, a_Player, a_EquippedItem, a_ClickedBlockPos, a_ClickedBlockFace, a_CursorPos);
- /*
- The client has a bug when a slab replaces snow and there's a slab above it.
- The client then combines the slab above, rather than replacing the snow.
- We send the block above the currently placed block back to the client to fix the bug.
- Ref.: https://forum.cuberite.org/thread-434-post-17388.html#pid17388
- */
- if ((a_ClickedBlockFace == BLOCK_FACE_TOP) && (a_ClickedBlockPos.y < cChunkDef::Height - 1))
+ /** Converts the single-slab blocktype to its equivalent double-slab blocktype. */
+ static BLOCKTYPE GetDoubleSlabType(BLOCKTYPE a_SingleSlabBlockType)
+ {
+ switch (a_SingleSlabBlockType)
{
- auto AbovePos = a_ClickedBlockPos.addedY(1);
- a_Player.SendBlocksAround(AbovePos.x, AbovePos.y, AbovePos.z, 1);
+ case E_BLOCK_STONE_SLAB: return E_BLOCK_DOUBLE_STONE_SLAB;
+ case E_BLOCK_WOODEN_SLAB: return E_BLOCK_DOUBLE_WOODEN_SLAB;
+ case E_BLOCK_RED_SANDSTONE_SLAB: return E_BLOCK_DOUBLE_RED_SANDSTONE_SLAB;
+ case E_BLOCK_PURPUR_SLAB: return E_BLOCK_PURPUR_DOUBLE_SLAB;
}
- return res;
+ UNREACHABLE("Unhandled slab type");
}
-
-
-protected:
-
- /** The block type to use when the slab combines into a doubleslab block. */
- BLOCKTYPE m_DoubleSlabBlockType;
};