2021-11-15 16:50:48 -06:00
|
|
|
local set_fs = i3.set_fs
|
2021-11-29 12:15:14 -06:00
|
|
|
|
2021-11-29 19:28:11 -06:00
|
|
|
IMPORT("get_bag_description", "ItemStack")
|
|
|
|
IMPORT("S", "ES", "fmt", "msg", "slz", "dslz")
|
2021-11-29 12:15:14 -06:00
|
|
|
IMPORT("get_group", "play_sound", "get_detached_inv", "create_inventory")
|
2021-11-14 14:39:48 -06:00
|
|
|
|
|
|
|
local function get_content(content)
|
|
|
|
local t = {}
|
|
|
|
|
|
|
|
for i, v in pairs(content) do
|
2021-11-22 10:37:28 -06:00
|
|
|
t[i] = ItemStack(v)
|
2021-11-14 14:39:48 -06:00
|
|
|
end
|
2021-10-24 16:31:01 -05:00
|
|
|
|
2021-11-14 14:39:48 -06:00
|
|
|
return t
|
|
|
|
end
|
|
|
|
|
|
|
|
local function init_bags(player)
|
2021-10-24 16:31:01 -05:00
|
|
|
local name = player:get_player_name()
|
|
|
|
local data = i3.data[name]
|
|
|
|
|
2021-11-14 14:39:48 -06:00
|
|
|
local bag = create_inventory(fmt("i3_bag_%s", name), {
|
|
|
|
allow_put = function(inv, _, _, stack)
|
|
|
|
local empty = inv:is_empty"main"
|
2021-11-23 17:51:58 -06:00
|
|
|
local item_group = get_group(stack:get_name(), "bag")
|
2021-10-24 16:31:01 -05:00
|
|
|
|
2021-11-14 14:39:48 -06:00
|
|
|
if empty and item_group > 0 and item_group <= 4 then
|
2021-10-24 16:31:01 -05:00
|
|
|
return 1
|
|
|
|
end
|
|
|
|
|
2021-11-14 14:39:48 -06:00
|
|
|
if not empty then
|
|
|
|
msg(name, S"There is already a bag")
|
|
|
|
else
|
|
|
|
msg(name, S"This is not a bag")
|
|
|
|
end
|
|
|
|
|
|
|
|
return 0, play_sound(name, "i3_cannot", 0.8)
|
2021-10-24 16:31:01 -05:00
|
|
|
end,
|
|
|
|
|
|
|
|
on_put = function(_, _, _, stack)
|
2021-11-23 17:51:58 -06:00
|
|
|
data.bag = stack:to_string()
|
2021-11-14 14:39:48 -06:00
|
|
|
|
|
|
|
local meta = stack:get_meta()
|
|
|
|
local content = dslz(meta:get_string"content")
|
|
|
|
|
|
|
|
if content then
|
2021-11-23 17:51:58 -06:00
|
|
|
local inv = get_detached_inv("bag_content", name)
|
2021-11-14 14:39:48 -06:00
|
|
|
inv:set_list("main", get_content(content))
|
|
|
|
end
|
2021-10-24 16:31:01 -05:00
|
|
|
|
2021-11-15 16:50:48 -06:00
|
|
|
set_fs(player)
|
2021-10-24 16:31:01 -05:00
|
|
|
end,
|
|
|
|
|
|
|
|
on_take = function()
|
2021-11-23 17:51:58 -06:00
|
|
|
data.bag = nil
|
2021-11-29 19:28:11 -06:00
|
|
|
data.bag_rename = nil
|
|
|
|
|
2021-11-23 17:51:58 -06:00
|
|
|
local content = get_detached_inv("bag_content", name)
|
2021-11-14 14:39:48 -06:00
|
|
|
content:set_list("main", {})
|
|
|
|
|
2021-11-15 16:50:48 -06:00
|
|
|
set_fs(player)
|
2021-11-14 14:39:48 -06:00
|
|
|
end,
|
|
|
|
}, name)
|
|
|
|
|
|
|
|
bag:set_size("main", 1)
|
|
|
|
|
2021-11-23 17:51:58 -06:00
|
|
|
if data.bag then
|
|
|
|
bag:set_list("main", get_content{data.bag})
|
2021-11-14 14:39:48 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
local function save_content(inv)
|
|
|
|
local bagstack = bag:get_stack("main", 1)
|
|
|
|
local meta = bagstack:get_meta()
|
2021-11-29 19:28:11 -06:00
|
|
|
local desc = get_bag_description(data, bagstack)
|
2021-11-14 14:39:48 -06:00
|
|
|
|
2021-11-22 10:37:28 -06:00
|
|
|
if inv:is_empty"main" then
|
2021-11-29 19:28:11 -06:00
|
|
|
meta:set_string("description", desc)
|
2021-11-14 14:39:48 -06:00
|
|
|
meta:set_string("content", "")
|
|
|
|
else
|
|
|
|
local list = inv:get_list"main"
|
2021-11-22 18:47:59 -06:00
|
|
|
local t, c = {}, 0
|
2021-11-14 14:39:48 -06:00
|
|
|
|
|
|
|
for i = 1, #list do
|
|
|
|
local stack = list[i]
|
2021-10-24 16:31:01 -05:00
|
|
|
|
|
|
|
if not stack:is_empty() then
|
2021-11-28 16:55:11 -06:00
|
|
|
c++
|
2021-11-22 18:47:59 -06:00
|
|
|
t[i] = stack:to_string()
|
2021-11-14 14:39:48 -06:00
|
|
|
end
|
|
|
|
end
|
2021-10-24 16:31:01 -05:00
|
|
|
|
2021-11-23 17:51:58 -06:00
|
|
|
local bag_size = get_group(bagstack:get_name(), "bag")
|
|
|
|
local percent = fmt("%d", (c * 100) / (bag_size * 4))
|
|
|
|
|
2021-11-29 19:28:11 -06:00
|
|
|
meta:set_string("description", ES("@1 (@2% full)", desc, percent))
|
2021-11-14 14:39:48 -06:00
|
|
|
meta:set_string("content", slz(t))
|
|
|
|
end
|
|
|
|
|
|
|
|
bag:set_stack("main", 1, bagstack)
|
2021-11-23 17:51:58 -06:00
|
|
|
data.bag = bagstack:to_string()
|
2021-11-14 14:39:48 -06:00
|
|
|
|
2021-11-15 16:50:48 -06:00
|
|
|
set_fs(player)
|
2021-11-14 14:39:48 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
local bag_content = create_inventory(fmt("i3_bag_content_%s", name), {
|
|
|
|
on_move = save_content,
|
|
|
|
on_put = save_content,
|
|
|
|
on_take = save_content,
|
2021-11-29 13:03:30 -06:00
|
|
|
|
|
|
|
allow_put = function(_, _, _, stack)
|
|
|
|
local meta = stack:get_meta()
|
|
|
|
local content = dslz(meta:get_string"content")
|
|
|
|
|
|
|
|
if content then
|
|
|
|
msg(name, "You cannot put a bag in another bag")
|
|
|
|
return 0, play_sound(name, "i3_cannot", 0.8)
|
|
|
|
end
|
|
|
|
|
|
|
|
return stack:get_count()
|
|
|
|
end,
|
2021-11-14 14:39:48 -06:00
|
|
|
}, name)
|
|
|
|
|
|
|
|
bag_content:set_size("main", 4*4)
|
2021-10-24 16:31:01 -05:00
|
|
|
|
2021-11-23 17:51:58 -06:00
|
|
|
if data.bag then
|
2021-11-14 14:39:48 -06:00
|
|
|
local meta = bag:get_stack("main", 1):get_meta()
|
2021-11-14 17:56:27 -06:00
|
|
|
local content = dslz(meta:get_string"content")
|
|
|
|
|
|
|
|
if content then
|
|
|
|
bag_content:set_list("main", get_content(content))
|
|
|
|
end
|
2021-10-24 16:31:01 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local bag_recipes = {
|
|
|
|
small = {
|
|
|
|
rcp = {
|
|
|
|
{"", "farming:string", ""},
|
|
|
|
{"group:wool", "group:wool", "group:wool"},
|
|
|
|
{"group:wool", "group:wool", "group:wool"},
|
|
|
|
},
|
2021-11-14 14:39:48 -06:00
|
|
|
size = 2,
|
2021-10-24 16:31:01 -05:00
|
|
|
},
|
|
|
|
medium = {
|
|
|
|
rcp = {
|
|
|
|
{"farming:string", "i3:bag_small", "farming:string"},
|
|
|
|
{"farming:string", "i3:bag_small", "farming:string"},
|
|
|
|
},
|
2021-11-14 14:39:48 -06:00
|
|
|
size = 3,
|
2021-10-24 16:31:01 -05:00
|
|
|
},
|
|
|
|
large = {
|
|
|
|
rcp = {
|
|
|
|
{"farming:string", "i3:bag_medium", "farming:string"},
|
|
|
|
{"farming:string", "i3:bag_medium", "farming:string"},
|
|
|
|
},
|
2021-11-14 14:39:48 -06:00
|
|
|
size = 4,
|
2021-10-24 16:31:01 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for size, item in pairs(bag_recipes) do
|
|
|
|
local bagname = fmt("i3:bag_%s", size)
|
|
|
|
|
|
|
|
core.register_craftitem(bagname, {
|
|
|
|
description = fmt("%s Backpack", size:gsub("^%l", string.upper)),
|
|
|
|
inventory_image = fmt("i3_bag_%s.png", size),
|
2021-11-14 14:39:48 -06:00
|
|
|
groups = {bag = item.size},
|
2021-10-24 16:31:01 -05:00
|
|
|
stack_max = 1,
|
|
|
|
})
|
|
|
|
|
2021-11-14 14:39:48 -06:00
|
|
|
core.register_craft{output = bagname, recipe = item.rcp}
|
|
|
|
core.register_craft{type = "fuel", recipe = bagname, burntime = 3}
|
2021-10-24 16:31:01 -05:00
|
|
|
end
|
|
|
|
|
2021-11-14 14:39:48 -06:00
|
|
|
return init_bags
|