From 748b121703fa28b10933f4432c09391e66179118 Mon Sep 17 00:00:00 2001 From: Tiger Wang Date: Sun, 28 Mar 2021 14:40:57 +0100 Subject: Unify DoWithBlockEntity (#5168) + DoWith calls now broadcast the block entity and mark the chunk dirty + Add block entity change queue to synchronise BE updates with block updates * Fixed a few incorrect assertions about BE type - Remove manual overloads --- src/Items/ItemBanner.h | 6 +++-- src/Items/ItemEnchantingTable.h | 12 +++------ src/Items/ItemMobHead.h | 58 +++++++++++++++++++---------------------- 3 files changed, 35 insertions(+), 41 deletions(-) (limited to 'src/Items') diff --git a/src/Items/ItemBanner.h b/src/Items/ItemBanner.h index 7b9e6d05b..3f082c5a5 100644 --- a/src/Items/ItemBanner.h +++ b/src/Items/ItemBanner.h @@ -203,13 +203,15 @@ public: } const auto BannerPos = AddFaceDirection(a_ClickedBlockPos, a_ClickedBlockFace); - return a_World.DoWithBlockEntityAt(BannerPos.x, BannerPos.y, BannerPos.z, [Color](cBlockEntity & a_BlockEntity) + a_World.DoWithBlockEntityAt(BannerPos, [Color](cBlockEntity & a_BlockEntity) { ASSERT((a_BlockEntity.GetBlockType() == E_BLOCK_STANDING_BANNER) || (a_BlockEntity.GetBlockType() == E_BLOCK_WALL_BANNER)); auto & Banner = static_cast(a_BlockEntity); Banner.SetBaseColor(Color); - return true; + return false; }); + + return true; } }; diff --git a/src/Items/ItemEnchantingTable.h b/src/Items/ItemEnchantingTable.h index c8eb42cac..12835cb4a 100644 --- a/src/Items/ItemEnchantingTable.h +++ b/src/Items/ItemEnchantingTable.h @@ -46,16 +46,12 @@ private: } const auto PlacePos = AddFaceDirection(a_ClickedBlockPos, a_ClickedBlockFace); - a_World.DoWithBlockEntityAt(PlacePos.x, PlacePos.y, PlacePos.z, [&a_EquippedItem](cBlockEntity & a_Entity) + a_World.DoWithBlockEntityAt(PlacePos, [&a_EquippedItem](cBlockEntity & a_Entity) { - if (a_Entity.GetBlockType() != E_BLOCK_ENCHANTMENT_TABLE) - { - return true; - } + ASSERT(a_Entity.GetBlockType() == E_BLOCK_ENCHANTMENT_TABLE); - auto & EnchantingTable = static_cast(a_Entity); - EnchantingTable.SetCustomName(a_EquippedItem.m_CustomName); - return true; + static_cast(a_Entity).SetCustomName(a_EquippedItem.m_CustomName); + return false; }); return true; diff --git a/src/Items/ItemMobHead.h b/src/Items/ItemMobHead.h index ef16f6c96..f1e963e1f 100644 --- a/src/Items/ItemMobHead.h +++ b/src/Items/ItemMobHead.h @@ -75,26 +75,22 @@ public: auto BlockMeta = static_cast(a_ClickedBlockFace); // Use a callback to set the properties of the mob head block entity: - a_World.DoWithBlockEntityAt(a_PlacePos.x, a_PlacePos.y, a_PlacePos.z, [&](cBlockEntity & a_BlockEntity) - { - if (a_BlockEntity.GetBlockType() != E_BLOCK_HEAD) - { - return false; - } - auto & MobHeadEntity = static_cast(a_BlockEntity); + a_World.DoWithBlockEntityAt(a_PlacePos, [&](cBlockEntity & a_BlockEntity) + { + ASSERT(a_BlockEntity.GetBlockType() == E_BLOCK_HEAD); - int Rotation = 0; - if (BlockMeta == 1) - { - Rotation = FloorC(a_Player.GetYaw() * 16.0f / 360.0f + 0.5f) & 0x0f; - } + auto & MobHeadEntity = static_cast(a_BlockEntity); - MobHeadEntity.SetType(HeadType); - MobHeadEntity.SetRotation(static_cast(Rotation)); - MobHeadEntity.GetWorld()->BroadcastBlockEntity(MobHeadEntity.GetPos()); - return false; + int Rotation = 0; + if (BlockMeta == 1) + { + Rotation = FloorC(a_Player.GetYaw() * 16.0f / 360.0f + 0.5f) & 0x0f; } - ); + + MobHeadEntity.SetType(HeadType); + MobHeadEntity.SetRotation(static_cast(Rotation)); + return false; + }); } @@ -243,23 +239,23 @@ public: return false; } - // If it is a mob head, check the correct head type using the block entity: - if (BlockType == E_BLOCK_HEAD) - { - bool IsWitherHead = false; - a_World.DoWithBlockEntityAt(BlockX, BlockY, BlockZ, [&](cBlockEntity & a_Entity) + // If it is a mob head, check it's a wither skull using the block entity: + if ( + (BlockType == E_BLOCK_HEAD) && + !a_World.DoWithBlockEntityAt({ BlockX, BlockY, BlockZ }, [&](cBlockEntity & a_BlockEntity) + { + if (a_BlockEntity.GetBlockType() != E_BLOCK_HEAD) { - ASSERT(a_Entity.GetBlockType() == E_BLOCK_HEAD); - auto & MobHead = static_cast(a_Entity); - IsWitherHead = (MobHead.GetType() == SKULL_TYPE_WITHER); - return true; + return false; } - ); - if (!IsWitherHead) - { - return false; - } + + return static_cast(a_BlockEntity).GetType() == SKULL_TYPE_WITHER; + }) + ) + { + return false; } + // Matched, continue checking AirBlocks.emplace_back(BlockX, BlockY, BlockZ, E_BLOCK_AIR, 0); } // for i - a_Image -- cgit v1.2.3