diff options
author | peterbell10 <peterbell10@live.co.uk> | 2020-04-03 23:23:38 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-04-03 23:23:38 +0200 |
commit | aac592f98598aae327d07b2a1bb155e33c6d51b1 (patch) | |
tree | 24f90211d753a7ec87585e919ef7c7acdfa49909 /src | |
parent | New hotfix to prevent calling OnBroken (#4600) (diff) | |
download | cuberite-aac592f98598aae327d07b2a1bb155e33c6d51b1.tar cuberite-aac592f98598aae327d07b2a1bb155e33c6d51b1.tar.gz cuberite-aac592f98598aae327d07b2a1bb155e33c6d51b1.tar.bz2 cuberite-aac592f98598aae327d07b2a1bb155e33c6d51b1.tar.lz cuberite-aac592f98598aae327d07b2a1bb155e33c6d51b1.tar.xz cuberite-aac592f98598aae327d07b2a1bb155e33c6d51b1.tar.zst cuberite-aac592f98598aae327d07b2a1bb155e33c6d51b1.zip |
Diffstat (limited to '')
-rw-r--r-- | src/BlockArea.cpp | 133 | ||||
-rw-r--r-- | src/BlockArea.h | 13 | ||||
-rw-r--r-- | src/BlockEntities/BlockEntity.cpp | 49 | ||||
-rw-r--r-- | src/BlockEntities/BlockEntity.h | 9 | ||||
-rw-r--r-- | src/Chunk.cpp | 31 | ||||
-rw-r--r-- | src/Chunk.h | 5 | ||||
-rw-r--r-- | src/ChunkDef.h | 1 | ||||
-rw-r--r-- | src/Generating/ChunkDesc.cpp | 8 | ||||
-rw-r--r-- | src/SetChunkData.cpp | 3 | ||||
-rw-r--r-- | src/SetChunkData.h | 1 | ||||
-rwxr-xr-x | src/WorldStorage/WSSAnvil.cpp | 71 | ||||
-rwxr-xr-x | src/WorldStorage/WSSAnvil.h | 33 |
12 files changed, 144 insertions, 213 deletions
diff --git a/src/BlockArea.cpp b/src/BlockArea.cpp index bf9c285c1..1d63f147d 100644 --- a/src/BlockArea.cpp +++ b/src/BlockArea.cpp @@ -571,11 +571,11 @@ void cBlockArea::CopyTo(cBlockArea & a_Into) const } if (HasBlockEntities()) { - ClearBlockEntities(*(a_Into.m_BlockEntities)); + a_Into.m_BlockEntities->clear(); for (const auto & keyPair: *m_BlockEntities) { const auto & pos = keyPair.second->GetPos(); - a_Into.m_BlockEntities->insert({keyPair.first, keyPair.second->Clone(pos)}); + a_Into.m_BlockEntities->emplace(keyPair.first, keyPair.second->Clone(pos)); } } } @@ -665,36 +665,22 @@ void cBlockArea::Crop(int a_AddMinX, int a_SubMaxX, int a_AddMinY, int a_SubMaxY } if (HasBlockEntities()) { - auto maxX = m_Size.x - a_SubMaxX; - auto maxY = m_Size.y - a_SubMaxY; - auto maxZ = m_Size.z - a_SubMaxZ; + const Vector3i AddMin{ a_AddMinX, a_AddMinY, a_AddMinZ }; + const cCuboid CropBox{ AddMin, m_Size - Vector3i{a_SubMaxX, a_SubMaxY, a_SubMaxZ} }; // Move and crop block Entities: cBlockEntities oldBE; std::swap(oldBE, *m_BlockEntities); - for (const auto & keyPair: oldBE) + for (auto & keyPair: oldBE) { auto & be = keyPair.second; - auto posX = be->GetPosX(); - auto posY = be->GetPosY(); - auto posZ = be->GetPosZ(); - if ( - (posX < a_AddMinX) || (posX >= maxX) || - (posY < a_AddMinY) || (posY >= maxY) || - (posZ < a_AddMinZ) || (posZ >= maxZ) - ) - { - // The block entity is out of new coord range, remove it: - delete be; - } - else + auto Pos = be->GetPos(); + if (CropBox.IsInside(Pos)) { // The block entity is within the new coords, recalculate its coords to match the new area: - posX -= a_AddMinX; - posY -= a_AddMinY; - posZ -= a_AddMinZ; - be->SetPos({posX, posY, posZ}); - m_BlockEntities->insert({MakeIndex(posX, posY, posZ), std::move(be)}); + Pos -= AddMin; + be->SetPos(Pos); + m_BlockEntities->emplace(MakeIndex(Pos.x, Pos.y, Pos.z), std::move(be)); } } } @@ -732,14 +718,14 @@ void cBlockArea::Expand(int a_SubMinX, int a_AddMaxX, int a_SubMinY, int a_AddMa // Move block entities: cBlockEntities oldBE; std::swap(oldBE, *m_BlockEntities); - for (const auto & keyPair: oldBE) + for (auto & keyPair: oldBE) { auto & be = keyPair.second; auto posX = be->GetPosX() + a_SubMinX; auto posY = be->GetPosY() + a_SubMinY; auto posZ = be->GetPosZ() + a_SubMinZ; be->SetPos({posX, posY, posZ}); - m_BlockEntities->insert({MakeIndex(posX, posY, posZ), std::move(be)}); + m_BlockEntities->emplace(MakeIndex(posX, posY, posZ), std::move(be)); } } @@ -833,7 +819,7 @@ void cBlockArea::Fill(int a_DataTypes, BLOCKTYPE a_BlockType, NIBBLETYPE a_Block } else { - ClearBlockEntities(*m_BlockEntities); + m_BlockEntities->clear(); } } } @@ -893,7 +879,7 @@ void cBlockArea::FillRelCuboid(int a_MinRelX, int a_MaxRelX, int a_MinRelY, int } else { - ClearBlockEntities(*m_BlockEntities); + m_BlockEntities->clear(); } } } @@ -1093,7 +1079,7 @@ void cBlockArea::RotateCCW(void) { cBlockEntities oldBE; std::swap(oldBE, *m_BlockEntities); - for (const auto & keyPair: oldBE) + for (auto & keyPair: oldBE) { auto & be = keyPair.second; auto newX = be->GetPosZ(); @@ -1101,7 +1087,7 @@ void cBlockArea::RotateCCW(void) auto newZ = m_Size.x - be->GetPosX() - 1; auto newIdx = newX + newZ * m_Size.z + newY * m_Size.x * m_Size.z; be->SetPos({newX, newY, newZ}); - m_BlockEntities->insert({newIdx, std::move(be)}); + m_BlockEntities->emplace(newIdx, std::move(be)); } } @@ -1153,7 +1139,7 @@ void cBlockArea::RotateCW(void) { cBlockEntities oldBE; std::swap(oldBE, *m_BlockEntities); - for (const auto & keyPair: oldBE) + for (auto & keyPair: oldBE) { auto & be = keyPair.second; auto newX = m_Size.z - be->GetPosZ() - 1; @@ -1161,7 +1147,7 @@ void cBlockArea::RotateCW(void) auto newZ = be->GetPosX(); auto newIdx = newX + newZ * m_Size.z + newY * m_Size.x * m_Size.z; be->SetPos({newX, newY, newZ}); - m_BlockEntities->insert({newIdx, std::move(be)}); + m_BlockEntities->emplace(newIdx, std::move(be)); } } @@ -1212,7 +1198,7 @@ void cBlockArea::MirrorXY(void) { cBlockEntities oldBE; std::swap(oldBE, *m_BlockEntities); - for (const auto & keyPair: oldBE) + for (auto & keyPair: oldBE) { auto & be = keyPair.second; auto newX = be->GetPosX(); @@ -1220,7 +1206,7 @@ void cBlockArea::MirrorXY(void) auto newZ = MaxZ - be->GetPosZ(); auto newIdx = MakeIndex(newX, newY, newZ); be->SetPos({newX, newY, newZ}); - m_BlockEntities->insert({newIdx, std::move(be)}); + m_BlockEntities->emplace(newIdx, std::move(be)); } } } @@ -1269,7 +1255,7 @@ void cBlockArea::MirrorXZ(void) { cBlockEntities oldBE; std::swap(oldBE, *m_BlockEntities); - for (const auto & keyPair: oldBE) + for (auto & keyPair: oldBE) { auto & be = keyPair.second; auto newX = be->GetPosX(); @@ -1277,7 +1263,7 @@ void cBlockArea::MirrorXZ(void) auto newZ = be->GetPosZ(); auto newIdx = MakeIndex(newX, newY, newZ); be->SetPos({newX, newY, newZ}); - m_BlockEntities->insert({newIdx, std::move(be)}); + m_BlockEntities->emplace(newIdx, std::move(be)); } } } @@ -1326,7 +1312,7 @@ void cBlockArea::MirrorYZ(void) { cBlockEntities oldBE; std::swap(oldBE, *m_BlockEntities); - for (const auto & keyPair: oldBE) + for (auto & keyPair: oldBE) { auto & be = keyPair.second; auto newX = MaxX - be->GetPosX(); @@ -1334,7 +1320,7 @@ void cBlockArea::MirrorYZ(void) auto newZ = be->GetPosZ(); auto newIdx = MakeIndex(newX, newY, newZ); be->SetPos({newX, newY, newZ}); - m_BlockEntities->insert({newIdx, std::move(be)}); + m_BlockEntities->emplace(newIdx, std::move(be)); } } } @@ -1385,7 +1371,7 @@ void cBlockArea::RotateCCWNoMeta(void) { cBlockEntities oldBE; std::swap(oldBE, *m_BlockEntities); - for (const auto & keyPair: oldBE) + for (auto & keyPair: oldBE) { auto & be = keyPair.second; auto newX = be->GetPosZ(); @@ -1393,7 +1379,7 @@ void cBlockArea::RotateCCWNoMeta(void) auto newZ = m_Size.x - be->GetPosX() - 1; auto newIdx = newX + newZ * m_Size.z + newY * m_Size.x * m_Size.z; be->SetPos({newX, newY, newZ}); - m_BlockEntities->insert({newIdx, std::move(be)}); + m_BlockEntities->emplace(newIdx, std::move(be)); } } @@ -1446,7 +1432,7 @@ void cBlockArea::RotateCWNoMeta(void) { cBlockEntities oldBE; std::swap(oldBE, *m_BlockEntities); - for (const auto & keyPair: oldBE) + for (auto & keyPair: oldBE) { auto & be = keyPair.second; auto newX = m_Size.z - be->GetPosZ() - 1; @@ -1454,7 +1440,7 @@ void cBlockArea::RotateCWNoMeta(void) auto newZ = be->GetPosX(); auto newIdx = newX + newZ * m_Size.z + newY * m_Size.x * m_Size.z; be->SetPos({newX, newY, newZ}); - m_BlockEntities->insert({newIdx, std::move(be)}); + m_BlockEntities->emplace(newIdx, std::move(be)); } } @@ -1502,7 +1488,7 @@ void cBlockArea::MirrorXYNoMeta(void) { cBlockEntities oldBE; std::swap(oldBE, *m_BlockEntities); - for (const auto & keyPair: oldBE) + for (auto & keyPair: oldBE) { auto & be = keyPair.second; auto newX = be->GetPosX(); @@ -1510,7 +1496,7 @@ void cBlockArea::MirrorXYNoMeta(void) auto newZ = MaxZ - be->GetPosZ(); auto newIdx = MakeIndex(newX, newY, newZ); be->SetPos({newX, newY, newZ}); - m_BlockEntities->insert({newIdx, std::move(be)}); + m_BlockEntities->emplace(newIdx, std::move(be)); } } } @@ -1556,7 +1542,7 @@ void cBlockArea::MirrorXZNoMeta(void) { cBlockEntities oldBE; std::swap(oldBE, *m_BlockEntities); - for (const auto & keyPair: oldBE) + for (auto & keyPair: oldBE) { auto & be = keyPair.second; auto newX = be->GetPosX(); @@ -1564,7 +1550,7 @@ void cBlockArea::MirrorXZNoMeta(void) auto newZ = be->GetPosZ(); auto newIdx = MakeIndex(newX, newY, newZ); be->SetPos({newX, newY, newZ}); - m_BlockEntities->insert({newIdx, std::move(be)}); + m_BlockEntities->emplace(newIdx, std::move(be)); } } } @@ -1610,7 +1596,7 @@ void cBlockArea::MirrorYZNoMeta(void) { cBlockEntities oldBE; std::swap(oldBE, *m_BlockEntities); - for (const auto & keyPair: oldBE) + for (auto & keyPair: oldBE) { auto & be = keyPair.second; auto newX = MaxX - be->GetPosX(); @@ -1618,7 +1604,7 @@ void cBlockArea::MirrorYZNoMeta(void) auto newZ = be->GetPosZ(); auto newIdx = MakeIndex(newX, newY, newZ); be->SetPos({newX, newY, newZ}); - m_BlockEntities->insert({newIdx, std::move(be)}); + m_BlockEntities->emplace(newIdx, std::move(be)); } } } @@ -1649,7 +1635,7 @@ void cBlockArea::SetRelBlockType(int a_RelX, int a_RelY, int a_RelZ, BLOCKTYPE a if (cBlockEntity::IsBlockEntityBlockType(a_BlockType)) { NIBBLETYPE meta = HasBlockMetas() ? m_BlockMetas[idx] : 0; - m_BlockEntities->insert({idx, cBlockEntity::CreateByBlockType(a_BlockType, meta, {a_RelX, a_RelY, a_RelZ})}); + m_BlockEntities->emplace(idx, cBlockEntity::CreateByBlockType(a_BlockType, meta, {a_RelX, a_RelY, a_RelZ})); } } } @@ -1842,7 +1828,7 @@ void cBlockArea::SetRelBlockTypeMeta(int a_RelX, int a_RelY, int a_RelZ, B } if (cBlockEntity::IsBlockEntityBlockType(a_BlockType)) { - m_BlockEntities->insert({idx, cBlockEntity::CreateByBlockType(a_BlockType, a_BlockMeta, {a_RelX, a_RelY, a_RelZ})}); + m_BlockEntities->emplace(idx, cBlockEntity::CreateByBlockType(a_BlockType, a_BlockMeta, {a_RelX, a_RelY, a_RelZ})); } } } @@ -2432,7 +2418,7 @@ void cBlockArea::RelSetData( if (cBlockEntity::IsBlockEntityBlockType(a_BlockType)) { // The block type should have a block entity attached to it, create an empty one: - m_BlockEntities->insert({Index, cBlockEntity::CreateByBlockType(a_BlockType, a_BlockMeta, {a_RelX, a_RelY, a_RelZ})}); + m_BlockEntities->emplace(Index, cBlockEntity::CreateByBlockType(a_BlockType, a_BlockMeta, {a_RelX, a_RelY, a_RelZ})); } } } @@ -2600,19 +2586,6 @@ void cBlockArea::MergeByStrategy(const cBlockArea & a_Src, int a_RelX, int a_Rel -void cBlockArea::ClearBlockEntities(cBlockEntities & a_BlockEntities) -{ - for (auto & keyPair: a_BlockEntities) - { - delete keyPair.second; - } - a_BlockEntities.clear(); -} - - - - - void cBlockArea::MergeBlockEntities(int a_RelX, int a_RelY, int a_RelZ, const cBlockArea & a_Src) { // Only supported with both BlockEntities and BlockTypes (caller should check): @@ -2652,13 +2625,13 @@ void cBlockArea::MergeBlockEntities(int a_RelX, int a_RelY, int a_RelZ, const cB auto itrSrc = a_Src.m_BlockEntities->find(srcIdx); if (itrSrc != a_Src.m_BlockEntities->end()) { - m_BlockEntities->insert({idx, itrSrc->second->Clone({x, y, z})}); + m_BlockEntities->emplace(idx, itrSrc->second->Clone({x, y, z})); continue; } } // No BE found in a_Src, insert a new empty one: NIBBLETYPE meta = HasBlockMetas() ? m_BlockMetas[idx] : 0; - m_BlockEntities->insert({idx, cBlockEntity::CreateByBlockType(type, meta, {x, y, z})}); + m_BlockEntities->emplace(idx, cBlockEntity::CreateByBlockType(type, meta, {x, y, z})); } // for x, z, y } @@ -2694,7 +2667,7 @@ void cBlockArea::RescanBlockEntities(void) } // Create a new BE for this block: NIBBLETYPE meta = HasBlockMetas() ? m_BlockMetas[idx] : 0; - m_BlockEntities->insert({idx, cBlockEntity::CreateByBlockType(type, meta, {x, y, z})}); + m_BlockEntities->emplace(idx, cBlockEntity::CreateByBlockType(type, meta, {x, y, z})); } // for x, z, y } @@ -2715,11 +2688,7 @@ void cBlockArea::RemoveNonMatchingBlockEntities(void) auto type = m_BlockTypes[static_cast<size_t>(keyPair.first)]; if (type == keyPair.second->GetBlockType()) { - m_BlockEntities->insert({keyPair.first, std::move(keyPair.second)}); - } - else - { - delete keyPair.second; + m_BlockEntities->insert(std::move(keyPair)); } } } @@ -2735,23 +2704,7 @@ cBlockEntity * cBlockArea::GetBlockEntityRel(Vector3i a_RelPos) return nullptr; } auto itr = m_BlockEntities->find(MakeIndex(a_RelPos)); - return (itr == m_BlockEntities->end()) ? nullptr : itr->second; -} - - - - - -//////////////////////////////////////////////////////////////////////////////// -// cBlockArea::sBlockEntityDeleter: - -void cBlockArea::sBlockEntitiesDeleter::operator () (cBlockEntities * a_BlockEntities) -{ - if (a_BlockEntities != nullptr) - { - ClearBlockEntities(*a_BlockEntities); - delete a_BlockEntities; - } + return (itr == m_BlockEntities->end()) ? nullptr : itr->second.get(); } @@ -2996,7 +2949,7 @@ void cBlockArea::cChunkReader::BlockEntity(cBlockEntity * a_BlockEntity) } auto areaPos = a_BlockEntity->GetPos() - m_Area.m_Origin; auto Idx = m_Area.MakeIndex(areaPos); - m_Area.m_BlockEntities->insert({Idx, a_BlockEntity->Clone(areaPos)}); + m_Area.m_BlockEntities->emplace(Idx, a_BlockEntity->Clone(areaPos)); } diff --git a/src/BlockArea.h b/src/BlockArea.h index 9c0cb4ec9..f9dd388c0 100644 --- a/src/BlockArea.h +++ b/src/BlockArea.h @@ -19,6 +19,7 @@ #include "ChunkDataCallback.h" #include "Cuboid.h" #include "FunctionRef.h" +#include "BlockEntities/BlockEntity.h" @@ -456,6 +457,7 @@ protected: using NIBBLEARRAY = std::unique_ptr<NIBBLETYPE[]>; using BLOCKARRAY = std::unique_ptr<BLOCKTYPE[]>; + using cBlockEntitiesPtr = std::unique_ptr<cBlockEntities>; Vector3i m_Origin; Vector3i m_Size; @@ -469,14 +471,6 @@ protected: NIBBLEARRAY m_BlockLight; // Each light value is stored as a separate byte for faster access NIBBLEARRAY m_BlockSkyLight; // Each light value is stored as a separate byte for faster access - /** Deleter to clear the block entities before deleting the container. */ - struct sBlockEntitiesDeleter - { - void operator () (cBlockEntities * a_BlockEntities); - }; - - using cBlockEntitiesPtr = std::unique_ptr<cBlockEntities, sBlockEntitiesDeleter>; - /** The block entities contained within the area. Only valid if the area was created / read with the baBlockEntities flag. The block entities are owned by this object. */ @@ -512,9 +506,6 @@ protected: template <bool MetasValid> void MergeByStrategy(const cBlockArea & a_Src, int a_RelX, int a_RelY, int a_RelZ, eMergeStrategy a_Strategy, const NIBBLETYPE * SrcMetas, NIBBLETYPE * DstMetas); - /** Clears the block entities from the specified container, freeing each blockentity. */ - static void ClearBlockEntities(cBlockEntities & a_BlockEntities); - /** Updates m_BlockEntities to remove BEs that no longer match the blocktype at their coords, and clones from a_Src the BEs that are missing. a_RelX, a_RelY and a_RelZ are relative coords that should be added to all BEs from a_Src before checking them. If a block should have a BE but one cannot be found in either this or a_Src, a new one is created. */ diff --git a/src/BlockEntities/BlockEntity.cpp b/src/BlockEntities/BlockEntity.cpp index ace8e4662..33ab3253d 100644 --- a/src/BlockEntities/BlockEntity.cpp +++ b/src/BlockEntities/BlockEntity.cpp @@ -73,29 +73,28 @@ bool cBlockEntity::IsBlockEntityBlockType(BLOCKTYPE a_BlockType) -cBlockEntity * cBlockEntity::CreateByBlockType(BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, Vector3i a_Pos, cWorld * a_World) +OwnedBlockEntity cBlockEntity::CreateByBlockType(BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, Vector3i a_Pos, cWorld * a_World) { switch (a_BlockType) { - case E_BLOCK_BEACON: return new cBeaconEntity (a_BlockType, a_BlockMeta, a_Pos, a_World); - case E_BLOCK_BED: return new cBedEntity (a_BlockType, a_BlockMeta, a_Pos, a_World); - case E_BLOCK_BREWING_STAND: return new cBrewingstandEntity(a_BlockType, a_BlockMeta, a_Pos, a_World); - case E_BLOCK_CHEST: return new cChestEntity (a_BlockType, a_BlockMeta, a_Pos, a_World); - case E_BLOCK_COMMAND_BLOCK: return new cCommandBlockEntity(a_BlockType, a_BlockMeta, a_Pos, a_World); - case E_BLOCK_DISPENSER: return new cDispenserEntity (a_BlockType, a_BlockMeta, a_Pos, a_World); - case E_BLOCK_DROPPER: return new cDropperEntity (a_BlockType, a_BlockMeta, a_Pos, a_World); - case E_BLOCK_ENDER_CHEST: return new cEnderChestEntity (a_BlockType, a_BlockMeta, a_Pos, a_World); - case E_BLOCK_FLOWER_POT: return new cFlowerPotEntity (a_BlockType, a_BlockMeta, a_Pos, a_World); - case E_BLOCK_FURNACE: return new cFurnaceEntity (a_BlockType, a_BlockMeta, a_Pos, a_World); - case E_BLOCK_HEAD: return new cMobHeadEntity (a_BlockType, a_BlockMeta, a_Pos, a_World); - case E_BLOCK_HOPPER: return new cHopperEntity (a_BlockType, a_BlockMeta, a_Pos, a_World); - case E_BLOCK_JUKEBOX: return new cJukeboxEntity (a_BlockType, a_BlockMeta, a_Pos, a_World); - case E_BLOCK_LIT_FURNACE: return new cFurnaceEntity (a_BlockType, a_BlockMeta, a_Pos, a_World); - case E_BLOCK_MOB_SPAWNER: return new cMobSpawnerEntity (a_BlockType, a_BlockMeta, a_Pos, a_World); - case E_BLOCK_NOTE_BLOCK: return new cNoteEntity (a_BlockType, a_BlockMeta, a_Pos, a_World); - case E_BLOCK_SIGN_POST: return new cSignEntity (a_BlockType, a_BlockMeta, a_Pos, a_World); - case E_BLOCK_TRAPPED_CHEST: return new cChestEntity (a_BlockType, a_BlockMeta, a_Pos, a_World); - case E_BLOCK_WALLSIGN: return new cSignEntity (a_BlockType, a_BlockMeta, a_Pos, a_World); + case E_BLOCK_BED: return cpp14::make_unique<cBedEntity >(a_BlockType, a_BlockMeta, a_Pos, a_World); + case E_BLOCK_BREWING_STAND: return cpp14::make_unique<cBrewingstandEntity>(a_BlockType, a_BlockMeta, a_Pos, a_World); + case E_BLOCK_CHEST: return cpp14::make_unique<cChestEntity >(a_BlockType, a_BlockMeta, a_Pos, a_World); + case E_BLOCK_COMMAND_BLOCK: return cpp14::make_unique<cCommandBlockEntity>(a_BlockType, a_BlockMeta, a_Pos, a_World); + case E_BLOCK_DISPENSER: return cpp14::make_unique<cDispenserEntity >(a_BlockType, a_BlockMeta, a_Pos, a_World); + case E_BLOCK_DROPPER: return cpp14::make_unique<cDropperEntity >(a_BlockType, a_BlockMeta, a_Pos, a_World); + case E_BLOCK_ENDER_CHEST: return cpp14::make_unique<cEnderChestEntity >(a_BlockType, a_BlockMeta, a_Pos, a_World); + case E_BLOCK_FLOWER_POT: return cpp14::make_unique<cFlowerPotEntity >(a_BlockType, a_BlockMeta, a_Pos, a_World); + case E_BLOCK_FURNACE: return cpp14::make_unique<cFurnaceEntity >(a_BlockType, a_BlockMeta, a_Pos, a_World); + case E_BLOCK_HEAD: return cpp14::make_unique<cMobHeadEntity >(a_BlockType, a_BlockMeta, a_Pos, a_World); + case E_BLOCK_HOPPER: return cpp14::make_unique<cHopperEntity >(a_BlockType, a_BlockMeta, a_Pos, a_World); + case E_BLOCK_JUKEBOX: return cpp14::make_unique<cJukeboxEntity >(a_BlockType, a_BlockMeta, a_Pos, a_World); + case E_BLOCK_LIT_FURNACE: return cpp14::make_unique<cFurnaceEntity >(a_BlockType, a_BlockMeta, a_Pos, a_World); + case E_BLOCK_MOB_SPAWNER: return cpp14::make_unique<cMobSpawnerEntity >(a_BlockType, a_BlockMeta, a_Pos, a_World); + case E_BLOCK_NOTE_BLOCK: return cpp14::make_unique<cNoteEntity >(a_BlockType, a_BlockMeta, a_Pos, a_World); + case E_BLOCK_SIGN_POST: return cpp14::make_unique<cSignEntity >(a_BlockType, a_BlockMeta, a_Pos, a_World); + case E_BLOCK_TRAPPED_CHEST: return cpp14::make_unique<cChestEntity >(a_BlockType, a_BlockMeta, a_Pos, a_World); + case E_BLOCK_WALLSIGN: return cpp14::make_unique<cSignEntity >(a_BlockType, a_BlockMeta, a_Pos, a_World); default: { LOGD("%s: Requesting creation of an unknown block entity - block type %d (%s)", @@ -111,11 +110,11 @@ cBlockEntity * cBlockEntity::CreateByBlockType(BLOCKTYPE a_BlockType, NIBBLETYPE -cBlockEntity * cBlockEntity::Clone(Vector3i a_Pos) +OwnedBlockEntity cBlockEntity::Clone(Vector3i a_Pos) { - auto res = std::unique_ptr<cBlockEntity>(CreateByBlockType(m_BlockType, m_BlockMeta, a_Pos, nullptr)); + auto res = CreateByBlockType(m_BlockType, m_BlockMeta, a_Pos, nullptr); res->CopyFrom(*this); - return res.release(); + return res; } @@ -128,7 +127,3 @@ void cBlockEntity::CopyFrom(const cBlockEntity & a_Src) ASSERT(m_BlockType == a_Src.m_BlockType); ASSERT(m_BlockMeta == a_Src.m_BlockMeta); } - - - - diff --git a/src/BlockEntities/BlockEntity.h b/src/BlockEntities/BlockEntity.h index cb377422e..2617edfb6 100644 --- a/src/BlockEntities/BlockEntity.h +++ b/src/BlockEntities/BlockEntity.h @@ -1,6 +1,7 @@ #pragma once +#include <unordered_map> @@ -31,6 +32,10 @@ class cChunk; class cPlayer; class cWorld; +class cBlockEntity; + +using OwnedBlockEntity = std::unique_ptr<cBlockEntity>; +using cBlockEntities = std::unordered_map<size_t, OwnedBlockEntity>; @@ -73,11 +78,11 @@ public: /** Creates a new block entity for the specified block type at the specified absolute pos. If a_World is valid, then the entity is created bound to that world Returns nullptr for unknown block types. */ - static cBlockEntity * CreateByBlockType(BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, Vector3i a_Pos, cWorld * a_World = nullptr); + static OwnedBlockEntity CreateByBlockType(BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, Vector3i a_Pos, cWorld * a_World = nullptr); /** Makes an exact copy of this block entity, except for its m_World (set to nullptr), and at a new position. Uses CopyFrom() to copy the properties. */ - cBlockEntity * Clone(Vector3i a_Pos); + OwnedBlockEntity Clone(Vector3i a_Pos); /** Copies all properties of a_Src into this entity, except for its m_World and location. Each non-abstract descendant should override to copy its specific properties, and call diff --git a/src/Chunk.cpp b/src/Chunk.cpp index d9601e451..17e130cdd 100644 --- a/src/Chunk.cpp +++ b/src/Chunk.cpp @@ -111,10 +111,6 @@ cChunk::~cChunk() // LOGINFO("### delete cChunk() (%i, %i) from %p, thread 0x%x ###", m_PosX, m_PosZ, this, GetCurrentThreadId()); - for (auto & KeyPair : m_BlockEntities) - { - delete KeyPair.second; - } m_BlockEntities.clear(); // Remove and destroy all entities that are not players: @@ -306,7 +302,7 @@ void cChunk::GetAllData(cChunkDataCallback & a_Callback) for (auto & KeyPair : m_BlockEntities) { - a_Callback.BlockEntity(KeyPair.second); + a_Callback.BlockEntity(KeyPair.second.get()); } } @@ -326,17 +322,13 @@ void cChunk::SetAllData(cSetChunkData & a_SetChunkData) m_IsLightValid = a_SetChunkData.IsLightValid(); // Clear the block entities present - either the loader / saver has better, or we'll create empty ones: - for (auto & KeyPair : m_BlockEntities) - { - delete KeyPair.second; - } m_BlockEntities = std::move(a_SetChunkData.GetBlockEntities()); // Check that all block entities have a valid blocktype at their respective coords (DEBUG-mode only): #ifdef _DEBUG for (auto & KeyPair : m_BlockEntities) { - cBlockEntity * Block = KeyPair.second; + cBlockEntity * Block = KeyPair.second.get(); BLOCKTYPE EntityBlockType = Block->GetBlockType(); BLOCKTYPE WorldBlockType = GetBlock(Block->GetRelX(), Block->GetPosY(), Block->GetRelZ()); ASSERT(WorldBlockType == EntityBlockType); @@ -449,7 +441,6 @@ void cChunk::WriteBlockArea(cBlockArea & a_Area, int a_MinBlockX, int a_MinBlock { if (affectedArea.IsInside(itr->second->GetPos())) { - delete itr->second; itr = m_BlockEntities.erase(itr); } else @@ -483,7 +474,7 @@ void cChunk::WriteBlockArea(cBlockArea & a_Area, int a_MinBlockX, int a_MinBlock } auto clone = be->Clone({posX, posY, posZ}); clone->SetWorld(m_World); - AddBlockEntityClean(clone); + AddBlockEntityClean(std::move(clone)); m_World->BroadcastBlockEntity({posX, posY, posZ}); } } @@ -1347,8 +1338,6 @@ void cChunk::SetBlock(Vector3i a_RelPos, BLOCKTYPE a_BlockType, NIBBLETYPE a_Blo { BlockEntity->Destroy(); RemoveBlockEntity(BlockEntity); - delete BlockEntity; - BlockEntity = nullptr; } // If the new block is a block entity, create the entity object: @@ -1528,20 +1517,20 @@ void cChunk::SendBlockTo(int a_RelX, int a_RelY, int a_RelZ, cClientHandle * a_C -void cChunk::AddBlockEntity(cBlockEntity * a_BlockEntity) +void cChunk::AddBlockEntity(OwnedBlockEntity a_BlockEntity) { MarkDirty(); - AddBlockEntityClean(a_BlockEntity); + AddBlockEntityClean(std::move(a_BlockEntity)); } -void cChunk::AddBlockEntityClean(cBlockEntity * a_BlockEntity) +void cChunk::AddBlockEntityClean(OwnedBlockEntity a_BlockEntity) { int Idx = MakeIndex(a_BlockEntity->GetRelX(), a_BlockEntity->GetPosY(), a_BlockEntity->GetRelZ()); - auto Result = m_BlockEntities.insert({ Idx, a_BlockEntity }); + auto Result = m_BlockEntities.emplace(Idx, std::move(a_BlockEntity)); UNUSED(Result); ASSERT(Result.second); // No block entity already at this position } @@ -1561,7 +1550,7 @@ cBlockEntity * cChunk::GetBlockEntity(Vector3i a_AbsPos) } auto itr = m_BlockEntities.find(static_cast<size_t>(MakeIndexNoCheck(relPos))); - return (itr == m_BlockEntities.end()) ? nullptr : itr->second; + return (itr == m_BlockEntities.end()) ? nullptr : itr->second.get(); } @@ -1572,7 +1561,7 @@ cBlockEntity * cChunk::GetBlockEntityRel(Vector3i a_RelPos) { ASSERT(IsValidRelPos(a_RelPos)); auto itr = m_BlockEntities.find(static_cast<size_t>(MakeIndexNoCheck(a_RelPos))); - return (itr == m_BlockEntities.end()) ? nullptr : itr->second; + return (itr == m_BlockEntities.end()) ? nullptr : itr->second.get(); } @@ -1959,7 +1948,7 @@ bool cChunk::GenericForEachBlockEntity(cFunctionRef<bool(tyEntity &)> a_Callback // The blockentity list is locked by the parent chunkmap's CS for (auto & KeyPair : m_BlockEntities) { - cBlockEntity * Block = KeyPair.second; + cBlockEntity * Block = KeyPair.second.get(); if ( (sizeof...(tBlocktype) == 0) || // Let empty list mean all block entities (IsOneOf<tBlocktype...>(Block->GetBlockType())) diff --git a/src/Chunk.h b/src/Chunk.h index dd21d0e8f..39dc07a9f 100644 --- a/src/Chunk.h +++ b/src/Chunk.h @@ -1,6 +1,7 @@ #pragma once +#include "BlockEntities/BlockEntity.h" #include "Entities/Entity.h" #include "ChunkData.h" @@ -653,10 +654,10 @@ private: void GetThreeRandomNumbers(int & a_X, int & a_Y, int & a_Z, int a_MaxX, int a_MaxY, int a_MaxZ); void RemoveBlockEntity(cBlockEntity * a_BlockEntity); - void AddBlockEntity (cBlockEntity * a_BlockEntity); + void AddBlockEntity (OwnedBlockEntity a_BlockEntity); /** Add a block entity to the chunk without marking the chunk dirty */ - void AddBlockEntityClean(cBlockEntity * a_BlockEntity); + void AddBlockEntityClean(OwnedBlockEntity a_BlockEntity); /** Creates a block entity for each block that needs a block entity and doesn't have one already */ void CreateBlockEntities(void); diff --git a/src/ChunkDef.h b/src/ChunkDef.h index 9a6ed9f04..f93fed249 100644 --- a/src/ChunkDef.h +++ b/src/ChunkDef.h @@ -31,7 +31,6 @@ class cChunkCoords; using OwnedEntity = std::unique_ptr<cEntity>; using cEntityList = std::vector<OwnedEntity>; -using cBlockEntities = std::map<size_t, cBlockEntity *>; diff --git a/src/Generating/ChunkDesc.cpp b/src/Generating/ChunkDesc.cpp index 060901bba..340e3f805 100644 --- a/src/Generating/ChunkDesc.cpp +++ b/src/Generating/ChunkDesc.cpp @@ -578,7 +578,7 @@ cBlockEntity * cChunkDesc::GetBlockEntity(int a_RelX, int a_RelY, int a_RelZ) if (itr != m_BlockEntities.end()) { // Already in the list: - cBlockEntity * BlockEntity = itr->second; + cBlockEntity * BlockEntity = itr->second.get(); if (BlockEntity->GetBlockType() == GetBlockType(a_RelX, a_RelY, a_RelZ)) { // Correct type, already present. Return it: @@ -595,14 +595,14 @@ cBlockEntity * cChunkDesc::GetBlockEntity(int a_RelX, int a_RelY, int a_RelZ) int AbsZ = a_RelZ + m_Coords.m_ChunkZ * cChunkDef::Width; // The block entity is not created yet, try to create it and add to list: - cBlockEntity * be = cBlockEntity::CreateByBlockType(GetBlockType(a_RelX, a_RelY, a_RelZ), GetBlockMeta(a_RelX, a_RelY, a_RelZ), {AbsX, a_RelY, AbsZ}); + auto be = cBlockEntity::CreateByBlockType(GetBlockType(a_RelX, a_RelY, a_RelZ), GetBlockMeta(a_RelX, a_RelY, a_RelZ), {AbsX, a_RelY, AbsZ}); if (be == nullptr) { // No block entity for this block type return nullptr; } - m_BlockEntities.insert({ Idx, be }); - return be; + auto res = m_BlockEntities.emplace(Idx, std::move(be)); + return res.first->second.get(); } diff --git a/src/SetChunkData.cpp b/src/SetChunkData.cpp index 19448147a..d27e53d1c 100644 --- a/src/SetChunkData.cpp +++ b/src/SetChunkData.cpp @@ -131,7 +131,7 @@ void cSetChunkData::RemoveInvalidBlockEntities(void) { for (cBlockEntities::iterator itr = m_BlockEntities.begin(); itr != m_BlockEntities.end();) { - cBlockEntity * BlockEntity = itr->second; + auto & BlockEntity = itr->second; BLOCKTYPE EntityBlockType = BlockEntity->GetBlockType(); BLOCKTYPE WorldBlockType = m_ChunkData.GetBlock({BlockEntity->GetRelX(), BlockEntity->GetPosY(), BlockEntity->GetRelZ()}); if (EntityBlockType != WorldBlockType) @@ -142,7 +142,6 @@ void cSetChunkData::RemoveInvalidBlockEntities(void) ItemTypeToString(EntityBlockType), EntityBlockType, ItemTypeToString(WorldBlockType), WorldBlockType ); - delete BlockEntity; itr = m_BlockEntities.erase(itr); } else diff --git a/src/SetChunkData.h b/src/SetChunkData.h index 3e1dde35a..1744d3b78 100644 --- a/src/SetChunkData.h +++ b/src/SetChunkData.h @@ -6,6 +6,7 @@ #pragma once #include "ChunkData.h" +#include "BlockEntities/BlockEntity.h" diff --git a/src/WorldStorage/WSSAnvil.cpp b/src/WorldStorage/WSSAnvil.cpp index 0480680dd..65facd817 100755 --- a/src/WorldStorage/WSSAnvil.cpp +++ b/src/WorldStorage/WSSAnvil.cpp @@ -612,17 +612,15 @@ void cWSSAnvil::LoadBlockEntitiesFromNBT(cBlockEntities & a_BlockEntities, const // Load the proper BlockEntity type based on the block type: BLOCKTYPE BlockType = cChunkDef::GetBlock(a_BlockTypes, relPos); NIBBLETYPE BlockMeta = cChunkDef::GetNibble(a_BlockMetas, relPos); - std::unique_ptr<cBlockEntity> be(LoadBlockEntityFromNBT(a_NBT, Child, absPos, BlockType, BlockMeta)); - if (be.get() == nullptr) + auto be = LoadBlockEntityFromNBT(a_NBT, Child, absPos, BlockType, BlockMeta); + if (be == nullptr) { continue; } // Add the BlockEntity to the loaded data: auto Idx = cChunkDef::MakeIndex(be->GetRelX(), be->GetPosY(), be->GetRelZ()); - a_BlockEntities.insert({ Idx, be.get() }); - // Release after inserting in case it throws. - be.release(); + a_BlockEntities.emplace(Idx, std::move(be)); } // for Child - tag children } @@ -630,7 +628,7 @@ void cWSSAnvil::LoadBlockEntitiesFromNBT(cBlockEntities & a_BlockEntities, const -cBlockEntity * cWSSAnvil::LoadBlockEntityFromNBT(const cParsedNBT & a_NBT, int a_Tag, Vector3i a_Pos, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta) +OwnedBlockEntity cWSSAnvil::LoadBlockEntityFromNBT(const cParsedNBT & a_NBT, int a_Tag, Vector3i a_Pos, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta) { ASSERT((a_Pos.y >= 0) && (a_Pos.y < cChunkDef::Height)); @@ -892,7 +890,7 @@ bool cWSSAnvil::CheckBlockEntityType(const cParsedNBT & a_NBT, int a_TagIdx, con -cBlockEntity * cWSSAnvil::LoadBeaconFromNBT(const cParsedNBT & a_NBT, int a_TagIdx, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, Vector3i a_Pos) +OwnedBlockEntity cWSSAnvil::LoadBeaconFromNBT(const cParsedNBT & a_NBT, int a_TagIdx, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, Vector3i a_Pos) { // Check if the data has a proper type: static const AStringVector expectedTypes({"Beacon", "minecraft:beacon"}); @@ -928,14 +926,14 @@ cBlockEntity * cWSSAnvil::LoadBeaconFromNBT(const cParsedNBT & a_NBT, int a_TagI LoadItemGridFromNBT(Beacon->GetContents(), a_NBT, Items); } - return Beacon.release(); + return std::move(Beacon); } -cBlockEntity * cWSSAnvil::LoadBedFromNBT(const cParsedNBT & a_NBT, int a_TagIdx, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, Vector3i a_Pos) +OwnedBlockEntity cWSSAnvil::LoadBedFromNBT(const cParsedNBT & a_NBT, int a_TagIdx, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, Vector3i a_Pos) { // Check if the data has a proper type: static const AStringVector expectedTypes({ "Bed", "minecraft:bed" }); @@ -953,15 +951,14 @@ cBlockEntity * cWSSAnvil::LoadBedFromNBT(const cParsedNBT & a_NBT, int a_TagIdx, Color = static_cast<short>(a_NBT.GetInt(ColorIDx)); } - auto Bed = cpp14::make_unique<cBedEntity>(a_BlockType, a_BlockMeta, a_Pos, m_World, Color); - return Bed.release(); + return cpp14::make_unique<cBedEntity>(a_BlockType, a_BlockMeta, a_Pos, m_World, Color); } -cBlockEntity * cWSSAnvil::LoadBrewingstandFromNBT(const cParsedNBT & a_NBT, int a_TagIdx, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, Vector3i a_Pos) +OwnedBlockEntity cWSSAnvil::LoadBrewingstandFromNBT(const cParsedNBT & a_NBT, int a_TagIdx, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, Vector3i a_Pos) { // Check if the data has a proper type: static const AStringVector expectedTypes({ "Brewingstand", "minecraft:brewing_stand" }); @@ -1012,14 +1009,14 @@ cBlockEntity * cWSSAnvil::LoadBrewingstandFromNBT(const cParsedNBT & a_NBT, int // Restart brewing: Brewingstand->LoadRecipes(); Brewingstand->ContinueBrewing(); - return Brewingstand.release(); + return std::move(Brewingstand); } -cBlockEntity * cWSSAnvil::LoadChestFromNBT(const cParsedNBT & a_NBT, int a_TagIdx, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, Vector3i a_Pos) +OwnedBlockEntity cWSSAnvil::LoadChestFromNBT(const cParsedNBT & a_NBT, int a_TagIdx, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, Vector3i a_Pos) { // Check if the data has a proper type: // Note that older Cuberite code used "TrappedChest" for trapped chests; new code mimics vanilla and uses "Chest" throughout, but we allow migration here: @@ -1036,14 +1033,14 @@ cBlockEntity * cWSSAnvil::LoadChestFromNBT(const cParsedNBT & a_NBT, int a_TagId } auto Chest = cpp14::make_unique<cChestEntity>(a_BlockType, a_BlockMeta, a_Pos, m_World); LoadItemGridFromNBT(Chest->GetContents(), a_NBT, Items); - return Chest.release(); + return std::move(Chest); } -cBlockEntity * cWSSAnvil::LoadCommandBlockFromNBT(const cParsedNBT & a_NBT, int a_TagIdx, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, Vector3i a_Pos) +OwnedBlockEntity cWSSAnvil::LoadCommandBlockFromNBT(const cParsedNBT & a_NBT, int a_TagIdx, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, Vector3i a_Pos) { // Check if the data has a proper type: static const AStringVector expectedTypes({ "Control", "minecraft:command_block" }); @@ -1074,14 +1071,14 @@ cBlockEntity * cWSSAnvil::LoadCommandBlockFromNBT(const cParsedNBT & a_NBT, int // TODO 2014-01-18 xdot: Figure out what TrackOutput is and parse it. - return CmdBlock.release(); + return std::move(CmdBlock); } -cBlockEntity * cWSSAnvil::LoadDispenserFromNBT(const cParsedNBT & a_NBT, int a_TagIdx, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, Vector3i a_Pos) +OwnedBlockEntity cWSSAnvil::LoadDispenserFromNBT(const cParsedNBT & a_NBT, int a_TagIdx, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, Vector3i a_Pos) { // Check if the data has a proper type: static const AStringVector expectedTypes({ "Trap", "minecraft:dispenser" }); @@ -1097,14 +1094,14 @@ cBlockEntity * cWSSAnvil::LoadDispenserFromNBT(const cParsedNBT & a_NBT, int a_T } auto Dispenser = cpp14::make_unique<cDispenserEntity>(a_BlockType, a_BlockMeta, a_Pos, m_World); LoadItemGridFromNBT(Dispenser->GetContents(), a_NBT, Items); - return Dispenser.release(); + return std::move(Dispenser); } -cBlockEntity * cWSSAnvil::LoadDropperFromNBT(const cParsedNBT & a_NBT, int a_TagIdx, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, Vector3i a_Pos) +OwnedBlockEntity cWSSAnvil::LoadDropperFromNBT(const cParsedNBT & a_NBT, int a_TagIdx, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, Vector3i a_Pos) { // Check if the data has a proper type: static const AStringVector expectedTypes({ "Dropper", "minecraft:dropper" }); @@ -1120,14 +1117,14 @@ cBlockEntity * cWSSAnvil::LoadDropperFromNBT(const cParsedNBT & a_NBT, int a_Tag } auto Dropper = cpp14::make_unique<cDropperEntity>(a_BlockType, a_BlockMeta, a_Pos, m_World); LoadItemGridFromNBT(Dropper->GetContents(), a_NBT, Items); - return Dropper.release(); + return std::move(Dropper); } -cBlockEntity * cWSSAnvil::LoadFlowerPotFromNBT(const cParsedNBT & a_NBT, int a_TagIdx, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, Vector3i a_Pos) +OwnedBlockEntity cWSSAnvil::LoadFlowerPotFromNBT(const cParsedNBT & a_NBT, int a_TagIdx, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, Vector3i a_Pos) { // Check if the data has a proper type: static const AStringVector expectedTypes({ "FlowerPot", "minecraft:flower_pot" }); @@ -1159,14 +1156,14 @@ cBlockEntity * cWSSAnvil::LoadFlowerPotFromNBT(const cParsedNBT & a_NBT, int a_T } FlowerPot->SetItem(Item); - return FlowerPot.release(); + return std::move(FlowerPot); } -cBlockEntity * cWSSAnvil::LoadFurnaceFromNBT(const cParsedNBT & a_NBT, int a_TagIdx, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, Vector3i a_Pos) +OwnedBlockEntity cWSSAnvil::LoadFurnaceFromNBT(const cParsedNBT & a_NBT, int a_TagIdx, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, Vector3i a_Pos) { // Check if the data has a proper type: static const AStringVector expectedTypes({ "Furnace", "minecraft:furnace" }); @@ -1219,14 +1216,14 @@ cBlockEntity * cWSSAnvil::LoadFurnaceFromNBT(const cParsedNBT & a_NBT, int a_Tag // Restart cooking: Furnace->ContinueCooking(); Furnace->SetLoading(false); - return Furnace.release(); + return std::move(Furnace); } -cBlockEntity * cWSSAnvil::LoadHopperFromNBT(const cParsedNBT & a_NBT, int a_TagIdx, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, Vector3i a_Pos) +OwnedBlockEntity cWSSAnvil::LoadHopperFromNBT(const cParsedNBT & a_NBT, int a_TagIdx, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, Vector3i a_Pos) { // Check if the data has a proper type: static const AStringVector expectedTypes({ "Hopper", "minecraft:hopper" }); @@ -1242,14 +1239,14 @@ cBlockEntity * cWSSAnvil::LoadHopperFromNBT(const cParsedNBT & a_NBT, int a_TagI } auto Hopper = cpp14::make_unique<cHopperEntity>(a_BlockType, a_BlockMeta, a_Pos, m_World); LoadItemGridFromNBT(Hopper->GetContents(), a_NBT, Items); - return Hopper.release(); + return std::move(Hopper); } -cBlockEntity * cWSSAnvil::LoadJukeboxFromNBT(const cParsedNBT & a_NBT, int a_TagIdx, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, Vector3i a_Pos) +OwnedBlockEntity cWSSAnvil::LoadJukeboxFromNBT(const cParsedNBT & a_NBT, int a_TagIdx, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, Vector3i a_Pos) { // Check if the data has a proper type: static const AStringVector expectedTypes({ "RecordPlayer", "minecraft:jukebox" }); @@ -1264,14 +1261,14 @@ cBlockEntity * cWSSAnvil::LoadJukeboxFromNBT(const cParsedNBT & a_NBT, int a_Tag { Jukebox->SetRecord(a_NBT.GetInt(Record)); } - return Jukebox.release(); + return std::move(Jukebox); } -cBlockEntity * cWSSAnvil::LoadMobSpawnerFromNBT(const cParsedNBT & a_NBT, int a_TagIdx, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, Vector3i a_Pos) +OwnedBlockEntity cWSSAnvil::LoadMobSpawnerFromNBT(const cParsedNBT & a_NBT, int a_TagIdx, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, Vector3i a_Pos) { // Check if the data has a proper type: static const AStringVector expectedTypes({ "MobSpawner", "minecraft:mob_spawner" }); @@ -1313,14 +1310,14 @@ cBlockEntity * cWSSAnvil::LoadMobSpawnerFromNBT(const cParsedNBT & a_NBT, int a_ MobSpawner->SetSpawnDelay(a_NBT.GetShort(Delay)); } - return MobSpawner.release(); + return std::move(MobSpawner); } -cBlockEntity * cWSSAnvil::LoadMobHeadFromNBT(const cParsedNBT & a_NBT, int a_TagIdx, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, Vector3i a_Pos) +OwnedBlockEntity cWSSAnvil::LoadMobHeadFromNBT(const cParsedNBT & a_NBT, int a_TagIdx, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, Vector3i a_Pos) { // Check if the data has a proper type: static const AStringVector expectedTypes({ "Skull", "minecraft:skull" }); @@ -1387,14 +1384,14 @@ cBlockEntity * cWSSAnvil::LoadMobHeadFromNBT(const cParsedNBT & a_NBT, int a_Tag MobHead->SetOwner(OwnerUUID, OwnerName, OwnerTexture, OwnerTextureSignature); } - return MobHead.release(); + return std::move(MobHead); } -cBlockEntity * cWSSAnvil::LoadNoteBlockFromNBT(const cParsedNBT & a_NBT, int a_TagIdx, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, Vector3i a_Pos) +OwnedBlockEntity cWSSAnvil::LoadNoteBlockFromNBT(const cParsedNBT & a_NBT, int a_TagIdx, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, Vector3i a_Pos) { // Check if the data has a proper type: static const AStringVector expectedTypes({ "Music", "minecraft:noteblock" }); @@ -1409,14 +1406,14 @@ cBlockEntity * cWSSAnvil::LoadNoteBlockFromNBT(const cParsedNBT & a_NBT, int a_T { NoteBlock->SetPitch(static_cast<char>(a_NBT.GetByte(note))); } - return NoteBlock.release(); + return std::move(NoteBlock); } -cBlockEntity * cWSSAnvil::LoadSignFromNBT(const cParsedNBT & a_NBT, int a_TagIdx, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, Vector3i a_Pos) +OwnedBlockEntity cWSSAnvil::LoadSignFromNBT(const cParsedNBT & a_NBT, int a_TagIdx, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, Vector3i a_Pos) { // Check if the data has a proper type: static const AStringVector expectedTypes({ "Sign", "minecraft:sign" }); @@ -1451,7 +1448,7 @@ cBlockEntity * cWSSAnvil::LoadSignFromNBT(const cParsedNBT & a_NBT, int a_TagIdx Sign->SetLine(3, DecodeSignLine(a_NBT.GetString(currentLine))); } - return Sign.release(); + return std::move(Sign); } diff --git a/src/WorldStorage/WSSAnvil.h b/src/WorldStorage/WSSAnvil.h index 96945363e..949ff61ff 100755 --- a/src/WorldStorage/WSSAnvil.h +++ b/src/WorldStorage/WSSAnvil.h @@ -8,6 +8,7 @@ #pragma once +#include "../BlockEntities/BlockEntity.h" #include "WorldStorage.h" #include "FastNBT.h" @@ -135,7 +136,7 @@ protected: /** Loads the data for a block entity from the specified NBT tag. Returns the loaded block entity, or nullptr upon failure. */ - cBlockEntity * LoadBlockEntityFromNBT(const cParsedNBT & a_NBT, int a_Tag, Vector3i a_Pos, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta); + OwnedBlockEntity LoadBlockEntityFromNBT(const cParsedNBT & a_NBT, int a_Tag, Vector3i a_Pos, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta); /** Loads a cItem contents from the specified NBT tag; returns true if successful. Doesn't load the Slot tag */ bool LoadItemFromNBT(cItem & a_Item, const cParsedNBT & a_NBT, int a_TagIdx); @@ -155,21 +156,21 @@ protected: The coordinates are used only for the log message. */ bool CheckBlockEntityType(const cParsedNBT & a_NBT, int a_TagIdx, const AStringVector & a_ExpectedTypes, Vector3i a_Pos); - cBlockEntity * LoadBeaconFromNBT (const cParsedNBT & a_NBT, int a_TagIdx, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, Vector3i a_Pos); - cBlockEntity * LoadBedFromNBT (const cParsedNBT & a_NBT, int a_TagIdx, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, Vector3i a_Pos); - cBlockEntity * LoadBrewingstandFromNBT(const cParsedNBT & a_NBT, int a_TagIdx, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, Vector3i a_Pos); - cBlockEntity * LoadChestFromNBT (const cParsedNBT & a_NBT, int a_TagIdx, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, Vector3i a_Pos); - cBlockEntity * LoadCommandBlockFromNBT(const cParsedNBT & a_NBT, int a_TagIdx, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, Vector3i a_Pos); - cBlockEntity * LoadDispenserFromNBT (const cParsedNBT & a_NBT, int a_TagIdx, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, Vector3i a_Pos); - cBlockEntity * LoadDropperFromNBT (const cParsedNBT & a_NBT, int a_TagIdx, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, Vector3i a_Pos); - cBlockEntity * LoadFlowerPotFromNBT (const cParsedNBT & a_NBT, int a_TagIdx, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, Vector3i a_Pos); - cBlockEntity * LoadFurnaceFromNBT (const cParsedNBT & a_NBT, int a_TagIdx, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, Vector3i a_Pos); - cBlockEntity * LoadHopperFromNBT (const cParsedNBT & a_NBT, int a_TagIdx, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, Vector3i a_Pos); - cBlockEntity * LoadJukeboxFromNBT (const cParsedNBT & a_NBT, int a_TagIdx, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, Vector3i a_Pos); - cBlockEntity * LoadMobHeadFromNBT (const cParsedNBT & a_NBT, int a_TagIdx, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, Vector3i a_Pos); - cBlockEntity * LoadMobSpawnerFromNBT (const cParsedNBT & a_NBT, int a_TagIdx, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, Vector3i a_Pos); - cBlockEntity * LoadNoteBlockFromNBT (const cParsedNBT & a_NBT, int a_TagIdx, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, Vector3i a_Pos); - cBlockEntity * LoadSignFromNBT (const cParsedNBT & a_NBT, int a_TagIdx, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, Vector3i a_Pos); + OwnedBlockEntity LoadBeaconFromNBT (const cParsedNBT & a_NBT, int a_TagIdx, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, Vector3i a_Pos); + OwnedBlockEntity LoadBedFromNBT (const cParsedNBT & a_NBT, int a_TagIdx, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, Vector3i a_Pos); + OwnedBlockEntity LoadBrewingstandFromNBT(const cParsedNBT & a_NBT, int a_TagIdx, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, Vector3i a_Pos); + OwnedBlockEntity LoadChestFromNBT (const cParsedNBT & a_NBT, int a_TagIdx, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, Vector3i a_Pos); + OwnedBlockEntity LoadCommandBlockFromNBT(const cParsedNBT & a_NBT, int a_TagIdx, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, Vector3i a_Pos); + OwnedBlockEntity LoadDispenserFromNBT (const cParsedNBT & a_NBT, int a_TagIdx, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, Vector3i a_Pos); + OwnedBlockEntity LoadDropperFromNBT (const cParsedNBT & a_NBT, int a_TagIdx, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, Vector3i a_Pos); + OwnedBlockEntity LoadFlowerPotFromNBT (const cParsedNBT & a_NBT, int a_TagIdx, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, Vector3i a_Pos); + OwnedBlockEntity LoadFurnaceFromNBT (const cParsedNBT & a_NBT, int a_TagIdx, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, Vector3i a_Pos); + OwnedBlockEntity LoadHopperFromNBT (const cParsedNBT & a_NBT, int a_TagIdx, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, Vector3i a_Pos); + OwnedBlockEntity LoadJukeboxFromNBT (const cParsedNBT & a_NBT, int a_TagIdx, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, Vector3i a_Pos); + OwnedBlockEntity LoadMobHeadFromNBT (const cParsedNBT & a_NBT, int a_TagIdx, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, Vector3i a_Pos); + OwnedBlockEntity LoadMobSpawnerFromNBT (const cParsedNBT & a_NBT, int a_TagIdx, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, Vector3i a_Pos); + OwnedBlockEntity LoadNoteBlockFromNBT (const cParsedNBT & a_NBT, int a_TagIdx, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, Vector3i a_Pos); + OwnedBlockEntity LoadSignFromNBT (const cParsedNBT & a_NBT, int a_TagIdx, BLOCKTYPE a_BlockType, NIBBLETYPE a_BlockMeta, Vector3i a_Pos); void LoadEntityFromNBT(cEntityList & a_Entities, const cParsedNBT & a_NBT, int a_EntityTagIdx, const char * a_IDTag, size_t a_IDTagLength); |