invrefill: add small CSM for inventory refilling (bots and stuff)

right now it just has .refill which looks for a named shulker with some items and refills the inv with that
This commit is contained in:
cron 2021-01-10 17:58:46 +00:00
parent 2c68047fbf
commit 50ea941098
2 changed files with 81 additions and 0 deletions

View File

@ -0,0 +1,80 @@
-- CC0/Unlicense Emilia 2021
refill = {}
local function nameformat(description)
description = description:gsub(string.char(0x1b) .. "%(.@[^)]+%)", "")
description = description:match("([^\n]*)")
return description
end
local function find_named(list, name, test)
for i, v in ipairs(list) do
if (v:get_name():find("shulker_box")
and nameformat(v:get_description()) == name
and (test and test(v))) then
return i
end
end
end
local function hasitems(stack)
local list = minetest.deserialize(stack:get_metadata())
for i, v in ipairs(list) do
if not ItemStack(v):is_empty() then
return true
end
end
return false
end
local function shulk_switch(name)
local plinv = minetest.get_inventory("current_player")
local pos = find_named(plinv.main, name, hasitems)
--or find_named(plinv.enderchest, name, hasitems))
if pos then
minetest.localplayer:set_wield_index(pos)
return true
end
end
local function shulk_place(pos, name)
if shulk_switch(name) then
minetest.place_node(pos)
return true
end
end
local function invposformat(pos)
pos = vector.round(pos)
return string.format("nodemeta:%i,%i,%i", pos.x, pos.y, pos.z)
end
local function do_refill(pos)
local q = quint.invaction_new()
quint.invaction_dump(q,
{location = invposformat(pos), inventory = "main"},
{location = "current_player", inventory = "main"})
quint.invaction_apply(q)
end
local function queue_refill(pos, name)
if shulk_place(pos, name) then
minetest.display_chat_message(invposformat(pos))
minetest.after(1, do_refill, pos)
minetest.after(2, minetest.dig_node, pos)
end
end
function refill.refill_here(name)
local pos = vector.round(minetest.localplayer:get_pos())
queue_refill(pos, name)
end
minetest.register_chatcommand("refill", {
description = "Refill the inventory with a named shulker.",
params = "<shulker name>",
func = refill.refill_here
})

View File

@ -43,3 +43,4 @@ load_mod_render = true
load_mod_combat = true
load_mod_waterbot = true
load_mod_bookbot = true
load_mod_invrefill = true