From 487f9a2aa9b5497495cef1ac3b9c7a603e69f862 Mon Sep 17 00:00:00 2001 From: Mattes D Date: Tue, 21 Apr 2020 22:19:22 +0200 Subject: Vector3 in Handlers (#4680) Refactored all cBlockHandler and cItemHandler descendants to use Vector3. --- src/Items/ItemDoor.h | 40 +++++++++++++++++++++++++--------------- 1 file changed, 25 insertions(+), 15 deletions(-) (limited to 'src/Items/ItemDoor.h') diff --git a/src/Items/ItemDoor.h b/src/Items/ItemDoor.h index 790e08ffc..aec6bc0fe 100644 --- a/src/Items/ItemDoor.h +++ b/src/Items/ItemDoor.h @@ -9,38 +9,45 @@ -class cItemDoorHandler : +class cItemDoorHandler: public cItemHandler { + using Super = cItemHandler; + public: - cItemDoorHandler(int a_ItemType) : - cItemHandler(a_ItemType) + + cItemDoorHandler(int a_ItemType): + Super(a_ItemType) { } + + virtual bool GetBlocksToPlace( cWorld & a_World, cPlayer & a_Player, const cItem & a_EquippedItem, - int a_BlockX, int a_BlockY, int a_BlockZ, eBlockFace a_BlockFace, - int a_CursorX, int a_CursorY, int a_CursorZ, + const Vector3i a_PlacedBlockPos, + eBlockFace a_ClickedBlockFace, + const Vector3i a_CursorPos, sSetBlockVector & a_BlocksToSet ) override { // Vanilla only allows door placement while clicking on the top face of the block below the door: - if (a_BlockFace != BLOCK_FACE_TOP) + if (a_ClickedBlockFace != BLOCK_FACE_TOP) { return false; } // Door (bottom block) can be placed in Y range of [1, 254]: - if ((a_BlockY < 1) || (a_BlockY >= cChunkDef::Height - 2)) + if ((a_PlacedBlockPos.y < 1) || (a_PlacedBlockPos.y >= cChunkDef::Height - 2)) { return false; } // The door needs a compatible block below it: - if (!cBlockDoorHandler::CanBeOn(a_World.GetBlock(a_BlockX, a_BlockY - 1, a_BlockZ), a_World.GetBlockMeta(a_BlockX, a_BlockY - 1, a_BlockZ))) + auto BelowPos = a_PlacedBlockPos.addedY(-1); + if (!cBlockDoorHandler::CanBeOn(a_World.GetBlock(BelowPos), a_World.GetBlockMeta(BelowPos))) { return false; } @@ -64,8 +71,9 @@ public: } // Check the two blocks that will get replaced by the door: - BLOCKTYPE LowerBlockType = a_World.GetBlock(a_BlockX, a_BlockY, a_BlockZ); - BLOCKTYPE UpperBlockType = a_World.GetBlock(a_BlockX, a_BlockY + 1, a_BlockZ); + auto UpperBlockPos = a_PlacedBlockPos.addedY(1); + BLOCKTYPE LowerBlockType = a_World.GetBlock(a_PlacedBlockPos); + BLOCKTYPE UpperBlockType = a_World.GetBlock(UpperBlockPos); if ( !cBlockDoorHandler::CanReplaceBlock(LowerBlockType) || !cBlockDoorHandler::CanReplaceBlock(UpperBlockType)) @@ -78,10 +86,10 @@ public: Vector3i RelDirToOutside = cBlockDoorHandler::GetRelativeDirectionToOutside(LowerBlockMeta); Vector3i LeftNeighborPos = RelDirToOutside; LeftNeighborPos.TurnCW(); - LeftNeighborPos.Move(a_BlockX, a_BlockY, a_BlockZ); + LeftNeighborPos.Move(a_PlacedBlockPos); Vector3i RightNeighborPos = RelDirToOutside; RightNeighborPos.TurnCCW(); - RightNeighborPos.Move(a_BlockX, a_BlockY, a_BlockZ); + RightNeighborPos.Move(a_PlacedBlockPos); // Decide whether the hinge is on the left (default) or on the right: NIBBLETYPE UpperBlockMeta = 0x08; @@ -89,7 +97,7 @@ public: BLOCKTYPE RightNeighborBlock = a_World.GetBlock(RightNeighborPos); /* // DEBUG: - FLOGD("Door being placed at {0}", Vector3i{a_BlockX, a_BlockY, a_BlockZ}); + FLOGD("Door being placed at {0}", a_PlacedBlockPos); FLOGD("RelDirToOutside: {0}", RelDirToOutside); FLOGD("Left neighbor at {0}: {1} ({2})", LeftNeighborPos, LeftNeighborBlock, ItemTypeToString(LeftNeighborBlock)); FLOGD("Right neighbor at {0}: {1} ({2})", RightNeighborPos, RightNeighborBlock, ItemTypeToString(RightNeighborBlock)); @@ -108,12 +116,14 @@ public: } // Set the blocks: - a_BlocksToSet.emplace_back(a_BlockX, a_BlockY, a_BlockZ, BlockType, LowerBlockMeta); - a_BlocksToSet.emplace_back(a_BlockX, a_BlockY + 1, a_BlockZ, BlockType, UpperBlockMeta); + a_BlocksToSet.emplace_back(a_PlacedBlockPos, BlockType, LowerBlockMeta); + a_BlocksToSet.emplace_back(UpperBlockPos, BlockType, UpperBlockMeta); return true; } + + virtual bool IsPlaceable(void) override { return true; -- cgit v1.2.3