diff --git a/doc/en/scripting.md b/doc/en/scripting.md index 2f815077..1a1c5d38 100644 --- a/doc/en/scripting.md +++ b/doc/en/scripting.md @@ -464,6 +464,12 @@ item.defs_count() -> int Returns count of available item IDs. +```python +item.icon(itemid: int) -> str +``` + +Returns item icon name to use in 'src' property of an image element + ## *hud* library diff --git a/doc/ru/scripting.md b/doc/ru/scripting.md index 2335431e..cd85c39d 100644 --- a/doc/ru/scripting.md +++ b/doc/ru/scripting.md @@ -458,6 +458,12 @@ item.defs_count() -> int Возвращает общее число доступных предметов (включая сгенерированные) +```python +item.icon(itemid: int) -> str +``` + +Возвращает имя иконки предмета для использования в свойстве 'src' элемента image + ## Библиотека hud ```python diff --git a/src/logic/scripting/lua/libitem.cpp b/src/logic/scripting/lua/libitem.cpp index 25466217..c159b49b 100644 --- a/src/logic/scripting/lua/libitem.cpp +++ b/src/logic/scripting/lua/libitem.cpp @@ -5,14 +5,20 @@ using namespace scripting; -static int l_item_name(lua::State* L) { +static ItemDef* get_item_def(lua::State* L, int idx) { auto indices = content->getIndices(); - auto id = lua::tointeger(L, 1); + auto id = lua::tointeger(L, idx); if (static_cast(id) >= indices->countItemDefs()) { - return 0; + return nullptr; } - auto def = indices->getItemDef(id); - return lua::pushstring(L, def->name); + return indices->getItemDef(id); +} + +static int l_item_name(lua::State* L) { + if (auto def = get_item_def(L, 1)) { + return lua::pushstring(L, def->name); + } + return 0; } static int l_item_index(lua::State* L) { @@ -21,23 +27,35 @@ static int l_item_index(lua::State* L) { } static int l_item_stack_size(lua::State* L) { - auto indices = content->getIndices(); - auto id = lua::tointeger(L, 1); - if (static_cast(id) >= indices->countItemDefs()) { - return 0; + if (auto def = get_item_def(L, 1)) { + return lua::pushinteger(L, def->stackSize); } - auto def = indices->getItemDef(id); - return lua::pushinteger(L, def->stackSize); + return 0; } static int l_item_defs_count(lua::State* L) { return lua::pushinteger(L, indices->countItemDefs()); } +static int l_item_get_icon(lua::State* L) { + if (auto def = get_item_def(L, 1)) { + switch (def->iconType) { + case item_icon_type::none: + return 0; + case item_icon_type::sprite: + return lua::pushstring(L, def->icon); + case item_icon_type::block: + return lua::pushstring(L, "block-previews:"+def->icon); + } + } + return 0; +} + const luaL_Reg itemlib [] = { {"index", lua::wrap}, {"name", lua::wrap}, {"stack_size", lua::wrap}, {"defs_count", lua::wrap}, + {"icon", lua::wrap}, {NULL, NULL} };