From a23e03534d6bea556878b5c014955b989ddb0dc9 Mon Sep 17 00:00:00 2001 From: Jeinzi Date: Fri, 9 Feb 2024 15:41:28 +0100 Subject: [PATCH] Add Lua function to remove a registered decoration, fix #12881 --- src/objdef.cpp | 13 +++++++++++++ src/objdef.h | 1 + src/script/lua_api/l_mapgen.cpp | 15 +++++++++++++++ src/script/lua_api/l_mapgen.h | 3 +++ 4 files changed, 32 insertions(+) diff --git a/src/objdef.cpp b/src/objdef.cpp index 482544d37..f645abc30 100644 --- a/src/objdef.cpp +++ b/src/objdef.cpp @@ -136,6 +136,19 @@ void ObjDefManager::clear() } +void ObjDefManager::clearByName(const std::string &name) +{ + for (auto it = m_objects.begin(); it != m_objects.end(); ++it) { + auto obj = *it; + if (obj && !strcasecmp(name.c_str(), obj->name.c_str())) { + delete obj; + m_objects.erase(it); + break; + } + } +} + + u32 ObjDefManager::validateHandle(ObjDefHandle handle) const { ObjDefType type; diff --git a/src/objdef.h b/src/objdef.h index e40324a88..ef762077e 100644 --- a/src/objdef.h +++ b/src/objdef.h @@ -78,6 +78,7 @@ public: virtual const char *getObjectTitle() const { return "ObjDef"; } virtual void clear(); + virtual void clearByName(const std::string &name); virtual ObjDef *getByName(const std::string &name) const; //// Add new/get/set object definitions by handle diff --git a/src/script/lua_api/l_mapgen.cpp b/src/script/lua_api/l_mapgen.cpp index 86e0c1c32..c12ec2d72 100644 --- a/src/script/lua_api/l_mapgen.cpp +++ b/src/script/lua_api/l_mapgen.cpp @@ -1428,6 +1428,20 @@ int ModApiMapgen::l_clear_registered_decorations(lua_State *L) } +// clear_decoration(name) +int ModApiMapgen::l_clear_decoration(lua_State *L) +{ + NO_MAP_LOCK_REQUIRED; + luaL_checktype(L, 1, LUA_TSTRING); + const char *name = lua_tostring(L, 1); + + DecorationManager *dmgr = + getServer(L)->getEmergeManager()->getWritableDecorationManager(); + dmgr->clearByName(name); + return 0; +} + + // clear_registered_ores() int ModApiMapgen::l_clear_registered_ores(lua_State *L) { @@ -1880,6 +1894,7 @@ void ModApiMapgen::Initialize(lua_State *L, int top) API_FCT(clear_registered_biomes); API_FCT(clear_registered_decorations); + API_FCT(clear_decoration); API_FCT(clear_registered_ores); API_FCT(clear_registered_schematics); diff --git a/src/script/lua_api/l_mapgen.h b/src/script/lua_api/l_mapgen.h index e7984b2dd..9ea60f26b 100644 --- a/src/script/lua_api/l_mapgen.h +++ b/src/script/lua_api/l_mapgen.h @@ -114,6 +114,9 @@ private: // clear_registered_decorations() static int l_clear_registered_decorations(lua_State *L); + // clear_decoration() + static int l_clear_decoration(lua_State *L); + // clear_registered_schematics() static int l_clear_registered_schematics(lua_State *L);