LuaItemStack: Fix and document behavior of set_name, set_count, set_wear, set_metadata

This commit is contained in:
kwolekr 2014-12-28 23:58:06 -05:00
parent acb3519502
commit 3c637b4baf
2 changed files with 35 additions and 16 deletions

@ -2152,13 +2152,16 @@ ItemStack: A stack of items.
methods: methods:
- is_empty(): return true if stack is empty - is_empty(): return true if stack is empty
- get_name(): returns item name (e.g. "default:stone") - get_name(): returns item name (e.g. "default:stone")
- set_name(itemname) - set_name(itemname): returns true/false (success)
^ clears item on failure
- get_count(): returns number of items on the stack - get_count(): returns number of items on the stack
- set_count(count) - set_count(count): returns true/false (success)
^ clears item on failure
- get_wear(): returns tool wear (0-65535), 0 for non-tools - get_wear(): returns tool wear (0-65535), 0 for non-tools
- set_wear(wear) - set_wear(wear): returns true/false (success)
^ clears item on failure
- get_metadata(): returns metadata (a string attached to an item stack) - get_metadata(): returns metadata (a string attached to an item stack)
- set_metadata(metadata) - set_metadata(metadata): returns true
- clear(): removes all items from the stack, making it empty - clear(): removes all items from the stack, making it empty
- replace(item): replace the contents of this stack (item can also - replace(item): replace the contents of this stack (item can also
be an itemstring or table) be an itemstring or table)

@ -63,11 +63,15 @@ int LuaItemStack::l_set_name(lua_State *L)
NO_MAP_LOCK_REQUIRED; NO_MAP_LOCK_REQUIRED;
LuaItemStack *o = checkobject(L, 1); LuaItemStack *o = checkobject(L, 1);
ItemStack &item = o->m_stack; ItemStack &item = o->m_stack;
bool status = true;
item.name = luaL_checkstring(L, 2); item.name = luaL_checkstring(L, 2);
if (item.name == "" || item.empty()) {
if (item.name == "" || item.empty())
item.clear(); item.clear();
status = false;
}
lua_pushboolean(L, status);
return 1; return 1;
} }
@ -87,11 +91,18 @@ int LuaItemStack::l_set_count(lua_State *L)
NO_MAP_LOCK_REQUIRED; NO_MAP_LOCK_REQUIRED;
LuaItemStack *o = checkobject(L, 1); LuaItemStack *o = checkobject(L, 1);
ItemStack &item = o->m_stack; ItemStack &item = o->m_stack;
item.count = luaL_checkinteger(L, 2);
if (item.name == "" || item.empty()) bool status;
lua_Integer count = luaL_checkinteger(L, 2);
if (count <= 65535) {
item.count = count;
status = true;
} else {
item.clear(); item.clear();
status = false;
}
lua_pushboolean(L, status);
return 1; return 1;
} }
@ -111,11 +122,18 @@ int LuaItemStack::l_set_wear(lua_State *L)
NO_MAP_LOCK_REQUIRED; NO_MAP_LOCK_REQUIRED;
LuaItemStack *o = checkobject(L, 1); LuaItemStack *o = checkobject(L, 1);
ItemStack &item = o->m_stack; ItemStack &item = o->m_stack;
item.wear = luaL_checkinteger(L, 2);
if (item.wear > 65535) bool status;
lua_Integer wear = luaL_checkinteger(L, 2);
if (wear <= 65535) {
item.wear = wear;
status = true;
} else {
item.clear(); item.clear();
status = false;
}
lua_pushboolean(L, status);
return 1; return 1;
} }
@ -138,11 +156,9 @@ int LuaItemStack::l_set_metadata(lua_State *L)
size_t len = 0; size_t len = 0;
const char *ptr = luaL_checklstring(L, 2, &len); const char *ptr = luaL_checklstring(L, 2, &len);
if (ptr) item.metadata.assign(ptr, len);
item.metadata.assign(ptr, len);
else
item.metadata = "";
lua_pushboolean(L, true);
return 1; return 1;
} }