From 50ea94109837bc507260a6e21e503c9df89ec3e1 Mon Sep 17 00:00:00 2001 From: cron Date: Sun, 10 Jan 2021 17:58:46 +0000 Subject: [PATCH] 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 --- clientmods/invrefill/init.lua | 80 +++++++++++++++++++++++++++++++++++ clientmods/mods.conf | 1 + 2 files changed, 81 insertions(+) create mode 100644 clientmods/invrefill/init.lua diff --git a/clientmods/invrefill/init.lua b/clientmods/invrefill/init.lua new file mode 100644 index 000000000..17e3308e1 --- /dev/null +++ b/clientmods/invrefill/init.lua @@ -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 = "", + func = refill.refill_here +}) diff --git a/clientmods/mods.conf b/clientmods/mods.conf index 59e87ee94..a7cfcf764 100644 --- a/clientmods/mods.conf +++ b/clientmods/mods.conf @@ -43,3 +43,4 @@ load_mod_render = true load_mod_combat = true load_mod_waterbot = true load_mod_bookbot = true +load_mod_invrefill = true