add function item.icon(...,)

This commit is contained in:
MihailRis 2024-06-30 23:38:57 +03:00
parent 71c754b039
commit 26e2068164
3 changed files with 41 additions and 11 deletions

View File

@ -464,6 +464,12 @@ item.defs_count() -> int
Returns count of available item IDs. 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 ## *hud* library

View File

@ -458,6 +458,12 @@ item.defs_count() -> int
Возвращает общее число доступных предметов (включая сгенерированные) Возвращает общее число доступных предметов (включая сгенерированные)
```python
item.icon(itemid: int) -> str
```
Возвращает имя иконки предмета для использования в свойстве 'src' элемента image
## Библиотека hud ## Библиотека hud
```python ```python

View File

@ -5,15 +5,21 @@
using namespace scripting; 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 indices = content->getIndices();
auto id = lua::tointeger(L, 1); auto id = lua::tointeger(L, idx);
if (static_cast<size_t>(id) >= indices->countItemDefs()) { if (static_cast<size_t>(id) >= indices->countItemDefs()) {
return 0; return nullptr;
} }
auto def = indices->getItemDef(id); 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 lua::pushstring(L, def->name);
} }
return 0;
}
static int l_item_index(lua::State* L) { static int l_item_index(lua::State* L) {
auto name = lua::require_string(L, 1); auto name = lua::require_string(L, 1);
@ -21,23 +27,35 @@ static int l_item_index(lua::State* L) {
} }
static int l_item_stack_size(lua::State* L) { static int l_item_stack_size(lua::State* L) {
auto indices = content->getIndices(); if (auto def = get_item_def(L, 1)) {
auto id = lua::tointeger(L, 1);
if (static_cast<size_t>(id) >= indices->countItemDefs()) {
return 0;
}
auto def = indices->getItemDef(id);
return lua::pushinteger(L, def->stackSize); return lua::pushinteger(L, def->stackSize);
} }
return 0;
}
static int l_item_defs_count(lua::State* L) { static int l_item_defs_count(lua::State* L) {
return lua::pushinteger(L, indices->countItemDefs()); 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 [] = { const luaL_Reg itemlib [] = {
{"index", lua::wrap<l_item_index>}, {"index", lua::wrap<l_item_index>},
{"name", lua::wrap<l_item_name>}, {"name", lua::wrap<l_item_name>},
{"stack_size", lua::wrap<l_item_stack_size>}, {"stack_size", lua::wrap<l_item_stack_size>},
{"defs_count", lua::wrap<l_item_defs_count>}, {"defs_count", lua::wrap<l_item_defs_count>},
{"icon", lua::wrap<l_item_get_icon>},
{NULL, NULL} {NULL, NULL}
}; };