Craftitem aliases

This commit is contained in:
Perttu Ahola 2011-12-04 00:39:20 +02:00
parent 8730dfb1f9
commit 829c632511
5 changed files with 80 additions and 996 deletions

@ -94,6 +94,7 @@
-- minetest.register_abm(abm definition) -- minetest.register_abm(abm definition)
-- minetest.alias_node(name, convert_to) -- minetest.alias_node(name, convert_to)
-- minetest.alias_tool(name, convert_to) -- minetest.alias_tool(name, convert_to)
-- minetest.alias_craftitem(name, convert_to)
-- minetest.register_globalstep(func(dtime)) -- minetest.register_globalstep(func(dtime))
-- minetest.register_on_placenode(func(pos, newnode, placer)) -- minetest.register_on_placenode(func(pos, newnode, placer))
-- minetest.register_on_dignode(func(pos, oldnode, digger)) -- minetest.register_on_dignode(func(pos, oldnode, digger))

File diff suppressed because it is too large Load Diff

@ -23,6 +23,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "log.h" #include "log.h"
#include <sstream> #include <sstream>
#include "utility.h" #include "utility.h"
#include <map>
CraftItemDefinition::CraftItemDefinition(): CraftItemDefinition::CraftItemDefinition():
imagename(""), imagename(""),
@ -102,14 +103,14 @@ public:
virtual bool registerCraftItem(std::string itemname, const CraftItemDefinition &def) virtual bool registerCraftItem(std::string itemname, const CraftItemDefinition &def)
{ {
infostream<<"registerCraftItem: registering CraftItem \""<<itemname<<"\""<<std::endl; infostream<<"registerCraftItem: registering CraftItem \""<<itemname<<"\""<<std::endl;
/*core::map<std::string, CraftItemDefinition*>::Node *n;
n = m_item_definitions.find(itemname);
if(n != NULL){
errorstream<<"registerCraftItem: registering item \""<<toolname
<<"\" failed: name is already registered"<<std::endl;
return false;
}*/
m_item_definitions[itemname] = new CraftItemDefinition(def); m_item_definitions[itemname] = new CraftItemDefinition(def);
// Remove conflicting alias if it exists
bool alias_removed = (m_aliases.erase(itemname) != 0);
if(alias_removed)
infostream<<"cidef: erased alias "<<itemname
<<" because item was defined"<<std::endl;
return true; return true;
} }
virtual void clear() virtual void clear()
@ -120,6 +121,19 @@ public:
delete i.getNode()->getValue(); delete i.getNode()->getValue();
} }
m_item_definitions.clear(); m_item_definitions.clear();
m_aliases.clear();
}
virtual void setAlias(const std::string &name,
const std::string &convert_to)
{
if(getCraftItemDefinition(name) != NULL){
infostream<<"nidef: not setting alias "<<name<<" -> "<<convert_to
<<": "<<name<<" is already defined"<<std::endl;
return;
}
infostream<<"nidef: setting alias "<<name<<" -> "<<convert_to
<<std::endl;
m_aliases[name] = convert_to;
} }
virtual void serialize(std::ostream &os) virtual void serialize(std::ostream &os)
{ {
@ -138,6 +152,14 @@ public:
def->serialize(tmp_os); def->serialize(tmp_os);
os<<serializeString(tmp_os.str()); os<<serializeString(tmp_os.str());
} }
writeU16(os, m_aliases.size());
for(std::map<std::string, std::string>::const_iterator
i = m_aliases.begin(); i != m_aliases.end(); i++)
{
os<<serializeString(i->first);
os<<serializeString(i->second);
}
} }
virtual void deSerialize(std::istream &is) virtual void deSerialize(std::istream &is)
{ {
@ -158,10 +180,21 @@ public:
// Register // Register
registerCraftItem(name, def); registerCraftItem(name, def);
} }
u16 num_aliases = readU16(is);
if(!is.eof()){
for(u16 i=0; i<num_aliases; i++){
std::string name = deSerializeString(is);
std::string convert_to = deSerializeString(is);
m_aliases[name] = convert_to;
}
}
} }
private: private:
// Key is name // Key is name
core::map<std::string, CraftItemDefinition*> m_item_definitions; core::map<std::string, CraftItemDefinition*> m_item_definitions;
// Aliases
std::map<std::string, std::string> m_aliases;
}; };
IWritableCraftItemDefManager* createCraftItemDefManager() IWritableCraftItemDefManager* createCraftItemDefManager()

@ -63,6 +63,11 @@ public:
virtual bool registerCraftItem(std::string itemname, const CraftItemDefinition &def)=0; virtual bool registerCraftItem(std::string itemname, const CraftItemDefinition &def)=0;
virtual void clear()=0; virtual void clear()=0;
// Set an alias so that entries 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.
virtual void setAlias(const std::string &name,
const std::string &convert_to)=0;
virtual void serialize(std::ostream &os)=0; virtual void serialize(std::ostream &os)=0;
virtual void deSerialize(std::istream &is)=0; virtual void deSerialize(std::istream &is)=0;

@ -1158,6 +1158,24 @@ static int l_alias_tool(lua_State *L)
return 0; /* number of results */ return 0; /* number of results */
} }
// alias_craftitem(name, convert_to_name)
static int l_alias_craftitem(lua_State *L)
{
std::string name = luaL_checkstring(L, 1);
std::string convert_to = luaL_checkstring(L, 2);
// Get server from registry
lua_getfield(L, LUA_REGISTRYINDEX, "minetest_server");
Server *server = (Server*)lua_touserdata(L, -1);
// And get the writable CraftItem definition manager from the server
IWritableCraftItemDefManager *craftitemdef =
server->getWritableCraftItemDefManager();
craftitemdef->setAlias(name, convert_to);
return 0; /* number of results */
}
// register_craft({output=item, recipe={{item00,item10},{item01,item11}}) // register_craft({output=item, recipe={{item00,item10},{item01,item11}})
static int l_register_craft(lua_State *L) static int l_register_craft(lua_State *L)
{ {
@ -1312,6 +1330,7 @@ static const struct luaL_Reg minetest_f [] = {
{"register_abm", l_register_abm}, {"register_abm", l_register_abm},
{"alias_node", l_alias_node}, {"alias_node", l_alias_node},
{"alias_tool", l_alias_tool}, {"alias_tool", l_alias_tool},
{"alias_craftitem", l_alias_craftitem},
{"setting_get", l_setting_get}, {"setting_get", l_setting_get},
{"setting_getbool", l_setting_getbool}, {"setting_getbool", l_setting_getbool},
{"chat_send_all", l_chat_send_all}, {"chat_send_all", l_chat_send_all},