mirror of
https://github.com/minetest-mods/i3.git
synced 2024-12-25 18:30:38 +01:00
Clean bag code (warning: break backward compat)
This commit is contained in:
parent
88b1e56c87
commit
0cedd16efe
4
init.lua
4
init.lua
@ -12,6 +12,7 @@ i3 = {
|
||||
INV_SIZE = 4*9,
|
||||
HOTBAR_LEN = 9,
|
||||
ITEM_BTN_SIZE = 1.1,
|
||||
DROP_BAG_ON_DIE = true,
|
||||
MIN_FORMSPEC_VERSION = 4,
|
||||
SAVE_INTERVAL = 600, -- Player data save interval (in seconds)
|
||||
|
||||
@ -24,9 +25,8 @@ i3 = {
|
||||
},
|
||||
|
||||
META_SAVES = {
|
||||
bag = true,
|
||||
home = true,
|
||||
bag_item = true,
|
||||
bag_size = true,
|
||||
waypoints = true,
|
||||
inv_items = true,
|
||||
drop_items = true,
|
||||
|
35
src/bags.lua
35
src/bags.lua
@ -1,14 +1,8 @@
|
||||
local set_fs = i3.set_fs
|
||||
local ItemStack = ItemStack
|
||||
local S, ES, fmt, msg, slz, dslz = i3.get("S", "ES", "fmt", "msg", "slz", "dslz")
|
||||
local play_sound, create_inventory = i3.get("play_sound", "create_inventory")
|
||||
|
||||
local function get_content_inv(name)
|
||||
return core.get_inventory {
|
||||
type = "detached",
|
||||
name = fmt("i3_bag_content_%s", name)
|
||||
}
|
||||
end
|
||||
local get_group, play_sound, get_detached_inv, create_inventory =
|
||||
i3.get("get_group", "play_sound", "get_detached_inv", "create_inventory")
|
||||
|
||||
local function get_content(content)
|
||||
local t = {}
|
||||
@ -27,7 +21,7 @@ local function init_bags(player)
|
||||
local bag = create_inventory(fmt("i3_bag_%s", name), {
|
||||
allow_put = function(inv, _, _, stack)
|
||||
local empty = inv:is_empty"main"
|
||||
local item_group = core.get_item_group(stack:get_name(), "bag")
|
||||
local item_group = get_group(stack:get_name(), "bag")
|
||||
|
||||
if empty and item_group > 0 and item_group <= 4 then
|
||||
return 1
|
||||
@ -43,14 +37,13 @@ local function init_bags(player)
|
||||
end,
|
||||
|
||||
on_put = function(_, _, _, stack)
|
||||
data.bag_item = stack:to_string()
|
||||
data.bag_size = core.get_item_group(stack:get_name(), "bag")
|
||||
data.bag = stack:to_string()
|
||||
|
||||
local meta = stack:get_meta()
|
||||
local content = dslz(meta:get_string"content")
|
||||
|
||||
if content then
|
||||
local inv = get_content_inv(name)
|
||||
local inv = get_detached_inv("bag_content", name)
|
||||
inv:set_list("main", get_content(content))
|
||||
end
|
||||
|
||||
@ -58,10 +51,8 @@ local function init_bags(player)
|
||||
end,
|
||||
|
||||
on_take = function()
|
||||
data.bag_item = nil
|
||||
data.bag_size = nil
|
||||
|
||||
local content = get_content_inv(name)
|
||||
data.bag = nil
|
||||
local content = get_detached_inv("bag_content", name)
|
||||
content:set_list("main", {})
|
||||
|
||||
set_fs(player)
|
||||
@ -70,8 +61,8 @@ local function init_bags(player)
|
||||
|
||||
bag:set_size("main", 1)
|
||||
|
||||
if data.bag_item then
|
||||
bag:set_list("main", get_content{data.bag_item})
|
||||
if data.bag then
|
||||
bag:set_list("main", get_content{data.bag})
|
||||
end
|
||||
|
||||
local function save_content(inv)
|
||||
@ -94,13 +85,15 @@ local function init_bags(player)
|
||||
end
|
||||
end
|
||||
|
||||
local percent = fmt("%d", (c * 100) / (data.bag_size * 4))
|
||||
local bag_size = get_group(bagstack:get_name(), "bag")
|
||||
local percent = fmt("%d", (c * 100) / (bag_size * 4))
|
||||
|
||||
meta:set_string("description", ES("@1 (@2% full)", bagstack:get_description(), percent))
|
||||
meta:set_string("content", slz(t))
|
||||
end
|
||||
|
||||
bag:set_stack("main", 1, bagstack)
|
||||
data.bag_item = bagstack:to_string()
|
||||
data.bag = bagstack:to_string()
|
||||
|
||||
set_fs(player)
|
||||
end
|
||||
@ -113,7 +106,7 @@ local function init_bags(player)
|
||||
|
||||
bag_content:set_size("main", 4*4)
|
||||
|
||||
if data.bag_item then
|
||||
if data.bag then
|
||||
local meta = bag:get_stack("main", 1):get_meta()
|
||||
local content = dslz(meta:get_string"content")
|
||||
|
||||
|
@ -9,12 +9,12 @@ local fmt, find, match, sub, lower, split = i3.get("fmt", "find", "match", "sub"
|
||||
local vec_new, vec_eq, vec_round = i3.get("vec_new", "vec_eq", "vec_round")
|
||||
local sort, copy, insert, remove, indexof = i3.get("sort", "copy", "insert", "remove", "indexof")
|
||||
|
||||
local msg, is_fav, pos_to_str, str_to_pos, add_hud_waypoint, play_sound =
|
||||
i3.get("msg", "is_fav", "pos_to_str", "str_to_pos", "add_hud_waypoint", "play_sound")
|
||||
local search, get_sorting_idx, sort_inventory, sort_by_category, get_recipes =
|
||||
i3.get("search", "get_sorting_idx", "sort_inventory", "sort_by_category", "get_recipes")
|
||||
local show_item, get_stack, craft_stack, clean_name, compressible, check_privs, safe_teleport =
|
||||
i3.get("show_item", "get_stack", "craft_stack", "clean_name", "compressible", "check_privs", "safe_teleport")
|
||||
local msg, is_fav, pos_to_str, str_to_pos, add_hud_waypoint, play_sound, spawn_item =
|
||||
i3.get("msg", "is_fav", "pos_to_str", "str_to_pos", "add_hud_waypoint", "play_sound", "spawn_item")
|
||||
local search, get_sorting_idx, sort_inventory, sort_by_category, get_recipes, get_detached_inv =
|
||||
i3.get("search", "get_sorting_idx", "sort_inventory", "sort_by_category", "get_recipes", "get_detached_inv")
|
||||
local valid_item, get_stack, craft_stack, clean_name, compressible, check_privs, safe_teleport =
|
||||
i3.get("valid_item", "get_stack", "craft_stack", "clean_name", "compressible", "check_privs", "safe_teleport")
|
||||
|
||||
local function reset_data(data)
|
||||
data.filter = ""
|
||||
@ -258,7 +258,7 @@ local function select_item(player, data, _f)
|
||||
local i = 1
|
||||
|
||||
for _, v in ipairs(items) do
|
||||
if show_item(reg_items[clean_name(v)]) then
|
||||
if valid_item(reg_items[clean_name(v)]) then
|
||||
insert(data.alt_items, idx + i, v)
|
||||
i = i + 1
|
||||
end
|
||||
@ -451,6 +451,18 @@ core.register_on_dieplayer(function(player)
|
||||
local data = i3.data[name]
|
||||
if not data then return end
|
||||
|
||||
if i3.DROP_BAG_ON_DIE then
|
||||
local bagstack = ItemStack(data.bag)
|
||||
spawn_item(player, bagstack)
|
||||
end
|
||||
|
||||
data.bag = nil
|
||||
local bag = get_detached_inv("bag", name)
|
||||
local content = get_detached_inv("bag_content", name)
|
||||
|
||||
bag:set_list("main", {})
|
||||
content:set_list("main", {})
|
||||
|
||||
set_fs(player)
|
||||
end)
|
||||
|
||||
|
@ -226,7 +226,7 @@ local function item_has_groups(item_groups, groups)
|
||||
return true
|
||||
end
|
||||
|
||||
local function show_item(def)
|
||||
local function valid_item(def)
|
||||
return def and def.groups.not_in_creative_inventory ~= 1 and
|
||||
def.description and def.description ~= ""
|
||||
end
|
||||
@ -237,7 +237,7 @@ local function groups_to_items(groups, get_all)
|
||||
local stereotype = i3.group_stereotypes[group]
|
||||
local def = core.registered_items[stereotype]
|
||||
|
||||
if show_item(def) then
|
||||
if valid_item(def) then
|
||||
return stereotype
|
||||
end
|
||||
end
|
||||
@ -245,7 +245,7 @@ local function groups_to_items(groups, get_all)
|
||||
local names = {}
|
||||
|
||||
for name, def in pairs(core.registered_items) do
|
||||
if show_item(def) and item_has_groups(def.groups, groups) then
|
||||
if valid_item(def) and item_has_groups(def.groups, groups) then
|
||||
if get_all then
|
||||
insert(names, name)
|
||||
else
|
||||
@ -559,6 +559,13 @@ local function add_hud_waypoint(player, name, pos, color)
|
||||
}
|
||||
end
|
||||
|
||||
local function get_detached_inv(name, player_name)
|
||||
return core.get_inventory {
|
||||
type = "detached",
|
||||
name = fmt("i3_%s_%s", name, player_name)
|
||||
}
|
||||
end
|
||||
|
||||
local function createunpack(n)
|
||||
local ret = {"local t = ... return "}
|
||||
|
||||
@ -612,9 +619,7 @@ local _ = {
|
||||
msg = msg,
|
||||
|
||||
-- Misc. functions
|
||||
get_stack = get_stack,
|
||||
craft_stack = craft_stack,
|
||||
show_item = show_item,
|
||||
valid_item = valid_item,
|
||||
spawn_item = spawn_item,
|
||||
clean_name = clean_name,
|
||||
play_sound = play_sound,
|
||||
@ -626,9 +631,15 @@ local _ = {
|
||||
slz = core.serialize,
|
||||
dslz = core.deserialize,
|
||||
ESC = core.formspec_escape,
|
||||
get_group = core.get_item_group,
|
||||
pos_to_str = core.pos_to_string,
|
||||
str_to_pos = core.string_to_pos,
|
||||
check_privs = core.check_player_privs,
|
||||
|
||||
-- Inventory
|
||||
get_stack = get_stack,
|
||||
craft_stack = craft_stack,
|
||||
get_detached_inv = get_detached_inv,
|
||||
create_inventory = core.create_detached_inventory,
|
||||
|
||||
-- Registered items
|
||||
|
21
src/gui.lua
21
src/gui.lua
@ -12,7 +12,8 @@ local reg_items, reg_tools, reg_entities = i3.get("reg_items", "reg_tools", "reg
|
||||
local maxn, sort, concat, copy, insert, remove, unpack =
|
||||
i3.get("maxn", "sort", "concat", "copy", "insert", "remove", "unpack")
|
||||
|
||||
local true_str, is_fav, is_num = i3.get("true_str", "is_fav", "is_num")
|
||||
local true_str, is_fav, is_num, get_group =
|
||||
i3.get("true_str", "is_fav", "is_num", "get_group")
|
||||
local groups_to_items, compression_active, compressible =
|
||||
i3.get("groups_to_items", "compression_active", "compressible")
|
||||
local get_sorting_idx, is_group, extract_groups, item_has_groups =
|
||||
@ -285,7 +286,7 @@ local function get_waypoint_fs(fs, data, player, yextra, ctn_len)
|
||||
fs("style_type[label;font_size=16]")
|
||||
end
|
||||
|
||||
local function get_container(fs, data, player, yoffset, ctn_len, award_list, awards_unlocked, award_list_nb)
|
||||
local function get_container(fs, data, player, yoffset, ctn_len, award_list, awards_unlocked, award_list_nb, bag_size)
|
||||
local name = data.player_name
|
||||
local esc_name = ESC(name)
|
||||
|
||||
@ -345,7 +346,7 @@ local function get_container(fs, data, player, yoffset, ctn_len, award_list, awa
|
||||
|
||||
if not inv:is_empty"main" then
|
||||
local v = {{1.9, 2, 0.12}, {3.05, 5, 0.06}, {4.2, 10}, {4.75, 10}}
|
||||
local h, m, yy = unpack(v[data.bag_size])
|
||||
local h, m, yy = unpack(v[bag_size])
|
||||
|
||||
fs("image", 0.5, yextra + 1.85, 0.6, 0.6, PNG.arrow_content)
|
||||
fs(fmt("style[bg_content;bgimg=%s;fgimg=i3_blank.png;bgimg_middle=10,%u;sound=]",
|
||||
@ -356,13 +357,12 @@ local function get_container(fs, data, player, yoffset, ctn_len, award_list, awa
|
||||
|
||||
local x, size, spacing = 1.45, 0.9, 0.12
|
||||
|
||||
if data.bag_size == 4 then
|
||||
if bag_size == 4 then
|
||||
x, size, spacing = 1.7, 0.8, 0.1
|
||||
end
|
||||
|
||||
fs(fmt("style_type[list;size=%f;spacing=%f]", size, spacing))
|
||||
fs(fmt("list[detached:i3_bag_content_%s;main;%f,%f;4,%u;]",
|
||||
esc_name, x, yextra + 1.3, data.bag_size))
|
||||
fs(fmt("list[detached:i3_bag_content_%s;main;%f,%f;4,%u;]", esc_name, x, yextra + 1.3, bag_size))
|
||||
fs("style_type[list;size=1;spacing=0.15]")
|
||||
end
|
||||
|
||||
@ -545,9 +545,10 @@ local function get_inventory_fs(player, data, fs)
|
||||
|
||||
local awards_unlocked, award_list, award_list_nb = 0
|
||||
local max_val = damage_enabled and 12 or 7
|
||||
local bag_size = get_group(ItemStack(data.bag):get_name(), "bag")
|
||||
|
||||
if data.subcat == 1 and data.bag_size then
|
||||
max_val = max_val + min(32, 6 + ((data.bag_size - 1) * 10))
|
||||
if data.subcat == 1 and bag_size > 0 then
|
||||
max_val = max_val + min(32, 6 + ((bag_size - 1) * 10))
|
||||
|
||||
elseif i3.modules.armor and data.subcat == 2 then
|
||||
if data.scrbar_inv >= max_val then
|
||||
@ -580,14 +581,14 @@ local function get_inventory_fs(player, data, fs)
|
||||
end
|
||||
|
||||
fs(fmt([[
|
||||
scrollbaroptions[arrows=hide;thumbsize=%u;max=%u]
|
||||
scrollbaroptions[arrows=hide;thumbsize=%d;max=%d]
|
||||
scrollbar[%f,0.2;0.2,%f;vertical;scrbar_inv;%u]
|
||||
scrollbaroptions[arrows=default;thumbsize=0;max=1000]
|
||||
]],
|
||||
(max_val * 4) / 12, max_val, 9.8, ctn_hgt, data.scrbar_inv))
|
||||
|
||||
fs(fmt("scroll_container[3.9,0.2;%f,%f;scrbar_inv;vertical]", ctn_len, ctn_hgt))
|
||||
get_container(fs, data, player, yoffset, ctn_len, award_list, awards_unlocked, award_list_nb)
|
||||
get_container(fs, data, player, yoffset, ctn_len, award_list, awards_unlocked, award_list_nb, bag_size)
|
||||
fs("scroll_container_end[]")
|
||||
|
||||
local btn = {
|
||||
|
@ -6,8 +6,8 @@ local maxn, copy, insert, sort, match = i3.get("maxn", "copy", "insert", "sort",
|
||||
|
||||
local is_group, extract_groups, item_has_groups, groups_to_items =
|
||||
i3.get("is_group", "extract_groups", "item_has_groups", "groups_to_items")
|
||||
local true_str, is_table, show_item, table_merge, table_replace, rcp_eq =
|
||||
i3.get("true_str", "is_table", "show_item", "table_merge", "table_replace", "rcp_eq")
|
||||
local true_str, is_table, valid_item, table_merge, table_replace, rcp_eq =
|
||||
i3.get("true_str", "is_table", "valid_item", "table_merge", "table_replace", "rcp_eq")
|
||||
|
||||
local function get_burntime(item)
|
||||
return core.get_craft_result{method = "fuel", items = {item}}.time
|
||||
@ -30,7 +30,7 @@ local function get_item_usages(item, recipe, added)
|
||||
|
||||
if groups then
|
||||
for name, def in pairs(reg_items) do
|
||||
if not added[name] and show_item(def) and item_has_groups(def.groups, groups) then
|
||||
if not added[name] and valid_item(def) and item_has_groups(def.groups, groups) then
|
||||
local usage = copy(recipe)
|
||||
table_replace(usage.items, item, name)
|
||||
|
||||
@ -40,7 +40,7 @@ local function get_item_usages(item, recipe, added)
|
||||
added[name] = true
|
||||
end
|
||||
end
|
||||
elseif show_item(reg_items[item]) then
|
||||
elseif valid_item(reg_items[item]) then
|
||||
i3.usages_cache[item] = i3.usages_cache[item] or {}
|
||||
insert(i3.usages_cache[item], 1, recipe)
|
||||
end
|
||||
@ -254,7 +254,7 @@ local function init_recipes()
|
||||
local _select, _preselect = {}, {}
|
||||
|
||||
for name, def in pairs(reg_items) do
|
||||
if name ~= "" and show_item(def) then
|
||||
if name ~= "" and valid_item(def) then
|
||||
cache_drops(name, def.drop)
|
||||
cache_fuel(name)
|
||||
cache_recipes(name)
|
||||
|
Loading…
Reference in New Issue
Block a user