summaryrefslogtreecommitdiffstats
path: root/src/Entities/Entity.cpp
diff options
context:
space:
mode:
authorTiger Wang <ziwei.tiger@hotmail.co.uk>2016-12-18 21:41:26 +0100
committerTiger Wang <ziwei.tiger@hotmail.co.uk>2016-12-18 21:41:26 +0100
commit31b86f3886e4b8991861b007df091faafc8be782 (patch)
treedd491351e4763eb3cd2f583843c494b2a9d8ddd8 /src/Entities/Entity.cpp
parenttest (diff)
downloadcuberite-31b86f3886e4b8991861b007df091faafc8be782.tar
cuberite-31b86f3886e4b8991861b007df091faafc8be782.tar.gz
cuberite-31b86f3886e4b8991861b007df091faafc8be782.tar.bz2
cuberite-31b86f3886e4b8991861b007df091faafc8be782.tar.lz
cuberite-31b86f3886e4b8991861b007df091faafc8be782.tar.xz
cuberite-31b86f3886e4b8991861b007df091faafc8be782.tar.zst
cuberite-31b86f3886e4b8991861b007df091faafc8be782.zip
Diffstat (limited to '')
-rw-r--r--src/Entities/Entity.cpp51
1 files changed, 31 insertions, 20 deletions
diff --git a/src/Entities/Entity.cpp b/src/Entities/Entity.cpp
index 8d74ee99a..489b55cf8 100644
--- a/src/Entities/Entity.cpp
+++ b/src/Entities/Entity.cpp
@@ -132,7 +132,9 @@ const char * cEntity::GetParentClass(void) const
bool cEntity::Initialize(cWorld & a_World)
{
- if (cPluginManager::Get()->CallHookSpawningEntity(a_World, *this))
+ ASSERT(a_EntityWorld.IsInTickThread());
+
+ if (cPluginManager::Get()->CallHookSpawningEntity(a_EntityWorld, *this) && !IsPlayer())
{
return false;
}
@@ -144,15 +146,22 @@ bool cEntity::Initialize(cWorld & a_World)
);
*/
- ASSERT(m_World == nullptr);
ASSERT(GetParentChunk() == nullptr);
- a_World.AddEntity(this);
- ASSERT(m_World != nullptr);
+ cpp14::move_on_copy_wrapper<decltype(a_Entity)> Entity(std::move(a_Entity));
+
+ // So that entities do not appear in the middle of ticks
+ a_EntityWorld.QueueTask(
+ [Entity, &a_EntityWorld](cWorld & a_World)
+ {
+ auto & EntityPtr = *Entity.value;
- cPluginManager::Get()->CallHookSpawnedEntity(a_World, *this);
+ EntityPtr.SetWorld(&a_EntityWorld);
+ a_EntityWorld.AddEntity(std::move(Entity.value));
- // Spawn the entity on the clients:
- a_World.BroadcastSpawnEntity(*this);
+ a_EntityWorld.BroadcastSpawnEntity(EntityPtr);
+ cPluginManager::Get()->CallHookSpawnedEntity(a_EntityWorld, EntityPtr);
+ }
+ );
return true;
}
@@ -209,8 +218,7 @@ cChunk * cEntity::GetParentChunk() const
void cEntity::Destroy(bool a_ShouldBroadcast)
{
- ASSERT(IsTicking());
- ASSERT(GetParentChunk() != nullptr);
+ ASSERT(GetWorld()->IsInTickThread());
SetIsTicking(false);
if (a_ShouldBroadcast)
@@ -218,17 +226,20 @@ void cEntity::Destroy(bool a_ShouldBroadcast)
m_World->BroadcastDestroyEntity(*this);
}
- cChunk * ParentChunk = GetParentChunk();
- m_World->QueueTask([this, ParentChunk](cWorld & a_World)
- {
- LOGD("Destroying entity #%i (%s) from chunk (%d, %d)",
- this->GetUniqueID(), this->GetClass(),
- ParentChunk->GetPosX(), ParentChunk->GetPosZ()
- );
- ParentChunk->RemoveEntity(this);
- delete this;
- });
- Destroyed();
+ // So that entities do not disappear unexpectedly during ticks
+ m_World->QueueTask(
+ [this](cWorld & a_World)
+ {
+ auto ParentChunk = GetParentChunk();
+ LOGD("Destroying entity #%i (%s) from chunk (%d, %d)",
+ GetUniqueID(), GetClass(),
+ ParentChunk->GetPosX(), ParentChunk->GetPosZ()
+ );
+
+ Destroyed(); // TODO: rename to OnPreDestroy()
+ ParentChunk->RemoveEntity(*this);
+ }
+ );
}