From cdb58adb4fa3a105d1f4c451724cb631afb4fbd5 Mon Sep 17 00:00:00 2001 From: cora Date: Tue, 31 Aug 2021 21:48:57 +0200 Subject: [PATCH] wasplib: add inv management helpers --- builtin/client/wasplib.lua | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/builtin/client/wasplib.lua b/builtin/client/wasplib.lua index 4a1df00f6..39f36f3a6 100644 --- a/builtin/client/wasplib.lua +++ b/builtin/client/wasplib.lua @@ -254,6 +254,15 @@ function ws.find_item_in_table(items,rnd) return false end +function ws.find_empty(inv) + for i, v in ipairs(inv) do + if v:is_empty() then + return i + end + end + return false +end + function ws.find_named(inv, name) if not inv then return -1 end if not name then return end @@ -263,6 +272,21 @@ function ws.find_named(inv, name) end end end + +function ws.itemnameformat(description) + description = description:gsub(string.char(0x1b) .. "%(.@[^)]+%)", "") + description = description:match("([^\n]*)") + return description +end + +function ws.find_nametagged(list, name) + for i, v in ipairs(list) do + if ws.itemnameformat(v:get_description()) == name then + return i + end + end +end + local hotbar_slot=8 function ws.to_hotbar(it,hslot) local tpos=nil @@ -301,6 +325,14 @@ function ws.switch_to_item(itname,hslot) end return false end +function ws.in_inv(itname) + if not minetest.localplayer then return false end + local plinv = minetest.get_inventory("current_player") + local pos = ws.find_named(plinv.main, itname) + if pos then + return true + end +end function core.switch_to_item(item) return ws.switch_to_item(item) end @@ -363,6 +395,9 @@ function ws.invparse(location) end end +function ws.invpos(p) + return "nodemeta:"..p.x..","..p.y..","..p.z +end -- TOOLS