diff options
author | PilzAdam <pilzadam@minetest.net> | 2013-09-28 22:47:09 +0200 |
---|---|---|
committer | PilzAdam <pilzadam@minetest.net> | 2013-09-28 23:46:48 +0200 |
commit | a9c0961e0c9c3b854aca34b6d582198be1cd2277 (patch) | |
tree | 23985c8a62f0cca08257e112f358de09e382ba0c /src | |
parent | 731392866f3f2595a08ca279b1d39584f6783ec0 (diff) | |
download | minetest-a9c0961e0c9c3b854aca34b6d582198be1cd2277.tar.gz minetest-a9c0961e0c9c3b854aca34b6d582198be1cd2277.tar.bz2 minetest-a9c0961e0c9c3b854aca34b6d582198be1cd2277.zip |
Add set_name(), set_count(), set_wear() and set_metadata() to Lua ItemStack
Diffstat (limited to 'src')
-rw-r--r-- | src/script/lua_api/l_item.cpp | 63 | ||||
-rw-r--r-- | src/script/lua_api/l_item.h | 12 |
2 files changed, 75 insertions, 0 deletions
diff --git a/src/script/lua_api/l_item.cpp b/src/script/lua_api/l_item.cpp index a43b2858f..512cd7398 100644 --- a/src/script/lua_api/l_item.cpp +++ b/src/script/lua_api/l_item.cpp @@ -57,6 +57,20 @@ int LuaItemStack::l_get_name(lua_State *L) return 1; } +// set_name(self, name) +int LuaItemStack::l_set_name(lua_State *L) +{ + NO_MAP_LOCK_REQUIRED; + LuaItemStack *o = checkobject(L, 1); + ItemStack &item = o->m_stack; + item.name = luaL_checkstring(L, 2); + + if (item.name == "" || item.empty()) + item.clear(); + + return 1; +} + // get_count(self) -> number int LuaItemStack::l_get_count(lua_State *L) { @@ -67,6 +81,20 @@ int LuaItemStack::l_get_count(lua_State *L) return 1; } +// set_count(self, number) +int LuaItemStack::l_set_count(lua_State *L) +{ + NO_MAP_LOCK_REQUIRED; + LuaItemStack *o = checkobject(L, 1); + ItemStack &item = o->m_stack; + item.count = luaL_checkinteger(L, 2); + + if (item.name == "" || item.empty()) + item.clear(); + + return 1; +} + // get_wear(self) -> number int LuaItemStack::l_get_wear(lua_State *L) { @@ -77,6 +105,20 @@ int LuaItemStack::l_get_wear(lua_State *L) return 1; } +// set_wear(self, number) +int LuaItemStack::l_set_wear(lua_State *L) +{ + NO_MAP_LOCK_REQUIRED; + LuaItemStack *o = checkobject(L, 1); + ItemStack &item = o->m_stack; + item.wear = luaL_checkinteger(L, 2); + + if (item.wear > 65535) + item.clear(); + + return 1; +} + // get_metadata(self) -> string int LuaItemStack::l_get_metadata(lua_State *L) { @@ -87,6 +129,23 @@ int LuaItemStack::l_get_metadata(lua_State *L) return 1; } +// set_metadata(self, string) +int LuaItemStack::l_set_metadata(lua_State *L) +{ + NO_MAP_LOCK_REQUIRED; + LuaItemStack *o = checkobject(L, 1); + ItemStack &item = o->m_stack; + + size_t len = 0; + const char *ptr = luaL_checklstring(L, 2, &len); + if (ptr) + item.metadata.assign(ptr, len); + else + item.metadata = ""; + + return 1; +} + // clear(self) -> true int LuaItemStack::l_clear(lua_State *L) { @@ -363,9 +422,13 @@ const char LuaItemStack::className[] = "ItemStack"; const luaL_reg LuaItemStack::methods[] = { luamethod(LuaItemStack, is_empty), luamethod(LuaItemStack, get_name), + luamethod(LuaItemStack, set_name), luamethod(LuaItemStack, get_count), + luamethod(LuaItemStack, set_count), luamethod(LuaItemStack, get_wear), + luamethod(LuaItemStack, set_wear), luamethod(LuaItemStack, get_metadata), + luamethod(LuaItemStack, set_metadata), luamethod(LuaItemStack, clear), luamethod(LuaItemStack, replace), luamethod(LuaItemStack, to_string), diff --git a/src/script/lua_api/l_item.h b/src/script/lua_api/l_item.h index 7c2e1b098..7f5a1130d 100644 --- a/src/script/lua_api/l_item.h +++ b/src/script/lua_api/l_item.h @@ -41,15 +41,27 @@ private: // get_name(self) -> string static int l_get_name(lua_State *L); + // set_name(self, name) + static int l_set_name(lua_State *L); + // get_count(self) -> number static int l_get_count(lua_State *L); + // set_count(self, number) + static int l_set_count(lua_State *L); + // get_wear(self) -> number static int l_get_wear(lua_State *L); + // set_wear(self, number) + static int l_set_wear(lua_State *L); + // get_metadata(self) -> string static int l_get_metadata(lua_State *L); + // set_metadata(self, string) + static int l_set_metadata(lua_State *L); + // clear(self) -> true static int l_clear(lua_State *L); |