2021-11-15 16:50:48 -06:00
|
|
|
local ItemStack = ItemStack
|
|
|
|
local loadstring = loadstring
|
2021-12-06 16:42:27 -06:00
|
|
|
local reg_items = core.registered_items
|
2021-11-29 19:28:11 -06:00
|
|
|
local translate = core.get_translated_string
|
2021-11-25 12:17:38 -06:00
|
|
|
local vec_new, vec_add, vec_mul = vector.new, vector.add, vector.multiply
|
2021-11-15 16:50:48 -06:00
|
|
|
local sort, concat, insert = table.sort, table.concat, table.insert
|
|
|
|
local min, floor, ceil = math.min, math.floor, math.ceil
|
2021-11-29 19:28:11 -06:00
|
|
|
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
|
2021-11-07 15:31:47 -06:00
|
|
|
|
2022-06-16 10:55:27 -05:00
|
|
|
if not core.registered_privileges.creative then
|
|
|
|
core.register_privilege("creative", {
|
|
|
|
description = "Allow player to use creative inventory",
|
|
|
|
give_to_singleplayer = false,
|
|
|
|
give_to_admin = false,
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
2021-11-07 15:31:47 -06:00
|
|
|
local old_is_creative_enabled = core.is_creative_enabled
|
|
|
|
|
|
|
|
function core.is_creative_enabled(name)
|
|
|
|
if name == "" then
|
|
|
|
return old_is_creative_enabled(name)
|
|
|
|
end
|
|
|
|
|
|
|
|
return core.check_player_privs(name, {creative = true}) or old_is_creative_enabled(name)
|
|
|
|
end
|
2021-10-18 20:24:32 -05:00
|
|
|
|
2021-11-29 20:52:18 -06:00
|
|
|
local S = core.get_translator"i3"
|
2021-10-24 22:31:58 -05:00
|
|
|
local ES = function(...) return core.formspec_escape(S(...)) end
|
|
|
|
|
|
|
|
local function is_num(x)
|
|
|
|
return type(x) == "number"
|
|
|
|
end
|
|
|
|
|
|
|
|
local function is_str(x)
|
|
|
|
return type(x) == "string"
|
|
|
|
end
|
|
|
|
|
|
|
|
local function is_table(x)
|
|
|
|
return type(x) == "table"
|
|
|
|
end
|
|
|
|
|
|
|
|
local function is_func(x)
|
|
|
|
return type(x) == "function"
|
|
|
|
end
|
|
|
|
|
|
|
|
local function true_str(str)
|
|
|
|
return is_str(str) and str ~= ""
|
|
|
|
end
|
|
|
|
|
|
|
|
local function true_table(x)
|
|
|
|
return is_table(x) and next(x)
|
|
|
|
end
|
|
|
|
|
2021-10-18 20:24:32 -05:00
|
|
|
local function reset_compression(data)
|
|
|
|
data.alt_items = nil
|
|
|
|
data.expand = ""
|
|
|
|
end
|
|
|
|
|
2021-11-07 15:31:47 -06:00
|
|
|
local function msg(name, str)
|
2021-11-07 16:12:09 -06:00
|
|
|
local prefix = "[i3]"
|
|
|
|
return core.chat_send_player(name, fmt("%s %s", core.colorize("#ff0", prefix), str))
|
2021-11-07 15:31:47 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
local function err(str)
|
|
|
|
return core.log("error", str)
|
|
|
|
end
|
|
|
|
|
|
|
|
local function round(num, decimal)
|
|
|
|
local mul = 10 ^ decimal
|
2021-11-15 16:50:48 -06:00
|
|
|
return floor(num * mul + 0.5) / mul
|
2021-11-07 15:31:47 -06:00
|
|
|
end
|
|
|
|
|
2021-11-29 19:28:11 -06:00
|
|
|
local function toupper(str)
|
|
|
|
return str:gsub("%f[%w]%l", upper):gsub("_", " ")
|
|
|
|
end
|
|
|
|
|
2022-06-29 18:04:22 -05:00
|
|
|
local function utf8_len(str)
|
2022-06-30 08:18:32 -05:00
|
|
|
local c = 0
|
|
|
|
|
|
|
|
for _ in str:gmatch"([%z\1-\127\194-\244][\128-\191]*)" do -- Arguably working duct-tape code
|
|
|
|
c++
|
|
|
|
end
|
|
|
|
|
|
|
|
return c
|
2022-06-29 18:04:22 -05:00
|
|
|
end
|
|
|
|
|
2021-11-29 19:28:11 -06:00
|
|
|
local function get_bag_description(data, stack)
|
|
|
|
local desc = translate(data.lang_code, stack:get_description())
|
2021-11-29 20:52:18 -06:00
|
|
|
desc = split(desc, "(")[1] or desc
|
|
|
|
desc = toupper(desc:trim())
|
2021-11-29 19:28:11 -06:00
|
|
|
|
|
|
|
return desc
|
|
|
|
end
|
|
|
|
|
2021-10-18 20:24:32 -05:00
|
|
|
local function search(data)
|
|
|
|
reset_compression(data)
|
|
|
|
|
|
|
|
local filter = data.filter
|
|
|
|
local opt = "^(.-)%+([%w_]+)=([%w_,]+)"
|
|
|
|
local search_filter = next(i3.search_filters) and match(filter, opt)
|
|
|
|
local filters = {}
|
|
|
|
|
|
|
|
if search_filter then
|
|
|
|
search_filter = search_filter:trim()
|
|
|
|
|
|
|
|
for filter_name, values in gmatch(filter, sub(opt, 6)) do
|
|
|
|
if i3.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]
|
2021-12-06 16:42:27 -06:00
|
|
|
local def = reg_items[item]
|
2022-04-25 09:00:49 -05:00
|
|
|
local desc = lower(translate(data.lang_code, def.description)) or ""
|
2021-10-18 20:24:32 -05:00
|
|
|
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 = i3.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
|
|
|
|
|
2021-11-28 16:55:11 -06:00
|
|
|
j++
|
2021-10-18 20:24:32 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
local ok = true
|
|
|
|
|
|
|
|
for keyword in gmatch(filter, "%S+") do
|
|
|
|
if not find(search_in, keyword, 1, true) then
|
|
|
|
ok = nil
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if ok then
|
|
|
|
to_add = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if to_add then
|
2021-11-28 16:55:11 -06:00
|
|
|
c++
|
2021-10-18 20:24:32 -05:00
|
|
|
filtered_list[c] = item
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
data.items = filtered_list
|
|
|
|
end
|
|
|
|
|
2021-10-24 22:31:58 -05:00
|
|
|
local function table_replace(t, val, new)
|
|
|
|
for k, v in pairs(t) do
|
|
|
|
if v == val then
|
|
|
|
t[k] = new
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-10-18 20:24:32 -05:00
|
|
|
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
|
2021-11-28 16:55:11 -06:00
|
|
|
c++
|
2021-10-18 20:24:32 -05:00
|
|
|
t1[c] = t2[i]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return t1
|
|
|
|
end
|
|
|
|
|
2021-10-24 22:31:58 -05:00
|
|
|
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
|
2021-11-28 16:55:11 -06:00
|
|
|
c++
|
2021-10-24 22:31:58 -05:00
|
|
|
diff[c] = v
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return diff
|
|
|
|
end
|
|
|
|
|
2021-11-03 18:53:14 -05:00
|
|
|
local function rcp_eq(rcp, rcp2)
|
|
|
|
if rcp.type ~= rcp2.type then return end
|
|
|
|
if rcp.width ~= rcp2.width then return end
|
|
|
|
if #rcp.items ~= #rcp2.items then return end
|
|
|
|
if rcp.output ~= rcp2.output then return end
|
|
|
|
|
|
|
|
for i, item in pairs(rcp.items) do
|
|
|
|
if item ~= rcp2.items[i] then return end
|
|
|
|
end
|
2021-10-24 22:31:58 -05:00
|
|
|
|
2021-11-03 18:53:14 -05:00
|
|
|
for i, item in pairs(rcp2.items) do
|
|
|
|
if item ~= rcp.items[i] then return end
|
2021-10-24 22:31:58 -05:00
|
|
|
end
|
|
|
|
|
2021-11-03 18:53:14 -05:00
|
|
|
return true
|
2021-10-24 22:31:58 -05:00
|
|
|
end
|
|
|
|
|
2021-11-07 15:31:47 -06:00
|
|
|
local function clean_name(item)
|
|
|
|
if sub(item, 1, 1) == ":" or sub(item, 1, 1) == " " or sub(item, 1, 1) == "_" then
|
|
|
|
item = sub(item, 2)
|
|
|
|
end
|
|
|
|
|
|
|
|
return item
|
|
|
|
end
|
|
|
|
|
2021-10-18 20:24:32 -05:00
|
|
|
local function is_group(item)
|
|
|
|
return sub(item, 1, 6) == "group:"
|
|
|
|
end
|
|
|
|
|
|
|
|
local function extract_groups(str)
|
2022-06-18 12:47:23 -05:00
|
|
|
return split(sub(str, 7), ",")
|
2021-10-18 20:24:32 -05:00
|
|
|
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
|
|
|
|
|
2021-11-23 17:51:58 -06:00
|
|
|
local function valid_item(def)
|
2021-10-18 20:24:32 -05:00
|
|
|
return def and def.groups.not_in_creative_inventory ~= 1 and
|
|
|
|
def.description and def.description ~= ""
|
|
|
|
end
|
|
|
|
|
2022-06-18 19:29:08 -05:00
|
|
|
local function get_group_stereotype(group)
|
|
|
|
local stereotype = i3.group_stereotypes[group]
|
|
|
|
local def = reg_items[stereotype]
|
|
|
|
|
|
|
|
if valid_item(def) then
|
|
|
|
return stereotype
|
2021-10-18 20:24:32 -05:00
|
|
|
end
|
2022-06-18 19:29:08 -05:00
|
|
|
end
|
2021-10-18 20:24:32 -05:00
|
|
|
|
2022-06-18 19:29:08 -05:00
|
|
|
local function groups_to_items(groups)
|
2021-10-18 20:24:32 -05:00
|
|
|
local names = {}
|
|
|
|
|
2021-12-06 16:42:27 -06:00
|
|
|
for name, def in pairs(reg_items) do
|
2021-11-23 17:51:58 -06:00
|
|
|
if valid_item(def) and item_has_groups(def.groups, groups) then
|
2022-06-18 19:29:08 -05:00
|
|
|
insert(names, name)
|
2021-10-18 20:24:32 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-06-18 20:30:48 -05:00
|
|
|
sort(names)
|
|
|
|
|
2022-06-18 19:29:08 -05:00
|
|
|
return names
|
2021-10-18 20:24:32 -05:00
|
|
|
end
|
|
|
|
|
2022-06-18 14:50:13 -05:00
|
|
|
local function is_cube(drawtype)
|
|
|
|
return drawtype == "normal" or drawtype == "liquid" or
|
|
|
|
sub(drawtype, 1, 9) == "glasslike" or
|
|
|
|
sub(drawtype, 1, 8) == "allfaces"
|
|
|
|
end
|
|
|
|
|
|
|
|
local function get_cube(tiles)
|
|
|
|
if not true_table(tiles) then
|
|
|
|
return "i3_blank.png"
|
|
|
|
end
|
|
|
|
|
|
|
|
local top = tiles[1] or "i3_blank.png"
|
|
|
|
if is_table(top) then
|
|
|
|
top = top.name or top.image
|
|
|
|
end
|
|
|
|
|
|
|
|
local left = tiles[3] or top or "i3_blank.png"
|
|
|
|
if is_table(left) then
|
|
|
|
left = left.name or left.image
|
|
|
|
end
|
|
|
|
|
|
|
|
local right = tiles[5] or left or "i3_blank.png"
|
|
|
|
if is_table(right) then
|
|
|
|
right = right.name or right.image
|
|
|
|
end
|
|
|
|
|
|
|
|
return core.inventorycube(top, left, right)
|
|
|
|
end
|
|
|
|
|
2021-10-18 20:24:32 -05:00
|
|
|
local function apply_recipe_filters(recipes, player)
|
|
|
|
for _, filter in pairs(i3.recipe_filters) do
|
|
|
|
recipes = filter(recipes, player)
|
|
|
|
end
|
|
|
|
|
|
|
|
return recipes
|
|
|
|
end
|
|
|
|
|
|
|
|
local function compression_active(data)
|
2021-12-12 17:20:26 -06:00
|
|
|
return i3.settings.item_compression and not next(i3.recipe_filters) and data.filter == ""
|
2021-10-18 20:24:32 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
local function compressible(item, data)
|
|
|
|
return compression_active(data) and i3.compress_groups[item]
|
|
|
|
end
|
|
|
|
|
2022-01-02 07:30:03 -06:00
|
|
|
local function is_fav(data)
|
|
|
|
for i = 1, #data.favs do
|
|
|
|
if data.favs[i] == data.query_item then
|
|
|
|
return i
|
2021-10-18 20:24:32 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-10-24 16:31:01 -05:00
|
|
|
local function sort_by_category(data)
|
|
|
|
reset_compression(data)
|
|
|
|
local items = data.items_raw
|
|
|
|
|
|
|
|
if data.filter ~= "" then
|
|
|
|
search(data)
|
|
|
|
items = data.items
|
|
|
|
end
|
|
|
|
|
|
|
|
local new = {}
|
|
|
|
|
|
|
|
for i = 1, #items do
|
|
|
|
local item = items[i]
|
|
|
|
local to_add = true
|
|
|
|
|
2021-11-01 09:34:10 -05:00
|
|
|
if data.itab == 2 then
|
2021-11-07 15:31:47 -06:00
|
|
|
to_add = core.registered_nodes[item]
|
2021-11-01 09:34:10 -05:00
|
|
|
elseif data.itab == 3 then
|
2021-11-07 15:31:47 -06:00
|
|
|
to_add = core.registered_craftitems[item] or core.registered_tools[item]
|
2021-10-24 16:31:01 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
if to_add then
|
2021-11-15 16:50:48 -06:00
|
|
|
insert(new, item)
|
2021-10-24 16:31:01 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
data.items = new
|
|
|
|
end
|
|
|
|
|
|
|
|
local function spawn_item(player, stack)
|
|
|
|
local dir = player:get_look_dir()
|
|
|
|
local ppos = player:get_pos()
|
2021-11-18 19:08:29 -06:00
|
|
|
ppos.y = ppos.y + player:get_properties().eye_height
|
2021-11-24 21:22:46 -06:00
|
|
|
local look_at = vec_add(ppos, vec_mul(dir, 1))
|
2021-10-24 16:31:01 -05:00
|
|
|
|
|
|
|
core.add_item(look_at, stack)
|
|
|
|
end
|
|
|
|
|
2021-11-07 15:31:47 -06:00
|
|
|
local function get_recipes(player, item)
|
2021-12-06 16:42:27 -06:00
|
|
|
item = core.registered_aliases[item] or item
|
|
|
|
local recipes = i3.recipes_cache[item]
|
|
|
|
local usages = i3.usages_cache[item]
|
2021-11-07 15:31:47 -06:00
|
|
|
|
|
|
|
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 get_stack(player, stack)
|
|
|
|
local inv = player:get_inventory()
|
|
|
|
|
|
|
|
if inv:room_for_item("main", stack) then
|
|
|
|
inv:add_item("main", stack)
|
|
|
|
else
|
|
|
|
spawn_item(player, stack)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-11-14 14:39:48 -06:00
|
|
|
local function craft_stack(player, 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)
|
2022-06-18 12:47:23 -05:00
|
|
|
local groupname = name:sub(7)
|
2022-06-18 19:29:08 -05:00
|
|
|
local item_groups = i3.groups[groupname].items or groups_to_items(groups)
|
2021-11-14 14:39:48 -06:00
|
|
|
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
|
2021-11-15 16:50:48 -06:00
|
|
|
local c = min(remaining, _count)
|
2021-11-14 14:39:48 -06:00
|
|
|
items[item] = c
|
2021-11-28 16:55:11 -06:00
|
|
|
remaining -= c
|
2021-11-14 14:39:48 -06:00
|
|
|
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
|
2021-11-15 16:50:48 -06:00
|
|
|
local iter = ceil(count / stackmax)
|
2021-11-14 14:39:48 -06:00
|
|
|
local leftover = count
|
|
|
|
|
|
|
|
for _ = 1, iter do
|
2021-11-15 16:50:48 -06:00
|
|
|
local c = min(stackmax, leftover)
|
2021-11-14 14:39:48 -06:00
|
|
|
local stack = ItemStack(fmt("%s %s", stackname, c))
|
|
|
|
get_stack(player, stack)
|
2021-11-28 16:55:11 -06:00
|
|
|
leftover -= stackmax
|
2021-11-14 14:39:48 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local function play_sound(name, sound, volume)
|
|
|
|
core.sound_play(sound, {to_player = name, gain = volume}, true)
|
|
|
|
end
|
|
|
|
|
2021-11-07 15:31:47 -06:00
|
|
|
local function safe_teleport(player, pos)
|
2021-11-08 11:46:45 -06:00
|
|
|
local name = player:get_player_name()
|
2021-11-15 22:09:37 -06:00
|
|
|
play_sound(name, "i3_teleport", 0.8)
|
2021-11-08 11:46:45 -06:00
|
|
|
|
2021-11-07 15:31:47 -06:00
|
|
|
local vel = player:get_velocity()
|
2021-11-24 21:22:46 -06:00
|
|
|
player:add_velocity(vec_mul(vel, -1))
|
2021-11-25 20:32:04 -06:00
|
|
|
|
|
|
|
local p = vec_new(pos)
|
2021-11-28 16:55:11 -06:00
|
|
|
p.y += 0.25
|
2021-11-25 20:32:04 -06:00
|
|
|
|
2021-11-25 12:17:38 -06:00
|
|
|
player:set_pos(p)
|
2021-11-07 15:31:47 -06:00
|
|
|
end
|
|
|
|
|
2021-11-01 19:35:30 -05:00
|
|
|
local function get_sorting_idx(name)
|
|
|
|
local idx = 1
|
2021-10-31 16:11:41 -05:00
|
|
|
|
2021-11-01 19:35:30 -05:00
|
|
|
for i, def in ipairs(i3.sorting_methods) do
|
|
|
|
if name == def.name then
|
|
|
|
idx = i
|
2021-10-31 16:11:41 -05:00
|
|
|
end
|
2021-11-01 19:35:30 -05:00
|
|
|
end
|
2021-10-31 16:11:41 -05:00
|
|
|
|
2021-11-01 19:35:30 -05:00
|
|
|
return idx
|
2021-10-31 16:11:41 -05:00
|
|
|
end
|
|
|
|
|
2021-11-01 19:35:30 -05:00
|
|
|
local function sorter(inv, reverse, mode)
|
2021-11-15 16:50:48 -06:00
|
|
|
sort(inv, function(a, b)
|
2021-11-01 19:35:30 -05:00
|
|
|
if mode == 1 then
|
|
|
|
a, b = a:get_name(), b:get_name()
|
|
|
|
else
|
|
|
|
a, b = a:get_count(), b:get_count()
|
|
|
|
end
|
2021-10-31 16:11:41 -05:00
|
|
|
|
|
|
|
if reverse then
|
|
|
|
return a > b
|
|
|
|
end
|
|
|
|
|
|
|
|
return a < b
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2021-11-01 19:35:30 -05:00
|
|
|
local function pre_sorting(list, start_i)
|
|
|
|
local new_inv, special = {}, {}
|
2021-10-31 16:11:41 -05:00
|
|
|
|
2021-11-01 19:35:30 -05:00
|
|
|
for i = start_i, #list do
|
|
|
|
local stack = list[i]
|
|
|
|
local empty = stack:is_empty()
|
|
|
|
local meta = stack:get_meta():to_table()
|
|
|
|
local wear = stack:get_wear() > 0
|
2021-10-31 16:11:41 -05:00
|
|
|
|
2021-11-01 19:35:30 -05:00
|
|
|
if not empty then
|
|
|
|
if next(meta.fields) or wear then
|
2021-11-15 16:50:48 -06:00
|
|
|
insert(special, stack)
|
2021-11-01 19:35:30 -05:00
|
|
|
else
|
2021-11-15 16:50:48 -06:00
|
|
|
insert(new_inv, stack)
|
2021-11-01 19:35:30 -05:00
|
|
|
end
|
|
|
|
end
|
2021-11-01 09:12:32 -05:00
|
|
|
end
|
|
|
|
|
2021-11-01 19:35:30 -05:00
|
|
|
new_inv = table_merge(new_inv, special)
|
|
|
|
return new_inv
|
2021-11-01 09:12:32 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
local function compress_items(list, start_i)
|
2021-11-01 19:35:30 -05:00
|
|
|
local hash, new_inv, special = {}, {}, {}
|
2021-10-31 16:11:41 -05:00
|
|
|
|
2021-11-01 09:12:32 -05:00
|
|
|
for i = start_i, #list do
|
2021-11-01 19:35:30 -05:00
|
|
|
local stack = list[i]
|
|
|
|
local name = stack:get_name()
|
|
|
|
local count = stack:get_count()
|
2021-10-31 16:11:41 -05:00
|
|
|
local stackmax = stack:get_stack_max()
|
2021-11-01 19:35:30 -05:00
|
|
|
local empty = stack:is_empty()
|
|
|
|
local meta = stack:get_meta():to_table()
|
|
|
|
local wear = stack:get_wear() > 0
|
2021-10-31 16:11:41 -05:00
|
|
|
|
|
|
|
if not empty then
|
|
|
|
if next(meta.fields) or wear or count >= stackmax then
|
2021-11-15 16:50:48 -06:00
|
|
|
insert(special, stack)
|
2021-10-31 16:11:41 -05:00
|
|
|
else
|
2021-11-01 19:35:30 -05:00
|
|
|
hash[name] = hash[name] or 0
|
2021-11-28 16:55:11 -06:00
|
|
|
hash[name] += count
|
2021-10-31 16:11:41 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-11-01 19:35:30 -05:00
|
|
|
for name, count in pairs(hash) do
|
2021-10-31 16:11:41 -05:00
|
|
|
local stackmax = ItemStack(name):get_stack_max()
|
2021-11-15 16:50:48 -06:00
|
|
|
local iter = ceil(count / stackmax)
|
2021-10-31 16:11:41 -05:00
|
|
|
local leftover = count
|
|
|
|
|
|
|
|
for _ = 1, iter do
|
2021-11-15 16:50:48 -06:00
|
|
|
insert(new_inv, ItemStack(fmt("%s %u", name, min(stackmax, leftover))))
|
2021-11-28 16:55:11 -06:00
|
|
|
leftover -= stackmax
|
2021-10-31 16:11:41 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-11-01 19:35:30 -05:00
|
|
|
new_inv = table_merge(new_inv, special)
|
|
|
|
return new_inv
|
2021-10-31 16:11:41 -05:00
|
|
|
end
|
|
|
|
|
2022-02-27 11:53:26 -06:00
|
|
|
local function drop_items(player, inv, list, start_i, rej, remove)
|
2021-11-08 13:13:51 -06:00
|
|
|
for i = start_i, #list do
|
2021-11-08 12:53:00 -06:00
|
|
|
local stack = list[i]
|
|
|
|
local name = stack:get_name()
|
|
|
|
|
|
|
|
for _, it in ipairs(rej) do
|
|
|
|
if name == it then
|
2022-02-27 11:53:26 -06:00
|
|
|
if not remove then
|
|
|
|
spawn_item(player, stack)
|
|
|
|
end
|
|
|
|
|
2021-11-08 12:53:00 -06:00
|
|
|
inv:set_stack("main", i, ItemStack(""))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-11-14 14:39:48 -06:00
|
|
|
return inv:get_list"main"
|
2021-11-08 12:53:00 -06:00
|
|
|
end
|
|
|
|
|
2021-10-31 16:11:41 -05:00
|
|
|
local function sort_inventory(player, data)
|
2021-11-01 09:12:32 -05:00
|
|
|
local inv = player:get_inventory()
|
2021-11-14 14:39:48 -06:00
|
|
|
local list = inv:get_list"main"
|
|
|
|
local size = inv:get_size"main"
|
2021-12-12 17:20:26 -06:00
|
|
|
local start_i = data.ignore_hotbar and (i3.settings.hotbar_len + 1) or 1
|
2021-11-01 09:12:32 -05:00
|
|
|
|
2021-11-14 14:39:48 -06:00
|
|
|
if true_table(data.drop_items) then
|
2022-02-27 11:53:26 -06:00
|
|
|
list = drop_items(player, inv, list, start_i, data.drop_items, true)
|
2021-11-08 12:53:00 -06:00
|
|
|
end
|
|
|
|
|
2021-10-31 16:11:41 -05:00
|
|
|
if data.inv_compress then
|
2021-11-01 09:12:32 -05:00
|
|
|
list = compress_items(list, start_i)
|
2021-11-01 19:35:30 -05:00
|
|
|
else
|
|
|
|
list = pre_sorting(list, start_i)
|
2021-10-31 16:11:41 -05:00
|
|
|
end
|
|
|
|
|
2021-11-01 09:43:38 -05:00
|
|
|
local idx = get_sorting_idx(data.sort)
|
|
|
|
local new_inv = i3.sorting_methods[idx].func(list, data)
|
2021-11-01 19:35:30 -05:00
|
|
|
if not new_inv then return end
|
2021-10-31 16:11:41 -05:00
|
|
|
|
2021-11-01 19:35:30 -05:00
|
|
|
if not data.ignore_hotbar then
|
|
|
|
inv:set_list("main", new_inv)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
for i = start_i, size do
|
|
|
|
local index = i - start_i + 1
|
|
|
|
inv:set_stack("main", i, new_inv[index] or "")
|
2021-10-31 16:11:41 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-01-10 19:49:44 -06:00
|
|
|
local function reset_data(data)
|
|
|
|
data.filter = ""
|
|
|
|
data.expand = ""
|
|
|
|
data.pagenum = 1
|
|
|
|
data.rnum = 1
|
|
|
|
data.unum = 1
|
|
|
|
data.scrbar_rcp = 1
|
|
|
|
data.scrbar_usg = 1
|
|
|
|
data.query_item = nil
|
2022-06-29 18:53:07 -05:00
|
|
|
data.enable_search = nil
|
2022-01-10 19:49:44 -06:00
|
|
|
data.recipes = nil
|
|
|
|
data.usages = nil
|
|
|
|
data.export_rcp = nil
|
|
|
|
data.export_usg = nil
|
|
|
|
data.alt_items = nil
|
|
|
|
data.confirm_trash = nil
|
|
|
|
data.show_settings = nil
|
|
|
|
data.show_setting = "home"
|
|
|
|
data.items = data.items_raw
|
|
|
|
|
|
|
|
if data.itab > 1 then
|
|
|
|
sort_by_category(data)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-11-08 13:41:47 -06:00
|
|
|
local function add_hud_waypoint(player, name, pos, color)
|
|
|
|
return player:hud_add {
|
|
|
|
hud_elem_type = "waypoint",
|
|
|
|
name = name,
|
2022-03-11 21:30:20 -06:00
|
|
|
text = "m",
|
2021-11-08 13:41:47 -06:00
|
|
|
world_pos = pos,
|
|
|
|
number = color,
|
|
|
|
z_index = -300,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2021-11-23 17:51:58 -06:00
|
|
|
local function get_detached_inv(name, player_name)
|
|
|
|
return core.get_inventory {
|
|
|
|
type = "detached",
|
|
|
|
name = fmt("i3_%s_%s", name, player_name)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2021-12-08 09:43:41 -06:00
|
|
|
-- Much faster implementation of `unpack`
|
2021-11-15 16:50:48 -06:00
|
|
|
local function createunpack(n)
|
|
|
|
local ret = {"local t = ... return "}
|
|
|
|
|
|
|
|
for k = 2, n do
|
|
|
|
ret[2 + (k - 2) * 4] = "t["
|
|
|
|
ret[3 + (k - 2) * 4] = k - 1
|
|
|
|
ret[4 + (k - 2) * 4] = "]"
|
|
|
|
|
|
|
|
if k ~= n then
|
|
|
|
ret[5 + (k - 2) * 4] = ","
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return loadstring(concat(ret))
|
|
|
|
end
|
|
|
|
|
|
|
|
local newunpack = createunpack(33)
|
|
|
|
|
2021-10-25 00:09:21 -05:00
|
|
|
-------------------------------------------------------------------------------
|
|
|
|
|
2021-10-31 16:11:41 -05:00
|
|
|
local _ = {
|
2021-10-24 22:31:58 -05:00
|
|
|
-- Groups
|
|
|
|
is_group = is_group,
|
|
|
|
extract_groups = extract_groups,
|
|
|
|
item_has_groups = item_has_groups,
|
|
|
|
groups_to_items = groups_to_items,
|
2022-06-18 19:29:08 -05:00
|
|
|
get_group_stereotype = get_group_stereotype,
|
2021-10-24 22:31:58 -05:00
|
|
|
|
|
|
|
-- Compression
|
|
|
|
compressible = compressible,
|
|
|
|
compression_active = compression_active,
|
|
|
|
|
|
|
|
-- Sorting
|
|
|
|
search = search,
|
2021-11-01 19:35:30 -05:00
|
|
|
sorter = sorter,
|
2021-11-07 15:31:47 -06:00
|
|
|
get_recipes = get_recipes,
|
2021-10-31 16:11:41 -05:00
|
|
|
sort_inventory = sort_inventory,
|
|
|
|
get_sorting_idx = get_sorting_idx,
|
2021-10-24 22:31:58 -05:00
|
|
|
sort_by_category = sort_by_category,
|
|
|
|
apply_recipe_filters = apply_recipe_filters,
|
|
|
|
|
2021-11-07 15:31:47 -06:00
|
|
|
-- Type checks
|
2021-10-24 22:31:58 -05:00
|
|
|
is_fav = is_fav,
|
|
|
|
is_str = is_str,
|
|
|
|
is_num = is_num,
|
|
|
|
is_func = is_func,
|
|
|
|
true_str = true_str,
|
|
|
|
true_table = true_table,
|
2021-11-07 15:31:47 -06:00
|
|
|
|
|
|
|
-- Console
|
|
|
|
err = err,
|
|
|
|
msg = msg,
|
|
|
|
|
|
|
|
-- Misc. functions
|
2022-06-18 14:50:13 -05:00
|
|
|
is_cube = is_cube,
|
|
|
|
get_cube = get_cube,
|
2021-11-29 12:15:14 -06:00
|
|
|
ItemStack = ItemStack,
|
2021-11-23 17:51:58 -06:00
|
|
|
valid_item = valid_item,
|
2021-11-07 15:31:47 -06:00
|
|
|
spawn_item = spawn_item,
|
2021-10-24 22:31:58 -05:00
|
|
|
clean_name = clean_name,
|
2021-11-14 14:39:48 -06:00
|
|
|
play_sound = play_sound,
|
2022-01-10 19:49:44 -06:00
|
|
|
reset_data = reset_data,
|
2021-11-07 15:31:47 -06:00
|
|
|
safe_teleport = safe_teleport,
|
2021-11-08 13:41:47 -06:00
|
|
|
add_hud_waypoint = add_hud_waypoint,
|
2021-10-24 22:31:58 -05:00
|
|
|
|
|
|
|
-- Core functions
|
|
|
|
clr = core.colorize,
|
2021-10-25 00:09:21 -05:00
|
|
|
slz = core.serialize,
|
|
|
|
dslz = core.deserialize,
|
2021-10-24 22:31:58 -05:00
|
|
|
ESC = core.formspec_escape,
|
2021-11-29 12:15:14 -06:00
|
|
|
draw_cube = core.inventorycube,
|
2021-11-23 17:51:58 -06:00
|
|
|
get_group = core.get_item_group,
|
2021-11-08 13:41:47 -06:00
|
|
|
pos_to_str = core.pos_to_string,
|
|
|
|
str_to_pos = core.string_to_pos,
|
2021-10-24 22:31:58 -05:00
|
|
|
check_privs = core.check_player_privs,
|
2021-11-25 20:32:04 -06:00
|
|
|
get_player_by_name = core.get_player_by_name,
|
2021-11-30 13:47:01 -06:00
|
|
|
get_connected_players = core.get_connected_players,
|
2021-11-23 17:51:58 -06:00
|
|
|
|
|
|
|
-- Inventory
|
|
|
|
get_stack = get_stack,
|
|
|
|
craft_stack = craft_stack,
|
|
|
|
get_detached_inv = get_detached_inv,
|
2021-11-29 19:28:11 -06:00
|
|
|
get_bag_description = get_bag_description,
|
2021-11-14 14:39:48 -06:00
|
|
|
create_inventory = core.create_detached_inventory,
|
2021-10-24 22:31:58 -05:00
|
|
|
|
|
|
|
-- Registered items
|
|
|
|
reg_items = core.registered_items,
|
|
|
|
reg_nodes = core.registered_nodes,
|
|
|
|
reg_tools = core.registered_tools,
|
|
|
|
reg_aliases = core.registered_aliases,
|
|
|
|
reg_entities = core.registered_entities,
|
|
|
|
reg_craftitems = core.registered_craftitems,
|
|
|
|
|
|
|
|
-- i18n
|
|
|
|
S = S,
|
|
|
|
ES = ES,
|
|
|
|
translate = core.get_translated_string,
|
|
|
|
|
|
|
|
-- String
|
|
|
|
sub = string.sub,
|
|
|
|
find = string.find,
|
|
|
|
fmt = string.format,
|
|
|
|
upper = string.upper,
|
|
|
|
lower = string.lower,
|
|
|
|
split = string.split,
|
|
|
|
match = string.match,
|
|
|
|
gmatch = string.gmatch,
|
2021-11-29 19:28:11 -06:00
|
|
|
toupper = toupper,
|
2022-06-29 18:04:22 -05:00
|
|
|
utf8_len = utf8_len,
|
2021-10-24 22:31:58 -05:00
|
|
|
|
|
|
|
-- Table
|
|
|
|
maxn = table.maxn,
|
|
|
|
sort = table.sort,
|
|
|
|
copy = table.copy,
|
|
|
|
concat = table.concat,
|
|
|
|
insert = table.insert,
|
|
|
|
remove = table.remove,
|
|
|
|
indexof = table.indexof,
|
2021-11-15 16:50:48 -06:00
|
|
|
unpack = newunpack,
|
2021-10-24 22:31:58 -05:00
|
|
|
is_table = is_table,
|
|
|
|
table_merge = table_merge,
|
|
|
|
table_replace = table_replace,
|
2021-11-03 18:53:14 -05:00
|
|
|
rcp_eq = rcp_eq,
|
2021-10-24 22:31:58 -05:00
|
|
|
array_diff = array_diff,
|
|
|
|
|
|
|
|
-- Math
|
|
|
|
round = round,
|
|
|
|
min = math.min,
|
|
|
|
max = math.max,
|
|
|
|
ceil = math.ceil,
|
|
|
|
floor = math.floor,
|
|
|
|
random = math.random,
|
|
|
|
|
|
|
|
-- Vectors
|
|
|
|
vec_new = vector.new,
|
|
|
|
vec_add = vector.add,
|
2021-11-24 21:22:46 -06:00
|
|
|
vec_sub = vector.subtract,
|
|
|
|
vec_mul = vector.multiply,
|
2021-10-24 22:31:58 -05:00
|
|
|
vec_round = vector.round,
|
|
|
|
vec_eq = vector.equals,
|
2021-10-18 20:24:32 -05:00
|
|
|
}
|
2021-10-25 00:09:21 -05:00
|
|
|
|
|
|
|
function i3.get(...)
|
|
|
|
local t = {}
|
|
|
|
|
|
|
|
for i, var in ipairs{...} do
|
2021-10-31 16:11:41 -05:00
|
|
|
t[i] = _[var]
|
2021-10-25 00:09:21 -05:00
|
|
|
end
|
|
|
|
|
2021-11-15 16:50:48 -06:00
|
|
|
return newunpack(t)
|
2021-10-25 00:09:21 -05:00
|
|
|
end
|