Compare commits

..

1 Commits
1.4.1 ... 1.4

Author SHA1 Message Date
6d2658ac25 Some cleaning 2021-11-22 17:42:44 +01:00
6 changed files with 75 additions and 81 deletions

View File

@ -1,4 +1,4 @@
local modpath = core.get_modpath"i3"
local modpath = core.get_modpath "i3"
local function lf(path)
return loadfile(modpath .. path)
@ -12,7 +12,6 @@ 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)
@ -25,8 +24,9 @@ i3 = {
},
META_SAVES = {
bag = true,
home = true,
bag_item = true,
bag_size = true,
waypoints = true,
inv_items = true,
drop_items = true,
@ -76,7 +76,7 @@ local slz, dslz, copy = i3.get("slz", "dslz", "copy")
local str_to_pos, add_hud_waypoint = i3.get("str_to_pos", "add_hud_waypoint")
local set_fs = i3.set_fs
i3.data = dslz(storage:get_string"data") or {}
i3.data = dslz(storage:get_string "data") or {}
local init_bags = i3.files.bags()
local init_inventories = i3.files.detached()
@ -185,7 +185,7 @@ end
local function init_hudbar(player)
core.after(0, function()
player:hud_set_hotbar_itemcount(i3.HOTBAR_LEN)
player:hud_set_hotbar_image"i3_hotbar.png"
player:hud_set_hotbar_image("i3_hotbar.png")
end)
end

View File

@ -1,8 +1,14 @@
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 get_group, play_sound, get_detached_inv, create_inventory =
i3.get("get_group", "play_sound", "get_detached_inv", "create_inventory")
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 function get_content(content)
local t = {}
@ -21,7 +27,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 = get_group(stack:get_name(), "bag")
local item_group = core.get_item_group(stack:get_name(), "bag")
if empty and item_group > 0 and item_group <= 4 then
return 1
@ -37,13 +43,14 @@ local function init_bags(player)
end,
on_put = function(_, _, _, stack)
data.bag = stack:to_string()
data.bag_item = stack:to_string()
data.bag_size = core.get_item_group(stack:get_name(), "bag")
local meta = stack:get_meta()
local content = dslz(meta:get_string"content")
if content then
local inv = get_detached_inv("bag_content", name)
local inv = get_content_inv(name)
inv:set_list("main", get_content(content))
end
@ -51,8 +58,10 @@ local function init_bags(player)
end,
on_take = function()
data.bag = nil
local content = get_detached_inv("bag_content", name)
data.bag_item = nil
data.bag_size = nil
local content = get_content_inv(name)
content:set_list("main", {})
set_fs(player)
@ -61,39 +70,48 @@ local function init_bags(player)
bag:set_size("main", 1)
if data.bag then
bag:set_list("main", get_content{data.bag})
if data.bag_item then
bag:set_list("main", get_content{data.bag_item})
end
local function save_content(inv)
local bagstack = bag:get_stack("main", 1)
local meta = bagstack:get_meta()
meta:set_string("description", "")
if inv:is_empty"main" then
if inv:is_empty("main") then
meta:set_string("description", "")
meta:set_string("content", "")
else
local list = inv:get_list"main"
local t, c = {}, 0
local t = {}
for i = 1, #list do
local stack = list[i]
if not stack:is_empty() then
c = c + 1
t[i] = stack:to_string()
end
end
local bag_size = get_group(bagstack:get_name(), "bag")
local percent = fmt("%d", (c * 100) / (bag_size * 4))
local function count_items()
local c = 0
for _ in pairs(t) do
c = c + 1
end
return c
end
local percent = fmt("%.1f", (count_items() * 100) / (data.bag_size * 4))
meta:set_string("description", "")
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 = bagstack:to_string()
data.bag_item = bagstack:to_string()
set_fs(player)
end
@ -106,7 +124,7 @@ local function init_bags(player)
bag_content:set_size("main", 4*4)
if data.bag then
if data.bag_item then
local meta = bag:get_stack("main", 1):get_meta()
local content = dslz(meta:get_string"content")

View File

@ -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, 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 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 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 valid_item(reg_items[clean_name(v)]) then
if show_item(reg_items[clean_name(v)]) then
insert(data.alt_items, idx + i, v)
i = i + 1
end
@ -451,18 +451,6 @@ 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)

View File

