minetest-mod-getinv/init.lua

34 lines
1.3 KiB
Lua
Raw Normal View History

2022-08-27 06:29:22 -07:00
core.register_privilege("getinv","Allows use /getinv command")
core.register_chatcommand("getinv", {
description = "List items of player's inventory",
params = "'<player> [listname]' or '<player> -list' to list all inventories",
privs = {getinv=true},
func = function(name,param)
local pname, ilist = param:match("^(%S+) (.+)$")
if not (pname and ilist) then
pname = param
ilist = "main"
end
local out = ""
local inv = core.get_inventory({type="player",name=pname})
if not inv then return false, "No Player" end
if ilist == "-list" then
local lists = inv:get_lists()
if not lists then return false, "Error getting inventories list" end
for listname,_ in pairs(lists) do
out = out..listname.."("..inv:get_size(listname).."), "
end
return true, "Inventories of "..pname..": "..out
end
local list = inv:get_list(ilist)
2022-08-27 06:41:25 -07:00
local isempty = inv:is_empty(ilist)
if not list or isempty == true then return false, "List not exists or empty" end
2022-08-27 06:29:22 -07:00
for _,stack in ipairs(list) do
2022-08-27 06:54:36 -07:00
local descr = stack:get_short_description()
2022-08-27 06:29:22 -07:00
if descr and descr ~= "" then
out = out..descr..", "
end
end
2022-08-27 06:54:36 -07:00
return true, "Inventory '"..ilist.."' of "..pname..": "..core.strip_colors(out)
2022-08-27 06:29:22 -07:00
end})