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