Bags can have custom names

This commit is contained in:
Jean-Patrick Guerrero 2021-11-30 02:28:11 +01:00
parent 94a86fc0c4
commit 95b0434f95
7 changed files with 71 additions and 17 deletions

View File

@ -1,6 +1,7 @@
local set_fs = i3.set_fs
IMPORT("S", "ES", "fmt", "msg", "slz", "dslz", "ItemStack")
IMPORT("get_bag_description", "ItemStack")
IMPORT("S", "ES", "fmt", "msg", "slz", "dslz")
IMPORT("get_group", "play_sound", "get_detached_inv", "create_inventory")
local function get_content(content)
@ -51,6 +52,8 @@ local function init_bags(player)
on_take = function()
data.bag = nil
data.bag_rename = nil
local content = get_detached_inv("bag_content", name)
content:set_list("main", {})
@ -67,9 +70,10 @@ local function init_bags(player)
local function save_content(inv)
local bagstack = bag:get_stack("main", 1)
local meta = bagstack:get_meta()
meta:set_string("description", "")
local desc = get_bag_description(data, bagstack)
if inv:is_empty"main" then
meta:set_string("description", desc)
meta:set_string("content", "")
else
local list = inv:get_list"main"
@ -87,7 +91,7 @@ local function init_bags(player)
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("description", ES("@1 (@2% full)", desc, percent))
meta:set_string("content", slz(t))
end

View File

