diff --git a/src/mapblock.cpp b/src/mapblock.cpp index 36210cad1..e2204f083 100644 --- a/src/mapblock.cpp +++ b/src/mapblock.cpp @@ -94,7 +94,7 @@ bool MapBlock::onObjectsActivation() const auto count = m_static_objects.getStoredSize(); verbosestream << "MapBlock::onObjectsActivation(): " - << "activating " << count << "objects in block " << getPos() + << "activating " << count << " objects in block " << getPos() << std::endl; if (count > g_settings->getU16("max_objects_per_block")) { diff --git a/src/server/luaentity_sao.cpp b/src/server/luaentity_sao.cpp index 9917eb53a..963fd63dc 100644 --- a/src/server/luaentity_sao.cpp +++ b/src/server/luaentity_sao.cpp @@ -71,7 +71,7 @@ LuaEntitySAO::LuaEntitySAO(ServerEnvironment *env, v3f pos, const std::string &d break; } // create object - infostream << "LuaEntitySAO::create(name=\"" << name << "\" state is"; + infostream << "LuaEntitySAO(name=\"" << name << "\" state is "; if (state.empty()) infostream << "empty"; else @@ -289,6 +289,8 @@ std::string LuaEntitySAO::getClientInitializationData(u16 protocol_version) void LuaEntitySAO::getStaticData(std::string *result) const { + assert(isStaticAllowed()); + std::ostringstream os(std::ios::binary); // version must be 1 to keep backwards-compatibility. See version2 writeU8(os, 1); diff --git a/src/serverenvironment.cpp b/src/serverenvironment.cpp index 47fff1ef4..dc629f307 100644 --- a/src/serverenvironment.cpp +++ b/src/serverenvironment.cpp @@ -1251,12 +1251,11 @@ void ServerEnvironment::clearObjects(ClearObjectsMode mode) // Delete static object if block is loaded deleteStaticFromBlock(obj, id, MOD_REASON_CLEAR_ALL_OBJECTS, true); + obj->markForRemoval(); // If known by some client, don't delete immediately - if (obj->m_known_by_count > 0) { - obj->markForRemoval(); + if (obj->m_known_by_count > 0) return false; - } processActiveObjectRemove(obj); @@ -1894,6 +1893,12 @@ u16 ServerEnvironment::addActiveObjectRaw(std::unique_ptr ob return 0; } + // Register reference in scripting api (must be done before post-init) + m_script->addObjectReference(object); + // Post-initialize object + // Note that this can change the value of isStaticAllowed() in case of LuaEntitySAO + object->addedToEnvironment(dtime_s); + // Add static data to block if (object->isStaticAllowed()) { // Add static object to active static list of the block @@ -1916,15 +1921,14 @@ u16 ServerEnvironment::addActiveObjectRaw(std::unique_ptr ob << "could not emerge block " << p << " for storing id=" << object->getId() << " statically" << std::endl; // clean in case of error + object->markForRemoval(); + processActiveObjectRemove(object); m_ao_manager.removeObject(object->getId()); return 0; } } - // Register reference in scripting api (must be done before post-init) - m_script->addObjectReference(object); - // Post-initialize object - object->addedToEnvironment(dtime_s); + assert(object->m_static_exists == object->isStaticAllowed()); return object->getId(); } @@ -1937,13 +1941,6 @@ void ServerEnvironment::removeRemovedObjects() ScopeProfiler sp(g_profiler, "ServerEnvironment::removeRemovedObjects()", SPT_AVG); auto clear_cb = [this](ServerActiveObject *obj, u16 id) { - // This shouldn't happen but check it - if (!obj) { - errorstream << "ServerEnvironment::removeRemovedObjects(): " - << "NULL object found. id=" << id << std::endl; - return true; - } - /* We will handle objects marked for removal or deactivation */ @@ -2280,6 +2277,11 @@ bool ServerEnvironment::saveStaticToBlock( void ServerEnvironment::processActiveObjectRemove(ServerActiveObject *obj) { + // markForRemoval or markForDeactivation should have been called before + // Not because it's strictly necessary but because the Lua callback is + // bound to that. + assert(obj->isGone()); + // Tell the object about removal obj->removingFromEnvironment(); // Deregister in scripting api diff --git a/src/staticobject.cpp b/src/staticobject.cpp index d728f5d10..8fbfcef8b 100644 --- a/src/staticobject.cpp +++ b/src/staticobject.cpp @@ -25,6 +25,7 @@ StaticObject::StaticObject(const ServerActiveObject *s_obj, const v3f &pos_): type(s_obj->getType()), pos(pos_) { + assert(s_obj->isStaticAllowed()); s_obj->getStaticData(&data); }