From bbfc0dbf1734b814f1b7e2a3cb4492f619da6b37 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Mon, 17 Feb 2025 04:40:18 +0300 Subject: [PATCH] add inventory.get_uses, inventory.use, item.uses & update base:bazalt_breaker --- res/content/base/items/bazalt_breaker.json | 3 ++- res/content/base/scripts/bazalt_breaker.lua | 3 ++- res/scripts/stdmin.lua | 25 +++++++++++++++++++++ src/items/ItemDef.hpp | 2 +- src/logic/scripting/lua/libs/libitem.cpp | 11 ++++++++- 5 files changed, 40 insertions(+), 4 deletions(-) diff --git a/res/content/base/items/bazalt_breaker.json b/res/content/base/items/bazalt_breaker.json index 59aeabdb..9e70fa55 100644 --- a/res/content/base/items/bazalt_breaker.json +++ b/res/content/base/items/bazalt_breaker.json @@ -1,4 +1,5 @@ { "icon-type": "sprite", - "icon": "items:bazalt_breaker" + "icon": "items:bazalt_breaker", + "uses": 100 } diff --git a/res/content/base/scripts/bazalt_breaker.lua b/res/content/base/scripts/bazalt_breaker.lua index 78b872b2..ec2e54bc 100644 --- a/res/content/base/scripts/bazalt_breaker.lua +++ b/res/content/base/scripts/bazalt_breaker.lua @@ -1,3 +1,4 @@ -function on_block_break_by(x, y, z, p) +function on_block_break_by(x, y, z, pid) block.set(x, y, z, 0, 0) + inventory.use(player.get_inventory(pid)) end diff --git a/res/scripts/stdmin.lua b/res/scripts/stdmin.lua index 5ebf0176..60824c84 100644 --- a/res/scripts/stdmin.lua +++ b/res/scripts/stdmin.lua @@ -497,3 +497,28 @@ end function file.prefix(path) return path:match("^([^:]+)") end + +function inventory.get_uses(invid, slot) + local uses = inventory.get_data(invid, slot, "uses") + if uses == nil then + return item.uses(inventory.get(invid, slot)) + end + return uses +end + + +function inventory.use(invid, slot) + local itemid, count = inventory.get(invid, slot) + if itemid == nil then + return + end + local item_uses = inventory.get_uses(invid, slot) + if item_uses == nil then + return + end + if item_uses == 1 then + inventory.set(invid, slot, itemid, count - 1) + elseif item_uses > 1 then + inventory.set_data(invid, slot, "uses", item_uses - 1) + end +end diff --git a/src/items/ItemDef.hpp b/src/items/ItemDef.hpp index 620a8d4d..36b9fe2d 100644 --- a/src/items/ItemDef.hpp +++ b/src/items/ItemDef.hpp @@ -38,7 +38,7 @@ struct ItemDef { uint8_t emission[4] {0, 0, 0, 0}; /// @brief Default item uses count - int16_t uses = 100; + int16_t uses = -1; ItemIconType iconType = ItemIconType::SPRITE; std::string icon = "blocks:notfound"; diff --git a/src/logic/scripting/lua/libs/libitem.cpp b/src/logic/scripting/lua/libs/libitem.cpp index c4ac49ec..b905f1d9 100644 --- a/src/logic/scripting/lua/libs/libitem.cpp +++ b/src/logic/scripting/lua/libs/libitem.cpp @@ -80,6 +80,13 @@ static int l_emission(lua::State* L) { return 0; } +static int l_uses(lua::State* L) { + if (auto def = get_item_def(L, 1)) { + return lua::pushinteger(L, def->uses); + } + return 0; +} + const luaL_Reg itemlib[] = { {"index", lua::wrap}, {"name", lua::wrap}, @@ -90,4 +97,6 @@ const luaL_Reg itemlib[] = { {"placing_block", lua::wrap}, {"model_name", lua::wrap}, {"emission", lua::wrap}, - {NULL, NULL}}; + {"uses", lua::wrap}, + {NULL, NULL} +};