cm_bags/init.lua

309 lines
9.6 KiB
Lua

local S = minetest.get_translator(minetest.get_current_modname())
local INV_SUFIX = "_bag_tmp"
local function getBagSize(stack)
if not stack:get_definition().groups.bag_size then
return 0
end
return stack:get_definition().groups.bag_size
end
local function create_detached_inventory(player_name, bag_stack)
local inv_name = player_name .. INV_SUFIX
--Chaeck if something is broken. To prevent item voiding
local inv = minetest.get_inventory({ type = "detached", name = inv_name })
if inv ~= nil then
minetest.chat_send_all("Bag is already open!")
return nil
end
minetest.create_detached_inventory(inv_name, {
--Forbids puting bags into bags
allow_put = function(inv, listname, index, stack, player)
if minetest.get_item_group(stack:get_name(), "bag_size") > 0 then
return 0
else
return stack:get_stack_max()
end
end,
}, player_name)
inv = minetest.get_inventory({ type = "detached", name = inv_name })
inv:set_size("main", getBagSize(bag_stack) * 9)
local meta = bag_stack:get_meta()
--minetest.chat_send_all(dump(meta:to_table()))
local content = meta:get_string("bag:content")
local list = content and minetest.deserialize(content) or {}
for i, stack in ipairs(list) do
inv:set_stack("main", i, stack)
--[[if stack ~= nil then
minetest.chat_send_all(dump(stack:to_string()))
end]]
end
return inv_name
end
local function openBag(
player,
player_name,
bag_stack,
context --[[opened equipped or inhand]]
)
--minetest.chat_send_all("Enter")
local inv_name = create_detached_inventory(player_name, bag_stack)
if inv_name == nil then
return
end
local form = "size[9,"
.. (getBagSize(bag_stack) + 6)
.. "]"
.. mcl_formspec.get_itemslot_bg(0, 0, 9, getBagSize(bag_stack))
.. "list[detached:"
.. inv_name
.. ";main;0,0;9,"
.. getBagSize(bag_stack)
.. ";]"
.. mcl_formspec.get_itemslot_bg(0, getBagSize(bag_stack) + 1.5, 9, 3)
.. "list[current_player;main;0,"
.. (getBagSize(bag_stack) + 1.5)
.. ";9,3;9]"
.. mcl_formspec.get_itemslot_bg(0, getBagSize(bag_stack) + 5, 9, 1)
.. "list[current_player;main;0,"
.. (getBagSize(bag_stack) + 5)
.. ";9,1;]"
.. "listring[current_player;main]"
.. "listring[detached:"
.. inv_name
.. ";main]"
.. "listring[current_player;main]"
minetest.show_formspec(player_name, "bag:" .. context, form)
end
local function close_wielded_bag(player)
local bag_stack = player:get_wielded_item()
local inv_name = player:get_player_name() .. INV_SUFIX
local inv = minetest.get_inventory({ type = "detached", name = inv_name })
local stacks = {}
for i = 0, inv:get_size("main"), 1 do
stacks[i] = inv:get_stack("main", i):to_string()
end
bag_stack:get_meta():set_string("bag:content", minetest.serialize(stacks))
player:set_wielded_item(bag_stack)
minetest.remove_detached_inventory(inv_name)
end
minetest.register_on_player_receive_fields(function(player, formname, fields)
--[[ Support for special "bag" slot at player gear.
if formname == "bag:equipped" and fields.quit then
--minetest.chat_send_all("Exit")
local player_inv = player:get_inventory()
local bag_stack = player_inv:get_stack("bag", 1)
local inv_name = player:get_player_name() .. "_bag_tmp"
local inv = minetest.get_inventory({ type = "detached", name = inv_name })
local stacks = {}
for i = 0, inv:get_size("main"), 1 do
stacks[i] = inv:get_stack("main", i):to_string()
end
bag_stack:get_meta():set_string("bag:content", minetest.serialize(stacks))
player_inv:set_stack("backpack", 1, backapack_stack)
--minetest.chat_send_all(dump(meta:to_table()))
minetest.remove_detached_inventory(inv_name)
else]]
if formname == "bag:inhand" and fields.quit then
close_wielded_bag(player)
end
end)
-- Forbid moving bag while it is open. To prevent item deletion on bag closure.
minetest.register_allow_player_inventory_action(function(player, action, inventory, inventory_info)
if
action == "move"
and inventory_info.from_list == "main"
and inventory_info.from_index == player:get_wield_index()
then
local bag_inv = minetest.get_inventory({ type = "detached", name = player:get_player_name() .. INV_SUFIX })
if bag_inv ~= nil then
return 0
end
end
if action == "take" and inventory_info.listname == "main" and inventory_info.index == player:get_wield_index() then
local bag_inv = minetest.get_inventory({ type = "detached", name = player:get_player_name() .. INV_SUFIX })
if bag_inv ~= nil then
return 0
end
end
end)
--[[
--A chat command for now. Wonna change to hot-key later.
minetest.register_chatcommand("bag", {
func = function(player_name)
local player = minetest.get_player_by_name(player_name)
local player_inv = player:get_inventory()
local backapack_stack = player_inv:get_stack("backpack", 1)
if backapack_stack:is_empty() then
return false, "No backpack equiped!"
end
openBackpack(player, player_name, backapack_stack, "equipped")
return true, ""
end,
})
minetest.register_allow_player_inventory_action(function(player, action, inventory, inventory_info)
if action == "move" and inventory_info.to_list == "backpack" then
local itemstack = inventory:get_stack(inventory_info.from_list, inventory_info.from_index)
if not (minetest.get_item_group(itemstack:get_name(), "backpack_size") > 0) then
return 0
else
return itemstack:get_stack_max()
end
end
end)
]]
local function try_get_destination(node_def, node_pos, user, source_inv, source_list)
local dst_list = "main"
local dst_inv, stack_id
if node_def._mcl_hoppers_on_try_push then
dst_inv, dst_list, stack_id =
node_def._mcl_hoppers_on_try_push(node_pos, user:get_pos(), source_inv, source_list)
else
local dst_meta = minetest.get_meta(node_pos)
dst_inv = dst_meta:get_inventory()
stack_id = mcl_util.select_stack(source_inv, source_list, dst_inv, dst_list, nil, 1)
end
return dst_inv, dst_list, stack_id
end
local function on_bag_place(itemstack, user, pointed_thing)
if pointed_thing.type == "node" then
local node_pos = pointed_thing.under
local node = minetest.get_node(node_pos)
local def = minetest.registered_nodes[node.name]
if user and user:get_player_control().sneak
--and minetest.get_item_group(pointed_thing.name, "container") == 2
then
local inv_name = create_detached_inventory(user:get_player_name(), itemstack)
if inv_name == nil then
return
end
local inv = minetest.get_inventory({ type = "detached", name = inv_name })
local dst_inv, dst_list, stack_id
dst_inv, dst_list, stack_id = try_get_destination(def, node_pos, user, inv, "main")
minetest.chat_send_all(stack_id)
while stack_id do
mcl_util.move_item(inv, "main", stack_id, dst_inv, dst_list)
dst_inv, dst_list, stack_id = try_get_destination(def, node_pos, user, inv, "main")
end
close_wielded_bag(user)
elseif def and def.on_rightclick then
-- Call node's on_rightclick method if defined
def.on_rightclick(node_pos, node, user)
else
openBag(user, user:get_player_name(), itemstack, "inhand")
end
elseif pointed_thing.type == "object" then
local object = pointed_thing.ref
local entity_def = object:get_luaentity()
if entity_def and entity_def.on_rightclick then
-- Call object's on_rightclick method if defined
entity_def.on_rightclick(entity_def, user)
else
openBag(user, user:get_player_name(), itemstack, "inhand")
end
else
openBag(user, user:get_player_name(), itemstack, "inhand")
end
end
-- register backapack's
minetest.register_tool("cm_bags:leather", {
description = S("Leather bag"),
inventory_image = "bag_leather.png",
groups = { bag_size = 1 },
on_place = on_bag_place,
on_secondary_use = on_bag_place,
})
minetest.register_tool("cm_bags:iron", {
description = S("Iron bag"),
inventory_image = "bag_iron.png",
groups = { bag_size = 2 },
on_place = on_bag_place,
on_secondary_use = on_bag_place,
})
minetest.register_tool("cm_bags:gold", {
description = S("Gold bag"),
inventory_image = "bag_gold.png",
groups = { bag_size = 3 },
on_place = on_bag_place,
on_secondary_use = on_bag_place,
})
minetest.register_tool("cm_bags:diamond", {
description = S("Diamond bag"),
inventory_image = "bag_diamond.png",
groups = { bag_size = 4 },
on_place = on_bag_place,
on_secondary_use = on_bag_place,
})
-- register bag crafts
minetest.register_craft({
output = "cm_bags:leather",
recipe = {
{ "mcl_mobitems:leather", "mcl_mobitems:leather", "mcl_mobitems:leather" },
{ "mcl_mobitems:leather", "mcl_chests:chest", "mcl_mobitems:leather" },
{ "mcl_mobitems:leather", "mcl_mobitems:leather", "mcl_mobitems:leather" },
},
})
minetest.register_craft({
output = "cm_bags:iron",
recipe = {
{ "mcl_core:iron_ingot", "mcl_core:iron_ingot", "mcl_core:iron_ingot" },
{ "mcl_core:iron_ingot", "cm_bags:leather", "mcl_core:iron_ingot" },
{ "mcl_core:iron_ingot", "mcl_core:iron_ingot", "mcl_core:iron_ingot" },
},
})
minetest.register_craft({
output = "cm_bags:gold",
recipe = {
{ "mcl_core:gold_ingot", "mcl_core:gold_ingot", "mcl_core:gold_ingot" },
{ "mcl_core:gold_ingot", "cm_bags:iron", "mcl_core:gold_ingot" },
{ "mcl_core:gold_ingot", "mcl_core:gold_ingot", "mcl_core:gold_ingot" },
},
})
minetest.register_craft({
output = "cm_bags:diamond",
recipe = {
{ "mcl_core:diamond", "mcl_core:diamond", "mcl_core:diamond" },
{ "mcl_core:diamond", "cm_bags:gold", "mcl_core:diamond" },
{ "mcl_core:diamond", "mcl_core:diamond", "mcl_core:diamond" },
},
})
--transfer content of backpack on craft
minetest.register_on_craft(function(itemstack, player, old_craft_grid, craft_inv)
if minetest.get_item_group(itemstack:get_name(), "bag_size") > 0 then
local content = old_craft_grid[5]:get_meta():get_string("bag:content")
itemstack:get_meta():set_string("bag:content", content)
return itemstack
end
end)