Cleaning + API fix
This commit is contained in:
parent
9afbee72ce
commit
5587341f2e
|
@ -280,6 +280,8 @@ function i3.compress(item, def)
|
||||||
return err "i3.compress: replace string missing"
|
return err "i3.compress: replace string missing"
|
||||||
elseif not is_table(def.by) then
|
elseif not is_table(def.by) then
|
||||||
return err "i3.compress: replace substrings missing"
|
return err "i3.compress: replace substrings missing"
|
||||||
|
elseif i3.compressed[item] then
|
||||||
|
return err(fmt("i3.compress: item '%s' is already compressed", item))
|
||||||
end
|
end
|
||||||
|
|
||||||
local t = {}
|
local t = {}
|
||||||
|
@ -302,8 +304,6 @@ function i3.add_sorting_method(def)
|
||||||
return err "i3.add_sorting_method: name missing"
|
return err "i3.add_sorting_method: name missing"
|
||||||
elseif not is_func(def.func) then
|
elseif not is_func(def.func) then
|
||||||
return err "i3.add_sorting_method: function missing"
|
return err "i3.add_sorting_method: function missing"
|
||||||
elseif i3.compressed[def.name] then
|
|
||||||
return err(fmt("i3.add_sorting_method: item '%s' is already compressed", def.name))
|
|
||||||
end
|
end
|
||||||
|
|
||||||
insert(i3.sorting_methods, def)
|
insert(i3.sorting_methods, def)
|
||||||
|
|
123
src/common.lua
123
src/common.lua
|
@ -1,8 +1,15 @@
|
||||||
local translate = core.get_translated_string
|
local fmt, find, match, gmatch, sub, split, lower =
|
||||||
local fmt, find, gmatch, match, sub, split, lower =
|
string.format, string.find, string.match, string.gmatch, string.sub, string.split, string.lower
|
||||||
string.format, string.find, string.gmatch, string.match, string.sub, string.split, string.lower
|
|
||||||
local reg_items, reg_nodes, reg_craftitems, reg_tools =
|
local old_is_creative_enabled = core.is_creative_enabled
|
||||||
core.registered_items, core.registered_nodes, core.registered_craftitems, core.registered_tools
|
|
||||||
|
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
|
||||||
|
|
||||||
local S = core.get_translator "i3"
|
local S = core.get_translator "i3"
|
||||||
local ES = function(...) return core.formspec_escape(S(...)) end
|
local ES = function(...) return core.formspec_escape(S(...)) end
|
||||||
|
@ -36,6 +43,19 @@ local function reset_compression(data)
|
||||||
data.expand = ""
|
data.expand = ""
|
||||||
end
|
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 search(data)
|
local function search(data)
|
||||||
reset_compression(data)
|
reset_compression(data)
|
||||||
|
|
||||||
|
@ -60,7 +80,7 @@ local function search(data)
|
||||||
for i = 1, #data.items_raw do
|
for i = 1, #data.items_raw do
|
||||||
local item = data.items_raw[i]
|
local item = data.items_raw[i]
|
||||||
local def = core.registered_items[item]
|
local def = core.registered_items[item]
|
||||||
local desc = lower(translate(data.lang_code, def and def.description)) or ""
|
local desc = lower(core.get_translated_string(data.lang_code, def and def.description)) or ""
|
||||||
local search_in = fmt("%s %s", item, desc)
|
local search_in = fmt("%s %s", item, desc)
|
||||||
local temp, j, to_add = {}, 1
|
local temp, j, to_add = {}, 1
|
||||||
|
|
||||||
|
@ -174,6 +194,14 @@ local function rcp_eq(rcp, rcp2)
|
||||||
return true
|
return true
|
||||||
end
|
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 is_group(item)
|
local function is_group(item)
|
||||||
return sub(item, 1, 6) == "group:"
|
return sub(item, 1, 6) == "group:"
|
||||||
end
|
end
|
||||||
|
@ -202,7 +230,7 @@ local function groups_to_items(groups, get_all)
|
||||||
if not get_all and #groups == 1 then
|
if not get_all and #groups == 1 then
|
||||||
local group = groups[1]
|
local group = groups[1]
|
||||||
local stereotype = i3.group_stereotypes[group]
|
local stereotype = i3.group_stereotypes[group]
|
||||||
local def = reg_items[stereotype]
|
local def = core.registered_items[stereotype]
|
||||||
|
|
||||||
if show_item(def) then
|
if show_item(def) then
|
||||||
return stereotype
|
return stereotype
|
||||||
|
@ -211,7 +239,7 @@ local function groups_to_items(groups, get_all)
|
||||||
|
|
||||||
local names = {}
|
local names = {}
|
||||||
|
|
||||||
for name, def in pairs(reg_items) do
|
for name, def in pairs(core.registered_items) do
|
||||||
if show_item(def) and item_has_groups(def.groups, groups) then
|
if show_item(def) and item_has_groups(def.groups, groups) then
|
||||||
if get_all then
|
if get_all then
|
||||||
names[#names + 1] = name
|
names[#names + 1] = name
|
||||||
|
@ -240,27 +268,6 @@ local function compressible(item, data)
|
||||||
return compression_active(data) and i3.compress_groups[item]
|
return compression_active(data) and i3.compress_groups[item]
|
||||||
end
|
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 function is_fav(favs, query_item)
|
||||||
local fav, i
|
local fav, i
|
||||||
for j = 1, #favs do
|
for j = 1, #favs do
|
||||||
|
@ -290,9 +297,9 @@ local function sort_by_category(data)
|
||||||
local to_add = true
|
local to_add = true
|
||||||
|
|
||||||
if data.itab == 2 then
|
if data.itab == 2 then
|
||||||
to_add = reg_nodes[item]
|
to_add = core.registered_nodes[item]
|
||||||
elseif data.itab == 3 then
|
elseif data.itab == 3 then
|
||||||
to_add = reg_craftitems[item] or reg_tools[item]
|
to_add = core.registered_craftitems[item] or core.registered_tools[item]
|
||||||
end
|
end
|
||||||
|
|
||||||
if to_add then
|
if to_add then
|
||||||
|
@ -312,6 +319,42 @@ local function spawn_item(player, stack)
|
||||||
core.add_item(look_at, stack)
|
core.add_item(look_at, stack)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local function get_recipes(player, item)
|
||||||
|
local clean_item = core.registered_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(vector.multiply(vel, -1))
|
||||||
|
player:set_pos(pos)
|
||||||
|
end
|
||||||
|
|
||||||
local function get_sorting_idx(name)
|
local function get_sorting_idx(name)
|
||||||
local idx = 1
|
local idx = 1
|
||||||
|
|
||||||
|
@ -442,23 +485,30 @@ local _ = {
|
||||||
-- Sorting
|
-- Sorting
|
||||||
search = search,
|
search = search,
|
||||||
sorter = sorter,
|
sorter = sorter,
|
||||||
|
get_recipes = get_recipes,
|
||||||
sort_inventory = sort_inventory,
|
sort_inventory = sort_inventory,
|
||||||
get_sorting_idx = get_sorting_idx,
|
get_sorting_idx = get_sorting_idx,
|
||||||
sort_by_category = sort_by_category,
|
sort_by_category = sort_by_category,
|
||||||
apply_recipe_filters = apply_recipe_filters,
|
apply_recipe_filters = apply_recipe_filters,
|
||||||
|
|
||||||
-- Misc. functions
|
-- Type checks
|
||||||
err = err,
|
|
||||||
msg = msg,
|
|
||||||
is_fav = is_fav,
|
is_fav = is_fav,
|
||||||
is_str = is_str,
|
is_str = is_str,
|
||||||
is_num = is_num,
|
is_num = is_num,
|
||||||
is_func = is_func,
|
is_func = is_func,
|
||||||
show_item = show_item,
|
|
||||||
spawn_item = spawn_item,
|
|
||||||
true_str = true_str,
|
true_str = true_str,
|
||||||
true_table = true_table,
|
true_table = true_table,
|
||||||
|
|
||||||
|
-- Console
|
||||||
|
err = err,
|
||||||
|
msg = msg,
|
||||||
|
|
||||||
|
-- Misc. functions
|
||||||
|
get_stack = get_stack,
|
||||||
|
show_item = show_item,
|
||||||
|
spawn_item = spawn_item,
|
||||||
clean_name = clean_name,
|
clean_name = clean_name,
|
||||||
|
safe_teleport = safe_teleport,
|
||||||
|
|
||||||
-- Core functions
|
-- Core functions
|
||||||
clr = core.colorize,
|
clr = core.colorize,
|
||||||
|
@ -517,7 +567,6 @@ local _ = {
|
||||||
vec_add = vector.add,
|
vec_add = vector.add,
|
||||||
vec_round = vector.round,
|
vec_round = vector.round,
|
||||||
vec_eq = vector.equals,
|
vec_eq = vector.equals,
|
||||||
vec_mul = vector.multiply,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function i3.get(...)
|
function i3.get(...)
|
||||||
|
|
|
@ -4,26 +4,16 @@ local S, clr = i3.get("S", "clr")
|
||||||
local min, ceil, random = i3.get("min", "ceil", "random")
|
local min, ceil, random = i3.get("min", "ceil", "random")
|
||||||
local reg_items, reg_aliases = i3.get("reg_items", "reg_aliases")
|
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 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 vec_new, vec_eq, vec_round = i3.get("vec_new", "vec_eq", "vec_round")
|
||||||
local sort, copy, insert, remove, indexof = i3.get("sort", "copy", "insert", "remove", "indexof")
|
local sort, copy, insert, remove, indexof = i3.get("sort", "copy", "insert", "remove", "indexof")
|
||||||
|
|
||||||
local msg, is_fav = i3.get("msg", "is_fav")
|
local msg, is_fav = i3.get("msg", "is_fav")
|
||||||
local is_group, extract_groups, groups_to_items =
|
local is_group, extract_groups, groups_to_items =
|
||||||
i3.get("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 =
|
local search, get_sorting_idx, sort_inventory, sort_by_category, get_recipes =
|
||||||
i3.get("search", "get_sorting_idx", "sort_inventory", "sort_by_category", "apply_recipe_filters")
|
i3.get("search", "get_sorting_idx", "sort_inventory", "sort_by_category", "get_recipes")
|
||||||
local show_item, spawn_item, clean_name, compressible, check_privs =
|
local show_item, get_stack, clean_name, compressible, check_privs, safe_teleport =
|
||||||
i3.get("show_item", "spawn_item", "clean_name", "compressible", "check_privs")
|
i3.get("show_item", "get_stack", "clean_name", "compressible", "check_privs", "safe_teleport")
|
||||||
|
|
||||||
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)
|
local function reset_data(data)
|
||||||
data.filter = ""
|
data.filter = ""
|
||||||
|
@ -49,42 +39,6 @@ local function reset_data(data)
|
||||||
end
|
end
|
||||||
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 {
|
i3.new_tab {
|
||||||
name = "inventory",
|
name = "inventory",
|
||||||
description = S"Inventory",
|
description = S"Inventory",
|
||||||
|
|
Ŝarĝante…
Reference in New Issue