@ -226,7 +226,7 @@ local function item_has_groups(item_groups, groups)
return true
end
local function valid_item(def)
local function show_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 valid_item(def) then
if show_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 valid_item(def) and item_has_groups(def.groups, groups) then
if show_item(def) and item_has_groups(def.groups, groups) then
if get_all then
insert(names, name)
else
@ -559,13 +559,6 @@ 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 "}
@ -619,7 +612,9 @@ local _ = {
msg = msg,
-- Misc. functions
valid_item = valid_item,
get_stack = get_stack,
craft_stack = craft_stack,
show_item = show_item,
spawn_item = spawn_item,
clean_name = clean_name,
play_sound = play_sound,
@ -631,15 +626,9 @@ 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

View File

@ -12,8 +12,7 @@ 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, get_group =
i3.get("true_str", "is_fav", "is_num", "get_group")
local true_str, is_fav, is_num = i3.get("true_str", "is_fav", "is_num")
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 =
@ -156,7 +155,7 @@ local function add_subtitle(fs, name, y, ctn_len, font_size, sep, label)
end
local function get_award_list(data, fs, ctn_len, yextra, award_list, awards_unlocked, award_list_nb)
local percent = fmt("%.1f%%", (awards_unlocked * 100) / award_list_nb):gsub("%.0", "")
local percent = fmt("%.1f%%", (awards_unlocked * 100) / award_list_nb):gsub(".0", "")
add_subtitle(fs, "awards", yextra, ctn_len, 18, false,
ES("Achievements: @1 of @2 (@3)", awards_unlocked, award_list_nb, percent))
@ -286,7 +285,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, bag_size)
local function get_container(fs, data, player, yoffset, ctn_len, award_list, awards_unlocked, award_list_nb)
local name = data.player_name
local esc_name = ESC(name)
@ -346,7 +345,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[bag_size])
local h, m, yy = unpack(v[data.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=]",
@ -357,12 +356,13 @@ local function get_container(fs, data, player, yoffset, ctn_len, award_list, awa
local x, size, spacing = 1.45, 0.9, 0.12
if bag_size == 4 then
if data.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, bag_size))
fs(fmt("list[detached:i3_bag_content_%s;main;%f,%f;4,%u;]",
esc_name, x, yextra + 1.3, data.bag_size))
fs("style_type[list;size=1;spacing=0.15]")
end
@ -441,10 +441,10 @@ local function show_popup(fs, data)
local show_misc = data.show_setting == "misc"
fs(fmt("style[setting_home;textcolor=%s;font=bold;sound=i3_click]",
show_home and colors.yellow or "#fff"),
fmt("style[setting_sorting;textcolor=%s;font=bold;sound=i3_click]",
show_sorting and colors.yellow or "#fff"),
fmt("style[setting_misc;textcolor=%s;font=bold;sound=i3_click]",
show_home and colors.yellow or "#fff"))
fs(fmt("style[setting_sorting;textcolor=%s;font=bold;sound=i3_click]",
show_sorting and colors.yellow or "#fff"))
fs(fmt("style[setting_misc;textcolor=%s;font=bold;sound=i3_click]",
show_misc and colors.yellow or "#fff"))
fs("button", 2.2, 9.25, 1.8, 0.55, "setting_home", "Home")
@ -469,8 +469,8 @@ local function show_popup(fs, data)
elseif show_sorting then
fs("button", 2.1, 9.7, 6, 0.8, "select_sorting", ES"Select the inventory sorting method:")
fs(fmt("style[prev_sort;fgimg=%s;fgimg_hovered=%s]", PNG.prev, PNG.prev_hover),
fmt("style[next_sort;fgimg=%s;fgimg_hovered=%s]", PNG.next, PNG.next_hover))
fs(fmt("style[prev_sort;fgimg=%s;fgimg_hovered=%s]", PNG.prev, PNG.prev_hover))
fs(fmt("style[next_sort;fgimg=%s;fgimg_hovered=%s]", PNG.next, PNG.next_hover))
fs("image_button", 2.2, 10.6, 0.35, 0.35, "", "prev_sort", "")
fs("image_button", 7.65, 10.6, 0.35, 0.35, "", "next_sort", "")
@ -545,10 +545,9 @@ 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 bag_size > 0 then
max_val = max_val + min(32, 6 + ((bag_size - 1) * 10))
if data.subcat == 1 and data.bag_size then
max_val = max_val + min(32, 6 + ((data.bag_size - 1) * 10))
elseif i3.modules.armor and data.subcat == 2 then
if data.scrbar_inv >= max_val then
@ -581,14 +580,14 @@ local function get_inventory_fs(player, data, fs)
end
fs(fmt([[
scrollbaroptions[arrows=hide;thumbsize=%d;max=%d]
scrollbaroptions[arrows=hide;thumbsize=%u;max=%u]
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, bag_size)
get_container(fs, data, player, yoffset, ctn_len, award_list, awards_unlocked, award_list_nb)
fs("scroll_container_end[]")
local btn = {

View File

@ -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, valid_item, table_merge, table_replace, rcp_eq =
i3.get("true_str", "is_table", "valid_item", "table_merge", "table_replace", "rcp_eq")
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 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 valid_item(def) and item_has_groups(def.groups, groups) then
if not added[name] and show_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 valid_item(reg_items[item]) then
elseif show_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 valid_item(def) then
if name ~= "" and show_item(def) then
cache_drops(name, def.drop)
cache_fuel(name)
cache_recipes(name)