mirror of
https://github.com/minetest-mods/i3.git
synced 2025-06-30 15:40:24 +02:00
Rename folder
This commit is contained in:
326
src/api.lua
Normal file
326
src/api.lua
Normal file
@ -0,0 +1,326 @@
|
||||
local make_fs = i3.files.gui()
|
||||
|
||||
local gmatch, match, split = i3.get("gmatch", "match", "split")
|
||||
local S, err, fmt, reg_items = i3.get("S", "err", "fmt", "reg_items")
|
||||
local sorter, sort_inventory = i3.get("sorter", "sort_inventory")
|
||||
local sort, concat, copy, insert, remove = i3.get("sort", "concat", "copy", "insert", "remove")
|
||||
local true_str, true_table, is_str, is_func, is_table, clean_name =
|
||||
i3.get("true_str", "true_table", "is_str", "is_func", "is_table", "clean_name")
|
||||
|
||||
function i3.register_craft_type(name, def)
|
||||
if not true_str(name) then
|
||||
return err "i3.register_craft_type: name missing"
|
||||
elseif not true_table(def) then
|
||||
return err "i3.register_craft_type: definition missing"
|
||||
elseif not is_str(def.description) then
|
||||
def.description = ""
|
||||
end
|
||||
|
||||
i3.craft_types[name] = def
|
||||
end
|
||||
|
||||
function i3.register_craft(def)
|
||||
local width, c = 0, 0
|
||||
|
||||
if true_str(def.url) then
|
||||
if not i3.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
|
||||
|
||||
i3.http.fetch({url = def.url}, function(result)
|
||||
if result.succeeded then
|
||||
local t = core.parse_json(result.data)
|
||||
if is_table(t) then
|
||||
return i3.register_craft(t)
|
||||
end
|
||||
end
|
||||
end)
|
||||
|
||||
return
|
||||
end
|
||||
|
||||
if not true_table(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+")
|
||||
i3.recipes_cache[item] = i3.recipes_cache[item] or {}
|
||||
|
||||
def.custom = true
|
||||
def.width = width
|
||||
|
||||
insert(i3.recipes_cache[item], def)
|
||||
end
|
||||
|
||||
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
|
||||
|
||||
i3.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
|
||||
|
||||
i3.recipe_filters = {[name] = f}
|
||||
end
|
||||
|
||||
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
|
||||
|
||||
i3.search_filters[name] = f
|
||||
end
|
||||
|
||||
function i3.get_recipes(item)
|
||||
return {
|
||||
recipes = i3.recipes_cache[item],
|
||||
usages = i3.usages_cache[item]
|
||||
}
|
||||
end
|
||||
|
||||
function i3.set_fs(player, _fs)
|
||||
if not player or player.is_fake_player then return end
|
||||
local name = player:get_player_name()
|
||||
local data = i3.data[name]
|
||||
if not data then return end
|
||||
|
||||
if data.auto_sorting then
|
||||
sort_inventory(player, data)
|
||||
end
|
||||
|
||||
local fs = fmt("%s%s", make_fs(player, data), _fs or "")
|
||||
player:set_inventory_formspec(fs)
|
||||
end
|
||||
|
||||
function i3.new_tab(def)
|
||||
if not true_table(def) then
|
||||
return err "i3.new_tab: tab definition missing"
|
||||
elseif not true_str(def.name) then
|
||||
return err "i3.new_tab: tab name missing"
|
||||
elseif not true_str(def.description) then
|
||||
return err "i3.new_tab: description missing"
|
||||
elseif #i3.tabs == 6 then
|
||||
return err(fmt("i3.new_tab: cannot add '%s' tab. Limit reached (6).", def.name))
|
||||
end
|
||||
|
||||
i3.tabs[#i3.tabs + 1] = def
|
||||
end
|
||||
|
||||
function i3.remove_tab(tabname)
|
||||
if not true_str(tabname) then
|
||||
return err "i3.remove_tab: tab name missing"
|
||||
end
|
||||
|
||||
for i, def in ipairs(i3.tabs) do
|
||||
if tabname == def.name then
|
||||
remove(i3.tabs, i)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function i3.get_current_tab(player)
|
||||
local name = player:get_player_name()
|
||||
local data = i3.data[name]
|
||||
|
||||
return data.tab
|
||||
end
|
||||
|
||||
function i3.set_tab(player, tabname)
|
||||
local name = player:get_player_name()
|
||||
local data = i3.data[name]
|
||||
|
||||
if not tabname or tabname == "" then
|
||||
data.tab = 0
|
||||
return
|
||||
end
|
||||
|
||||
local found
|
||||
|
||||
for i, def in ipairs(i3.tabs) do
|
||||
if not found and def.name == tabname then
|
||||
data.tab = i
|
||||
found = true
|
||||
end
|
||||
end
|
||||
|
||||
if not found then
|
||||
return err(fmt("i3.set_tab: tab name '%s' does not exist", tabname))
|
||||
end
|
||||
end
|
||||
|
||||
function i3.override_tab(tabname, newdef)
|
||||
if not true_table(newdef) then
|
||||
return err "i3.override_tab: tab definition missing"
|
||||
elseif not true_str(newdef.name) then
|
||||
return err "i3.override_tab: tab name missing"
|
||||
elseif not true_str(newdef.description) then
|
||||
return err "i3.override_tab: description missing"
|
||||
end
|
||||
|
||||
for i, def in ipairs(i3.tabs) do
|
||||
if def.name == tabname then
|
||||
i3.tabs[i] = newdef
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
i3.register_craft_type("digging", {
|
||||
description = S"Digging",
|
||||
icon = "i3_steelpick.png",
|
||||
})
|
||||
|
||||
i3.register_craft_type("digging_chance", {
|
||||
description = S"Digging (by chance)",
|
||||
icon = "i3_mesepick.png",
|
||||
})
|
||||
|
||||
i3.add_search_filter("groups", function(item, groups)
|
||||
local def = reg_items[item]
|
||||
local has_groups = true
|
||||
|
||||
for _, group in ipairs(groups) do
|
||||
if not def.groups[group] then
|
||||
has_groups = nil
|
||||
break
|
||||
end
|
||||
end
|
||||
|
||||
return has_groups
|
||||
end)
|
||||
|
||||
function i3.compress(item, def)
|
||||
if not true_str(item) then
|
||||
return err "i3.compress: item name missing"
|
||||
elseif not true_table(def) then
|
||||
return err "i3.compress: replace definition missing"
|
||||
elseif not true_str(def.replace) then
|
||||
return err "i3.compress: replace string missing"
|
||||
elseif not is_table(def.by) then
|
||||
return err "i3.compress: replace substrings missing"
|
||||
end
|
||||
|
||||
local t = {}
|
||||
i3.compress_groups[item] = i3.compress_groups[item] or {}
|
||||
|
||||
for _, str in ipairs(def.by) do
|
||||
local it = item:gsub(def.replace, str)
|
||||
|
||||
insert(t, it)
|
||||
insert(i3.compress_groups[item], it)
|
||||
|
||||
i3.compressed[it] = true
|
||||
end
|
||||
end
|
||||
|
||||
function i3.add_sorting_method(def)
|
||||
if not true_table(def) then
|
||||
return err "i3.add_sorting_method: definition missing"
|
||||
elseif not true_str(def.name) then
|
||||
return err "i3.add_sorting_method: name missing"
|
||||
elseif not is_func(def.func) then
|
||||
return err "i3.add_sorting_method: function missing"
|
||||
end
|
||||
|
||||
insert(i3.sorting_methods, def)
|
||||
end
|
||||
|
||||
i3.add_sorting_method {
|
||||
name = "alphabetical",
|
||||
description = S"Sort items by name (A-Z)",
|
||||
func = function(list, data)
|
||||
sorter(list, data.reverse_sorting, 1)
|
||||
return list
|
||||
end
|
||||
}
|
||||
|
||||
i3.add_sorting_method {
|
||||
name = "numerical",
|
||||
description = S"Sort items by number of items per stack",
|
||||
func = function(list, data)
|
||||
sorter(list, data.reverse_sorting, 2)
|
||||
return list
|
||||
end,
|
||||
}
|
96
src/bags.lua
Normal file
96
src/bags.lua
Normal file
@ -0,0 +1,96 @@
|
||||
local S, fmt, msg, spawn_item = i3.get("S", "fmt", "msg", "spawn_item")
|
||||
|
||||
local function init_backpack(player)
|
||||
local name = player:get_player_name()
|
||||
local data = i3.data[name]
|
||||
local inv = player:get_inventory()
|
||||
|
||||
inv:set_size("main", data.bag_size and i3.BAG_SIZES[data.bag_size] or i3.INV_SIZE)
|
||||
|
||||
data.bag = core.create_detached_inventory(fmt("%s_backpack", name), {
|
||||
allow_put = function(_inv, listname, _, stack)
|
||||
local empty = _inv:get_stack(listname, 1):is_empty()
|
||||
local item_group = minetest.get_item_group(stack:get_name(), "bag")
|
||||
|
||||
if empty and item_group > 0 and item_group <= #i3.BAG_SIZES then
|
||||
return 1
|
||||
end
|
||||
|
||||
msg(name, S"This is not a backpack")
|
||||
|
||||
return 0
|
||||
end,
|
||||
|
||||
on_put = function(_, _, _, stack)
|
||||
local stackname = stack:get_name()
|
||||
data.bag_item = stackname
|
||||
data.bag_size = minetest.get_item_group(stackname, "bag")
|
||||
|
||||
inv:set_size("main", i3.BAG_SIZES[data.bag_size])
|
||||
i3.set_fs(player)
|
||||
end,
|
||||
|
||||
on_take = function()
|
||||
for i = i3.INV_SIZE + 1, i3.BAG_SIZES[data.bag_size] do
|
||||
local stack = inv:get_stack("main", i)
|
||||
|
||||
if not stack:is_empty() then
|
||||
spawn_item(player, stack)
|
||||
end
|
||||
end
|
||||
|
||||
data.bag_item = nil
|
||||
data.bag_size = nil
|
||||
|
||||
inv:set_size("main", i3.INV_SIZE)
|
||||
i3.set_fs(player)
|
||||
end,
|
||||
})
|
||||
|
||||
data.bag:set_size("main", 1)
|
||||
|
||||
if data.bag_item then
|
||||
data.bag:set_stack("main", 1, data.bag_item)
|
||||
end
|
||||
end
|
||||
|
||||
local bag_recipes = {
|
||||
small = {
|
||||
rcp = {
|
||||
{"", "farming:string", ""},
|
||||
{"group:wool", "group:wool", "group:wool"},
|
||||
{"group:wool", "group:wool", "group:wool"},
|
||||
},
|
||||
size = 1,
|
||||
},
|
||||
medium = {
|
||||
rcp = {
|
||||
{"farming:string", "i3:bag_small", "farming:string"},
|
||||
{"farming:string", "i3:bag_small", "farming:string"},
|
||||
},
|
||||
size = 2,
|
||||
},
|
||||
large = {
|
||||
rcp = {
|
||||
{"farming:string", "i3:bag_medium", "farming:string"},
|
||||
{"farming:string", "i3:bag_medium", "farming:string"},
|
||||
},
|
||||
size = 3,
|
||||
},
|
||||
}
|
||||
|
||||
for size, item in pairs(bag_recipes) do
|
||||
local bagname = fmt("i3:bag_%s", size)
|
||||
|
||||
core.register_craftitem(bagname, {
|
||||
description = fmt("%s Backpack", size:gsub("^%l", string.upper)),
|
||||
inventory_image = fmt("i3_bag_%s.png", size),
|
||||
stack_max = 1,
|
||||
groups = {bag = item.size}
|
||||
})
|
||||
|
||||
core.register_craft {output = bagname, recipe = item.rcp}
|
||||
core.register_craft {type = "fuel", recipe = bagname, burntime = 3}
|
||||
end
|
||||
|
||||
return init_backpack
|
568
src/common.lua
Normal file
568
src/common.lua
Normal file
@ -0,0 +1,568 @@
|
||||
local translate = core.get_translated_string
|
||||
local fmt, find, gmatch, match, sub, split, lower =
|
||||
string.format, string.find, string.gmatch, string.match, string.sub, string.split, string.lower
|
||||
local reg_items, reg_nodes, reg_craftitems, reg_tools =
|
||||
core.registered_items, core.registered_nodes, core.registered_craftitems, core.registered_tools
|
||||
|
||||
local S = core.get_translator "i3"
|
||||
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
|
||||
|
||||
local function reset_compression(data)
|
||||
data.alt_items = nil
|
||||
data.expand = ""
|
||||
end
|
||||
|
||||
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]
|
||||
local def = core.registered_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 = 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
|
||||
|
||||
j = j + 1
|
||||
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
|
||||
c = c + 1
|
||||
filtered_list[c] = item
|
||||
end
|
||||
end
|
||||
|
||||
data.items = filtered_list
|
||||
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 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 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
|
||||
table.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
|
||||
table.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 is_group(item)
|
||||
return sub(item, 1, 6) == "group:"
|
||||
end
|
||||
|
||||
local function extract_groups(str)
|
||||
if sub(str, 1, 6) == "group:" then
|
||||
return split(sub(str, 7), ",")
|
||||
end
|
||||
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 show_item(def)
|
||||
return def and def.groups.not_in_creative_inventory ~= 1 and
|
||||
def.description and def.description ~= ""
|
||||
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 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 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)
|
||||
return i3.item_compression and not next(i3.recipe_filters) and data.filter == ""
|
||||
end
|
||||
|
||||
local function compressible(item, data)
|
||||
return compression_active(data) and i3.compress_groups[item]
|
||||
end
|
||||
|
||||
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
|
||||
|
||||
local function msg(name, str)
|
||||
return core.chat_send_player(name, fmt("[i3] %s", str))
|
||||
end
|
||||
|
||||
local function err(str)
|
||||
return core.log("error", str)
|
||||
end
|
||||
|
||||
local function round(num, decimal)
|
||||
local mul = 10 ^ decimal
|
||||
return math.floor(num * mul + 0.5) / mul
|
||||
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 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
|
||||
|
||||
if data.itab == 2 then
|
||||
to_add = reg_nodes[item]
|
||||
elseif data.itab == 3 then
|
||||
to_add = reg_craftitems[item] or reg_tools[item]
|
||||
end
|
||||
|
||||
if to_add then
|
||||
new[#new + 1] = item
|
||||
end
|
||||
end
|
||||
|
||||
data.items = new
|
||||
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 = vector.add(ppos, vector.multiply(dir, 1))
|
||||
|
||||
core.add_item(look_at, stack)
|
||||
end
|
||||
|
||||
local function get_sorting_idx(name)
|
||||
local idx = 1
|
||||
|
||||
for i, def in ipairs(i3.sorting_methods) do
|
||||
if name == def.name then
|
||||
idx = i
|
||||
end
|
||||
end
|
||||
|
||||
return idx
|
||||
end
|
||||
|
||||
local function sorter(inv, reverse, mode)
|
||||
table.sort(inv, function(a, b)
|
||||
if mode == 1 then
|
||||
a, b = a:get_name(), b:get_name()
|
||||
else
|
||||
a, b = a:get_count(), b:get_count()
|
||||
end
|
||||
|
||||
if reverse then
|
||||
return a > b
|
||||
end
|
||||
|
||||
return a < b
|
||||
end)
|
||||
end
|
||||
|
||||
local function pre_sorting(list, start_i)
|
||||
local new_inv, special = {}, {}
|
||||
|
||||
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
|
||||
|
||||
if not empty then
|
||||
if next(meta.fields) or wear then
|
||||
special[#special + 1] = stack
|
||||
else
|
||||
new_inv[#new_inv + 1] = stack
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
new_inv = table_merge(new_inv, special)
|
||||
return new_inv
|
||||
end
|
||||
|
||||
local function compress_items(list, start_i)
|
||||
local hash, new_inv, special = {}, {}, {}
|
||||
|
||||
for i = start_i, #list 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
|
||||
hash[name] = hash[name] or 0
|
||||
hash[name] = hash[name] + count
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
for name, count in pairs(hash) do
|
||||
local stackmax = ItemStack(name):get_stack_max()
|
||||
local iter = math.ceil(count / stackmax)
|
||||
local leftover = count
|
||||
|
||||
for _ = 1, iter do
|
||||
new_inv[#new_inv + 1] = ItemStack(fmt("%s %u", name, math.min(stackmax, leftover)))
|
||||
leftover = leftover - stackmax
|
||||
end
|
||||
end
|
||||
|
||||
new_inv = table_merge(new_inv, special)
|
||||
return new_inv
|
||||
end
|
||||
|
||||
local function sort_inventory(player, data)
|
||||
local inv = player:get_inventory()
|
||||
local list = inv:get_list("main")
|
||||
local size = inv:get_size("main")
|
||||
local start_i = data.ignore_hotbar and 10 or 1
|
||||
|
||||
if data.inv_compress then
|
||||
list = compress_items(list, start_i)
|
||||
else
|
||||
list = pre_sorting(list, start_i)
|
||||
end
|
||||
|
||||
local idx = get_sorting_idx(data.sort)
|
||||
local new_inv = i3.sorting_methods[idx].func(list, data)
|
||||
if not new_inv then return end
|
||||
|
||||
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 "")
|
||||
end
|
||||
end
|
||||
|
||||
-------------------------------------------------------------------------------
|
||||
|
||||
local _ = {
|
||||
-- Groups
|
||||
is_group = is_group,
|
||||
extract_groups = extract_groups,
|
||||
item_has_groups = item_has_groups,
|
||||
groups_to_items = groups_to_items,
|
||||
|
||||
-- Compression
|
||||
compressible = compressible,
|
||||
compression_active = compression_active,
|
||||
|
||||
-- Sorting
|
||||
search = search,
|
||||
sorter = sorter,
|
||||
sort_inventory = sort_inventory,
|
||||
get_sorting_idx = get_sorting_idx,
|
||||
sort_by_category = sort_by_category,
|
||||
apply_recipe_filters = apply_recipe_filters,
|
||||
|
||||
-- Misc. functions
|
||||
err = err,
|
||||
msg = msg,
|
||||
is_fav = is_fav,
|
||||
is_str = is_str,
|
||||
is_num = is_num,
|
||||
is_func = is_func,
|
||||
show_item = show_item,
|
||||
spawn_item = spawn_item,
|
||||
true_str = true_str,
|
||||
true_table = true_table,
|
||||
clean_name = clean_name,
|
||||
|
||||
-- Core functions
|
||||
clr = core.colorize,
|
||||
slz = core.serialize,
|
||||
dslz = core.deserialize,
|
||||
ESC = core.formspec_escape,
|
||||
check_privs = core.check_player_privs,
|
||||
|
||||
-- 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,
|
||||
|
||||
-- Table
|
||||
maxn = table.maxn,
|
||||
sort = table.sort,
|
||||
copy = table.copy,
|
||||
concat = table.concat,
|
||||
insert = table.insert,
|
||||
remove = table.remove,
|
||||
indexof = table.indexof,
|
||||
is_table = is_table,
|
||||
table_eq = table_eq,
|
||||
table_merge = table_merge,
|
||||
table_replace = table_replace,
|
||||
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,
|
||||
vec_round = vector.round,
|
||||
vec_eq = vector.equals,
|
||||
vec_mul = vector.multiply,
|
||||
}
|
||||
|
||||
function i3.get(...)
|
||||
local t = {}
|
||||
|
||||
for i, var in ipairs{...} do
|
||||
t[i] = _[var]
|
||||
end
|
||||
|
||||
return unpack(t)
|
||||
end
|
319
src/compress.lua
Normal file
319
src/compress.lua
Normal file
@ -0,0 +1,319 @@
|
||||
local fmt, copy, insert = i3.get("fmt", "copy", "insert")
|
||||
|
||||
local wood_types = {
|
||||
"acacia_wood", "aspen_wood", "junglewood", "pine_wood",
|
||||
}
|
||||
|
||||
local material_tools = {
|
||||
"bronze", "diamond", "mese", "stone", "wood",
|
||||
}
|
||||
|
||||
local material_stairs = {
|
||||
"acacia_wood", "aspen_wood", "brick", "bronzeblock", "cobble", "copperblock",
|
||||
"desert_cobble", "desert_sandstone", "desert_sandstone_block", "desert_sandstone_brick",
|
||||
"desert_stone", "desert_stone_block", "desert_stonebrick",
|
||||
"glass", "goldblock", "ice", "junglewood", "mossycobble", "obsidian",
|
||||
"obsidian_block", "obsidian_glass", "obsidianbrick", "pine_wood",
|
||||
"sandstone", "sandstone_block", "sandstonebrick",
|
||||
"silver_sandstone", "silver_sandstone_block", "silver_sandstone_brick",
|
||||
"snowblock", "steelblock", "stone", "stone_block", "stonebrick",
|
||||
"straw", "tinblock",
|
||||
}
|
||||
|
||||
local colors = {
|
||||
"black", "blue", "brown", "cyan", "dark_green", "dark_grey", "green",
|
||||
"grey", "magenta", "orange", "pink", "red", "violet", "yellow",
|
||||
}
|
||||
|
||||
local to_compress = {
|
||||
["default:wood"] = {
|
||||
replace = "wood",
|
||||
by = wood_types,
|
||||
},
|
||||
|
||||
["default:fence_wood"] = {
|
||||
replace = "wood",
|
||||
by = wood_types,
|
||||
},
|
||||
|
||||
["default:fence_rail_wood"] = {
|
||||
replace = "wood",
|
||||
by = wood_types,
|
||||
},
|
||||
|
||||
["default:mese_post_light"] = {
|
||||
replace = "mese_post_light",
|
||||
by = {
|
||||
"mese_post_light_acacia_wood",
|
||||
"mese_post_light_aspen_wood",
|
||||
"mese_post_light_junglewood",
|
||||
"mese_post_light_pine_wood",
|
||||
}
|
||||
},
|
||||
|
||||
["doors:gate_wood_closed"] = {
|
||||
replace = "wood",
|
||||
by = wood_types,
|
||||
},
|
||||
|
||||
["wool:white"] = {
|
||||
replace = "white",
|
||||
by = colors
|
||||
},
|
||||
|
||||
["dye:white"] = {
|
||||
replace = "white",
|
||||
by = colors
|
||||
},
|
||||
|
||||
["default:axe_steel"] = {
|
||||
replace = "steel",
|
||||
by = material_tools
|
||||
},
|
||||
|
||||
["default:pick_steel"] = {
|
||||
replace = "steel",
|
||||
by = material_tools
|
||||
},
|
||||
|
||||
["default:shovel_steel"] = {
|
||||
replace = "steel",
|
||||
by = material_tools
|
||||
},
|
||||
|
||||
["default:sword_steel"] = {
|
||||
replace = "steel",
|
||||
by = material_tools
|
||||
},
|
||||
|
||||
["farming:hoe_steel"] = {
|
||||
replace = "steel",
|
||||
by = {"wood", "stone"}
|
||||
},
|
||||
|
||||
["stairs:slab_wood"] = {
|
||||
replace = "wood",
|
||||
by = material_stairs
|
||||
},
|
||||
|
||||
["stairs:stair_wood"] = {
|
||||
replace = "wood",
|
||||
by = material_stairs
|
||||
},
|
||||
|
||||
["stairs:stair_inner_wood"] = {
|
||||
replace = "wood",
|
||||
by = material_stairs
|
||||
},
|
||||
|
||||
["stairs:stair_outer_wood"] = {
|
||||
replace = "wood",
|
||||
by = material_stairs
|
||||
},
|
||||
|
||||
["walls:cobble"] = {
|
||||
replace = "cobble",
|
||||
by = {"desertcobble", "mossycobble"}
|
||||
},
|
||||
}
|
||||
|
||||
local circular_saw_names = {
|
||||
{"micro", "_1"},
|
||||
{"panel", "_1"},
|
||||
{"micro", "_2"},
|
||||
{"panel", "_2"},
|
||||
{"micro", "_4"},
|
||||
{"panel", "_4"},
|
||||
{"micro", ""},
|
||||
{"panel", ""},
|
||||
|
||||
{"micro", "_12"},
|
||||
{"panel", "_12"},
|
||||
{"micro", "_14"},
|
||||
{"panel", "_14"},
|
||||
{"micro", "_15"},
|
||||
{"panel", "_15"},
|
||||
{"stair", "_outer"},
|
||||
{"stair", ""},
|
||||
|
||||
{"stair", "_inner"},
|
||||
{"slab", "_1"},
|
||||
{"slab", "_2"},
|
||||
{"slab", "_quarter"},
|
||||
{"slab", ""},
|
||||
{"slab", "_three_quarter"},
|
||||
{"slab", "_14"},
|
||||
{"slab", "_15"},
|
||||
|
||||
{"slab", "_two_sides"},
|
||||
{"slab", "_three_sides"},
|
||||
{"slab", "_three_sides_u"},
|
||||
{"stair", "_half"},
|
||||
{"stair", "_alt_1"},
|
||||
{"stair", "_alt_2"},
|
||||
{"stair", "_alt_4"},
|
||||
{"stair", "_alt"},
|
||||
{"stair", "_right_half"},
|
||||
|
||||
{"slope", ""},
|
||||
{"slope", "_half"},
|
||||
{"slope", "_half_raised"},
|
||||
{"slope", "_inner"},
|
||||
{"slope", "_inner_half"},
|
||||
{"slope", "_inner_half_raised"},
|
||||
{"slope", "_inner_cut"},
|
||||
{"slope", "_inner_cut_half"},
|
||||
|
||||
{"slope", "_inner_cut_half_raised"},
|
||||
{"slope", "_outer"},
|
||||
{"slope", "_outer_half"},
|
||||
{"slope", "_outer_half_raised"},
|
||||
{"slope", "_outer_cut"},
|
||||
{"slope", "_outer_cut_half"},
|
||||
{"slope", "_outer_cut_half_raised"},
|
||||
{"slope", "_cut"},
|
||||
}
|
||||
|
||||
local moreblocks_nodes = {
|
||||
"coal_stone",
|
||||
"wood_tile",
|
||||
"iron_checker",
|
||||
"circle_stone_bricks",
|
||||
"cobble_compressed",
|
||||
"plankstone",
|
||||
"clean_glass",
|
||||
"split_stone_tile",
|
||||
"all_faces_tree",
|
||||
"dirt_compressed",
|
||||
"coal_checker",
|
||||
"clean_glow_glass",
|
||||
"tar",
|
||||
"clean_super_glow_glass",
|
||||
"stone_tile",
|
||||
"cactus_brick",
|
||||
"super_glow_glass",
|
||||
"desert_cobble_compressed",
|
||||
"copperpatina",
|
||||
"coal_stone_bricks",
|
||||
"glow_glass",
|
||||
"cactus_checker",
|
||||
"all_faces_pine_tree",
|
||||
"all_faces_aspen_tree",
|
||||
"all_faces_acacia_tree",
|
||||
"all_faces_jungle_tree",
|
||||
"iron_stone",
|
||||
"grey_bricks",
|
||||
"wood_tile_left",
|
||||
"wood_tile_down",
|
||||
"wood_tile_center",
|
||||
"wood_tile_right",
|
||||
"wood_tile_full",
|
||||
"checker_stone_tile",
|
||||
"iron_glass",
|
||||
"iron_stone_bricks",
|
||||
"wood_tile_flipped",
|
||||
"wood_tile_offset",
|
||||
"coal_glass",
|
||||
|
||||
"straw",
|
||||
|
||||
"stone",
|
||||
"stone_block",
|
||||
"cobble",
|
||||
"mossycobble",
|
||||
"brick",
|
||||
"sandstone",
|
||||
"steelblock",
|
||||
"goldblock",
|
||||
"copperblock",
|
||||
"bronzeblock",
|
||||
"diamondblock",
|
||||
"tinblock",
|
||||
"desert_stone",
|
||||
"desert_stone_block",
|
||||
"desert_cobble",
|
||||
"meselamp",
|
||||
"glass",
|
||||
"tree",
|
||||
"wood",
|
||||
"jungletree",
|
||||
"junglewood",
|
||||
"pine_tree",
|
||||
"pine_wood",
|
||||
"acacia_tree",
|
||||
"acacia_wood",
|
||||
"aspen_tree",
|
||||
"aspen_wood",
|
||||
"obsidian",
|
||||
"obsidian_block",
|
||||
"obsidianbrick",
|
||||
"obsidian_glass",
|
||||
"stonebrick",
|
||||
"desert_stonebrick",
|
||||
"sandstonebrick",
|
||||
"silver_sandstone",
|
||||
"silver_sandstone_brick",
|
||||
"silver_sandstone_block",
|
||||
"desert_sandstone",
|
||||
"desert_sandstone_brick",
|
||||
"desert_sandstone_block",
|
||||
"sandstone_block",
|
||||
"coral_skeleton",
|
||||
"ice",
|
||||
}
|
||||
|
||||
local colors_moreblocks = copy(colors)
|
||||
insert(colors_moreblocks, "white")
|
||||
|
||||
local moreblocks_mods = {
|
||||
wool = colors_moreblocks,
|
||||
moreblocks = moreblocks_nodes,
|
||||
}
|
||||
|
||||
local t = {}
|
||||
|
||||
for mod, v in pairs(moreblocks_mods) do
|
||||
for _, nodename in ipairs(v) do
|
||||
t[nodename] = {}
|
||||
|
||||
for _, shape in ipairs(circular_saw_names) do
|
||||
local to_add = true
|
||||
|
||||
if shape[1] == "slope" and shape[2] == "" then
|
||||
to_add = nil
|
||||
end
|
||||
|
||||
if to_add then
|
||||
insert(t[nodename], fmt("%s_%s%s", shape[1], nodename, shape[2]))
|
||||
end
|
||||
end
|
||||
|
||||
local slope_name = fmt("slope_%s", nodename)
|
||||
|
||||
to_compress[fmt("%s:%s", mod, slope_name)] = {
|
||||
replace = slope_name,
|
||||
by = t[nodename]
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
local compressed = {}
|
||||
|
||||
for k, v in pairs(to_compress) do
|
||||
compressed[k] = compressed[k] or {}
|
||||
|
||||
for _, str in ipairs(v.by) do
|
||||
local it = k:gsub(v.replace, str)
|
||||
insert(compressed[k], it)
|
||||
end
|
||||
end
|
||||
|
||||
local _compressed = {}
|
||||
|
||||
for _, v in pairs(compressed) do
|
||||
for _, v2 in ipairs(v) do
|
||||
_compressed[v2] = true
|
||||
end
|
||||
end
|
||||
|
||||
i3.compress_groups, i3.compressed = compressed, _compressed
|
60
src/groups.lua
Normal file
60
src/groups.lua
Normal file
@ -0,0 +1,60 @@
|
||||
local S = i3.get("S")
|
||||
|
||||
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",
|
||||
}
|
||||
|
||||
i3.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",
|
||||
}
|
1349
src/gui.lua
Normal file
1349
src/gui.lua
Normal file
File diff suppressed because it is too large
Load Diff
597
src/inventory.lua
Normal file
597
src/inventory.lua
Normal file
@ -0,0 +1,597 @@
|
||||
local _, get_inventory_fs = i3.files.gui()
|
||||
|
||||
local S, clr = i3.get("S", "clr")
|
||||
local min, ceil, random = i3.get("min", "ceil", "random")
|
||||
local reg_items, reg_aliases = i3.get("reg_items", "reg_aliases")
|
||||
local fmt, find, match, sub, lower = i3.get("fmt", "find", "match", "sub", "lower")
|
||||
local vec_new, vec_mul, vec_eq, vec_round = i3.get("vec_new", "vec_mul", "vec_eq", "vec_round")
|
||||
local sort, copy, insert, remove, indexof = i3.get("sort", "copy", "insert", "remove", "indexof")
|
||||
|
||||
local msg, is_fav = i3.get("msg", "is_fav")
|
||||
local is_group, extract_groups, groups_to_items =
|
||||
i3.get("is_group", "extract_groups", "groups_to_items")
|
||||
local search, get_sorting_idx, sort_inventory, sort_by_category, apply_recipe_filters =
|
||||
i3.get("search", "get_sorting_idx", "sort_inventory", "sort_by_category", "apply_recipe_filters")
|
||||
local show_item, spawn_item, clean_name, compressible, check_privs =
|
||||
i3.get("show_item", "spawn_item", "clean_name", "compressible", "check_privs")
|
||||
|
||||
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 check_privs(name, {creative = true}) or old_is_creative_enabled(name)
|
||||
end
|
||||
|
||||
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
|
||||
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
|
||||
|
||||
local function get_recipes(player, item)
|
||||
local clean_item = reg_aliases[item] or item
|
||||
local recipes = i3.recipes_cache[clean_item]
|
||||
local usages = i3.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 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
|
||||
|
||||
local function safe_teleport(player, pos)
|
||||
pos.y = pos.y + 0.5
|
||||
local vel = player:get_velocity()
|
||||
player:add_velocity(vec_mul(vel, -1))
|
||||
player:set_pos(pos)
|
||||
end
|
||||
|
||||
i3.new_tab {
|
||||
name = "inventory",
|
||||
description = S"Inventory",
|
||||
formspec = get_inventory_fs,
|
||||
|
||||
fields = function(player, data, fields)
|
||||
local name = player:get_player_name()
|
||||
local inv = player:get_inventory()
|
||||
local sb_inv = fields.scrbar_inv
|
||||
|
||||
if fields.skins then
|
||||
local id = tonumber(fields.skins)
|
||||
local _skins = skins.get_skinlist_for_player(name)
|
||||
skins.set_player_skin(player, _skins[id])
|
||||
end
|
||||
|
||||
for field in pairs(fields) do
|
||||
if sub(field, 1, 4) == "btn_" then
|
||||
data.subcat = indexof(i3.SUBCAT, sub(field, 5))
|
||||
break
|
||||
|
||||
elseif sub(field, 1, 3) == "cb_" then
|
||||
local str = sub(field, 4)
|
||||
data[str] = false
|
||||
|
||||
if fields[field] == "true" then
|
||||
data[str] = true
|
||||
end
|
||||
|
||||
elseif sub(field, 1, 8) == "setting_" then
|
||||
data.show_setting = match(field, "_(%w+)$")
|
||||
|
||||
elseif find(field, "waypoint_%d+") then
|
||||
local id, action = match(field, "_(%d+)_(%w+)$")
|
||||
id = tonumber(id)
|
||||
local waypoint = data.waypoints[id]
|
||||
if not waypoint then return end
|
||||
|
||||
if action == "delete" then
|
||||
player:hud_remove(waypoint.id)
|
||||
remove(data.waypoints, id)
|
||||
|
||||
elseif action == "teleport" then
|
||||
local pos = vec_new(waypoint.pos)
|
||||
safe_teleport(player, pos)
|
||||
msg(name, fmt("Teleported to %s", clr("#ff0", waypoint.name)))
|
||||
|
||||
elseif action == "refresh" then
|
||||
local color = random(0xffffff)
|
||||
waypoint.color = color
|
||||
player:hud_change(waypoint.id, "number", color)
|
||||
|
||||
elseif action == "hide" then
|
||||
if waypoint.hide then
|
||||
local new_id = player:hud_add {
|
||||
hud_elem_type = "waypoint",
|
||||
name = waypoint.name,
|
||||
text = " m",
|
||||
world_pos = waypoint.pos,
|
||||
number = waypoint.color,
|
||||
z_index = -300,
|
||||
}
|
||||
|
||||
waypoint.id = new_id
|
||||
waypoint.hide = nil
|
||||
else
|
||||
player:hud_remove(waypoint.id)
|
||||
waypoint.hide = true
|
||||
end
|
||||
end
|
||||
|
||||
break
|
||||
end
|
||||
end
|
||||
|
||||
if fields.quit then
|
||||
data.confirm_trash = nil
|
||||
data.show_settings = nil
|
||||
|
||||
elseif fields.trash then
|
||||
data.show_settings = nil
|
||||
data.confirm_trash = true
|
||||
|
||||
elseif fields.settings then
|
||||
data.confirm_trash = nil
|
||||
data.show_settings = true
|
||||
|
||||
elseif fields.confirm_trash_yes or fields.confirm_trash_no then
|
||||
if fields.confirm_trash_yes then
|
||||
inv:set_list("main", {})
|
||||
inv:set_list("craft", {})
|
||||
end
|
||||
|
||||
data.confirm_trash = nil
|
||||
|
||||
elseif fields.close_settings then
|
||||
data.show_settings = nil
|
||||
|
||||
elseif fields.sort then
|
||||
sort_inventory(player, data)
|
||||
|
||||
elseif fields.prev_sort or fields.next_sort then
|
||||
local idx = get_sorting_idx(data.sort)
|
||||
local tot = #i3.sorting_methods
|
||||
|
||||
idx = idx - (fields.prev_sort and 1 or -1)
|
||||
|
||||
if idx > tot then
|
||||
idx = 1
|
||||
elseif idx == 0 then
|
||||
idx = tot
|
||||
end
|
||||
|
||||
data.sort = i3.sorting_methods[idx].name
|
||||
|
||||
elseif fields.home then
|
||||
if not data.home then
|
||||
return msg(name, "No home set")
|
||||
elseif not check_privs(name, {home = true}) then
|
||||
return msg(name, "'home' privilege missing")
|
||||
end
|
||||
|
||||
safe_teleport(player, core.string_to_pos(data.home))
|
||||
msg(name, S"Welcome back home!")
|
||||
|
||||
elseif fields.set_home then
|
||||
data.home = core.pos_to_string(player:get_pos(), 1)
|
||||
|
||||
elseif sb_inv and sub(sb_inv, 1, 3) == "CHG" then
|
||||
data.scrbar_inv = tonumber(match(sb_inv, "%d+"))
|
||||
return
|
||||
|
||||
elseif fields.waypoint_add then
|
||||
local pos = player:get_pos()
|
||||
|
||||
for _, v in ipairs(data.waypoints) do
|
||||
if vec_eq(vec_round(pos), vec_round(v.pos)) then
|
||||
return msg(name, "You already set a waypoint at this position")
|
||||
end
|
||||
end
|
||||
|
||||
local waypoint = fields.waypoint_name
|
||||
|
||||
if fields.waypoint_name == "" then
|
||||
waypoint = "Waypoint"
|
||||
end
|
||||
|
||||
local color = random(0xffffff)
|
||||
|
||||
local id = player:hud_add {
|
||||
hud_elem_type = "waypoint",
|
||||
name = waypoint,
|
||||
text = " m",
|
||||
world_pos = pos,
|
||||
number = color,
|
||||
z_index = -300,
|
||||
}
|
||||
|
||||
insert(data.waypoints, {name = waypoint, pos = pos, color = color, id = id})
|
||||
data.scrbar_inv = data.scrbar_inv + 1000
|
||||
end
|
||||
|
||||
return i3.set_fs(player)
|
||||
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 end
|
||||
|
||||
if compressible(item, data) then
|
||||
local idx
|
||||
|
||||
for i = 1, #data.items do
|
||||
local it = data.items[i]
|
||||
if it == item then
|
||||
idx = i
|
||||
break
|
||||
end
|
||||
end
|
||||
|
||||
if data.expand ~= "" then
|
||||
data.alt_items = nil
|
||||
|
||||
if item == data.expand then
|
||||
data.expand = nil
|
||||
return
|
||||
end
|
||||
end
|
||||
|
||||
if idx and item ~= data.expand then
|
||||
data.alt_items = copy(data.items)
|
||||
data.expand = item
|
||||
|
||||
if i3.compress_groups[item] then
|
||||
local items = copy(i3.compress_groups[item])
|
||||
insert(items, fmt("_%s", item))
|
||||
|
||||
sort(items, function(a, b)
|
||||
if a:sub(1, 1) == "_" then
|
||||
a = a:sub(2)
|
||||
end
|
||||
|
||||
return a < b
|
||||
end)
|
||||
|
||||
local i = 1
|
||||
|
||||
for _, v in ipairs(items) do
|
||||
if show_item(reg_items[clean_name(v)]) then
|
||||
insert(data.alt_items, idx + i, v)
|
||||
i = i + 1
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
else
|
||||
if 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)
|
||||
|
||||
return get_stack(player, stack)
|
||||
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
|
||||
end
|
||||
|
||||
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)
|
||||
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 iter = ceil(count / stackmax)
|
||||
local leftover = count
|
||||
|
||||
for _ = 1, iter do
|
||||
local c = min(stackmax, leftover)
|
||||
local stack = ItemStack(fmt("%s %s", stackname, c))
|
||||
get_stack(player, stack)
|
||||
leftover = leftover - stackmax
|
||||
end
|
||||
end
|
||||
|
||||
local function rcp_fields(player, data, fields)
|
||||
local name = player:get_player_name()
|
||||
local sb_rcp, sb_usg = fields.scrbar_rcp, fields.scrbar_usg
|
||||
|
||||
if fields.cancel then
|
||||
reset_data(data)
|
||||
|
||||
elseif fields.exit then
|
||||
data.query_item = nil
|
||||
|
||||
elseif fields.key_enter_field == "filter" or fields.search then
|
||||
if fields.filter == "" then
|
||||
reset_data(data)
|
||||
return i3.set_fs(player)
|
||||
end
|
||||
|
||||
local str = lower(fields.filter)
|
||||
if data.filter == str then return end
|
||||
|
||||
data.filter = str
|
||||
data.pagenum = 1
|
||||
|
||||
search(data)
|
||||
|
||||
if data.itab > 1 then
|
||||
sort_by_category(data)
|
||||
end
|
||||
|
||||
elseif fields.prev_page or fields.next_page then
|
||||
if data.pagemax == 1 then return end
|
||||
data.pagenum = data.pagenum - (fields.prev_page and 1 or -1)
|
||||
|
||||
if data.pagenum > data.pagemax then
|
||||
data.pagenum = 1
|
||||
elseif data.pagenum == 0 then
|
||||
data.pagenum = data.pagemax
|
||||
end
|
||||
|
||||
elseif fields.prev_recipe or fields.next_recipe then
|
||||
local num = data.rnum + (fields.prev_recipe and -1 or 1)
|
||||
data.rnum = data.recipes[num] and num or (fields.prev_recipe and #data.recipes or 1)
|
||||
data.export_rcp = nil
|
||||
data.scrbar_rcp = 1
|
||||
|
||||
elseif fields.prev_usage or fields.next_usage then
|
||||
local num = data.unum + (fields.prev_usage and -1 or 1)
|
||||
data.unum = data.usages[num] and num or (fields.prev_usage and #data.usages or 1)
|
||||
data.export_usg = nil
|
||||
data.scrbar_usg = 1
|
||||
|
||||
elseif fields.fav then
|
||||
local fav, i = is_fav(data.favs, data.query_item)
|
||||
local total = #data.favs
|
||||
|
||||
if total < i3.MAX_FAVS and not fav then
|
||||
data.favs[total + 1] = data.query_item
|
||||
elseif fav then
|
||||
remove(data.favs, i)
|
||||
end
|
||||
|
||||
elseif fields.export_rcp or fields.export_usg then
|
||||
if fields.export_rcp then
|
||||
data.export_rcp = not data.export_rcp
|
||||
|
||||
if not data.export_rcp then
|
||||
data.scrbar_rcp = 1
|
||||
end
|
||||
else
|
||||
data.export_usg = not data.export_usg
|
||||
|
||||
if not data.export_usg then
|
||||
data.scrbar_usg = 1
|
||||
end
|
||||
end
|
||||
|
||||
elseif (sb_rcp and sub(sb_rcp, 1, 3) == "CHG") or (sb_usg and sub(sb_usg, 1, 3) == "CHG") then
|
||||
data.scrbar_rcp = sb_rcp and tonumber(match(sb_rcp, "%d+"))
|
||||
data.scrbar_usg = sb_usg and tonumber(match(sb_usg, "%d+"))
|
||||
|
||||
elseif fields.craft_rcp or fields.craft_usg then
|
||||
craft_stack(player, data, fields.craft_rcp)
|
||||
|
||||
if fields.craft_rcp then
|
||||
data.export_rcp = nil
|
||||
data.scrbar_rcp = 1
|
||||
else
|
||||
data.export_usg = nil
|
||||
data.scrbar_usg = 1
|
||||
end
|
||||
else
|
||||
select_item(player, name, data, fields)
|
||||
end
|
||||
end
|
||||
|
||||
core.register_on_player_receive_fields(function(player, formname, fields)
|
||||
local name = player:get_player_name()
|
||||
|
||||
if formname == "i3_outdated" then
|
||||
return false, core.kick_player(name, "Come back when your client is up-to-date.")
|
||||
elseif formname ~= "" then
|
||||
return false
|
||||
end
|
||||
|
||||
local data = i3.data[name]
|
||||
if not data then return end
|
||||
|
||||
for f in pairs(fields) do
|
||||
if sub(f, 1, 4) == "tab_" then
|
||||
local tabname = sub(f, 5)
|
||||
i3.set_tab(player, tabname)
|
||||
break
|
||||
elseif sub(f, 1, 5) == "itab_" then
|
||||
data.pagenum = 1
|
||||
data.itab = tonumber(f:sub(-1))
|
||||
sort_by_category(data)
|
||||
end
|
||||
end
|
||||
|
||||
rcp_fields(player, data, fields)
|
||||
|
||||
local tab = i3.tabs[data.tab]
|
||||
|
||||
if tab and tab.fields then
|
||||
return true, tab.fields(player, data, fields)
|
||||
end
|
||||
|
||||
return true, i3.set_fs(player)
|
||||
end)
|
||||
|
||||
core.register_on_player_hpchange(function(player, hpchange)
|
||||
local name = player:get_player_name()
|
||||
local data = i3.data[name]
|
||||
if not data then return end
|
||||
|
||||
local hp_max = player:get_properties().hp_max
|
||||
data.hp = min(hp_max, player:get_hp() + hpchange)
|
||||
|
||||
i3.set_fs(player)
|
||||
end)
|
||||
|
||||
core.register_on_dieplayer(function(player)
|
||||
local name = player:get_player_name()
|
||||
local data = i3.data[name]
|
||||
if not data then return end
|
||||
|
||||
if data.bag_size then
|
||||
data.bag_item = nil
|
||||
data.bag_size = nil
|
||||
data.bag:set_list("main", {})
|
||||
|
||||
local inv = player:get_inventory()
|
||||
inv:set_size("main", i3.INV_SIZE)
|
||||
end
|
||||
|
||||
i3.set_fs(player)
|
||||
end)
|
||||
|
||||
core.register_on_chatcommand(function(name)
|
||||
local player = core.get_player_by_name(name)
|
||||
core.after(0, i3.set_fs, player)
|
||||
end)
|
||||
|
||||
core.register_on_priv_grant(function(name, _, priv)
|
||||
if priv == "creative" or priv == "all" then
|
||||
local data = i3.data[name]
|
||||
reset_data(data)
|
||||
data.favs = {}
|
||||
|
||||
local player = core.get_player_by_name(name)
|
||||
core.after(0, i3.set_fs, player)
|
||||
end
|
||||
end)
|
||||
|
||||
core.register_on_player_inventory_action(function(player, _, _, info)
|
||||
local name = player:get_player_name()
|
||||
|
||||
if not core.is_creative_enabled(name) and
|
||||
((info.from_list == "main" and info.to_list == "craft") or
|
||||
(info.from_list == "craft" and info.to_list == "main") or
|
||||
(info.from_list == "craftresult" and info.to_list == "main")) then
|
||||
i3.set_fs(player)
|
||||
end
|
||||
end)
|
||||
|
||||
local trash = core.create_detached_inventory("i3_trash", {
|
||||
allow_put = function(_, _, _, stack)
|
||||
return stack:get_count()
|
||||
end,
|
||||
on_put = function(inv, listname, _, _, player)
|
||||
inv:set_list(listname, {})
|
||||
|
||||
local name = player:get_player_name()
|
||||
|
||||
if not core.is_creative_enabled(name) then
|
||||
i3.set_fs(player)
|
||||
end
|
||||
end,
|
||||
})
|
||||
|
||||
trash:set_size("main", 1)
|
||||
|
||||
local output_rcp = core.create_detached_inventory("i3_output_rcp", {})
|
||||
output_rcp:set_size("main", 1)
|
||||
|
||||
local output_usg = core.create_detached_inventory("i3_output_usg", {})
|
||||
output_usg:set_size("main", 1)
|
13
src/model_aliases.lua
Normal file
13
src/model_aliases.lua
Normal file
@ -0,0 +1,13 @@
|
||||
local 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"},
|
||||
}
|
||||
|
||||
return model_alias
|
273
src/progressive.lua
Normal file
273
src/progressive.lua
Normal file
@ -0,0 +1,273 @@
|
||||
local singleplayer = core.is_singleplayer()
|
||||
|
||||
local fmt, search, table_merge, array_diff =
|
||||
i3.get("fmt", "search", "table_merge", "array_diff")
|
||||
local is_group, extract_groups, item_has_groups, apply_recipe_filters =
|
||||
i3.get("is_group", "extract_groups", "item_has_groups", "apply_recipe_filters")
|
||||
|
||||
local POLL_FREQ = 0.25
|
||||
local HUD_TIMER_MAX = 1.5
|
||||
|
||||
local function get_filtered_items(player, data)
|
||||
local items, known, c = {}, 0, 0
|
||||
|
||||
for i = 1, #i3.init_items do
|
||||
local item = i3.init_items[i]
|
||||
local recipes = i3.recipes_cache[item]
|
||||
local usages = i3.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 item_in_inv(item, inv_items)
|
||||
local inv_items_size = #inv_items
|
||||
|
||||
if is_group(item) then
|
||||
local groups = extract_groups(item)
|
||||
|
||||
for i = 1, inv_items_size do
|
||||
local def = core.registered_items[inv_items[i]]
|
||||
|
||||
if def then
|
||||
if item_has_groups(def.groups, groups) then
|
||||
return true
|
||||
end
|
||||
end
|
||||
end
|
||||
else
|
||||
for i = 1, inv_items_size do
|
||||
if inv_items[i] == item then
|
||||
return true
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local function recipe_in_inv(rcp, inv_items)
|
||||
for _, item in pairs(rcp.items) do
|
||||
if not item_in_inv(item, inv_items) then return end
|
||||
end
|
||||
|
||||
return true
|
||||
end
|
||||
|
||||
local function progressive_filter(recipes, player)
|
||||
if not recipes then
|
||||
return {}
|
||||
end
|
||||
|
||||
local name = player:get_player_name()
|
||||
local data = i3.data[name]
|
||||
|
||||
if #data.inv_items == 0 then
|
||||
return {}
|
||||
end
|
||||
|
||||
local filtered, c = {}, 0
|
||||
|
||||
for i = 1, #recipes do
|
||||
local recipe = recipes[i]
|
||||
if recipe_in_inv(recipe, data.inv_items) then
|
||||
c = c + 1
|
||||
filtered[c] = recipe
|
||||
end
|
||||
end
|
||||
|
||||
return filtered
|
||||
end
|
||||
|
||||
local item_lists = {"main", "craft", "craftpreview"}
|
||||
|
||||
local function get_inv_items(player)
|
||||
local inv = player:get_inventory()
|
||||
local stacks = {}
|
||||
|
||||
for i = 1, #item_lists do
|
||||
local list = inv:get_list(item_lists[i])
|
||||
table_merge(stacks, list)
|
||||
end
|
||||
|
||||
local inv_items, c = {}, 0
|
||||
|
||||
for i = 1, #stacks do
|
||||
local stack = stacks[i]
|
||||
|
||||
if not stack:is_empty() then
|
||||
local name = stack:get_name()
|
||||
if core.registered_items[name] then
|
||||
c = c + 1
|
||||
inv_items[c] = name
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return inv_items
|
||||
end
|
||||
|
||||
local function init_hud(player, data)
|
||||
data.hud = {
|
||||
bg = player:hud_add {
|
||||
hud_elem_type = "image",
|
||||
position = {x = 0.78, y = 1},
|
||||
alignment = {x = 1, y = 1},
|
||||
scale = {x = 370, y = 112},
|
||||
text = "i3_bg.png",
|
||||
z_index = 0xDEAD,
|
||||
},
|
||||
|
||||
book = player:hud_add {
|
||||
hud_elem_type = "image",
|
||||
position = {x = 0.79, y = 1.02},
|
||||
alignment = {x = 1, y = 1},
|
||||
scale = {x = 4, y = 4},
|
||||
text = "i3_book.png",
|
||||
z_index = 0xDEAD,
|
||||
},
|
||||
|
||||
text = player:hud_add {
|
||||
hud_elem_type = "text",
|
||||
position = {x = 0.84, y = 1.04},
|
||||
alignment = {x = 1, y = 1},
|
||||
number = 0xffffff,
|
||||
text = "",
|
||||
z_index = 0xDEAD,
|
||||
style = 1,
|
||||
},
|
||||
}
|
||||
end
|
||||
|
||||
local function show_hud_success(player, data)
|
||||
-- It'd better to have an engine function `hud_move` to only need
|
||||
-- 2 calls for the notification's back and forth.
|
||||
|
||||
local hud_info_bg = player:hud_get(data.hud.bg)
|
||||
local dt = 0.016
|
||||
|
||||
if hud_info_bg.position.y <= 0.9 then
|
||||
data.show_hud = false
|
||||
data.hud_timer = (data.hud_timer or 0) + dt
|
||||
end
|
||||
|
||||
if data.show_hud then
|
||||
for _, def in pairs(data.hud) do
|
||||
local hud_info = player:hud_get(def)
|
||||
|
||||
player:hud_change(def, "position", {
|
||||
x = hud_info.position.x,
|
||||
y = hud_info.position.y - (dt / 5)
|
||||
})
|
||||
end
|
||||
|
||||
player:hud_change(data.hud.text, "text",
|
||||
fmt("%u new recipe%s unlocked!", data.discovered, data.discovered > 1 and "s" or ""))
|
||||
|
||||
elseif data.show_hud == false then
|
||||
if data.hud_timer >= HUD_TIMER_MAX then
|
||||
for _, def in pairs(data.hud) do
|
||||
local hud_info = player:hud_get(def)
|
||||
|
||||
player:hud_change(def, "position", {
|
||||
x = hud_info.position.x,
|
||||
y = hud_info.position.y + (dt / 5)
|
||||
})
|
||||
end
|
||||
|
||||
if hud_info_bg.position.y >= 1 then
|
||||
data.show_hud = nil
|
||||
data.hud_timer = nil
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
-- Workaround. Need an engine call to detect when the contents of
|
||||
-- the player inventory changed, instead.
|
||||
local function poll_new_items()
|
||||
local players = core.get_connected_players()
|
||||
|
||||
for i = 1, #players do
|
||||
local player = players[i]
|
||||
local name = player:get_player_name()
|
||||
local data = i3.data[name]
|
||||
if not data then return end
|
||||
|
||||
local inv_items = get_inv_items(player)
|
||||
local diff = array_diff(inv_items, data.inv_items)
|
||||
|
||||
if #diff > 0 then
|
||||
data.inv_items = table_merge(diff, data.inv_items)
|
||||
local oldknown = data.known_recipes or 0
|
||||
local items = get_filtered_items(player, data)
|
||||
data.discovered = data.known_recipes - oldknown
|
||||
|
||||
if data.show_hud == nil and data.discovered > 0 then
|
||||
data.show_hud = true
|
||||
end
|
||||
|
||||
data.items_raw = items
|
||||
data.itab = 1
|
||||
|
||||
search(data)
|
||||
i3.set_fs(player)
|
||||
end
|
||||
end
|
||||
|
||||
core.after(POLL_FREQ, poll_new_items)
|
||||
end
|
||||
|
||||
poll_new_items()
|
||||
|
||||
if singleplayer then
|
||||
core.register_globalstep(function()
|
||||
local name = "singleplayer"
|
||||
local player = core.get_player_by_name(name)
|
||||
local data = i3.data[name]
|
||||
|
||||
if data and data.show_hud ~= nil then
|
||||
show_hud_success(player, data)
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
i3.add_recipe_filter("Default progressive filter", progressive_filter)
|
||||
|
||||
core.register_on_joinplayer(function(player)
|
||||
local name = player:get_player_name()
|
||||
local data = i3.data[name]
|
||||
if not data then return end
|
||||
|
||||
data.inv_items = data.inv_items or {}
|
||||
data.known_recipes = data.known_recipes or 0
|
||||
|
||||
local oldknown = data.known_recipes
|
||||
local items = get_filtered_items(player, data)
|
||||
data.discovered = data.known_recipes - oldknown
|
||||
|
||||
data.items_raw = items
|
||||
search(data)
|
||||
|
||||
if singleplayer then
|
||||
init_hud(player, data)
|
||||
|
||||
if data.show_hud == nil and data.discovered > 0 then
|
||||
data.show_hud = true
|
||||
end
|
||||
end
|
||||
end)
|
288
src/recipes.lua
Normal file
288
src/recipes.lua
Normal file
@ -0,0 +1,288 @@
|
||||
local replacements = {fuel = {}}
|
||||
|
||||
local fmt, reg_items, reg_aliases = i3.get("fmt", "reg_items", "reg_aliases")
|
||||
local maxn, copy, insert, sort, match = i3.get("maxn", "copy", "insert", "sort", "match")
|
||||
|
||||
local is_group, extract_groups, item_has_groups, groups_to_items =
|
||||
i3.get("is_group", "extract_groups", "item_has_groups", "groups_to_items")
|
||||
local true_str, is_table, show_item, table_merge, table_replace, table_eq =
|
||||
i3.get("true_str", "is_table", "show_item", "table_merge", "table_replace", "table_eq")
|
||||
|
||||
local function get_burntime(item)
|
||||
return core.get_craft_result{method = "fuel", items = {item}}.time
|
||||
end
|
||||
|
||||
local function cache_fuel(item)
|
||||
local burntime = get_burntime(item)
|
||||
if burntime > 0 then
|
||||
i3.fuel_cache[item] = {
|
||||
type = "fuel",
|
||||
items = {item},
|
||||
burntime = burntime,
|
||||
replacements = replacements.fuel[item],
|
||||
}
|
||||
end
|
||||
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)
|
||||
|
||||
i3.usages_cache[name] = i3.usages_cache[name] or {}
|
||||
insert(i3.usages_cache[name], 1, usage)
|
||||
|
||||
added[name] = true
|
||||
end
|
||||
end
|
||||
elseif show_item(reg_items[item]) then
|
||||
i3.usages_cache[item] = i3.usages_cache[item] or {}
|
||||
insert(i3.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 = i3.recipes_cache[item] or {}
|
||||
|
||||
for i = 1, #recipes do
|
||||
get_usages(recipes[i])
|
||||
end
|
||||
|
||||
if i3.fuel_cache[item] then
|
||||
i3.usages_cache[item] = table_merge(i3.usages_cache[item] or {}, {i3.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 = core.get_all_craft_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
|
||||
i3.recipes_cache[item] = table_merge(recipes, i3.recipes_cache[item] or {})
|
||||
end
|
||||
end
|
||||
|
||||
--[[ As `core.get_craft_recipe` and `core.get_all_craft_recipes` do not
|
||||
return the fuel, replacements and toolrepair recipes, we have to
|
||||
override `core.register_craft` and do some reverse engineering.
|
||||
See engine's issues #4901, #5745 and #8920. ]]
|
||||
|
||||
local old_register_craft = core.register_craft
|
||||
local rcp_num = {}
|
||||
|
||||
core.register_craft = function(def)
|
||||
old_register_craft(def)
|
||||
|
||||
if def.type == "toolrepair" then
|
||||
i3.toolrepair = def.additional_wear * -100
|
||||
end
|
||||
|
||||
local output = def.output or (true_str(def.recipe) and def.recipe) or nil
|
||||
if not output then return end
|
||||
output = {match(output, "%S+")}
|
||||
|
||||
local groups
|
||||
|
||||
if is_group(output[1]) then
|
||||
groups = extract_groups(output[1])
|
||||
output = groups_to_items(groups, true)
|
||||
end
|
||||
|
||||
for i = 1, #output do
|
||||
local item = output[i]
|
||||
rcp_num[item] = (rcp_num[item] or 0) + 1
|
||||
|
||||
if def.replacements then
|
||||
if def.type == "fuel" then
|
||||
replacements.fuel[item] = def.replacements
|
||||
else
|
||||
replacements[item] = replacements[item] or {}
|
||||
replacements[item][rcp_num[item]] = def.replacements
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local old_clear_craft = core.clear_craft
|
||||
|
||||
core.clear_craft = function(def)
|
||||
old_clear_craft(def)
|
||||
|
||||
if true_str(def) then
|
||||
return -- TODO
|
||||
elseif is_table(def) then
|
||||
return -- TODO
|
||||
end
|
||||
end
|
||||
|
||||
local function resolve_aliases(hash)
|
||||
for oldname, newname in pairs(reg_aliases) do
|
||||
cache_recipes(oldname)
|
||||
local recipes = i3.recipes_cache[oldname]
|
||||
|
||||
if recipes then
|
||||
if not i3.recipes_cache[newname] then
|
||||
i3.recipes_cache[newname] = {}
|
||||
end
|
||||
|
||||
local similar
|
||||
|
||||
for i = 1, #i3.recipes_cache[oldname] do
|
||||
local rcp_old = i3.recipes_cache[oldname][i]
|
||||
|
||||
for j = 1, #i3.recipes_cache[newname] do
|
||||
local rcp_new = copy(i3.recipes_cache[newname][j])
|
||||
rcp_new.output = oldname
|
||||
|
||||
if table_eq(rcp_old, rcp_new) then
|
||||
similar = true
|
||||
break
|
||||
end
|
||||
end
|
||||
|
||||
if not similar then
|
||||
insert(i3.recipes_cache[newname], rcp_old)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if newname ~= "" and i3.recipes_cache[oldname] and not hash[newname] then
|
||||
i3.init_items[#i3.init_items + 1] = newname
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
local function init_recipes()
|
||||
local _select, _preselect = {}, {}
|
||||
|
||||
for name, def in pairs(reg_items) do
|
||||
if name ~= "" and show_item(def) then
|
||||
cache_drops(name, def.drop)
|
||||
cache_fuel(name)
|
||||
cache_recipes(name)
|
||||
|
||||
_preselect[name] = true
|
||||
end
|
||||
end
|
||||
|
||||
for name in pairs(_preselect) do
|
||||
cache_usages(name)
|
||||
|
||||
i3.init_items[#i3.init_items + 1] = name
|
||||
_select[name] = true
|
||||
end
|
||||
|
||||
resolve_aliases(_select)
|
||||
sort(i3.init_items)
|
||||
|
||||
if i3.http and type(i3.export_url) == "string" then
|
||||
local post_data = {
|
||||
recipes = i3.recipes_cache,
|
||||
usages = i3.usages_cache,
|
||||
}
|
||||
|
||||
i3.http.fetch_async {
|
||||
url = i3.export_url,
|
||||
post_data = core.write_json(post_data),
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
return init_recipes
|
120
src/styles.lua
Normal file
120
src/styles.lua
Normal file
@ -0,0 +1,120 @@
|
||||
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 = "i3_sort.png",
|
||||
settings = "i3_settings.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_small = "i3_tab_small.png",
|
||||
tab_top = "i3_tab.png^\\[transformFY",
|
||||
furnace_anim = "i3_furnace_anim.png",
|
||||
bag = "i3_bag.png",
|
||||
armor = "i3_armor.png",
|
||||
awards = "i3_award.png",
|
||||
skins = "i3_skin.png",
|
||||
waypoints = "i3_waypoint.png",
|
||||
teleport = "i3_teleport.png",
|
||||
add = "i3_add.png",
|
||||
refresh = "i3_refresh.png",
|
||||
visible = "i3_visible.png^\\[brighten",
|
||||
nonvisible = "i3_non_visible.png",
|
||||
exit = "i3_exit.png",
|
||||
home = "i3_home.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_hover = "i3_sort.png^\\[brighten",
|
||||
settings_hover = "i3_settings.png^\\[brighten",
|
||||
prev_hover = "i3_next_hover.png^\\[transformFX",
|
||||
next_hover = "i3_next_hover.png",
|
||||
tab_hover = "i3_tab_hover.png",
|
||||
tab_small_hover = "i3_tab_small_hover.png",
|
||||
tab_hover_top = "i3_tab_hover.png^\\[transformFY",
|
||||
bag_hover = "i3_bag_hover.png",
|
||||
armor_hover = "i3_armor_hover.png",
|
||||
awards_hover = "i3_award_hover.png",
|
||||
skins_hover = "i3_skin_hover.png",
|
||||
waypoints_hover = "i3_waypoint_hover.png",
|
||||
teleport_hover = "i3_teleport.png^\\[brighten",
|
||||
add_hover = "i3_add.png^\\[brighten",
|
||||
refresh_hover = "i3_refresh.png^\\[brighten",
|
||||
exit_hover = "i3_exit.png^\\[brighten",
|
||||
home_hover = "i3_home.png^\\[brighten",
|
||||
}
|
||||
|
||||
local styles = string.format([[
|
||||
style_type[field;border=false;bgcolor=transparent]
|
||||
style_type[label,field;font_size=16]
|
||||
style_type[button;border=false;content_offset=0]
|
||||
style_type[image_button,item_image_button,checkbox;border=false;sound=i3_click]
|
||||
style_type[item_image_button;bgimg_hovered=%s]
|
||||
|
||||
style[pagenum,no_item,no_rcp;font=bold;font_size=18]
|
||||
style[exit;fgimg=%s;fgimg_hovered=%s;content_offset=0]
|
||||
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[waypoint_add;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;
|
||||
bgimg_pressed=i3_btn9_pressed.png;bgimg_middle=4,6]
|
||||
style[confirm_trash_yes,confirm_trash_no,set_home;noclip=true;font_size=16;
|
||||
bgimg=i3_btn9.png;bgimg_hovered=i3_btn9_hovered.png;
|
||||
bgimg_pressed=i3_btn9_pressed.png;bgimg_middle=4,6]
|
||||
]],
|
||||
PNG.slot,
|
||||
PNG.exit, PNG.exit_hover,
|
||||
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,
|
||||
PNG.add, PNG.add_hover)
|
||||
|
||||
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]",
|
||||
checkbox = "checkbox[%f,%f;%s;%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]",
|
||||
}
|
||||
|
||||
return PNG, styles, fs_elements
|
Reference in New Issue
Block a user