@ -3,8 +3,8 @@ local set_fs = i3.set_fs
IMPORT("vec_eq", "vec_round")
IMPORT("reg_items", "reg_aliases")
IMPORT("S", "min", "random", "ItemStack")
IMPORT("sort", "copy", "insert", "remove", "indexof")
IMPORT("S", "min", "random", "translate", "ItemStack")
IMPORT("fmt", "find", "match", "sub", "lower", "split")
IMPORT("msg", "is_fav", "pos_to_str", "str_to_pos", "add_hud_waypoint", "play_sound", "spawn_item")
IMPORT("search", "get_sorting_idx", "sort_inventory", "sort_by_category", "get_recipes", "get_detached_inv")
@ -118,6 +118,7 @@ i3.new_tab("inventory", {
data.confirm_trash = nil
data.show_settings = nil
data.waypoint_see = nil
data.bag_rename = nil
elseif fields.trash then
data.show_settings = nil
@ -175,6 +176,23 @@ i3.new_tab("inventory", {
elseif fields.set_home then
data.home = pos_to_str(player:get_pos(), 1)
elseif fields.bag_rename then
data.bag_rename = true
elseif fields.confirm_rename then
local bag = get_detached_inv("bag", name)
local bagstack = bag:get_stack("main", 1)
local meta = bagstack:get_meta()
local desc = translate(data.lang_code, bagstack:get_description())
local str = desc:match("(.*)%(+") or desc
local newname = fields.bag_newname:gsub("([%(%)])", "")
meta:set_string("description", desc:gsub(str:trim(), newname))
bag:set_stack("main", 1, bagstack)
data.bag = bagstack:to_string()
data.bag_rename = nil
elseif sb_inv and sub(sb_inv, 1, 3) == "CHG" then
data.scrbar_inv = tonumber(match(sb_inv, "%d+"))
return

View File

@ -1,11 +1,13 @@
local ItemStack = ItemStack
local loadstring = loadstring
local ESC = core.formspec_escape
local translate = core.get_translated_string
local vec_new, vec_add, vec_mul = vector.new, vector.add, vector.multiply
local sort, concat, insert = table.sort, table.concat, table.insert
local min, floor, ceil = math.min, math.floor, math.ceil
local fmt, find, match, gmatch, sub, split, lower =
string.format, string.find, string.match, string.gmatch, string.sub, string.split, string.lower
local fmt, find, match, gmatch, sub, split, lower, upper =
string.format, string.find, string.match, string.gmatch,
string.sub, string.split, string.lower, string.upper
local old_is_creative_enabled = core.is_creative_enabled
@ -63,6 +65,18 @@ local function round(num, decimal)
return floor(num * mul + 0.5) / mul
end
local function toupper(str)
return str:gsub("%f[%w]%l", upper):gsub("_", " ")
end
local function get_bag_description(data, stack)
local desc = translate(data.lang_code, stack:get_description())
desc = desc:match("(.*)%(+") or desc
desc = ESC(toupper(desc:trim()))
return desc
end
local function search(data)
reset_compression(data)
@ -87,7 +101,7 @@ local function search(data)
for i = 1, #data.items_raw do
local item = data.items_raw[i]
local def = core.registered_items[item]
local desc = lower(core.get_translated_string(data.lang_code, def and def.description)) or ""
local desc = lower(translate(data.lang_code, def and def.description)) or ""
local search_in = fmt("%s %s", item, desc)
local temp, j, to_add = {}, 1
@ -648,6 +662,7 @@ local _ = {
get_stack = get_stack,
craft_stack = craft_stack,
get_detached_inv = get_detached_inv,
get_bag_description = get_bag_description,
create_inventory = core.create_detached_inventory,
-- Registered items
@ -672,6 +687,7 @@ local _ = {
split = string.split,
match = string.match,
gmatch = string.gmatch,
toupper = toupper,
-- Table
maxn = table.maxn,

View File

@ -9,13 +9,14 @@ local VoxelArea, VoxelManip = VoxelArea, VoxelManip
IMPORT("clr", "ESC", "check_privs")
IMPORT("find", "match", "sub", "upper")
IMPORT("vec_new", "vec_sub", "vec_round")
IMPORT("S", "ES", "translate", "ItemStack")
IMPORT("min", "max", "floor", "ceil", "round")
IMPORT("reg_items", "reg_tools", "reg_entities")
IMPORT("get_bag_description", "get_detached_inv")
IMPORT("S", "ES", "translate", "ItemStack", "toupper")
IMPORT("groups_to_items", "compression_active", "compressible")
IMPORT("true_str", "is_fav", "is_num", "get_group", "str_to_pos")
IMPORT("maxn", "sort", "concat", "copy", "insert", "remove", "unpack")
IMPORT("get_sorting_idx", "is_group", "extract_groups", "item_has_groups", "get_detached_inv")
IMPORT("get_sorting_idx", "is_group", "extract_groups", "item_has_groups")
local function fmt(elem, ...)
if not fs_elements[elem] then
@ -34,10 +35,6 @@ local function weird_desc(str)
return not true_str(str) or find(str, "\n") or not find(str, "%u")
end
local function toupper(str)
return str:gsub("%f[%w]%l", upper):gsub("_", " ")
end
local function snip(str, limit)
return #str > limit and fmt("%s...", sub(str, 1, limit - 3)) or str
end
@ -405,12 +402,27 @@ local function get_container(fs, data, player, yoffset, ctn_len, award_list, awa
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 bagstack = bag:get_stack("main", 1)
local desc = get_bag_description(data, bagstack)
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=]",
PNG.bg_content, m))
fs("image_button", 1.1, yextra + 0.5 + (yy or 0), 4.75, h, "", "bg_content", "")
fs("hypertext", 1.3, yextra + 0.8, 4.3, 0.6, "content",
fmt("<global size=16><center><b>%s</b></center>", ES"Content"))
if not data.bag_rename then
fs("hypertext", 1.3, yextra + 0.8, 4.3, 0.6, "content",
fmt("<global size=16><center><b>%s</b></center>", desc))
fs("image_button", 5.22, yextra + 0.835, 0.25, 0.25, "", "bag_rename", "")
fs(fmt("tooltip[%s;%s]", "bag_rename", ES"Rename bag"))
else
fs("box", 1.7, yextra + 0.82, 2.6, 0.4, "#707070")
fs(fmt("field[1.8,%f;2.5,0.4;bag_newname;;%s]", yextra + 0.82, desc),
"field_close_on_enter[bag_newname;false]")
fs("hypertext", 4.4, yextra + 0.88, 0.8, 0.5, "confirm_rename",
fmt("<global size=16><tag name=action color=#fff hovercolor=%s>" ..
"<center><b><action name=ok>OK</action></b></center>", colors.yellow))
end
local x, size, spacing = 1.45, 0.9, 0.12

View File

@ -39,6 +39,7 @@ local PNG = {
exit = "i3_exit.png",
home = "i3_home.png",
flag = "i3_flag.png",
edit = "i3_edit.png",
cancel_hover = "i3_cancel.png^\\[brighten",
search_hover = "i3_search.png^\\[brighten",
@ -61,6 +62,7 @@ local PNG = {
refresh_hover = "i3_refresh.png^\\[brighten",
exit_hover = "i3_exit.png^\\[brighten",
home_hover = "i3_home.png^\\[brighten",
edit_hover = "i3_edit.png^\\[brighten",
}
local styles = string.format([[
@ -82,6 +84,7 @@ local styles = string.format([[
style[prev_usage;fgimg=%s;fgimg_hovered=%s]
style[next_usage;fgimg=%s;fgimg_hovered=%s]
style[waypoint_add;fgimg=%s;fgimg_hovered=%s;content_offset=0]
style[bag_rename;fgimg=%s;fgimg_hovered=%s;content_offset=0]
style[btn_bag,btn_armor,btn_skins;font=bold;font_size=18;content_offset=0;sound=i3_click]
style[craft_rcp,craft_usg;noclip=true;font_size=16;sound=i3_craft;
bgimg=i3_btn9.png;bgimg_hovered=i3_btn9_hovered.png;
@ -101,7 +104,8 @@ PNG.prev, PNG.prev_hover,
PNG.next, PNG.next_hover,
PNG.prev, PNG.prev_hover,
PNG.next, PNG.next_hover,
PNG.add, PNG.add_hover)
PNG.add, PNG.add_hover,
PNG.edit, PNG.edit_hover)
local fs_elements = {
label = "label[%f,%f;%s]",

BIN
textures/i3_edit.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 749 B

Binary file not shown.

Before

Width:  |  Height:  |  Size: 818 B

After

Width:  |  Height:  |  Size: 745 B