2020-03-25 18:46:35 +01:00
|
|
|
--[[
|
2019-06-16 10:26:40 +02:00
|
|
|
Bags for Minetest
|
2012-12-02 10:08:32 +01:00
|
|
|
|
2019-06-16 10:26:40 +02:00
|
|
|
Copyright (c) 2012 cornernote, Brett O'Donnell <cornernote@gmail.com>
|
|
|
|
License: GPLv3
|
|
|
|
--]]
|
2012-12-02 10:08:32 +01:00
|
|
|
|
2019-09-19 14:00:26 +02:00
|
|
|
local S = minetest.get_translator("unified_inventory")
|
2018-04-02 13:33:36 +02:00
|
|
|
local F = minetest.formspec_escape
|
2021-02-25 19:27:10 +01:00
|
|
|
local bags_inv_bg_prefix = "image[-0.1,1.0;10.05,"
|
2014-06-21 12:44:31 +02:00
|
|
|
|
2013-09-29 00:15:37 +02:00
|
|
|
unified_inventory.register_page("bags", {
|
2013-10-03 05:25:07 +02:00
|
|
|
get_formspec = function(player)
|
2013-09-29 00:15:37 +02:00
|
|
|
local player_name = player:get_player_name()
|
2019-03-31 11:26:02 +02:00
|
|
|
return { formspec = table.concat({
|
Improve consistency of inventory (and alike) imagery
In a number of places, background[] is misused to place the
inventory backdrop images. Where appropriate, image[] is used
instead, so that "ui_form_bg.png" actually serves as the one
and only true background image.
In so doing, I was able to remake the bag inventory images,
making them only big as is actually needed to hold 1, 2, or 3
rows of inventory slots.
This, in turn, allows a standardized main inventory image to
occupy the lower part of the window, which allows for
consistent inventory image positioning and sizing from one
page to another.
I also removed ui_misc_form.png. Nothing in UI uses it, and
any external mods that used it can just use the standard
inventory and its background.
Lastly, I reduced the background image to 512x384 px. It was
unnecessarily large before, considering it has no real detail.
The larger inventory images are all 512px wide, and multiples
of 64px in height. Before, they were oddly sized.
2021-02-24 13:00:29 +01:00
|
|
|
string.gsub(unified_inventory.standard_inv_bg, "YYY", "4.4"),
|
|
|
|
bags_inv_bg_prefix.."1.175;ui_bags_header.png]",
|
2019-03-31 11:26:02 +02:00
|
|
|
"label[0,0;" .. F(S("Bags")) .. "]",
|
2021-02-25 19:27:10 +01:00
|
|
|
"button[0,2.2;2,0.5;bag1;" .. F(S("Bag @1", 1)) .. "]",
|
|
|
|
"button[2,2.2;2,0.5;bag2;" .. F(S("Bag @1", 2)) .. "]",
|
|
|
|
"button[4,2.2;2,0.5;bag3;" .. F(S("Bag @1", 3)) .. "]",
|
|
|
|
"button[6,2.2;2,0.5;bag4;" .. F(S("Bag @1", 4)) .. "]",
|
2019-03-31 11:26:02 +02:00
|
|
|
"listcolors[#00000000;#00000000]",
|
2021-02-25 19:27:10 +01:00
|
|
|
"list[detached:" .. F(player_name) .. "_bags;bag1;0.5,1.1;1,1;]",
|
|
|
|
"list[detached:" .. F(player_name) .. "_bags;bag2;2.5,1.1;1,1;]",
|
|
|
|
"list[detached:" .. F(player_name) .. "_bags;bag3;4.5,1.1;1,1;]",
|
|
|
|
"list[detached:" .. F(player_name) .. "_bags;bag4;6.5,1.1;1,1;]"
|
2019-03-31 11:26:02 +02:00
|
|
|
}) }
|
2013-09-29 00:15:37 +02:00
|
|
|
end,
|
|
|
|
})
|
|
|
|
|
|
|
|
unified_inventory.register_button("bags", {
|
|
|
|
type = "image",
|
|
|
|
image = "ui_bags_icon.png",
|
2015-10-05 10:24:01 +02:00
|
|
|
tooltip = S("Bags"),
|
|
|
|
hide_lite=true
|
2013-09-29 00:15:37 +02:00
|
|
|
})
|
|
|
|
|
2018-06-21 21:23:21 +02:00
|
|
|
local function get_player_bag_stack(player, i)
|
|
|
|
return minetest.get_inventory({
|
|
|
|
type = "detached",
|
|
|
|
name = player:get_player_name() .. "_bags"
|
|
|
|
}):get_stack("bag" .. i, 1)
|
|
|
|
end
|
|
|
|
|
2019-03-31 11:26:02 +02:00
|
|
|
for bag_i = 1, 4 do
|
|
|
|
unified_inventory.register_page("bag" .. bag_i, {
|
2015-10-10 03:55:46 +02:00
|
|
|
get_formspec = function(player)
|
2019-03-31 11:26:02 +02:00
|
|
|
local stack = get_player_bag_stack(player, bag_i)
|
2013-09-29 00:15:37 +02:00
|
|
|
local image = stack:get_definition().inventory_image
|
2019-03-31 11:26:02 +02:00
|
|
|
local fs = {
|
Improve consistency of inventory (and alike) imagery
In a number of places, background[] is misused to place the
inventory backdrop images. Where appropriate, image[] is used
instead, so that "ui_form_bg.png" actually serves as the one
and only true background image.
In so doing, I was able to remake the bag inventory images,
making them only big as is actually needed to hold 1, 2, or 3
rows of inventory slots.
This, in turn, allows a standardized main inventory image to
occupy the lower part of the window, which allows for
consistent inventory image positioning and sizing from one
page to another.
I also removed ui_misc_form.png. Nothing in UI uses it, and
any external mods that used it can just use the standard
inventory and its background.
Lastly, I reduced the background image to 512x384 px. It was
unnecessarily large before, considering it has no real detail.
The larger inventory images are all 512px wide, and multiples
of 64px in height. Before, they were oddly sized.
2021-02-24 13:00:29 +01:00
|
|
|
string.gsub(unified_inventory.standard_inv_bg, "YYY", "4.4"),
|
2019-03-31 11:26:02 +02:00
|
|
|
"image[7,0;1,1;" .. image .. "]",
|
|
|
|
"label[0,0;" .. F(S("Bag @1", bag_i)) .. "]",
|
|
|
|
"listcolors[#00000000;#00000000]",
|
2021-02-25 19:27:10 +01:00
|
|
|
"list[current_player;bag" .. bag_i .. "contents;0,1.1;8,3;]",
|
2019-03-31 11:26:02 +02:00
|
|
|
"listring[current_name;bag" .. bag_i .. "contents]",
|
Improve consistency of inventory (and alike) imagery
In a number of places, background[] is misused to place the
inventory backdrop images. Where appropriate, image[] is used
instead, so that "ui_form_bg.png" actually serves as the one
and only true background image.
In so doing, I was able to remake the bag inventory images,
making them only big as is actually needed to hold 1, 2, or 3
rows of inventory slots.
This, in turn, allows a standardized main inventory image to
occupy the lower part of the window, which allows for
consistent inventory image positioning and sizing from one
page to another.
I also removed ui_misc_form.png. Nothing in UI uses it, and
any external mods that used it can just use the standard
inventory and its background.
Lastly, I reduced the background image to 512x384 px. It was
unnecessarily large before, considering it has no real detail.
The larger inventory images are all 512px wide, and multiples
of 64px in height. Before, they were oddly sized.
2021-02-24 13:00:29 +01:00
|
|
|
"listring[current_player;main]",
|
2019-03-31 11:26:02 +02:00
|
|
|
}
|
2013-09-29 00:15:37 +02:00
|
|
|
local slots = stack:get_definition().groups.bagslots
|
|
|
|
if slots == 8 then
|
Improve consistency of inventory (and alike) imagery
In a number of places, background[] is misused to place the
inventory backdrop images. Where appropriate, image[] is used
instead, so that "ui_form_bg.png" actually serves as the one
and only true background image.
In so doing, I was able to remake the bag inventory images,
making them only big as is actually needed to hold 1, 2, or 3
rows of inventory slots.
This, in turn, allows a standardized main inventory image to
occupy the lower part of the window, which allows for
consistent inventory image positioning and sizing from one
page to another.
I also removed ui_misc_form.png. Nothing in UI uses it, and
any external mods that used it can just use the standard
inventory and its background.
Lastly, I reduced the background image to 512x384 px. It was
unnecessarily large before, considering it has no real detail.
The larger inventory images are all 512px wide, and multiples
of 64px in height. Before, they were oddly sized.
2021-02-24 13:00:29 +01:00
|
|
|
fs[#fs + 1] = bags_inv_bg_prefix.."1.175;ui_bags_inv_small.png]"
|
2013-09-29 00:15:37 +02:00
|
|
|
elseif slots == 16 then
|
Improve consistency of inventory (and alike) imagery
In a number of places, background[] is misused to place the
inventory backdrop images. Where appropriate, image[] is used
instead, so that "ui_form_bg.png" actually serves as the one
and only true background image.
In so doing, I was able to remake the bag inventory images,
making them only big as is actually needed to hold 1, 2, or 3
rows of inventory slots.
This, in turn, allows a standardized main inventory image to
occupy the lower part of the window, which allows for
consistent inventory image positioning and sizing from one
page to another.
I also removed ui_misc_form.png. Nothing in UI uses it, and
any external mods that used it can just use the standard
inventory and its background.
Lastly, I reduced the background image to 512x384 px. It was
unnecessarily large before, considering it has no real detail.
The larger inventory images are all 512px wide, and multiples
of 64px in height. Before, they were oddly sized.
2021-02-24 13:00:29 +01:00
|
|
|
fs[#fs + 1] = bags_inv_bg_prefix.."2.35;ui_bags_inv_medium.png]"
|
2013-09-29 00:15:37 +02:00
|
|
|
elseif slots == 24 then
|
Improve consistency of inventory (and alike) imagery
In a number of places, background[] is misused to place the
inventory backdrop images. Where appropriate, image[] is used
instead, so that "ui_form_bg.png" actually serves as the one
and only true background image.
In so doing, I was able to remake the bag inventory images,
making them only big as is actually needed to hold 1, 2, or 3
rows of inventory slots.
This, in turn, allows a standardized main inventory image to
occupy the lower part of the window, which allows for
consistent inventory image positioning and sizing from one
page to another.
I also removed ui_misc_form.png. Nothing in UI uses it, and
any external mods that used it can just use the standard
inventory and its background.
Lastly, I reduced the background image to 512x384 px. It was
unnecessarily large before, considering it has no real detail.
The larger inventory images are all 512px wide, and multiples
of 64px in height. Before, they were oddly sized.
2021-02-24 13:00:29 +01:00
|
|
|
fs[#fs + 1] = bags_inv_bg_prefix.."3.525;ui_bags_inv_large.png]"
|
2013-09-29 00:15:37 +02:00
|
|
|
end
|
2017-03-01 23:28:40 +01:00
|
|
|
local player_name = player:get_player_name() -- For if statement.
|
2019-03-31 11:26:02 +02:00
|
|
|
if unified_inventory.trash_enabled
|
|
|
|
or unified_inventory.is_creative(player_name)
|
|
|
|
or minetest.get_player_privs(player_name).give then
|
2021-02-25 19:17:58 +01:00
|
|
|
fs[#fs + 1] = "image[5.91,-0.06;1.21,1.15;ui_bags_trash.png]"
|
2019-03-31 11:26:02 +02:00
|
|
|
.. "list[detached:trash;main;6,0.1;1,1;]"
|
2017-03-01 23:28:40 +01:00
|
|
|
end
|
2017-03-22 15:44:18 +01:00
|
|
|
local inv = player:get_inventory()
|
|
|
|
for i = 1, 4 do
|
2018-06-21 21:23:21 +02:00
|
|
|
local def = get_player_bag_stack(player, i):get_definition()
|
2017-03-22 15:44:18 +01:00
|
|
|
if def.groups.bagslots then
|
2019-03-31 11:26:02 +02:00
|
|
|
local list_name = "bag" .. i .. "contents"
|
2017-03-22 15:44:18 +01:00
|
|
|
local size = inv:get_size(list_name)
|
|
|
|
local used = 0
|
|
|
|
for si = 1, size do
|
|
|
|
local stk = inv:get_stack(list_name, si)
|
|
|
|
if not stk:is_empty() then
|
|
|
|
used = used + 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
local img = def.inventory_image
|
2019-03-31 11:26:02 +02:00
|
|
|
local label = F(S("Bag @1", i)) .. "\n" .. used .. "/" .. size
|
|
|
|
fs[#fs + 1] = string.format("image_button[%i,0;1,1;%s;bag%i;%s]",
|
|
|
|
i + 1, img, i, label)
|
2017-03-22 15:44:18 +01:00
|
|
|
end
|
|
|
|
end
|
2019-03-31 11:26:02 +02:00
|
|
|
return { formspec = table.concat(fs) }
|
2013-09-29 00:15:37 +02:00
|
|
|
end,
|
|
|
|
})
|
2016-10-08 11:07:41 +02:00
|
|
|
end
|
2013-09-29 00:15:37 +02:00
|
|
|
|
|
|
|
minetest.register_on_player_receive_fields(function(player, formname, fields)
|
2013-12-04 16:43:49 +01:00
|
|
|
if formname ~= "" then
|
|
|
|
return
|
|
|
|
end
|
2013-09-29 00:15:37 +02:00
|
|
|
for i = 1, 4 do
|
2019-03-31 11:26:02 +02:00
|
|
|
if fields["bag" .. i] then
|
2018-06-21 21:23:21 +02:00
|
|
|
local stack = get_player_bag_stack(player, i)
|
2013-09-29 00:15:37 +02:00
|
|
|
if not stack:get_definition().groups.bagslots then
|
|
|
|
return
|
|
|
|
end
|
2019-03-31 11:26:02 +02:00
|
|
|
unified_inventory.set_inventory_formspec(player, "bag" .. i)
|
2013-09-29 00:15:37 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
2018-06-21 21:23:21 +02:00
|
|
|
local function save_bags_metadata(player, bags_inv)
|
|
|
|
local is_empty = true
|
|
|
|
local bags = {}
|
|
|
|
for i = 1, 4 do
|
2019-03-31 11:26:02 +02:00
|
|
|
local bag = "bag" .. i
|
2018-06-21 21:23:21 +02:00
|
|
|
if not bags_inv:is_empty(bag) then
|
|
|
|
-- Stack limit is 1, otherwise use stack:to_string()
|
|
|
|
bags[i] = bags_inv:get_stack(bag, 1):get_name()
|
|
|
|
is_empty = false
|
|
|
|
end
|
|
|
|
end
|
2019-06-16 10:26:40 +02:00
|
|
|
local meta = player:get_meta()
|
2018-06-21 21:23:21 +02:00
|
|
|
if is_empty then
|
2019-06-16 10:26:40 +02:00
|
|
|
meta:set_string("unified_inventory:bags", nil)
|
2018-06-21 21:23:21 +02:00
|
|
|
else
|
2019-06-16 10:26:40 +02:00
|
|
|
meta:set_string("unified_inventory:bags",
|
2018-06-21 21:23:21 +02:00
|
|
|
minetest.serialize(bags))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local function load_bags_metadata(player, bags_inv)
|
|
|
|
local player_inv = player:get_inventory()
|
2019-06-16 10:26:40 +02:00
|
|
|
local meta = player:get_meta()
|
2021-03-09 11:12:50 +01:00
|
|
|
local bags_meta = meta:get("unified_inventory:bags")
|
2018-06-21 21:23:21 +02:00
|
|
|
local bags = bags_meta and minetest.deserialize(bags_meta) or {}
|
|
|
|
local dirty_meta = false
|
|
|
|
if not bags_meta then
|
|
|
|
-- Backwards compatiblity
|
|
|
|
for i = 1, 4 do
|
2019-03-31 11:26:02 +02:00
|
|
|
local bag = "bag" .. i
|
2018-06-21 21:23:21 +02:00
|
|
|
if not player_inv:is_empty(bag) then
|
|
|
|
-- Stack limit is 1, otherwise use stack:to_string()
|
|
|
|
bags[i] = player_inv:get_stack(bag, 1):get_name()
|
|
|
|
dirty_meta = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
-- Fill detached slots
|
|
|
|
for i = 1, 4 do
|
2019-03-31 11:26:02 +02:00
|
|
|
local bag = "bag" .. i
|
2018-06-21 21:23:21 +02:00
|
|
|
bags_inv:set_size(bag, 1)
|
|
|
|
bags_inv:set_stack(bag, 1, bags[i] or "")
|
|
|
|
end
|
|
|
|
|
|
|
|
if dirty_meta then
|
|
|
|
-- Requires detached inventory to be set up
|
|
|
|
save_bags_metadata(player, bags_inv)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Clean up deprecated garbage after saving
|
|
|
|
for i = 1, 4 do
|
2019-03-31 11:26:02 +02:00
|
|
|
local bag = "bag" .. i
|
2018-06-21 21:23:21 +02:00
|
|
|
player_inv:set_size(bag, 0)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-02 10:08:32 +01:00
|
|
|
minetest.register_on_joinplayer(function(player)
|
2016-11-26 19:32:21 +01:00
|
|
|
local player_name = player:get_player_name()
|
2019-03-31 11:26:02 +02:00
|
|
|
local bags_inv = minetest.create_detached_inventory(player_name .. "_bags",{
|
2012-12-02 10:08:32 +01:00
|
|
|
on_put = function(inv, listname, index, stack, player)
|
2019-03-31 11:26:02 +02:00
|
|
|
player:get_inventory():set_size(listname .. "contents",
|
2013-09-21 21:40:20 +02:00
|
|
|
stack:get_definition().groups.bagslots)
|
2018-06-21 21:23:21 +02:00
|
|
|
save_bags_metadata(player, inv)
|
2012-12-02 10:08:32 +01:00
|
|
|
end,
|
|
|
|
allow_put = function(inv, listname, index, stack, player)
|
2016-02-29 06:34:13 +01:00
|
|
|
local new_slots = stack:get_definition().groups.bagslots
|
2018-06-21 21:23:21 +02:00
|
|
|
if not new_slots then
|
|
|
|
return 0
|
|
|
|
end
|
|
|
|
local player_inv = player:get_inventory()
|
2019-03-31 11:26:02 +02:00
|
|
|
local old_slots = player_inv:get_size(listname .. "contents")
|
2016-02-29 06:34:13 +01:00
|
|
|
|
2018-06-21 21:23:21 +02:00
|
|
|
if new_slots >= old_slots then
|
|
|
|
return 1
|
|
|
|
end
|
|
|
|
|
|
|
|
-- using a smaller bag, make sure it fits
|
2019-03-31 11:26:02 +02:00
|
|
|
local old_list = player_inv:get_list(listname .. "contents")
|
2018-06-21 21:23:21 +02:00
|
|
|
local new_list = {}
|
|
|
|
local slots_used = 0
|
|
|
|
local use_new_list = false
|
|
|
|
|
|
|
|
for i, v in ipairs(old_list) do
|
|
|
|
if v and not v:is_empty() then
|
|
|
|
slots_used = slots_used + 1
|
|
|
|
use_new_list = i > new_slots
|
|
|
|
new_list[slots_used] = v
|
2016-02-29 06:34:13 +01:00
|
|
|
end
|
2012-12-02 10:08:32 +01:00
|
|
|
end
|
2018-06-21 21:23:21 +02:00
|
|
|
if new_slots >= slots_used then
|
|
|
|
if use_new_list then
|
2019-03-31 11:26:02 +02:00
|
|
|
player_inv:set_list(listname .. "contents", new_list)
|
2018-06-21 21:23:21 +02:00
|
|
|
end
|
|
|
|
return 1
|
|
|
|
end
|
2018-08-10 16:11:35 +02:00
|
|
|
-- New bag is smaller: Disallow inserting
|
|
|
|
return 0
|
2012-12-02 10:08:32 +01:00
|
|
|
end,
|
|
|
|
allow_take = function(inv, listname, index, stack, player)
|
2019-03-31 11:26:02 +02:00
|
|
|
if player:get_inventory():is_empty(listname .. "contents") then
|
2012-12-02 10:08:32 +01:00
|
|
|
return stack:get_count()
|
|
|
|
end
|
2018-06-21 21:23:21 +02:00
|
|
|
return 0
|
2012-12-02 10:08:32 +01:00
|
|
|
end,
|
2018-06-21 21:23:21 +02:00
|
|
|
on_take = function(inv, listname, index, stack, player)
|
2019-03-31 11:26:02 +02:00
|
|
|
player:get_inventory():set_size(listname .. "contents", 0)
|
2018-06-21 21:23:21 +02:00
|
|
|
save_bags_metadata(player, inv)
|
|
|
|
end,
|
|
|
|
allow_move = function()
|
2012-12-02 10:08:32 +01:00
|
|
|
return 0
|
|
|
|
end,
|
2016-11-26 19:32:21 +01:00
|
|
|
}, player_name)
|
2018-06-21 21:23:21 +02:00
|
|
|
|
|
|
|
load_bags_metadata(player, bags_inv)
|
2012-12-02 10:08:32 +01:00
|
|
|
end)
|
|
|
|
|
|
|
|
-- register bag tools
|
|
|
|
minetest.register_tool("unified_inventory:bag_small", {
|
2014-06-21 12:44:31 +02:00
|
|
|
description = S("Small Bag"),
|
2012-12-02 10:08:32 +01:00
|
|
|
inventory_image = "bags_small.png",
|
|
|
|
groups = {bagslots=8},
|
|
|
|
})
|
2013-09-21 21:40:20 +02:00
|
|
|
|
2012-12-02 10:08:32 +01:00
|
|
|
minetest.register_tool("unified_inventory:bag_medium", {
|
2014-06-21 12:44:31 +02:00
|
|
|
description = S("Medium Bag"),
|
2012-12-02 10:08:32 +01:00
|
|
|
inventory_image = "bags_medium.png",
|
|
|
|
groups = {bagslots=16},
|
|
|
|
})
|
2013-09-21 21:40:20 +02:00
|
|
|
|
2012-12-02 10:08:32 +01:00
|
|
|
minetest.register_tool("unified_inventory:bag_large", {
|
2014-06-21 12:44:31 +02:00
|
|
|
description = S("Large Bag"),
|
2012-12-02 10:08:32 +01:00
|
|
|
inventory_image = "bags_large.png",
|
|
|
|
groups = {bagslots=24},
|
|
|
|
})
|
|
|
|
|
|
|
|
-- register bag crafts
|
2016-08-06 17:44:37 +02:00
|
|
|
if minetest.get_modpath("farming") ~= nil then
|
|
|
|
minetest.register_craft({
|
|
|
|
output = "unified_inventory:bag_small",
|
|
|
|
recipe = {
|
2020-03-21 18:57:53 +01:00
|
|
|
{"", "farming:string", ""},
|
2016-08-06 17:44:37 +02:00
|
|
|
{"group:wool", "group:wool", "group:wool"},
|
|
|
|
{"group:wool", "group:wool", "group:wool"},
|
|
|
|
},
|
|
|
|
})
|
2013-09-21 21:40:20 +02:00
|
|
|
|
2016-08-06 17:44:37 +02:00
|
|
|
minetest.register_craft({
|
|
|
|
output = "unified_inventory:bag_medium",
|
|
|
|
recipe = {
|
|
|
|
{"", "", ""},
|
2020-03-21 18:57:53 +01:00
|
|
|
{"farming:string", "unified_inventory:bag_small", "farming:string"},
|
|
|
|
{"farming:string", "unified_inventory:bag_small", "farming:string"},
|
2016-08-06 17:44:37 +02:00
|
|
|
},
|
|
|
|
})
|
2013-09-21 21:40:20 +02:00
|
|
|
|
2016-08-06 17:44:37 +02:00
|
|
|
minetest.register_craft({
|
|
|
|
output = "unified_inventory:bag_large",
|
|
|
|
recipe = {
|
|
|
|
{"", "", ""},
|
2020-03-21 18:57:53 +01:00
|
|
|
{"farming:string", "unified_inventory:bag_medium", "farming:string"},
|
|
|
|
{"farming:string", "unified_inventory:bag_medium", "farming:string"},
|
2016-08-06 17:44:37 +02:00
|
|
|
},
|
|
|
|
})
|
|
|
|
end
|