Manage block entity lifetime with unique_ptr (#4080)
This commit is contained in:
@@ -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));
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user