i3 = {} -- Caches local pdata = {} local init_items = {} local searches = {} local recipes_cache = {} local usages_cache = {} local fuel_cache = {} local replacements = {fuel = {}} local toolrepair local tabs = {} local progressive_mode = core.settings:get_bool "i3_progressive_mode" local damage_enabled = core.settings:get_bool "enable_damage" local __3darmor, __skinsdb, __awards local sfinv, unified_inventory, old_unified_inventory_fn local http = core.request_http_api() local singleplayer = core.is_singleplayer() local reg_items = core.registered_items local reg_nodes = core.registered_nodes local reg_craftitems = core.registered_craftitems local reg_tools = core.registered_tools local reg_entities = core.registered_entities local reg_aliases = core.registered_aliases local log = core.log local after = core.after local clr = core.colorize local parse_json = core.parse_json local write_json = core.write_json local chat_send = core.chat_send_player local show_formspec = core.show_formspec local check_privs = core.check_player_privs local globalstep = core.register_globalstep local on_shutdown = core.register_on_shutdown local get_players = core.get_connected_players local get_craft_result = core.get_craft_result local translate = minetest.get_translated_string local on_joinplayer = core.register_on_joinplayer local get_all_recipes = core.get_all_craft_recipes local slz, dslz = core.serialize, core.deserialize local on_mods_loaded = core.register_on_mods_loaded local on_leaveplayer = core.register_on_leaveplayer local get_player_info = core.get_player_information local create_inventory = core.create_detached_inventory local on_receive_fields = core.register_on_player_receive_fields local ESC = core.formspec_escape local S = core.get_translator "i3" local ES = function(...) return ESC(S(...)) end local maxn, sort, concat, copy, insert, remove = table.maxn, table.sort, table.concat, table.copy, table.insert, table.remove local sprintf, find, gmatch, match, sub, split, upper, lower = string.format, string.find, string.gmatch, string.match, string.sub, string.split, string.upper, string.lower local min, max, floor, ceil, abs = math.min, math.max, math.floor, math.ceil, math.abs local pairs, ipairs, next, type, setmetatable, tonum, unpack, select = pairs, ipairs, next, type, setmetatable, tonumber, unpack, select local vec_add, vec_mul = vector.add, vector.multiply local ROWS = 9 local LINES = 10 local IPP = ROWS * LINES local MAX_FAVS = 6 local ITEM_BTN_SIZE = 1.1 local INV_SIZE = 36 local HOTBAR_COUNT = 9 -- Progressive mode local POLL_FREQ = 0.25 local HUD_TIMER_MAX = 1.5 local MIN_FORMSPEC_VERSION = 4 local META_SAVES = {"bag_size", "skin_id"} local BAG_SIZES = { small = INV_SIZE + 3, medium = INV_SIZE + 6, large = INV_SIZE + 9, } local PNG = { bg = "i3_bg.png", bg_full = "i3_bg_full.png", bar = "i3_bar.png", hotbar = "i3_hotbar.png", search = "i3_search.png", heart = "i3_heart.png", heart_half = "i3_heart_half.png", heart_grey = "i3_heart_grey.png", prev = "i3_next.png^\\[transformFX", next = "i3_next.png", arrow = "i3_arrow.png", trash = "i3_trash.png", sort_az = "i3_sort_az.png", sort_za = "i3_sort_za.png", compress = "i3_compress.png", fire = "i3_fire.png", fire_anim = "i3_fire_anim.png", book = "i3_book.png", sign = "i3_sign.png", cancel = "i3_cancel.png", export = "i3_export.png", slot = "i3_slot.png", tab = "i3_tab.png", tab_top = "i3_tab.png^\\[transformFY", furnace_anim = "i3_furnace_anim.png", cancel_hover = "i3_cancel.png^\\[brighten", search_hover = "i3_search.png^\\[brighten", export_hover = "i3_export.png^\\[brighten", trash_hover = "i3_trash.png^\\[brighten^\\[colorize:#f00:100", compress_hover = "i3_compress.png^\\[brighten", sort_az_hover = "i3_sort_az.png^\\[brighten", sort_za_hover = "i3_sort_za.png^\\[brighten", prev_hover = "i3_next_hover.png^\\[transformFX", next_hover = "i3_next_hover.png", tab_hover = "i3_tab_hover.png", tab_hover_top = "i3_tab_hover.png^\\[transformFY", } local fs_elements = { label = "label[%f,%f;%s]", box = "box[%f,%f;%f,%f;%s]", image = "image[%f,%f;%f,%f;%s]", tooltip = "tooltip[%f,%f;%f,%f;%s]", button = "button[%f,%f;%f,%f;%s;%s]", item_image = "item_image[%f,%f;%f,%f;%s]", hypertext = "hypertext[%f,%f;%f,%f;%s;%s]", bg9 = "background9[%f,%f;%f,%f;%s;false;%u]", scrollbar = "scrollbar[%f,%f;%f,%f;%s;%s;%u]", model = "model[%f,%f;%f,%f;%s;%s;%s;%s;%s;%s;%s]", image_button = "image_button[%f,%f;%f,%f;%s;%s;%s]", animated_image = "animated_image[%f,%f;%f,%f;;%s;%u;%u]", item_image_button = "item_image_button[%f,%f;%f,%f;%s;%s;%s]", } local styles = sprintf([[ style_type[label,field;font_size=16] style_type[image_button;border=false;sound=i3_click] style_type[item_image_button;border=false;bgimg_hovered=%s;sound=i3_click] style[filter;border=false] style[cancel;fgimg=%s;fgimg_hovered=%s;content_offset=0] style[search;fgimg=%s;fgimg_hovered=%s;content_offset=0] style[prev_page;fgimg=%s;fgimg_hovered=%s] style[next_page;fgimg=%s;fgimg_hovered=%s] style[prev_recipe;fgimg=%s;fgimg_hovered=%s] style[next_recipe;fgimg=%s;fgimg_hovered=%s] style[prev_usage;fgimg=%s;fgimg_hovered=%s] style[next_usage;fgimg=%s;fgimg_hovered=%s] style[pagenum,no_item,no_rcp;border=false;font=bold;font_size=18;content_offset=0] style[btn_bag,btn_armor,btn_skins;font=bold;font_size=18;border=false;content_offset=0;sound=i3_click] style[craft_rcp,craft_usg;border=false;noclip=true;font_size=16;sound=i3_craft; bgimg=i3_btn9.png;bgimg_hovered=i3_btn9_hovered.png; bgimg_pressed=i3_btn9_pressed.png;bgimg_middle=4,6] ]], PNG.slot, PNG.cancel, PNG.cancel_hover, PNG.search, PNG.search_hover, PNG.prev, PNG.prev_hover, PNG.next, PNG.next_hover, PNG.prev, PNG.prev_hover, PNG.next, PNG.next_hover, PNG.prev, PNG.prev_hover, PNG.next, PNG.next_hover) local function get_lang_code(info) return info and info.lang_code end local function get_formspec_version(info) return info and info.formspec_version or 1 end local function outdated(name) local fs = sprintf("size[5.8,1.3]image[0,0;1,1;%s]label[1,0;%s]button_exit[2.4,0.8;1,1;;OK]", PNG.book, "Your Minetest client is outdated.\nGet the latest version on minetest.net to use i3") show_formspec(name, "i3", fs) end local old_is_creative_enabled = core.is_creative_enabled function core.is_creative_enabled(name) return check_privs(name, {creative = true}) or old_is_creative_enabled(name) end i3.group_stereotypes = { dye = "dye:white", wool = "wool:white", wood = "default:wood", tree = "default:tree", sand = "default:sand", glass = "default:glass", stick = "default:stick", stone = "default:stone", leaves = "default:leaves", coal = "default:coal_lump", vessel = "vessels:glass_bottle", flower = "flowers:dandelion_yellow", water_bucket = "bucket:bucket_water", mesecon_conductor_craftable = "mesecons:wire_00000000_off", } local group_names = { dye = S"Any dye", coal = S"Any coal", sand = S"Any sand", tree = S"Any tree", wool = S"Any wool", glass = S"Any glass", stick = S"Any stick", stone = S"Any stone", carpet = S"Any carpet", flower = S"Any flower", leaves = S"Any leaves", vessel = S"Any vessel", wood = S"Any wood planks", mushroom = S"Any mushroom", ["color_red,flower"] = S"Any red flower", ["color_blue,flower"] = S"Any blue flower", ["color_black,flower"] = S"Any black flower", ["color_white,flower"] = S"Any white flower", ["color_green,flower"] = S"Any green flower", ["color_orange,flower"] = S"Any orange flower", ["color_yellow,flower"] = S"Any yellow flower", ["color_violet,flower"] = S"Any violet flower", ["color_red,dye"] = S"Any red dye", ["color_blue,dye"] = S"Any blue dye", ["color_grey,dye"] = S"Any grey dye", ["color_pink,dye"] = S"Any pink dye", ["color_cyan,dye"] = S"Any cyan dye", ["color_black,dye"] = S"Any black dye", ["color_white,dye"] = S"Any white dye", ["color_brown,dye"] = S"Any brown dye", ["color_green,dye"] = S"Any green dye", ["color_orange,dye"] = S"Any orange dye", ["color_yellow,dye"] = S"Any yellow dye", ["color_violet,dye"] = S"Any violet dye", ["color_magenta,dye"] = S"Any magenta dye", ["color_dark_grey,dye"] = S"Any dark grey dye", ["color_dark_green,dye"] = S"Any dark green dye", } i3.model_alias = { ["boats:boat"] = {name = "boats:boat", drawtype = "entity"}, ["carts:cart"] = {name = "carts:cart", drawtype = "entity", frames = "0,0"}, ["default:chest"] = {name = "default:chest_open"}, ["default:chest_locked"] = {name = "default:chest_locked_open"}, ["doors:door_wood"] = {name = "doors:door_wood_a"}, ["doors:door_glass"] = {name = "doors:door_glass_a"}, ["doors:door_obsidian_glass"] = {name = "doors:door_obsidian_glass_a"}, ["doors:door_steel"] = {name = "doors:door_steel_a"}, ["xpanes:door_steel_bar"] = {name = "xpanes:door_steel_bar_a"}, } local function err(str) return log("error", str) end local function msg(name, str) return chat_send(name, sprintf("[i3] %s", str)) end local function is_num(x) return type(x) == "number" end local function is_str(x) return type(x) == "string" end local function true_str(str) return is_str(str) and str ~= "" end local function is_table(x) return type(x) == "table" end local function is_func(x) return type(x) == "function" end local function is_group(item) return sub(item, 1, 6) == "group:" end local function fmt(elem, ...) if not fs_elements[elem] then return sprintf(elem, ...) end return sprintf(fs_elements[elem], ...) end local function clean_name(item) if sub(item, 1, 1) == ":" or sub(item, 1, 1) == " " then item = sub(item, 2) end return item end local function array_diff(t1, t2) local hash = {} for i = 1, #t1 do local v = t1[i] hash[v] = true end for i = 1, #t2 do local v = t2[i] hash[v] = nil end local diff, c = {}, 0 for i = 1, #t1 do local v = t1[i] if hash[v] then c = c + 1 diff[c] = v end end return diff end local function table_eq(T1, T2) local avoid_loops = {} local function recurse(t1, t2) if type(t1) ~= type(t2) then return end if not is_table(t1) then return t1 == t2 end if avoid_loops[t1] then return avoid_loops[t1] == t2 end avoid_loops[t1] = t2 local t2k, t2kv = {}, {} for k in pairs(t2) do if is_table(k) then insert(t2kv, k) end t2k[k] = true end for k1, v1 in pairs(t1) do local v2 = t2[k1] if type(k1) == "table" then local ok for i = 1, #t2kv do local tk = t2kv[i] if table_eq(k1, tk) and recurse(v1, t2[tk]) then remove(t2kv, i) t2k[tk] = nil ok = true break end end if not ok then return end else if v2 == nil then return end t2k[k1] = nil if not recurse(v1, v2) then return end end end if next(t2k) then return end return true end return recurse(T1, T2) end local function table_merge(t1, t2, hash) t1 = t1 or {} t2 = t2 or {} if hash then for k, v in pairs(t2) do t1[k] = v end else local c = #t1 for i = 1, #t2 do c = c + 1 t1[c] = t2[i] end end return t1 end local function table_replace(t, val, new) for k, v in pairs(t) do if v == val then t[k] = new end end end local function save_meta(player, entries) local name = player:get_player_name() local data = pdata[name] if not data then return end local meta = player:get_meta() for _, entry in ipairs(entries) do meta:set_string(entry, slz(data[entry])) end end local craft_types = {} function i3.register_craft_type(name, def) if not true_str(name) then return err "i3.register_craft_type: name missing" end if not is_str(def.description) then def.description = "" end craft_types[name] = def end function i3.register_craft(def) local width, c = 0, 0 if true_str(def.url) then if not http then return err(fmt([[i3.register_craft(): Unable to reach %s. No HTTP support for this mod: add it to the `secure.http_mods` or `secure.trusted_mods` setting.]], def.url)) end http.fetch({url = def.url}, function(result) if result.succeeded then local t = parse_json(result.data) if is_table(t) then return i3.register_craft(t) end end end) return end if not is_table(def) or not next(def) then return err "i3.register_craft: craft definition missing" end if #def > 1 then for _, v in pairs(def) do i3.register_craft(v) end return end if def.result then def.output = def.result -- Backward compatibility def.result = nil end if not true_str(def.output) then return err "i3.register_craft: output missing" end if not is_table(def.items) then def.items = {} end if def.grid then if not is_table(def.grid) then def.grid = {} end if not is_table(def.key) then def.key = {} end local cp = copy(def.grid) sort(cp, function(a, b) return #a > #b end) width = #cp[1] for i = 1, #def.grid do while #def.grid[i] < width do def.grid[i] = def.grid[i] .. " " end end for symbol in gmatch(concat(def.grid), ".") do c = c + 1 def.items[c] = def.key[symbol] end else local items, len = def.items, #def.items def.items = {} for i = 1, len do local rlen = #split(items[i], ",") if rlen > width then width = rlen end end for i = 1, len do while #split(items[i], ",") < width do items[i] = fmt("%s,", items[i]) end end for name in gmatch(concat(items, ","), "[%s%w_:]+") do c = c + 1 def.items[c] = clean_name(name) end end local item = match(def.output, "%S+") recipes_cache[item] = recipes_cache[item] or {} def.custom = true def.width = width insert(recipes_cache[item], def) end local recipe_filters = {} function i3.add_recipe_filter(name, f) if not true_str(name) then return err "i3.add_recipe_filter: name missing" elseif not is_func(f) then return err "i3.add_recipe_filter: function missing" end recipe_filters[name] = f end function i3.set_recipe_filter(name, f) if not is_str(name) then return err "i3.set_recipe_filter: name missing" elseif not is_func(f) then return err "i3.set_recipe_filter: function missing" end recipe_filters = {[name] = f} end function i3.delete_recipe_filter(name) recipe_filters[name] = nil end function i3.get_recipe_filters() return recipe_filters end local function apply_recipe_filters(recipes, player) for _, filter in pairs(recipe_filters) do recipes = filter(recipes, player) end return recipes end local search_filters = {} function i3.add_search_filter(name, f) if not true_str(name) then return err "i3.add_search_filter: name missing" elseif not is_func(f) then return err "i3.add_search_filter: function missing" end search_filters[name] = f end function i3.remove_search_filter(name) search_filters[name] = nil end function i3.get_search_filters() return search_filters end 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 local function get_desc(item) if sub(item, 1, 1) == "_" then item = sub(item, 2) end local def = reg_items[item] if def then local desc = ItemStack(item):get_short_description() if true_str(desc) then desc = desc:trim() if not find(desc, "%u") then desc = toupper(desc) end return desc elseif true_str(item) then return toupper(match(item, ":(.*)")) end end return S("Unknown Item (@1)", item) end local function item_has_groups(item_groups, groups) for i = 1, #groups do local group = groups[i] if (item_groups[group] or 0) == 0 then return end end return true end local function extract_groups(str) if sub(str, 1, 6) == "group:" then return split(sub(str, 7), ",") end end local function get_filtered_items(player, data) local items, known, c = {}, 0, 0 for i = 1, #init_items do local item = init_items[i] local recipes = recipes_cache[item] local usages = usages_cache[item] recipes = #apply_recipe_filters(recipes or {}, player) usages = #apply_recipe_filters(usages or {}, player) if recipes > 0 or usages > 0 then c = c + 1 items[c] = item if data then known = known + recipes + usages end end end if data then data.known_recipes = known end return items end local function get_burntime(item) return get_craft_result{method = "fuel", items = {item}}.time end local function cache_fuel(item) local burntime = get_burntime(item) if burntime > 0 then fuel_cache[item] = { type = "fuel", items = {item}, burntime = burntime, replacements = replacements.fuel[item], } end end local function show_item(def) return def and def.groups.not_in_creative_inventory ~= 1 and def.description and def.description ~= "" end local function search(data) local filter = data.filter if searches[filter] then data.items = searches[filter] return end local opt = "^(.-)%+([%w_]+)=([%w_,]+)" local search_filter = next(search_filters) and match(filter, opt) local filters = {} if search_filter then for filter_name, values in gmatch(filter, sub(opt, 6)) do if search_filters[filter_name] then values = split(values, ",") filters[filter_name] = values end end end local filtered_list, c = {}, 0 for i = 1, #data.items_raw do local item = data.items_raw[i] local def = reg_items[item] 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 if search_filter then for filter_name, values in pairs(filters) do if values then local func = search_filters[filter_name] to_add = (j > 1 and temp[item] or j == 1) and func(item, values) and (search_filter == "" or find(search_in, search_filter, 1, true)) if to_add then temp[item] = true end j = j + 1 end end else to_add = find(search_in, filter, 1, true) end if to_add then c = c + 1 filtered_list[c] = item end end if not next(recipe_filters) then -- Cache the results only if searched 2 times if searches[filter] == nil then searches[filter] = false else searches[filter] = filtered_list end end data.items = filtered_list end local function get_item_usages(item, recipe, added) local groups = extract_groups(item) 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 local usage = copy(recipe) table_replace(usage.items, item, name) usages_cache[name] = usages_cache[name] or {} insert(usages_cache[name], 1, usage) added[name] = true end end elseif show_item(reg_items[item]) then usages_cache[item] = usages_cache[item] or {} insert(usages_cache[item], 1, recipe) end end local function get_usages(recipe) local added = {} for _, item in pairs(recipe.items) do item = reg_aliases[item] or item if not added[item] then get_item_usages(item, recipe, added) added[item] = true end end end local function cache_usages(item) local recipes = recipes_cache[item] or {} for i = 1, #recipes do get_usages(recipes[i]) end if fuel_cache[item] then usages_cache[item] = table_merge(usages_cache[item] or {}, {fuel_cache[item]}) end end local function drop_table(name, drop) local count_sure = 0 local drop_items = drop.items or {} local max_items = drop.max_items for i = 1, #drop_items do local di = drop_items[i] local valid_rarity = di.rarity and di.rarity > 1 if di.rarity or not max_items or (max_items and not di.rarity and count_sure < max_items) then for j = 1, #di.items do local dstack = ItemStack(di.items[j]) local dname = dstack:get_name() local dcount = dstack:get_count() local empty = dstack:is_empty() if not empty and (dname ~= name or (dname == name and dcount > 1)) then local rarity = valid_rarity and di.rarity i3.register_craft{ type = rarity and "digging_chance" or "digging", items = {name}, output = fmt("%s %u", dname, dcount), rarity = rarity, tools = di.tools, } end end end if not di.rarity then count_sure = count_sure + 1 end end end local function cache_drops(name, drop) if true_str(drop) then local dstack = ItemStack(drop) local dname = dstack:get_name() local empty = dstack:is_empty() if not empty and dname ~= name then i3.register_craft{ type = "digging", items = {name}, output = drop, } end elseif is_table(drop) then drop_table(name, drop) end end local function cache_recipes(item) local recipes = get_all_recipes(item) if replacements[item] then local _recipes = {} for k, v in ipairs(recipes or {}) do _recipes[#recipes + 1 - k] = v end local shift = 0 local size_rpl = maxn(replacements[item]) local size_rcp = #_recipes if size_rpl > size_rcp then shift = size_rcp - size_rpl end for k, v in pairs(replacements[item]) do k = k + shift if _recipes[k] then _recipes[k].replacements = v end end recipes = _recipes end if recipes then recipes_cache[item] = table_merge(recipes, recipes_cache[item] or {}) end end local function get_recipes(player, item) local clean_item = reg_aliases[item] or item local recipes = recipes_cache[clean_item] local usages = usages_cache[clean_item] if recipes then recipes = apply_recipe_filters(recipes, player) end local no_recipes = not recipes or #recipes == 0 if no_recipes and not usages then return end usages = apply_recipe_filters(usages, player) local no_usages = not usages or #usages == 0 return not no_recipes and recipes or nil, not no_usages and usages or nil end local function groups_to_items(groups, get_all) if not get_all and #groups == 1 then local group = groups[1] local stereotype = i3.group_stereotypes[group] local def = reg_items[stereotype] if def and show_item(def) then return stereotype end end local names = {} for name, def in pairs(reg_items) do if show_item(def) and item_has_groups(def.groups, groups) then if get_all then names[#names + 1] = name else return name end end end return get_all and names or "" end local function __sort(inv, reverse) sort(inv, function(a, b) if not is_str(a) then a = a:get_name() end if not is_str(b) then b = b:get_name() end if reverse then return a > b end return a < b end) end local function sort_itemlist(player, az) local inv = player:get_inventory() local list = inv:get_list("main") local size = inv:get_size("main") local new_inv, stack_meta = {}, {} for i = 1, size do local stack = list[i] local name = stack:get_name() local count = stack:get_count() local empty = stack:is_empty() local meta = stack:get_meta():to_table() local wear = stack:get_wear() > 0 if not empty then if next(meta.fields) or wear then stack_meta[#stack_meta + 1] = stack else new_inv[#new_inv + 1] = fmt("%s %u", name, count) end end end for i = 1, #stack_meta do new_inv[#new_inv + 1] = stack_meta[i] end if az then __sort(new_inv) else __sort(new_inv, true) end inv:set_list("main", new_inv) end local function compress_items(player) local inv = player:get_inventory() local list = inv:get_list("main") local size = inv:get_size("main") local new_inv, _new_inv, special = {}, {}, {} for i = 1, size do local stack = list[i] local name = stack:get_name() local count = stack:get_count() local stackmax = stack:get_stack_max() local empty = stack:is_empty() local meta = stack:get_meta():to_table() local wear = stack:get_wear() > 0 if not empty then if next(meta.fields) or wear or count >= stackmax then special[#special + 1] = stack else new_inv[name] = new_inv[name] or 0 new_inv[name] = new_inv[name] + count end end end for name, count in pairs(new_inv) do local stackmax = ItemStack(name):get_stack_max() local iter = ceil(count / stackmax) local leftover = count for _ = 1, iter do _new_inv[#_new_inv + 1] = fmt("%s %u", name, min(stackmax, leftover)) leftover = leftover - stackmax end end for i = 1, #special do _new_inv[#_new_inv + 1] = special[i] end __sort(_new_inv) inv:set_list("main", _new_inv) end local function get_stack_max(inv, data, is_recipe, rcp) local list = inv:get_list("main") local size = inv:get_size("main") local counts_inv, counts_rcp, counts = {}, {}, {} local rcp_usg = is_recipe and "recipe" or "usage" for _, it in pairs(rcp.items) do counts_rcp[it] = (counts_rcp[it] or 0) + 1 end data.export_counts[rcp_usg] = {} data.export_counts[rcp_usg].rcp = counts_rcp for i = 1, size do local stack = list[i] if not stack:is_empty() then local item = stack:get_name() local count = stack:get_count() for name in pairs(counts_rcp) do if is_group(name) then local def = reg_items[item] if def then local groups = extract_groups(name) if item_has_groups(def.groups, groups) then counts_inv[name] = (counts_inv[name] or 0) + count end end end end counts_inv[item] = (counts_inv[item] or 0) + count end end data.export_counts[rcp_usg].inv = counts_inv for name in pairs(counts_rcp) do counts[name] = floor((counts_inv[name] or 0) / (counts_rcp[name] or 0)) end local max_stacks = math.huge for _, count in pairs(counts) do if count < max_stacks then max_stacks = count end end return max_stacks end local function spawn_item(player, stack) local dir = player:get_look_dir() local ppos = player:get_pos() ppos.y = ppos.y + 1.625 local look_at = vec_add(ppos, vec_mul(dir, 1)) core.add_item(look_at, stack) end local function get_stack(player, pname, stack, message) local inv = player:get_inventory() if inv:room_for_item("main", stack) then inv:add_item("main", stack) msg(pname, S("@1 added in your inventory", message)) else spawn_item(player, stack) msg(pname, S("@1 spawned", message)) end end local function craft_stack(player, pname, data, craft_rcp) local inv = player:get_inventory() local rcp_usg = craft_rcp and "recipe" or "usage" local output = craft_rcp and data.recipes[data.rnum].output or data.usages[data.unum].output output = ItemStack(output) local stackname, stackcount, stackmax = output:get_name(), output:get_count(), output:get_stack_max() local scrbar_val = data[fmt("scrbar_%s", craft_rcp and "rcp" or "usg")] or 1 for name, count in pairs(data.export_counts[rcp_usg].rcp) do local items = {[name] = count} if is_group(name) then items = {} local groups = extract_groups(name) local item_groups = groups_to_items(groups, true) local remaining = count for _, item in ipairs(item_groups) do for _name, _count in pairs(data.export_counts[rcp_usg].inv) do if item == _name and remaining > 0 then local c = min(remaining, _count) items[item] = c remaining = remaining - c end if remaining == 0 then break end end end end for k, v in pairs(items) do inv:remove_item("main", fmt("%s %s", k, v * scrbar_val)) end end local count = stackcount * scrbar_val local desc = get_desc(stackname) local iter = ceil(count / stackmax) local leftover = count for _ = 1, iter do local c = min(stackmax, leftover) local message if c > 1 then message = clr("#ff0", fmt("%s x %s", c, desc)) else message = clr("#ff0", fmt("%s", desc)) end local stack = ItemStack(fmt("%s %s", stackname, c)) get_stack(player, pname, stack, message) leftover = leftover - stackmax end end local function select_item(player, name, data, _f) local item for field in pairs(_f) do if find(field, ":") then item = field break end end if not item then return elseif sub(item, -4) == "_inv" then item = sub(item, 1, -5) elseif sub(item, 1, 1) == "_" then item = sub(item, 2) elseif sub(item, 1, 6) == "group|" then item = match(item, "([%w:_]+)$") end item = reg_aliases[item] or item if not reg_items[item] then return end if core.is_creative_enabled(name) then local stack = ItemStack(item) local stackmax = stack:get_stack_max() stack = fmt("%s %s", item, stackmax) get_stack(player, name, stack, clr("#ff0", fmt("%u x %s", stackmax, item))) return end if item == data.query_item then return end local recipes, usages = get_recipes(player, item) data.query_item = item data.recipes = recipes data.usages = usages data.rnum = 1 data.unum = 1 data.scrbar_rcp = 1 data.scrbar_usg = 1 data.export_rcp = nil data.export_usg = nil end local function repairable(tool) local def = reg_tools[tool] return toolrepair and def and def.groups and def.groups.disable_repair ~= 1 end local function is_fav(favs, query_item) local fav, i for j = 1, #favs do if favs[j] == query_item then fav = true i = j break end end return fav, i end local function get_tooltip(item, info) local tooltip if info.groups then sort(info.groups) tooltip = group_names[concat(info.groups, ",")] if not tooltip then local groupstr = {} for i = 1, #info.groups do insert(groupstr, clr("#ff0", info.groups[i])) end groupstr = concat(groupstr, ", ") tooltip = S("Any item belonging to the groups: @1", groupstr) end else tooltip = get_desc(item) end local function add(str) return fmt("%s\n%s", tooltip, str) end if info.cooktime then tooltip = add(S("Cooking time: @1", clr("#ff0", info.cooktime))) end if info.burntime then tooltip = add(S("Burning time: @1", clr("#ff0", info.burntime))) end if info.replace then for i = 1, #info.replace.items do local rpl = match(info.replace.items[i], "%S+") local desc = clr("#ff0", get_desc(rpl)) if info.replace.type == "cooking" then tooltip = add(S("Replaced by @1 on smelting", desc)) elseif info.replace.type == "fuel" then tooltip = add(S("Replaced by @1 on burning", desc)) else tooltip = add(S("Replaced by @1 on crafting", desc)) end end end if info.repair then tooltip = add(S("Repairable by step of @1", clr("#ff0", toolrepair .. "%"))) end if info.rarity then local chance = (1 / max(1, info.rarity)) * 100 tooltip = add(S("@1 of chance to drop", clr("#ff0", chance .. "%"))) end if info.tools then local several = #info.tools > 1 local names = several and "\n" or "" if several then for i = 1, #info.tools do names = fmt("%s\t\t- %s\n", names, clr("#ff0", get_desc(info.tools[i]))) end tooltip = add(S("Only drop if using one of these tools: @1", sub(names, 1, -2))) else tooltip = add(S("Only drop if using this tool: @1", clr("#ff0", get_desc(info.tools[1])))) end end return fmt("tooltip[%s;%s]", item, ESC(tooltip)) end local function get_output_fs(fs, data, rcp, is_recipe, shapeless, right, btn_size, _btn_size) local custom_recipe = craft_types[rcp.type] if custom_recipe or shapeless or rcp.type == "cooking" then local icon = custom_recipe and custom_recipe.icon or shapeless and "shapeless" or "furnace" if not custom_recipe then icon = fmt("i3_%s.png^\\[resize:16x16", icon) end local pos_x = right + btn_size + 0.42 local pos_y = data.yoffset + 0.9 if sub(icon, 1, 10) == "i3_furnace" then fs("animated_image", pos_x, pos_y, 0.5, 0.5, PNG.furnace_anim, 8, 180) else fs("image", pos_x, pos_y, 0.5, 0.5, icon) end local tooltip = custom_recipe and custom_recipe.description or shapeless and S"Shapeless" or S"Cooking" fs("tooltip", pos_x, pos_y, 0.5, 0.5, ESC(tooltip)) end local arrow_X = right + 0.2 + (_btn_size or ITEM_BTN_SIZE) local X = arrow_X + 1.2 local Y = data.yoffset + 1.4 fs("image", arrow_X, Y + 0.06, 1, 1, PNG.arrow) if rcp.type == "fuel" then fs("animated_image", X + 0.05, Y, ITEM_BTN_SIZE, ITEM_BTN_SIZE, PNG.fire_anim, 8, 180) else local item = rcp.output item = ItemStack(clean_name(item)) local name = item:get_name() local count = item:get_count() local bt_s = ITEM_BTN_SIZE * 1.2 fs("image", X, Y - 0.11, bt_s, bt_s, PNG.slot) local _name = fmt("_%s", name) fs("item_image_button", X + 0.11, Y, ITEM_BTN_SIZE, ITEM_BTN_SIZE, fmt("%s %u", name, count * (is_recipe and data.scrbar_rcp or data.scrbar_usg or 1)), _name, "") local def = reg_items[name] local unknown = not def or nil local desc = def and def.description local weird = name ~= "" and desc and weird_desc(desc) or nil local burntime = fuel_cache[name] and fuel_cache[name].burntime local infos = { unknown = unknown, weird = weird, burntime = burntime, repair = repairable(name), rarity = rcp.rarity, tools = rcp.tools, } if next(infos) then fs(get_tooltip(_name, infos)) end end end local function get_grid_fs(fs, data, rcp, is_recipe) local width = rcp.width or 1 local right, btn_size, _btn_size = 0, ITEM_BTN_SIZE local cooktime, shapeless if rcp.type == "cooking" then cooktime, width = width, 1 elseif width == 0 and not rcp.custom then shapeless = true local n = #rcp.items width = (n < 5 and n > 1) and 2 or min(3, max(1, n)) end local rows = ceil(maxn(rcp.items) / width) local large_recipe = width > 3 or rows > 3 if large_recipe then fs("style_type[item_image_button;border=true]") end for i = 1, width * rows do local item = rcp.items[i] or "" item = clean_name(item) local name = match(item, "%S*") local X = ceil((i - 1) % width - width) X = X + (X * 0.2) + data.xoffset + 3.9 local Y = ceil(i / width) - min(2, rows) Y = Y + (Y * 0.15) + data.yoffset + 1.4 if large_recipe then btn_size = (3 / width) * (3 / rows) + 0.3 _btn_size = btn_size local xi = (i - 1) % width local yi = floor((i - 1) / width) X = btn_size * xi + data.xoffset + 0.3 + (xi * 0.05) Y = btn_size * yi + data.yoffset + 0.2 + (yi * 0.05) end if X > right then right = X end local groups if is_group(name) then groups = extract_groups(name) item = groups_to_items(groups) end local label = groups and "\nG" or "" local replace for j = 1, #(rcp.replacements or {}) do local replacement = rcp.replacements[j] if replacement[1] == name then replace = replace or {type = rcp.type, items = {}} local added for _, v in ipairs(replace.items) do if replacement[2] == v then added = true break end end if not added then label = fmt("%s%s\nR", label ~= "" and "\n" or "", label) replace.items[#replace.items + 1] = replacement[2] end end end if not large_recipe then fs("image", X, Y, btn_size, btn_size, PNG.slot) end local btn_name = groups and fmt("group|%s|%s", groups[1], item) or item fs("item_image_button", X, Y, btn_size, btn_size, fmt("%s %u", item, is_recipe and data.scrbar_rcp or data.scrbar_usg or 1), btn_name, label) local def = reg_items[name] local unknown = not def or nil unknown = not groups and unknown or nil local desc = def and def.description local weird = name ~= "" and desc and weird_desc(desc) or nil local burntime = fuel_cache[name] and fuel_cache[name].burntime local infos = { unknown = unknown, weird = weird, groups = groups, burntime = burntime, cooktime = cooktime, replace = replace, } if next(infos) then fs(get_tooltip(btn_name, infos)) end end if large_recipe then fs("style_type[item_image_button;border=false]") end get_output_fs(fs, data, rcp, is_recipe, shapeless, right, btn_size, _btn_size) end local function get_rcp_lbl(fs, data, panel, rn, is_recipe) local lbl = ES("Usage @1 of @2", data.unum, rn) if is_recipe then lbl = ES("Recipe @1 of @2", data.rnum, rn) end local _lbl = translate(data.lang_code, lbl) local lbl_len = #_lbl:gsub("[\128-\191]", "") -- Count chars, not bytes in UTF-8 strings local shift = min(0.9, abs(12 - max(12, lbl_len)) * 0.15) fs("label", data.xoffset + 5.65 - shift, data.yoffset + 3.37, lbl) if rn > 1 then local btn_suffix = is_recipe and "recipe" or "usage" local prev_name = fmt("prev_%s", btn_suffix) local next_name = fmt("next_%s", btn_suffix) local x_arrow = data.xoffset + 5.09 local y_arrow = data.yoffset + 3.2 fs("image_button", x_arrow - shift, y_arrow, 0.3, 0.3, "", prev_name, "") fs("image_button", x_arrow + 2.3, y_arrow, 0.3, 0.3, "", next_name, "") end local rcp = is_recipe and panel.rcp[data.rnum] or panel.rcp[data.unum] get_grid_fs(fs, data, rcp, is_recipe) end local function get_model_fs(fs, data, def, model_alias) if model_alias then if model_alias.drawtype == "entity" then def = reg_entities[model_alias.name] local init_props = def.initial_properties def.textures = init_props and init_props.textures or def.textures def.mesh = init_props and init_props.mesh or def.mesh else def = reg_items[model_alias.name] end end local tiles = def.tiles or def.textures or {} local t = {} for _, v in ipairs(tiles) do local _name if v.color then if is_num(v.color) then local hex = fmt("%02x", v.color) while #hex < 8 do hex = "0" .. hex end _name = fmt("%s^[multiply:%s", v.name, fmt("#%s%s", sub(hex, 3), sub(hex, 1, 2))) else _name = fmt("%s^[multiply:%s", v.name, v.color) end elseif v.animation then _name = fmt("%s^[verticalframe:%u:0", v.name, v.animation.aspect_h) end t[#t + 1] = _name or v.name or v end while #t < 6 do t[#t + 1] = t[#t] end fs("model", data.xoffset + 6.6, data.yoffset + 0.05, 1.3, 1.3, "", def.mesh, concat(t, ","), "0,0", "true", "true", model_alias and model_alias.frames or "") end local function get_header(fs, data) local fav = is_fav(data.favs, data.query_item) local nfavs = #data.favs local star_x, star_y, star_size = data.xoffset + 0.4, data.yoffset + 0.5, 0.4 if nfavs < MAX_FAVS or (nfavs == MAX_FAVS and fav) then local fav_marked = fmt("i3_fav%s.png", fav and "_off" or "") fs(fmt("style[fav;fgimg=%s;fgimg_hovered=%s;fgimg_pressed=%s]", fmt("i3_fav%s.png", fav and "" or "_off"), fav_marked, fav_marked)) fs("image_button", star_x, star_y, star_size, star_size, "", "fav", "") fs(fmt("tooltip[fav;%s]", fav and ES"Unmark this item" or ES"Mark this item")) else fs(fmt("style[nofav;fgimg=%s;fgimg_hovered=%s;fgimg_pressed=%s]", "i3_fav_off.png", PNG.cancel, PNG.cancel)) fs("image_button", star_x, star_y, star_size, star_size, "", "nofav", "") fs(fmt("tooltip[nofav;%s]", ES"Cannot mark this item. Bookmark limit reached.")) end local desc_lim, name_lim = 32, 34 local desc = translate(data.lang_code, get_desc(data.query_item)) desc = ESC(desc) local tech_name = data.query_item local X = data.xoffset + 1.05 local Y1 = data.yoffset + 0.47 local Y2 = Y1 + 0.5 if #desc > desc_lim then fs("tooltip", X, Y1 - 0.1, 5.7, 0.24, desc) desc = snip(desc, desc_lim) end if #tech_name > name_lim then fs("tooltip", X, Y2 - 0.1, 5.7, 0.24, tech_name) tech_name = snip(tech_name, name_lim) end fs("style_type[label;font=bold;font_size=22]") fs("label", X, Y1, desc) fs("style_type[label;font=mono;font_size=16]") fs("label", X, Y2, clr("#7bf", tech_name)) fs("style_type[label;font=normal;font_size=16]") local def = reg_items[data.query_item] local model_alias = i3.model_alias[data.query_item] if def.drawtype == "mesh" or model_alias then get_model_fs(fs, data, def, model_alias) else fs("item_image", data.xoffset + 6.8, data.yoffset + 0.17, 1.1, 1.1, data.query_item) end end local function get_export_fs(fs, data, is_recipe, is_usage, max_stacks_rcp, max_stacks_usg) local name = is_recipe and "rcp" or "usg" local show_export = (is_recipe and data.export_rcp) or (is_usage and data.export_usg) fs(fmt("style[export_%s;fgimg=%s;fgimg_hovered=%s]", name, fmt("%s", show_export and PNG.export_hover or PNG.export), PNG.export_hover)) fs("image_button", data.xoffset + 7.35, data.yoffset + 0.2, 0.45, 0.45, "", fmt("export_%s", name), "") fs(fmt("tooltip[export_%s;%s]", name, ES"Quick crafting")) if not show_export then return end local craft_max = is_recipe and max_stacks_rcp or max_stacks_usg local stack_fs = (is_recipe and data.scrbar_rcp) or (is_usage and data.scrbar_usg) or 1 if stack_fs > craft_max then stack_fs = craft_max if is_recipe then data.scrbar_rcp = craft_max elseif is_usage then data.scrbar_usg = craft_max end end fs(fmt("style[scrbar_%s;noclip=true]", name), fmt("scrollbaroptions[min=1;max=%u;smallstep=1]", craft_max)) fs("scrollbar", data.xoffset + 8.1, data.yoffset, 3, 0.35, "horizontal", fmt("scrbar_%s", name), stack_fs) fs("button", data.xoffset + 8.1, data.yoffset + 0.4, 3, 0.7, fmt("craft_%s", name), ES("Craft (x@1)", stack_fs)) end local function get_rcp_extra(player, data, fs, panel, is_recipe, is_usage) local rn = panel.rcp and #panel.rcp if rn then local rcp_normal = is_recipe and panel.rcp[data.rnum].type == "normal" local usg_normal = is_usage and panel.rcp[data.unum].type == "normal" local max_stacks_rcp, max_stacks_usg = 0, 0 local inv = player:get_inventory() if rcp_normal then max_stacks_rcp = get_stack_max(inv, data, is_recipe, panel.rcp[data.rnum]) end if usg_normal then max_stacks_usg = get_stack_max(inv, data, is_recipe, panel.rcp[data.unum]) end if is_recipe and max_stacks_rcp == 0 then data.export_rcp = nil data.scrbar_rcp = 1 elseif is_usage and max_stacks_usg == 0 then data.export_usg = nil data.scrbar_usg = 1 end if max_stacks_rcp > 0 or max_stacks_usg > 0 then get_export_fs(fs, data, is_recipe, is_usage, max_stacks_rcp, max_stacks_usg) end get_rcp_lbl(fs, data, panel, rn, is_recipe) else local lbl = is_recipe and ES"No recipes" or ES"No usages" fs("button", data.xoffset + 0.1, data.yoffset + (panel.height / 2) - 0.5, 7.8, 1, "no_rcp", lbl) end end local function get_favs(fs, data) fs("label", data.xoffset + 0.4, data.yoffset + 0.4, ES"Bookmarks") for i = 1, #data.favs do local item = data.favs[i] local X = data.xoffset - 0.7 + (i * 1.2) local Y = data.yoffset + 0.8 if data.query_item == item then fs("image", X, Y, ITEM_BTN_SIZE, ITEM_BTN_SIZE, PNG.slot) end fs("item_image_button", X, Y, ITEM_BTN_SIZE, ITEM_BTN_SIZE, item, item, "") end end local function get_panels(player, data, fs) local _title = {name = "title", height = 1.4} local _favs = {name = "favs", height = 2.23} local _recipes = {name = "recipes", rcp = data.recipes, height = 3.9} local _usages = {name = "usages", rcp = data.usages, height = 3.9} local panels = {_title, _recipes, _usages, _favs} for idx = 1, #panels do local panel = panels[idx] data.yoffset = 0 if idx > 1 then for _idx = idx - 1, 1, -1 do data.yoffset = data.yoffset + panels[_idx].height + 0.1 end end fs("bg9", data.xoffset + 0.1, data.yoffset, 7.9, panel.height, PNG.bg_full, 10) local is_recipe, is_usage = panel.name == "recipes", panel.name == "usages" if is_recipe or is_usage then get_rcp_extra(player, data, fs, panel, is_recipe, is_usage) elseif panel.name == "title" then get_header(fs, data) elseif panel.name == "favs" then get_favs(fs, data) end end end local function add_subtitle(fs, name, y, ctn_len, font_size, label) fs(fmt("style[%s;font=bold;font_size=%u;border=false;content_offset=0]", name, font_size)) fs("button", 0, y, ctn_len, 0.5, name, ESC(label)) fs("image", 0, y + 0.55, ctn_len, 0.035, PNG.bar) 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", "") add_subtitle(fs, "awards", yextra, ctn_len, 18, ES("Achievements: @1 of @2 (@3)", awards_unlocked, award_list_nb, percent)) for i = 1, award_list_nb do local award = award_list[i] local y = yextra - 0.5 + i + (i * 0.3) local def, progress = award.def, award.progress local title, desc = def.title, def.description title = translate(data.lang_code, title) desc = translate(data.lang_code, desc):gsub("%.$", "") local title_lim, _title = 27 local desc_lim, _desc = 39 local icon_size = 1.1 local box_len = ctn_len - icon_size + 0.1 if #title > title_lim then _title = snip(title, title_lim) end if #desc > desc_lim then _desc = snip(desc, desc_lim) end if not award.unlocked and def.secret then title = ES"Secret award" desc = ES"Unlock this award to find out what it is" end local icon = def.icon or "awards_unknown.png" if not award.unlocked then icon = fmt("%s^\\[colorize:#000:180", icon) end fs[#fs + 1] = fmt("image", 0, y + 0.01, icon_size, icon_size, icon) fs[#fs + 1] = "style_type[box;colors=#bababa30,#bababa30,#bababa05,#bababa05]" fs[#fs + 1] = fmt("box", icon_size + 0.1, y, box_len, icon_size, "") if progress then local current, target = progress.current, progress.target local curr_bar = (current * box_len) / target fs[#fs + 1] = fmt("box", icon_size + 0.1, y + 0.8, box_len, 0.3, "#101010") fs[#fs + 1] = "style_type[box;colors=#9dc34c80,#9dc34c,#9dc34c,#9dc34c80]" fs[#fs + 1] = fmt("box", icon_size + 0.1, y + 0.8, curr_bar, 0.3, "") fs[#fs + 1] = "style_type[label;font_size=14]" fs[#fs + 1] = fmt("label", icon_size + 0.5, y + 0.97, fmt("%u / %u", current, target)) y = y - 0.14 end title = _title or title desc = _desc or desc fs[#fs + 1] = "style_type[label;font=bold;font_size=17]" fs[#fs + 1] = fmt("label", icon_size + 0.2, y + 0.4, title) fs[#fs + 1] = "style_type[label;font=normal;font_size=15]" fs[#fs + 1] = fmt("label", icon_size + 0.2, y + 0.75, clr("#bbbbbb", desc)) fs[#fs + 1] = "style_type[label;font_size=16]" end end local function get_ctn_content(fs, data, player, yoffset, ctn_len, award_list, awards_unlocked, award_list_nb) local name = player:get_player_name() add_subtitle(fs, "player_name", 0, ctn_len, 22, ESC(name)) local hp = damage_enabled and (data.hp or player:get_hp()) or 20 local half = ceil((hp / 2) % 1) local hearts = (hp / 2) + half local heart_size = 0.35 local heart_x, heart_h = 0.65, yoffset + 0.75 for i = 1, 10 do fs("image", heart_x + ((i - 1) * (heart_size + 0.1)), heart_h, heart_size, heart_size, PNG.heart_grey) end if damage_enabled then for i = 1, hearts do fs("image", heart_x + ((i - 1) * (heart_size + 0.1)), heart_h, heart_size, heart_size, (half == 1 and i == floor(hearts)) and PNG.heart_half or PNG.heart) end end fs(fmt("list[current_player;craft;%f,%f;3,3;]", 0, yoffset + 1.45)) fs("image", 3.47, yoffset + 2.69, 0.85, 0.85, PNG.arrow) fs(fmt("list[current_player;craftpreview;%f,%f;1,1;]", 4.45, yoffset + 2.6), fmt("list[detached:i3_trash;main;%f,%f;1,1;]", 4.45, yoffset + 3.75)) fs("image", 4.45, yoffset + 3.75, 1, 1, PNG.trash) local yextra = 5.4 local bag_equip = data.equip == "bag" local armor_equip = data.equip == "armor" local skins_equip = data.equip == "skins" fs(fmt("style[btn_bag;textcolor=%s]", bag_equip and "#fff" or "#aaa"), fmt("style[btn_armor;textcolor=%s]", armor_equip and "#fff" or "#aaa"), fmt("style[btn_skins;textcolor=%s]", skins_equip and "#fff" or "#aaa"), "style_type[button:hovered;textcolor=#fff]") fs("button", -0.14, yextra - 0.2, 2, 0.6, "btn_bag", ES"Bag") fs("button", 1.87, yextra - 0.2, 2, 0.6, "btn_armor", ES"Armor") fs("button", 3.87, yextra - 0.2, 2, 0.6, "btn_skins", ES"Skins") fs("box", 0, yextra + 0.4, ctn_len, 0.045, "#bababa50") fs("box", (bag_equip and 0) or (armor_equip and 2) or (skins_equip and 4), yextra + 0.4, 1.7, 0.045, "#f9826c") if bag_equip then fs(fmt("list[detached:%s_backpack;main;0,%f;1,1;]", ESC(name), yextra + 0.7)) if not data.bag:get_stack("main", 1):is_empty() then fs("hypertext", 1.2, yextra + 0.89, ctn_len - 1.9, 0.8, "", ES("The inventory is extended by @1 slots", BAG_SIZES[data.bag_size] - INV_SIZE)) end elseif armor_equip then if __3darmor then fs(fmt("list[detached:%s_armor;armor;0,%f;3,2;]", ESC(name), yextra + 0.7)) local armor_def = armor.def[name] fs("label", 3.65, yextra + 1.55, fmt("%s: %s", ES"Level", armor_def.level)) fs("label", 3.65, yextra + 2.05, fmt("%s: %s", ES"Heal", armor_def.heal)) else fs("hypertext", 0, yextra + 0.9, ctn_len, 0.6, "", "