wasplib: add inv management helpers
This commit is contained in:
parent
ed50852a6a
commit
cdb58adb4f
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user