mirror of
https://github.com/luanti-org/luanti.git
synced 2025-07-02 16:38:41 +00:00
Add minetest.unregister_item and minetest.register_alias_force
This commit is contained in:
parent
7eacdc7bb8
commit
aa33166386
8 changed files with 121 additions and 3 deletions
|
@ -466,11 +466,17 @@ public:
|
|||
infostream<<"ItemDefManager: erased alias "<<def.name
|
||||
<<" because item was defined"<<std::endl;
|
||||
}
|
||||
virtual void unregisterItem(const std::string &name)
|
||||
{
|
||||
verbosestream<<"ItemDefManager: unregistering \""<<name<<"\""<<std::endl;
|
||||
|
||||
delete m_item_definitions[name];
|
||||
m_item_definitions.erase(name);
|
||||
}
|
||||
virtual void registerAlias(const std::string &name,
|
||||
const std::string &convert_to)
|
||||
{
|
||||
if(m_item_definitions.find(name) == m_item_definitions.end())
|
||||
{
|
||||
if (m_item_definitions.find(name) == m_item_definitions.end()) {
|
||||
verbosestream<<"ItemDefManager: setting alias "<<name
|
||||
<<" -> "<<convert_to<<std::endl;
|
||||
m_aliases[name] = convert_to;
|
||||
|
|
|
@ -144,6 +144,7 @@ public:
|
|||
virtual void clear()=0;
|
||||
// Register item definition
|
||||
virtual void registerItem(const ItemDefinition &def)=0;
|
||||
virtual void unregisterItem(const std::string &name)=0;
|
||||
// Set an alias so that items named <name> will load as <convert_to>.
|
||||
// Alias is not set if <name> has already been defined.
|
||||
// Alias will be removed if <name> is defined at a later point of time.
|
||||
|
|
|
@ -778,6 +778,7 @@ public:
|
|||
content_t allocateId();
|
||||
virtual content_t set(const std::string &name, const ContentFeatures &def);
|
||||
virtual content_t allocateDummy(const std::string &name);
|
||||
virtual void removeNode(const std::string &name);
|
||||
virtual void updateAliases(IItemDefManager *idef);
|
||||
virtual void applyTextureOverrides(const std::string &override_filepath);
|
||||
virtual void updateTextures(IGameDef *gamedef,
|
||||
|
@ -1072,6 +1073,40 @@ content_t CNodeDefManager::allocateDummy(const std::string &name)
|
|||
}
|
||||
|
||||
|
||||
void CNodeDefManager::removeNode(const std::string &name)
|
||||
{
|
||||
// Pre-condition
|
||||
assert(name != "");
|
||||
|
||||
// Erase name from name ID mapping
|
||||
content_t id = CONTENT_IGNORE;
|
||||
if (m_name_id_mapping.getId(name, id)) {
|
||||
m_name_id_mapping.eraseName(name);
|
||||
m_name_id_mapping_with_aliases.erase(name);
|
||||
}
|
||||
|
||||
// Erase node content from all groups it belongs to
|
||||
for (std::map<std::string, GroupItems>::iterator iter_groups =
|
||||
m_group_to_items.begin();
|
||||
iter_groups != m_group_to_items.end();) {
|
||||
GroupItems &items = iter_groups->second;
|
||||
for (GroupItems::iterator iter_groupitems = items.begin();
|
||||
iter_groupitems != items.end();) {
|
||||
if (iter_groupitems->first == id)
|
||||
items.erase(iter_groupitems++);
|
||||
else
|
||||
iter_groupitems++;
|
||||
}
|
||||
|
||||
// Check if group is empty
|
||||
if (items.size() == 0)
|
||||
m_group_to_items.erase(iter_groups++);
|
||||
else
|
||||
iter_groups++;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void CNodeDefManager::updateAliases(IItemDefManager *idef)
|
||||
{
|
||||
std::set<std::string> all = idef->getAll();
|
||||
|
|
|
@ -384,6 +384,8 @@ public:
|
|||
const ContentFeatures &def)=0;
|
||||
// If returns CONTENT_IGNORE, could not allocate id
|
||||
virtual content_t allocateDummy(const std::string &name)=0;
|
||||
// Remove a node
|
||||
virtual void removeNode(const std::string &name)=0;
|
||||
|
||||
/*
|
||||
Update item alias mapping.
|
||||
|
|
|
@ -525,6 +525,27 @@ int ModApiItemMod::l_register_item_raw(lua_State *L)
|
|||
return 0; /* number of results */
|
||||
}
|
||||
|
||||
// unregister_item(name)
|
||||
int ModApiItemMod::l_unregister_item_raw(lua_State *L)
|
||||
{
|
||||
NO_MAP_LOCK_REQUIRED;
|
||||
std::string name = luaL_checkstring(L, 1);
|
||||
|
||||
IWritableItemDefManager *idef =
|
||||
getServer(L)->getWritableItemDefManager();
|
||||
|
||||
// Unregister the node
|
||||
if (idef->get(name).type == ITEM_NODE) {
|
||||
IWritableNodeDefManager *ndef =
|
||||
getServer(L)->getWritableNodeDefManager();
|
||||
ndef->removeNode(name);
|
||||
}
|
||||
|
||||
idef->unregisterItem(name);
|
||||
|
||||
return 0; /* number of results */
|
||||
}
|
||||
|
||||
// register_alias_raw(name, convert_to_name)
|
||||
int ModApiItemMod::l_register_alias_raw(lua_State *L)
|
||||
{
|
||||
|
@ -570,6 +591,7 @@ int ModApiItemMod::l_get_name_from_content_id(lua_State *L)
|
|||
void ModApiItemMod::Initialize(lua_State *L, int top)
|
||||
{
|
||||
API_FCT(register_item_raw);
|
||||
API_FCT(unregister_item_raw);
|
||||
API_FCT(register_alias_raw);
|
||||
API_FCT(get_content_id);
|
||||
API_FCT(get_name_from_content_id);
|
||||
|
|
|
@ -135,6 +135,7 @@ public:
|
|||
class ModApiItemMod : public ModApiBase {
|
||||
private:
|
||||
static int l_register_item_raw(lua_State *L);
|
||||
static int l_unregister_item_raw(lua_State *L);
|
||||
static int l_register_alias_raw(lua_State *L);
|
||||
static int l_get_content_id(lua_State *L);
|
||||
static int l_get_name_from_content_id(lua_State *L);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue