Minor code-style improvement and cleanup
This commit is contained in:
parent
eb96c89b5d
commit
52e146dee1
8
bags.lua
8
bags.lua
|
@ -168,12 +168,12 @@ end
|
|||
minetest.register_on_joinplayer(function(player)
|
||||
local player_name = player:get_player_name()
|
||||
local bags_inv = minetest.create_detached_inventory(player_name .. "_bags",{
|
||||
on_put = function(inv, listname, index, stack, player)
|
||||
on_put = function(inv, listname, _, stack, player)
|
||||
player:get_inventory():set_size(listname .. "contents",
|
||||
stack:get_definition().groups.bagslots)
|
||||
save_bags_metadata(player, inv)
|
||||
end,
|
||||
allow_put = function(inv, listname, index, stack, player)
|
||||
allow_put = function(_, listname, _, stack, player)
|
||||
local new_slots = stack:get_definition().groups.bagslots
|
||||
if not new_slots then
|
||||
return 0
|
||||
|
@ -207,13 +207,13 @@ minetest.register_on_joinplayer(function(player)
|
|||
-- New bag is smaller: Disallow inserting
|
||||
return 0
|
||||
end,
|
||||
allow_take = function(inv, listname, index, stack, player)
|
||||
allow_take = function(_, listname, _, stack, player)
|
||||
if player:get_inventory():is_empty(listname .. "contents") then
|
||||
return stack:get_count()
|
||||
end
|
||||
return 0
|
||||
end,
|
||||
on_take = function(inv, listname, index, stack, player)
|
||||
on_take = function(inv, listname, _, _, player)
|
||||
player:get_inventory():set_size(listname .. "contents", 0)
|
||||
save_bags_metadata(player, inv)
|
||||
end,
|
||||
|
|
|
@ -27,14 +27,14 @@ minetest.register_on_joinplayer(function(player)
|
|||
|
||||
-- Refill slot
|
||||
local refill = minetest.create_detached_inventory(player_name.."refill", {
|
||||
allow_put = function(inv, listname, index, stack, player)
|
||||
allow_put = function(_, _, _, stack)
|
||||
if unified_inventory.is_creative(player_name) then
|
||||
return stack:get_count()
|
||||
else
|
||||
return 0
|
||||
end
|
||||
end,
|
||||
on_put = function(inv, listname, index, stack, player)
|
||||
on_put = function(inv, listname, index, stack)
|
||||
local handle_refill = (minetest.registered_items[stack:get_name()] or {}).on_refill or default_refill
|
||||
stack = handle_refill(stack)
|
||||
inv:set_stack(listname, index, stack)
|
||||
|
@ -57,7 +57,7 @@ end
|
|||
minetest.register_on_player_receive_fields(function(player, formname, fields)
|
||||
local player_name = player:get_player_name()
|
||||
|
||||
local ui_peruser,draw_lite_mode = unified_inventory.get_per_player_formspec(player_name)
|
||||
local ui_peruser, _ = unified_inventory.get_per_player_formspec(player_name)
|
||||
|
||||
if formname ~= "" then
|
||||
return
|
||||
|
@ -69,7 +69,7 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
|
|||
unified_inventory.current_searchbox[player_name] = fields.searchbox
|
||||
end
|
||||
|
||||
for i, def in pairs(unified_inventory.buttons) do
|
||||
for _, def in pairs(unified_inventory.buttons) do
|
||||
if fields[def.name] then
|
||||
def.action(player)
|
||||
minetest.sound_play("click",
|
||||
|
@ -120,7 +120,7 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
|
|||
|
||||
-- Check clicked item image button
|
||||
local clicked_item
|
||||
for name, value in pairs(fields) do
|
||||
for name, _ in pairs(fields) do
|
||||
local new_dir, mangled_item = string.match(name, "^item_button_([a-z]+)_(.*)$")
|
||||
if new_dir and mangled_item then
|
||||
clicked_item = unified_inventory.demangle_for_formspec(mangled_item)
|
||||
|
|
|
@ -92,4 +92,3 @@ Register a non-standard craft recipe:
|
|||
width = 3,
|
||||
-- ^ Same as `minetest.register_recipe`
|
||||
})
|
||||
|
||||
|
|
|
@ -115,7 +115,6 @@ local function compute_group_item(group_name_list)
|
|||
return {item = bestitem, sole = false}
|
||||
end
|
||||
|
||||
|
||||
local group_item_cache = {}
|
||||
|
||||
function unified_inventory.get_group_item(group_name)
|
||||
|
@ -124,4 +123,3 @@ function unified_inventory.get_group_item(group_name)
|
|||
end
|
||||
return group_item_cache[group_name]
|
||||
end
|
||||
|
||||
|
|
2
init.lua
2
init.lua
|
@ -50,7 +50,7 @@ unified_inventory = {
|
|||
-- Disable default creative inventory
|
||||
local creative = rawget(_G, "creative") or rawget(_G, "creative_inventory")
|
||||
if creative then
|
||||
function creative.set_creative_formspec(player, start_i, pagenum)
|
||||
function creative.set_creative_formspec()
|
||||
return
|
||||
end
|
||||
end
|
||||
|
|
23
internal.lua
23
internal.lua
|
@ -1,14 +1,16 @@
|
|||
local S = minetest.get_translator("unified_inventory")
|
||||
local F = minetest.formspec_escape
|
||||
|
||||
-- This pair of encoding functions is used where variable text must go in
|
||||
-- button names, where the text might contain formspec metacharacters.
|
||||
-- We can escape button names for the formspec, to avoid screwing up
|
||||
-- form structure overall, but they then don't get de-escaped, and so
|
||||
-- the input we get back from the button contains the formspec escaping.
|
||||
-- This is a game engine bug, and in the anticipation that it might be
|
||||
-- fixed some day we don't want to rely on it. So for safety we apply
|
||||
-- an encoding that avoids all formspec metacharacters.
|
||||
--[[
|
||||
This pair of encoding functions is used where variable text must go in
|
||||
button names, where the text might contain formspec metacharacters.
|
||||
We can escape button names for the formspec, to avoid screwing up
|
||||
form structure overall, but they then don't get de-escaped, and so
|
||||
the input we get back from the button contains the formspec escaping.
|
||||
This is a game engine bug, and in the anticipation that it might be
|
||||
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)
|
||||
return string.gsub(str, "([^A-Za-z0-9])", function (c) return string.format("_%d_", string.byte(c)) end)
|
||||
end
|
||||
|
@ -47,7 +49,6 @@ function unified_inventory.get_per_player_formspec(player_name)
|
|||
end
|
||||
|
||||
function unified_inventory.get_formspec(player, page)
|
||||
|
||||
if not player then
|
||||
return ""
|
||||
end
|
||||
|
@ -93,7 +94,7 @@ function unified_inventory.get_formspec(player, page)
|
|||
|
||||
local filtered_inv_buttons = {}
|
||||
|
||||
for i, def in pairs(unified_inventory.buttons) do
|
||||
for _, def in pairs(unified_inventory.buttons) do
|
||||
if not (draw_lite_mode and def.hide_lite) then
|
||||
table.insert(filtered_inv_buttons, def)
|
||||
end
|
||||
|
@ -280,7 +281,7 @@ function unified_inventory.apply_filter(player, filter, search_dir)
|
|||
local ffilter
|
||||
if lfilter:sub(1, 6) == "group:" then
|
||||
local groups = lfilter:sub(7):split(",")
|
||||
ffilter = function(name, def)
|
||||
ffilter = function(_, def)
|
||||
for _, group in ipairs(groups) do
|
||||
if not def.groups[group]
|
||||
or def.groups[group] <= 0 then
|
||||
|
|
|
@ -73,4 +73,3 @@ minetest.register_globalstep(function(dtime)
|
|||
end
|
||||
end
|
||||
end)
|
||||
|
||||
|
|
|
@ -22,7 +22,7 @@ Example output:
|
|||
["default:axe_diamond"] = 2, -- unstackable item are counted too
|
||||
["wool:white"] = 6
|
||||
}
|
||||
]]--
|
||||
]]
|
||||
function unified_inventory.count_items(inv, lists)
|
||||
local counts = {}
|
||||
|
||||
|
@ -63,7 +63,7 @@ Example output:
|
|||
["wool:white"] = {[1] = true, [2] = true, [3] = true}
|
||||
["group:wood"] = {[4] = true, [5] = true, [6] = true}
|
||||
}
|
||||
--]]
|
||||
]]
|
||||
function unified_inventory.count_craft_positions(craft)
|
||||
local positions = {}
|
||||
local craft_items = craft.items
|
||||
|
@ -124,7 +124,7 @@ Example output:
|
|||
["wool:white"] = true
|
||||
}
|
||||
}
|
||||
--]]
|
||||
]]
|
||||
function unified_inventory.find_usable_items(inv_items, craft_items)
|
||||
local get_group = minetest.get_item_group
|
||||
local result = {}
|
||||
|
@ -173,7 +173,7 @@ Example output:
|
|||
["default:pine_wood"] = {[5] = true}
|
||||
}
|
||||
match_count = 2
|
||||
--]]
|
||||
]]
|
||||
function unified_inventory.match_items(inv_counts, craft_positions)
|
||||
local usable = unified_inventory.find_usable_items(inv_counts, craft_positions)
|
||||
local match_table = {}
|
||||
|
@ -237,7 +237,7 @@ Arguments:
|
|||
inv: minetest inventory reference
|
||||
lists: names of inventory lists
|
||||
stack: minetest item stack
|
||||
--]]
|
||||
]]
|
||||
function unified_inventory.remove_item(inv, lists, stack)
|
||||
local removed = ItemStack(nil)
|
||||
local leftover = ItemStack(stack)
|
||||
|
@ -266,7 +266,7 @@ Arguments:
|
|||
inv: minetest inventory reference
|
||||
lists: names of inventory lists
|
||||
stack: minetest item stack
|
||||
--]]
|
||||
]]
|
||||
function unified_inventory.add_item(inv, lists, stack)
|
||||
local leftover = ItemStack(stack)
|
||||
|
||||
|
@ -290,7 +290,7 @@ Arguments:
|
|||
src_list: name of source list
|
||||
dst_list: name of destination list
|
||||
exclude: set of positions to skip
|
||||
--]]
|
||||
]]
|
||||
function unified_inventory.swap_items(inv, src_list, dst_list, exclude)
|
||||
local size = inv:get_size(src_list)
|
||||
local empty = ItemStack(nil)
|
||||
|
@ -326,7 +326,7 @@ Arguments:
|
|||
dst_list: name of destination list
|
||||
match_table: table of matched items
|
||||
amount: amount of items per every position
|
||||
--]]
|
||||
]]
|
||||
function unified_inventory.move_match(player, src_list, dst_list, match_table, amount)
|
||||
local inv = player:get_inventory()
|
||||
local item_drop = minetest.item_drop
|
||||
|
@ -388,7 +388,7 @@ Arguments:
|
|||
dst_list: name of destination list
|
||||
craft: minetest craft recipe
|
||||
amount: desired amount of output items
|
||||
--]]
|
||||
]]
|
||||
function unified_inventory.craftguide_match_craft(player, src_list, dst_list, craft, amount)
|
||||
local inv = player:get_inventory()
|
||||
local src_dst_list = {src_list, dst_list}
|
||||
|
|
12
register.lua
12
register.lua
|
@ -21,7 +21,7 @@ local trash = minetest.create_detached_inventory("trash", {
|
|||
-- return 0
|
||||
-- end
|
||||
--end,
|
||||
on_put = function(inv, listname, index, stack, player)
|
||||
on_put = function(inv, listname, index, _, player)
|
||||
inv:set_stack(listname, index, nil)
|
||||
local player_name = player:get_player_name()
|
||||
minetest.sound_play("trash", {to_player=player_name, gain = 1.0})
|
||||
|
@ -414,7 +414,7 @@ unified_inventory.register_page("craftguide", {
|
|||
end,
|
||||
})
|
||||
|
||||
local function craftguide_giveme(player, formname, fields)
|
||||
local function craftguide_giveme(player, _, fields)
|
||||
local player_name = player:get_player_name()
|
||||
local player_privs = minetest.get_player_privs(player_name)
|
||||
if not player_privs.give and
|
||||
|
@ -425,7 +425,7 @@ local function craftguide_giveme(player, formname, fields)
|
|||
end
|
||||
|
||||
local amount
|
||||
for k, v in pairs(fields) do
|
||||
for k, _ in pairs(fields) do
|
||||
amount = k:match("craftguide_giveme_(.*)")
|
||||
if amount then break end
|
||||
end
|
||||
|
@ -441,9 +441,9 @@ local function craftguide_giveme(player, formname, fields)
|
|||
player_inv:add_item("main", {name = output, count = amount})
|
||||
end
|
||||
|
||||
local function craftguide_craft(player, formname, fields)
|
||||
local function craftguide_craft(player, _, fields)
|
||||
local amount
|
||||
for k, v in pairs(fields) do
|
||||
for k, _ in pairs(fields) do
|
||||
amount = k:match("craftguide_craft_(.*)")
|
||||
if amount then break end
|
||||
end
|
||||
|
@ -476,7 +476,7 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
|
|||
return
|
||||
end
|
||||
|
||||
for k, v in pairs(fields) do
|
||||
for k, _ in pairs(fields) do
|
||||
if k:match("craftguide_craft_") then
|
||||
craftguide_craft(player, formname, fields)
|
||||
return
|
||||
|
|
Binary file not shown.
Before Width: | Height: | Size: 8.4 KiB |
Binary file not shown.
Before Width: | Height: | Size: 2.1 KiB |
|
@ -244,4 +244,3 @@ end)
|
|||
minetest.register_on_leaveplayer(function(player)
|
||||
waypoints_temp[player:get_player_name()] = nil
|
||||
end)
|
||||
|
||||
|
|
Loading…
Reference in New Issue