use local "ui" to reference "unified_inventory", where practical

(makes code shorter, easier to read and write)
This commit is contained in:
Vanessa Dannenberg 2021-03-07 08:18:17 -05:00
parent d52303a89e
commit 55692900f9
5 changed files with 175 additions and 170 deletions

91
api.lua
View File

@ -1,5 +1,6 @@
local S = minetest.get_translator("unified_inventory")
local F = minetest.formspec_escape
local ui = unified_inventory
-- Create detached creative inventory after loading all mods
minetest.after(0.01, function()
@ -8,12 +9,12 @@ minetest.after(0.01, function()
if not rev_aliases[target] then rev_aliases[target] = {} end
table.insert(rev_aliases[target], source)
end
unified_inventory.items_list = {}
ui.items_list = {}
for name, def in pairs(minetest.registered_items) do
if (not def.groups.not_in_creative_inventory or
def.groups.not_in_creative_inventory == 0) and
def.description and def.description ~= "" then
table.insert(unified_inventory.items_list, name)
table.insert(ui.items_list, name)
local all_names = rev_aliases[name] or {}
table.insert(all_names, name)
for _, player_name in ipairs(all_names) do
@ -26,30 +27,30 @@ minetest.after(0.01, function()
for _,chk in pairs(recipe.items) do
local groupchk = string.find(chk, "group:")
if (not groupchk and not minetest.registered_items[chk])
or (groupchk and not unified_inventory.get_group_item(string.gsub(chk, "group:", "")).item)
or (groupchk and not ui.get_group_item(string.gsub(chk, "group:", "")).item)
or minetest.get_item_group(chk, "not_in_craft_guide") ~= 0 then
unknowns = true
end
end
if not unknowns then
unified_inventory.register_craft(recipe)
ui.register_craft(recipe)
end
end
end
end
end
end
table.sort(unified_inventory.items_list)
unified_inventory.items_list_size = #unified_inventory.items_list
print("Unified Inventory. inventory size: "..unified_inventory.items_list_size)
for _, name in ipairs(unified_inventory.items_list) do
table.sort(ui.items_list)
ui.items_list_size = #ui.items_list
print("Unified Inventory. inventory size: "..ui.items_list_size)
for _, name in ipairs(ui.items_list) do
local def = minetest.registered_items[name]
-- Simple drops
if type(def.drop) == "string" then
local dstack = ItemStack(def.drop)
if not dstack:is_empty() and dstack:get_name() ~= name then
unified_inventory.register_craft({
ui.register_craft({
type = "digging",
items = {name},
output = def.drop,
@ -115,7 +116,7 @@ minetest.after(0.01, function()
end
end
for itemstring, count in pairs(drop_guaranteed) do
unified_inventory.register_craft({
ui.register_craft({
type = "digging",
items = {name},
output = itemstring .. " " .. count,
@ -123,7 +124,7 @@ minetest.after(0.01, function()
})
end
for itemstring, count in pairs(drop_maybe) do
unified_inventory.register_craft({
ui.register_craft({
type = "digging_chance",
items = {name},
output = itemstring .. " " .. count,
@ -132,22 +133,22 @@ minetest.after(0.01, function()
end
end
end
for _, recipes in pairs(unified_inventory.crafts_for.recipe) do
for _, recipes in pairs(ui.crafts_for.recipe) do
for _, recipe in ipairs(recipes) do
local ingredient_items = {}
for _, spec in pairs(recipe.items) do
local matches_spec = unified_inventory.canonical_item_spec_matcher(spec)
for _, name in ipairs(unified_inventory.items_list) do
local matches_spec = ui.canonical_item_spec_matcher(spec)
for _, name in ipairs(ui.items_list) do
if matches_spec(name) then
ingredient_items[name] = true
end
end
end
for name, _ in pairs(ingredient_items) do
if unified_inventory.crafts_for.usage[name] == nil then
unified_inventory.crafts_for.usage[name] = {}
if ui.crafts_for.usage[name] == nil then
ui.crafts_for.usage[name] = {}
end
table.insert(unified_inventory.crafts_for.usage[name], recipe)
table.insert(ui.crafts_for.usage[name], recipe)
end
end
end
@ -156,9 +157,9 @@ end)
-- load_home
local function load_home()
local input = io.open(unified_inventory.home_filename, "r")
local input = io.open(ui.home_filename, "r")
if not input then
unified_inventory.home_pos = {}
ui.home_pos = {}
return
end
while true do
@ -167,25 +168,25 @@ local function load_home()
local y = input:read("*n")
local z = input:read("*n")
local name = input:read("*l")
unified_inventory.home_pos[name:sub(2)] = {x = x, y = y, z = z}
ui.home_pos[name:sub(2)] = {x = x, y = y, z = z}
end
io.close(input)
end
load_home()
function unified_inventory.set_home(player, pos)
function ui.set_home(player, pos)
local player_name = player:get_player_name()
unified_inventory.home_pos[player_name] = vector.round(pos)
ui.home_pos[player_name] = vector.round(pos)
-- save the home data from the table to the file
local output = io.open(unified_inventory.home_filename, "w")
for k, v in pairs(unified_inventory.home_pos) do
local output = io.open(ui.home_filename, "w")
for k, v in pairs(ui.home_pos) do
output:write(v.x.." "..v.y.." "..v.z.." "..k.."\n")
end
io.close(output)
end
function unified_inventory.go_home(player)
local pos = unified_inventory.home_pos[player:get_player_name()]
function ui.go_home(player)
local pos = ui.home_pos[player:get_player_name()]
if pos then
player:set_pos(pos)
return true
@ -194,7 +195,7 @@ function unified_inventory.go_home(player)
end
-- register_craft
function unified_inventory.register_craft(options)
function ui.register_craft(options)
if not options.output then
return
end
@ -205,10 +206,10 @@ function unified_inventory.register_craft(options)
if options.type == "normal" and options.width == 0 then
options = { type = "shapeless", items = options.items, output = options.output, width = 0 }
end
if not unified_inventory.crafts_for.recipe[itemstack:get_name()] then
unified_inventory.crafts_for.recipe[itemstack:get_name()] = {}
if not ui.crafts_for.recipe[itemstack:get_name()] then
ui.crafts_for.recipe[itemstack:get_name()] = {}
end
table.insert(unified_inventory.crafts_for.recipe[itemstack:get_name()],options)
table.insert(ui.crafts_for.recipe[itemstack:get_name()],options)
end
@ -219,7 +220,7 @@ local craft_type_defaults = {
}
function unified_inventory.craft_type_defaults(name, options)
function ui.craft_type_defaults(name, options)
if not options.description then
options.description = name
end
@ -228,13 +229,13 @@ function unified_inventory.craft_type_defaults(name, options)
end
function unified_inventory.register_craft_type(name, options)
unified_inventory.registered_craft_types[name] =
unified_inventory.craft_type_defaults(name, options)
function ui.register_craft_type(name, options)
ui.registered_craft_types[name] =
ui.craft_type_defaults(name, options)
end
unified_inventory.register_craft_type("normal", {
ui.register_craft_type("normal", {
description = F(S("Crafting")),
icon = "ui_craftgrid_icon.png",
width = 3,
@ -250,7 +251,7 @@ unified_inventory.register_craft_type("normal", {
})
unified_inventory.register_craft_type("shapeless", {
ui.register_craft_type("shapeless", {
description = F(S("Mixing")),
icon = "ui_craftgrid_icon.png",
width = 3,
@ -265,7 +266,7 @@ unified_inventory.register_craft_type("shapeless", {
})
unified_inventory.register_craft_type("cooking", {
ui.register_craft_type("cooking", {
description = F(S("Cooking")),
icon = "default_furnace_front.png",
width = 1,
@ -273,37 +274,37 @@ unified_inventory.register_craft_type("cooking", {
})
unified_inventory.register_craft_type("digging", {
ui.register_craft_type("digging", {
description = F(S("Digging")),
icon = "default_tool_steelpick.png",
width = 1,
height = 1,
})
unified_inventory.register_craft_type("digging_chance", {
ui.register_craft_type("digging_chance", {
description = "Digging (by chance)",
icon = "default_tool_steelpick.png^[transformFY.png",
width = 1,
height = 1,
})
function unified_inventory.register_page(name, def)
unified_inventory.pages[name] = def
function ui.register_page(name, def)
ui.pages[name] = def
end
function unified_inventory.register_button(name, def)
function ui.register_button(name, def)
if not def.action then
def.action = function(player)
unified_inventory.set_inventory_formspec(player, name)
ui.set_inventory_formspec(player, name)
end
end
def.name = name
table.insert(unified_inventory.buttons, def)
table.insert(ui.buttons, def)
end
function unified_inventory.is_creative(playername)
function ui.is_creative(playername)
return minetest.check_player_privs(playername, {creative=true})
or minetest.settings:get_bool("creative_mode")
end

View File

@ -7,15 +7,16 @@ License: GPLv3
local S = minetest.get_translator("unified_inventory")
local F = minetest.formspec_escape
local bags_inv_bg_prefix = "image[0.3,1.5;"..(unified_inventory.imgscale*8)..","
local ui = unified_inventory
local bags_inv_bg_prefix = "image[0.3,1.5;"..(ui.imgscale*8)..","
unified_inventory.register_page("bags", {
ui.register_page("bags", {
get_formspec = function(player)
local player_name = player:get_player_name()
return { formspec = table.concat({
unified_inventory.style_full.standard_inv_bg,
bags_inv_bg_prefix..unified_inventory.imgscale..";ui_bags_header.png]",
"label["..unified_inventory.style_full.form_header_x..","..unified_inventory.style_full.form_header_y..";" .. F(S("Bags")) .. "]",
ui.style_full.standard_inv_bg,
bags_inv_bg_prefix..ui.imgscale..";ui_bags_header.png]",
"label["..ui.style_full.form_header_x..","..ui.style_full.form_header_y..";" .. F(S("Bags")) .. "]",
"button[0.6125,2.75;1.875,0.75;bag1;" .. F(S("Bag @1", 1)) .. "]",
"button[3.1125,2.75;1.875,0.75;bag2;" .. F(S("Bag @1", 2)) .. "]",
"button[5.6125,2.75;1.875,0.75;bag3;" .. F(S("Bag @1", 3)) .. "]",
@ -29,7 +30,7 @@ unified_inventory.register_page("bags", {
end,
})
unified_inventory.register_button("bags", {
ui.register_button("bags", {
type = "image",
image = "ui_bags_icon.png",
tooltip = S("Bags"),
@ -44,12 +45,12 @@ local function get_player_bag_stack(player, i)
end
for bag_i = 1, 4 do
unified_inventory.register_page("bag" .. bag_i, {
ui.register_page("bag" .. bag_i, {
get_formspec = function(player)
local stack = get_player_bag_stack(player, bag_i)
local image = stack:get_definition().inventory_image
local fs = {
unified_inventory.style_full.standard_inv_bg,
ui.style_full.standard_inv_bg,
"image[9.2,0.4;1,1;" .. image .. "]",
"label[0.3,0.65;" .. F(S("Bag @1", bag_i)) .. "]",
"listcolors[#00000000;#00000000]",
@ -57,20 +58,20 @@ for bag_i = 1, 4 do
}
local slots = stack:get_definition().groups.bagslots
if slots == 8 then
fs[#fs + 1] = bags_inv_bg_prefix..unified_inventory.imgscale..";ui_bags_inv_small.png]"
fs[#fs + 1] = bags_inv_bg_prefix..ui.imgscale..";ui_bags_inv_small.png]"
elseif slots == 16 then
fs[#fs + 1] = bags_inv_bg_prefix..(unified_inventory.imgscale*2)..";ui_bags_inv_medium.png]"
fs[#fs + 1] = bags_inv_bg_prefix..(ui.imgscale*2)..";ui_bags_inv_medium.png]"
elseif slots == 24 then
fs[#fs + 1] = bags_inv_bg_prefix..(unified_inventory.imgscale*3)..";ui_bags_inv_large.png]"
fs[#fs + 1] = bags_inv_bg_prefix..(ui.imgscale*3)..";ui_bags_inv_large.png]"
end
fs[#fs + 1] = "list[current_player;bag" .. bag_i .. "contents;0.45,1.65;8,3;]"
fs[#fs + 1] = "listring[current_name;bag" .. bag_i .. "contents]"
local player_name = player:get_player_name() -- For if statement.
if unified_inventory.trash_enabled
or unified_inventory.is_creative(player_name)
if ui.trash_enabled
or ui.is_creative(player_name)
or minetest.get_player_privs(player_name).give then
fs[#fs + 1] = "image[7.8,0.25;"..unified_inventory.imgscale..","..unified_inventory.imgscale..";ui_trash_slot.png]"
fs[#fs + 1] = "image[7.8,0.25;"..ui.imgscale..","..ui.imgscale..";ui_trash_slot.png]"
.. "list[detached:trash;main;7.95,0.25;1,1;]"
end
local inv = player:get_inventory()
@ -107,7 +108,7 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
if not stack:get_definition().groups.bagslots then
return
end
unified_inventory.set_inventory_formspec(player, "bag" .. i)
ui.set_inventory_formspec(player, "bag" .. i)
return
end
end

View File

@ -1,5 +1,6 @@
local S = minetest.get_translator("unified_inventory")
local F = minetest.formspec_escape
local ui = unified_inventory
-- This pair of encoding functions is used where variable text must go in
-- button names, where the text might contain formspec metacharacters.
@ -10,44 +11,44 @@ local F = minetest.formspec_escape
-- fixed some day we don't want to rely on it. So for safety we apply
-- an encoding that avoids all formspec metacharacters.
function unified_inventory.mangle_for_formspec(str)
function ui.mangle_for_formspec(str)
return string.gsub(str, "([^A-Za-z0-9])", function (c) return string.format("_%d_", string.byte(c)) end)
end
function unified_inventory.demangle_for_formspec(str)
function ui.demangle_for_formspec(str)
return string.gsub(str, "_([0-9]+)_", function (v) return string.char(v) end)
end
function unified_inventory.get_per_player_formspec(player_name)
local lite = unified_inventory.lite_mode and not minetest.check_player_privs(player_name, {ui_full=true})
function ui.get_per_player_formspec(player_name)
local lite = ui.lite_mode and not minetest.check_player_privs(player_name, {ui_full=true})
local ui = unified_inventory.style_full
local style = ui.style_full
if lite then
ui = unified_inventory.style_lite
style = ui.style_lite
end
ui.items_per_page = ui.pagecols * ui.pagerows
ui.standard_inv = string.format("list[current_player;main;%f,%f;8,4;]",
ui.std_inv_x+0.15, ui.std_inv_y+0.15)
style.items_per_page = style.pagecols * style.pagerows
style.standard_inv = string.format("list[current_player;main;%f,%f;8,4;]",
style.std_inv_x+0.15, style.std_inv_y+0.15)
ui.standard_inv_bg = string.format("image[%f,%f;%f,%f;ui_main_inventory.png]",
ui.std_inv_x, ui.std_inv_y,
unified_inventory.imgscale*8, unified_inventory.imgscale*4)
return ui, lite
style.standard_inv_bg = string.format("image[%f,%f;%f,%f;ui_main_inventory.png]",
style.std_inv_x, style.std_inv_y,
ui.imgscale*8, ui.imgscale*4)
return style, lite
end
function unified_inventory.get_formspec(player, page)
function ui.get_formspec(player, page)
if not player then
return ""
end
local player_name = player:get_player_name()
local ui_peruser,draw_lite_mode = unified_inventory.get_per_player_formspec(player_name)
local ui_peruser,draw_lite_mode = ui.get_per_player_formspec(player_name)
unified_inventory.current_page[player_name] = page
local pagedef = unified_inventory.pages[page]
ui.current_page[player_name] = page
local pagedef = ui.pages[page]
if not pagedef then
return "" -- Invalid page name
@ -56,22 +57,22 @@ function unified_inventory.get_formspec(player, page)
local formspec = {
"formspec_version[4]size[17.75,12.25]",
pagedef.formspec_prepend and "" or "no_prepend[]",
unified_inventory.standard_background -- Background
ui.standard_background -- Background
}
local n = 4
if draw_lite_mode then
formspec[1] = "formspec_version[4]size[14,9.75]"
formspec[3] = unified_inventory.standard_background
formspec[3] = ui.standard_background
end
if unified_inventory.is_creative(player_name)
if ui.is_creative(player_name)
and page == "craft" then -- add the "Refill" slot.
formspec[n] = "image["..(ui_peruser.craft_x-2.5)..","..(ui_peruser.craft_y+2.5)..";"..unified_inventory.imgscale..","..unified_inventory.imgscale..";ui_single_slot.png]"
formspec[n] = "image["..(ui_peruser.craft_x-2.5)..","..(ui_peruser.craft_y+2.5)..";"..ui.imgscale..","..ui.imgscale..";ui_single_slot.png]"
n = n+1
end
local perplayer_formspec = unified_inventory.get_per_player_formspec(player_name)
local perplayer_formspec = ui.get_per_player_formspec(player_name)
local fsdata = pagedef.get_formspec(player, perplayer_formspec)
formspec[n] = fsdata.formspec
@ -84,7 +85,7 @@ function unified_inventory.get_formspec(player, page)
local filtered_inv_buttons = {}
for i, def in pairs(unified_inventory.buttons) do
for i, def in pairs(ui.buttons) do
if not (draw_lite_mode and def.hide_lite) then
table.insert(filtered_inv_buttons, def)
end
@ -136,7 +137,7 @@ function unified_inventory.get_formspec(player, page)
ui_peruser.page_buttons_y..";"..
(ui_peruser.searchwidth - 0.1)..","..
ui_peruser.btn_size..";searchbox;;"..
F(unified_inventory.current_searchbox[player_name]) .. "]"
F(ui.current_searchbox[player_name]) .. "]"
formspec[n+2] = "image_button["..(ui_peruser.page_buttons_x + ui_peruser.searchwidth)..","..
ui_peruser.page_buttons_y..";"..
ui_peruser.btn_size..","..ui_peruser.btn_size..
@ -185,23 +186,23 @@ function unified_inventory.get_formspec(player, page)
end
-- Items list
if #unified_inventory.filtered_items_list[player_name] == 0 then
if #ui.filtered_items_list[player_name] == 0 then
formspec[n] = "label["..ui_peruser.page_x..","..(ui_peruser.page_y+0.15)..";" .. F(no_matches) .. "]"
else
local dir = unified_inventory.active_search_direction[player_name]
local list_index = unified_inventory.current_index[player_name]
local dir = ui.active_search_direction[player_name]
local list_index = ui.current_index[player_name]
local page2 = math.floor(list_index / (ui_peruser.items_per_page) + 1)
local pagemax = math.floor(
(#unified_inventory.filtered_items_list[player_name] - 1)
(#ui.filtered_items_list[player_name] - 1)
/ (ui_peruser.items_per_page) + 1)
for y = 0, ui_peruser.pagerows - 1 do
for x = 0, ui_peruser.pagecols - 1 do
local name = unified_inventory.filtered_items_list[player_name][list_index]
local name = ui.filtered_items_list[player_name][list_index]
local item = minetest.registered_items[name]
if item then
-- Clicked on current item: Flip crafting direction
if name == unified_inventory.current_item[player_name] then
local cdir = unified_inventory.current_craft_direction[player_name]
if name == ui.current_item[player_name] then
local cdir = ui.current_craft_direction[player_name]
if cdir == "recipe" then
dir = "usage"
elseif cdir == "usage" then
@ -209,11 +210,11 @@ function unified_inventory.get_formspec(player, page)
end
else
-- Default: use active search direction by default
dir = unified_inventory.active_search_direction[player_name]
dir = ui.active_search_direction[player_name]
end
local button_name = "item_button_" .. dir .. "_"
.. unified_inventory.mangle_for_formspec(name)
.. ui.mangle_for_formspec(name)
formspec[n] = ("item_image_button[%f,%f;%f,%f;%s;%s;]"):format(
ui_peruser.page_x + x * ui_peruser.btn_spc,
ui_peruser.page_y + y * ui_peruser.btn_spc,
@ -234,21 +235,21 @@ function unified_inventory.get_formspec(player, page)
end
n= n+1
if unified_inventory.activefilter[player_name] ~= "" then
if ui.activefilter[player_name] ~= "" then
formspec[n] = "label["..ui_peruser.page_x..","..(ui_peruser.page_y - 0.65)..";" .. F(S("Filter")) .. ":]"
formspec[n+1] = "label["..ui_peruser.page_x..","..(ui_peruser.page_y - 0.25)..";"..F(unified_inventory.activefilter[player_name]).."]"
formspec[n+1] = "label["..ui_peruser.page_x..","..(ui_peruser.page_y - 0.25)..";"..F(ui.activefilter[player_name]).."]"
end
return table.concat(formspec, "")
end
function unified_inventory.set_inventory_formspec(player, page)
function ui.set_inventory_formspec(player, page)
if player then
player:set_inventory_formspec(unified_inventory.get_formspec(player, page))
player:set_inventory_formspec(ui.get_formspec(player, page))
end
end
--apply filter to the inventory list (create filtered copy of full one)
function unified_inventory.apply_filter(player, filter, search_dir)
function ui.apply_filter(player, filter, search_dir)
if not player then
return false
end
@ -277,26 +278,26 @@ function unified_inventory.apply_filter(player, filter, search_dir)
or llocaldesc and string.find(llocaldesc, lfilter, 1, true)
end
end
unified_inventory.filtered_items_list[player_name]={}
ui.filtered_items_list[player_name]={}
for name, def in pairs(minetest.registered_items) do
if (not def.groups.not_in_creative_inventory
or def.groups.not_in_creative_inventory == 0)
and def.description
and def.description ~= ""
and ffilter(name, def) then
table.insert(unified_inventory.filtered_items_list[player_name], name)
table.insert(ui.filtered_items_list[player_name], name)
end
end
table.sort(unified_inventory.filtered_items_list[player_name])
unified_inventory.filtered_items_list_size[player_name] = #unified_inventory.filtered_items_list[player_name]
unified_inventory.current_index[player_name] = 1
unified_inventory.activefilter[player_name] = filter
unified_inventory.active_search_direction[player_name] = search_dir
unified_inventory.set_inventory_formspec(player,
unified_inventory.current_page[player_name])
table.sort(ui.filtered_items_list[player_name])
ui.filtered_items_list_size[player_name] = #ui.filtered_items_list[player_name]
ui.current_index[player_name] = 1
ui.activefilter[player_name] = filter
ui.active_search_direction[player_name] = search_dir
ui.set_inventory_formspec(player,
ui.current_page[player_name])
end
function unified_inventory.items_in_group(groups)
function ui.items_in_group(groups)
local items = {}
for name, item in pairs(minetest.registered_items) do
for _, group in pairs(groups:split(',')) do
@ -308,7 +309,7 @@ function unified_inventory.items_in_group(groups)
return items
end
function unified_inventory.sort_inventory(inv)
function ui.sort_inventory(inv)
local inlist = inv:get_list("main")
local typecnt = {}
local typekeys = {}

View File

@ -1,6 +1,7 @@
local S = minetest.get_translator("unified_inventory")
local NS = function(s) return s end
local F = minetest.formspec_escape
local ui = unified_inventory
minetest.register_privilege("creative", {
description = S("Can use the creative inventory"),
@ -14,7 +15,7 @@ minetest.register_privilege("ui_full", {
local trash = minetest.create_detached_inventory("trash", {
--allow_put = function(inv, listname, index, stack, player)
-- if unified_inventory.is_creative(player:get_player_name()) then
-- if ui.is_creative(player:get_player_name()) then
-- return stack:get_count()
-- else
-- return 0
@ -28,19 +29,19 @@ local trash = minetest.create_detached_inventory("trash", {
})
trash:set_size("main", 1)
unified_inventory.register_button("craft", {
ui.register_button("craft", {
type = "image",
image = "ui_craft_icon.png",
tooltip = S("Crafting Grid")
})
unified_inventory.register_button("craftguide", {
ui.register_button("craftguide", {
type = "image",
image = "ui_craftguide_icon.png",
tooltip = S("Crafting Guide")
})
unified_inventory.register_button("home_gui_set", {
ui.register_button("home_gui_set", {
type = "image",
image = "ui_sethome_icon.png",
tooltip = S("Set home position"),
@ -48,8 +49,8 @@ unified_inventory.register_button("home_gui_set", {
action = function(player)
local player_name = player:get_player_name()
if minetest.check_player_privs(player_name, {home=true}) then
unified_inventory.set_home(player, player:get_pos())
local home = unified_inventory.home_pos[player_name]
ui.set_home(player, player:get_pos())
local home = ui.home_pos[player_name]
if home ~= nil then
minetest.sound_play("dingdong",
{to_player=player_name, gain = 1.0})
@ -59,7 +60,7 @@ unified_inventory.register_button("home_gui_set", {
else
minetest.chat_send_player(player_name,
S("You don't have the \"home\" privilege!"))
unified_inventory.set_inventory_formspec(player, unified_inventory.current_page[player_name])
ui.set_inventory_formspec(player, ui.current_page[player_name])
end
end,
condition = function(player)
@ -67,7 +68,7 @@ unified_inventory.register_button("home_gui_set", {
end,
})
unified_inventory.register_button("home_gui_go", {
ui.register_button("home_gui_go", {
type = "image",
image = "ui_gohome_icon.png",
tooltip = S("Go home"),
@ -75,13 +76,13 @@ unified_inventory.register_button("home_gui_go", {
action = function(player)
local player_name = player:get_player_name()
if minetest.check_player_privs(player_name, {home=true}) then
if unified_inventory.go_home(player) then
if ui.go_home(player) then
minetest.sound_play("teleport", {to_player = player_name})
end
else
minetest.chat_send_player(player_name,
S("You don't have the \"home\" privilege!"))
unified_inventory.set_inventory_formspec(player, unified_inventory.current_page[player_name])
ui.set_inventory_formspec(player, ui.current_page[player_name])
end
end,
condition = function(player)
@ -89,7 +90,7 @@ unified_inventory.register_button("home_gui_go", {
end,
})
unified_inventory.register_button("misc_set_day", {
ui.register_button("misc_set_day", {
type = "image",
image = "ui_sun_icon.png",
tooltip = S("Set time to day"),
@ -105,7 +106,7 @@ unified_inventory.register_button("misc_set_day", {
else
minetest.chat_send_player(player_name,
S("You don't have the settime privilege!"))
unified_inventory.set_inventory_formspec(player, unified_inventory.current_page[player_name])
ui.set_inventory_formspec(player, ui.current_page[player_name])
end
end,
condition = function(player)
@ -113,7 +114,7 @@ unified_inventory.register_button("misc_set_day", {
end,
})
unified_inventory.register_button("misc_set_night", {
ui.register_button("misc_set_night", {
type = "image",
image = "ui_moon_icon.png",
tooltip = S("Set time to night"),
@ -129,7 +130,7 @@ unified_inventory.register_button("misc_set_night", {
else
minetest.chat_send_player(player_name,
S("You don't have the settime privilege!"))
unified_inventory.set_inventory_formspec(player, unified_inventory.current_page[player_name])
ui.set_inventory_formspec(player, ui.current_page[player_name])
end
end,
condition = function(player)
@ -137,19 +138,19 @@ unified_inventory.register_button("misc_set_night", {
end,
})
unified_inventory.register_button("clear_inv", {
ui.register_button("clear_inv", {
type = "image",
image = "ui_trash_icon.png",
tooltip = S("Clear inventory"),
action = function(player)
local player_name = player:get_player_name()
if not unified_inventory.is_creative(player_name) then
if not ui.is_creative(player_name) then
minetest.chat_send_player(player_name,
S("This button has been disabled outside"
.." of creative mode to prevent"
.." accidental inventory trashing."
.."\nUse the trash slot instead."))
unified_inventory.set_inventory_formspec(player, unified_inventory.current_page[player_name])
ui.set_inventory_formspec(player, ui.current_page[player_name])
return
end
player:get_inventory():set_list("main", {})
@ -158,11 +159,11 @@ unified_inventory.register_button("clear_inv", {
{to_player=player_name, gain = 1.0})
end,
condition = function(player)
return unified_inventory.is_creative(player:get_player_name())
return ui.is_creative(player:get_player_name())
end,
})
unified_inventory.register_page("craft", {
ui.register_page("craft", {
get_formspec = function(player, perplayer_formspec)
local formheaderx = perplayer_formspec.form_header_x
@ -172,20 +173,20 @@ unified_inventory.register_page("craft", {
local craftresultx = craftx + 5
local player_name = player:get_player_name()
local formspec = "image["..craftx..","..crafty..";"..(unified_inventory.imgscale*6)..","..(unified_inventory.imgscale*3)..";ui_crafting_form.png]"
local formspec = "image["..craftx..","..crafty..";"..(ui.imgscale*6)..","..(ui.imgscale*3)..";ui_crafting_form.png]"
formspec = formspec..perplayer_formspec.standard_inv_bg
formspec = formspec.."label["..formheaderx..","..formheadery..";" ..F(S("Crafting")).."]"
formspec = formspec.."listcolors[#00000000;#00000000]"
formspec = formspec.."list[current_player;craftpreview;"..(craftresultx+0.15)..","..(crafty+0.15)..";1,1;]"
formspec = formspec.."list[current_player;craft;"..(craftx+0.15)..","..(crafty+0.15)..";3,3;]"
if unified_inventory.trash_enabled or unified_inventory.is_creative(player_name) or minetest.get_player_privs(player_name).give then
if ui.trash_enabled or ui.is_creative(player_name) or minetest.get_player_privs(player_name).give then
formspec = formspec.."label["..(craftx+6.45)..","..(crafty + 2.4)..";" .. F(S("Trash:")) .. "]"
formspec = formspec.."image["..(craftx+6.25)..","..(crafty + 2.5)..";"..unified_inventory.imgscale..","..unified_inventory.imgscale..";ui_trash_slot.png]"
formspec = formspec.."image["..(craftx+6.25)..","..(crafty + 2.5)..";"..ui.imgscale..","..ui.imgscale..";ui_trash_slot.png]"
formspec = formspec.."list[detached:trash;main;"..(craftx+6.4)..","..(crafty + 2.65)..";1,1;]"
end
formspec = formspec.."listring[current_name;craft]"
formspec = formspec.."listring[current_player;main]"
if unified_inventory.is_creative(player_name) then
if ui.is_creative(player_name) then
formspec = formspec.."label["..(craftx-2.3)..","..(crafty + 2.4)..";" .. F(S("Refill:")) .. "]"
formspec = formspec.."list[detached:"..F(player_name).."refill;main;"..(craftx-2.35)..","..(crafty + 2.65)..";1,1;]"
end
@ -208,18 +209,18 @@ local function stack_image_button(x, y, w, h, buttonname_prefix, item)
local selectitem = name
if name:sub(1, 6) == "group:" then
local group_name = name:sub(7)
local group_item = unified_inventory.get_group_item(group_name)
local group_item = ui.get_group_item(group_name)
show_is_group = not group_item.sole
displayitem = group_item.item or "unknown"
selectitem = group_item.sole and displayitem or name
end
local label = show_is_group and "G" or ""
local buttonname = F(buttonname_prefix..unified_inventory.mangle_for_formspec(selectitem))
local buttonname = F(buttonname_prefix..ui.mangle_for_formspec(selectitem))
local button = string.format("item_image_button[%f,%f;%f,%f;%s;%s;%s]",
x, y, w, h,
F(displayitem), buttonname, label)
if show_is_group then
local groupstring, andcount = unified_inventory.extract_groupnames(name)
local groupstring, andcount = ui.extract_groupnames(name)
local grouptip
if andcount == 1 then
grouptip = S("Any item belonging to the @1 group", groupstring)
@ -259,7 +260,7 @@ local other_dir = {
usage = "recipe",
}
unified_inventory.register_page("craftguide", {
ui.register_page("craftguide", {
get_formspec = function(player, perplayer_formspec)
local craftx = perplayer_formspec.craft_x
@ -277,7 +278,7 @@ unified_inventory.register_page("craftguide", {
"label["..formheaderx..","..formheadery..";" .. F(S("Crafting Guide")) .. "]",
"listcolors[#00000000;#00000000]"
}
local item_name = unified_inventory.current_item[player_name]
local item_name = ui.current_item[player_name]
if not item_name then
return { formspec = table.concat(fs) }
end
@ -291,17 +292,17 @@ unified_inventory.register_page("craftguide", {
item_name_shown = item_name
end
local dir = unified_inventory.current_craft_direction[player_name]
local dir = ui.current_craft_direction[player_name]
local rdir = dir == "recipe" and "usage" or "recipe"
local crafts = unified_inventory.crafts_for[dir][item_name]
local alternate = unified_inventory.alternate[player_name]
local crafts = ui.crafts_for[dir][item_name]
local alternate = ui.alternate[player_name]
local alternates, craft
if crafts and #crafts > 0 then
alternates = #crafts
craft = crafts[alternate]
end
local has_give = player_privs.give or unified_inventory.is_creative(player_name)
local has_give = player_privs.give or ui.is_creative(player_name)
fs[#fs + 1] = "image["..craftarrowx..","..crafty..";1.25,1.25;ui_crafting_arrow.png]"
fs[#fs + 1] = string.format("textarea[%f,%f;10,1;;%s: %s;]",
@ -334,8 +335,8 @@ unified_inventory.register_page("craftguide", {
"item_button_usage_", ItemStack(item_name))
end
local craft_type = unified_inventory.registered_craft_types[craft.type] or
unified_inventory.craft_type_defaults(craft.type, {})
local craft_type = ui.registered_craft_types[craft.type] or
ui.craft_type_defaults(craft.type, {})
if craft_type.icon then
fs[#fs + 1] = string.format("image[%f,%f;%f,%f;%s]",
craftarrowx+0.1, crafty + 0.95, 1, 1, craft_type.icon)
@ -426,7 +427,7 @@ local function craftguide_giveme(player, formname, fields)
local player_name = player:get_player_name()
local player_privs = minetest.get_player_privs(player_name)
if not player_privs.give and
not unified_inventory.is_creative(player_name) then
not ui.is_creative(player_name) then
minetest.log("action", "[unified_inventory] Denied give action to player " ..
player_name)
return
@ -441,7 +442,7 @@ local function craftguide_giveme(player, formname, fields)
amount = tonumber(amount) or 0
if amount == 0 then return end
local output = unified_inventory.current_item[player_name]
local output = ui.current_item[player_name]
if (not output) or (output == "") then return end
local player_inv = player:get_inventory()
@ -462,21 +463,21 @@ local function craftguide_craft(player, formname, fields)
local player_name = player:get_player_name()
local output = unified_inventory.current_item[player_name] or ""
local output = ui.current_item[player_name] or ""
if output == "" then return end
local crafts = unified_inventory.crafts_for[
unified_inventory.current_craft_direction[player_name]][output] or {}
local crafts = ui.crafts_for[
ui.current_craft_direction[player_name]][output] or {}
if #crafts == 0 then return end
local alternate = unified_inventory.alternate[player_name]
local alternate = ui.alternate[player_name]
local craft = crafts[alternate]
if craft.width > 3 then return end
unified_inventory.craftguide_match_craft(player, "main", "craft", craft, amount)
ui.craftguide_match_craft(player, "main", "craft", craft, amount)
unified_inventory.set_inventory_formspec(player, "craft")
ui.set_inventory_formspec(player, "craft")
end
minetest.register_on_player_receive_fields(function(player, formname, fields)

View File

@ -1,5 +1,6 @@
local S = minetest.get_translator("unified_inventory")
local F = minetest.formspec_escape
local ui = unified_inventory
local hud_colors = {
{"#FFFFFF", 0xFFFFFF, S("White")},
@ -14,30 +15,30 @@ local hud_colors_max = #hud_colors
-- Stores temporary player data (persists until player leaves)
local waypoints_temp = {}
unified_inventory.register_page("waypoints", {
ui.register_page("waypoints", {
get_formspec = function(player)
local player_name = player:get_player_name()
local wp_info_x = unified_inventory.style_full.form_header_x + 1.25
local wp_info_y = unified_inventory.style_full.form_header_y + 0.5
local wp_bottom_row = unified_inventory.style_full.std_inv_y - 1
local wp_buttons_rj = unified_inventory.style_full.std_inv_x + 10.1 - unified_inventory.style_full.btn_spc
local wp_edit_w = unified_inventory.style_full.btn_spc * 4 - 0.1
local wp_info_x = ui.style_full.form_header_x + 1.25
local wp_info_y = ui.style_full.form_header_y + 0.5
local wp_bottom_row = ui.style_full.std_inv_y - 1
local wp_buttons_rj = ui.style_full.std_inv_x + 10.1 - ui.style_full.btn_spc
local wp_edit_w = ui.style_full.btn_spc * 4 - 0.1
-- build a "fake" temp entry if the server took too long
-- during sign-on and returned an empty entry
if not waypoints_temp[player_name] then waypoints_temp[player_name] = {hud = 1} end
local waypoints = datastorage.get(player_name, "waypoints")
local formspec = unified_inventory.style_full.standard_inv_bg..
"label["..unified_inventory.style_full.form_header_x..","..unified_inventory.style_full.form_header_y..";" .. F(S("Waypoints")) .. "]"..
local formspec = ui.style_full.standard_inv_bg..
"label["..ui.style_full.form_header_x..","..ui.style_full.form_header_y..";" .. F(S("Waypoints")) .. "]"..
"image["..wp_info_x..","..wp_info_y..";1,1;ui_waypoints_icon.png]"
-- Tabs buttons:
for i = 1, 5 do
formspec = formspec ..
"image_button["..unified_inventory.style_full.main_button_x..","..
(wp_bottom_row - (5-i) * unified_inventory.style_full.btn_spc)..";"..
unified_inventory.style_full.btn_size..","..unified_inventory.style_full.btn_size..";" ..
"image_button["..ui.style_full.main_button_x..","..
(wp_bottom_row - (5-i) * ui.style_full.btn_spc)..";"..
ui.style_full.btn_size..","..ui.style_full.btn_size..";" ..
(i == waypoints.selected and "ui_blue_icon_background.png^" or "") ..
"ui_" .. i .. "_icon.png;" ..
"select_waypoint" .. i .. ";]" ..
@ -63,9 +64,9 @@ unified_inventory.register_page("waypoints", {
local x = 4
for _, b in pairs(btnlist) do
formspec = formspec ..
"image_button["..(wp_buttons_rj - unified_inventory.style_full.btn_spc * x)..","..
"image_button["..(wp_buttons_rj - ui.style_full.btn_spc * x)..","..
wp_bottom_row..";"..
unified_inventory.style_full.btn_size..","..unified_inventory.style_full.btn_size..";"..
ui.style_full.btn_size..","..ui.style_full.btn_size..";"..
b[1]..";"..
b[2]..i..";]"..
"tooltip["..b[2]..i..";"..F(S(b[3], b[4] or "")).."]"
@ -82,11 +83,11 @@ unified_inventory.register_page("waypoints", {
if temp.edit then
formspec = formspec ..
"field["..(wp_buttons_rj - wp_edit_w - 0.1)..","..(wp_bottom_row - unified_inventory.style_full.btn_spc)..";"..
wp_edit_w..","..unified_inventory.style_full.btn_size..";rename_box" .. i .. ";;"..
"field["..(wp_buttons_rj - wp_edit_w - 0.1)..","..(wp_bottom_row - ui.style_full.btn_spc)..";"..
wp_edit_w..","..ui.style_full.btn_size..";rename_box" .. i .. ";;"..
(waypoint.name or default_name).."]" ..
"image_button["..wp_buttons_rj..","..(wp_bottom_row - unified_inventory.style_full.btn_spc)..";"..
unified_inventory.style_full.btn_size..","..unified_inventory.style_full.btn_size..";"..
"image_button["..wp_buttons_rj..","..(wp_bottom_row - ui.style_full.btn_spc)..";"..
ui.style_full.btn_size..","..ui.style_full.btn_size..";"..
"ui_ok_icon.png;"..
"confirm_rename"..i.. ";]"..
"tooltip[confirm_rename" .. i .. ";"
@ -103,7 +104,7 @@ unified_inventory.register_page("waypoints", {
end,
})
unified_inventory.register_button("waypoints", {
ui.register_button("waypoints", {
type = "image",
image = "ui_waypoints_icon.png",
tooltip = S("Waypoints"),
@ -218,7 +219,7 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
update_hud(player, waypoints, temp, i)
end
if update_formspec then
unified_inventory.set_inventory_formspec(player, "waypoints")
ui.set_inventory_formspec(player, "waypoints")
end
if hit then return end
end