Minor code-style improvement and cleanup

This commit is contained in:
MoNTE48 2020-05-04 00:40:57 +02:00 committed by Maksim
parent eb96c89b5d
commit 52e146dee1
13 changed files with 164 additions and 168 deletions

View File

@ -11,8 +11,8 @@ minetest.after(0.01, function()
unified_inventory.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
def.groups.not_in_creative_inventory == 0) and
def.description and def.description ~= "" then
table.insert(unified_inventory.items_list, name)
local all_names = rev_aliases[name] or {}
table.insert(all_names, name)

View File

@ -1,8 +1,8 @@
--[[
Bags for Minetest
Bags for Minetest
Copyright (c) 2012 cornernote, Brett O'Donnell <cornernote@gmail.com>
License: GPLv3
Copyright (c) 2012 cornernote, Brett O'Donnell <cornernote@gmail.com>
License: GPLv3
--]]
local S = minetest.get_translator("unified_inventory")
@ -31,7 +31,7 @@ unified_inventory.register_button("bags", {
type = "image",
image = "ui_bags_icon.png",
tooltip = S("Bags"),
hide_lite=true
hide_lite = true
})
local function get_player_bag_stack(player, i)
@ -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,
@ -229,19 +229,19 @@ end)
minetest.register_tool("unified_inventory:bag_small", {
description = S("Small Bag"),
inventory_image = "bags_small.png",
groups = {bagslots=8},
groups = {bagslots = 8},
})
minetest.register_tool("unified_inventory:bag_medium", {
description = S("Medium Bag"),
inventory_image = "bags_medium.png",
groups = {bagslots=16},
groups = {bagslots = 16},
})
minetest.register_tool("unified_inventory:bag_large", {
description = S("Large Bag"),
inventory_image = "bags_large.png",
groups = {bagslots=24},
groups = {bagslots = 24},
})
-- register bag crafts
@ -270,6 +270,6 @@ if minetest.get_modpath("farming") ~= nil then
{"", "", ""},
{"farming:string", "unified_inventory:bag_medium", "farming:string"},
{"farming:string", "unified_inventory:bag_medium", "farming:string"},
},
},
})
end

View File

@ -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)

View File

@ -92,4 +92,3 @@ Register a non-standard craft recipe:
width = 3,
-- ^ Same as `minetest.register_recipe`
})

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -73,4 +73,3 @@ minetest.register_globalstep(function(dtime)
end
end
end)

View File

@ -3,26 +3,26 @@
-- according to the recipe.
--[[
Retrieve items from inventory lists and calculate their total count.
Return a table of "item name" - "total count" pairs.
Retrieve items from inventory lists and calculate their total count.
Return a table of "item name" - "total count" pairs.
Arguments:
inv: minetest inventory reference
lists: names of inventory lists to use
Arguments:
inv: minetest inventory reference
lists: names of inventory lists to use
Example usage:
-- Count items in "main" and "craft" lists of player inventory
unified_inventory.count_items(player_inv_ref, {"main", "craft"})
Example usage:
-- Count items in "main" and "craft" lists of player inventory
unified_inventory.count_items(player_inv_ref, {"main", "craft"})
Example output:
{
["default:pine_wood"] = 2,
["default:acacia_wood"] = 4,
["default:chest"] = 3,
["default:axe_diamond"] = 2, -- unstackable item are counted too
["wool:white"] = 6
}
]]--
Example output:
{
["default:pine_wood"] = 2,
["default:acacia_wood"] = 4,
["default:chest"] = 3,
["default:axe_diamond"] = 2, -- unstackable item are counted too
["wool:white"] = 6
}
]]
function unified_inventory.count_items(inv, lists)
local counts = {}
@ -47,23 +47,23 @@ function unified_inventory.count_items(inv, lists)
end
--[[
Retrieve craft recipe items and their positions in the crafting grid.
Return a table of "craft item name" - "set of positions" pairs.
Retrieve craft recipe items and their positions in the crafting grid.
Return a table of "craft item name" - "set of positions" pairs.
Note that if craft width is not 3 then positions are recalculated as
if items were placed on a 3x3 grid. Also note that craft can contain
groups of items with "group:" prefix.
Note that if craft width is not 3 then positions are recalculated as
if items were placed on a 3x3 grid. Also note that craft can contain
groups of items with "group:" prefix.
Arguments:
craft: minetest craft recipe
Arguments:
craft: minetest craft recipe
Example output:
-- Bed recipe
{
["wool:white"] = {[1] = true, [2] = true, [3] = true}
["group:wood"] = {[4] = true, [5] = true, [6] = true}
}
--]]
Example output:
-- Bed recipe
{
["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
@ -99,32 +99,32 @@ function unified_inventory.count_craft_positions(craft)
end
--[[
For every craft item find all matching inventory items.
- If craft item is a group then find all inventory items that matches
this group.
- If craft item is not a group (regular item) then find only this item.
For every craft item find all matching inventory items.
- If craft item is a group then find all inventory items that matches
this group.
- If craft item is not a group (regular item) then find only this item.
If inventory doesn't contain needed item then found set is empty for
this item.
If inventory doesn't contain needed item then found set is empty for
this item.
Return a table of "craft item name" - "set of matching inventory items"
pairs.
Return a table of "craft item name" - "set of matching inventory items"
pairs.
Arguments:
inv_items: table with items names as keys
craft_items: table with items names or groups as keys
Arguments:
inv_items: table with items names as keys
craft_items: table with items names or groups as keys
Example output:
{
["group:wood"] = {
["default:pine_wood"] = true,
["default:acacia_wood"] = true
},
["wool:white"] = {
["wool:white"] = true
Example output:
{
["group:wood"] = {
["default:pine_wood"] = true,
["default:acacia_wood"] = true
},
["wool:white"] = {
["wool:white"] = true
}
}
}
--]]
]]
function unified_inventory.find_usable_items(inv_items, craft_items)
local get_group = minetest.get_item_group
local result = {}
@ -152,28 +152,28 @@ function unified_inventory.find_usable_items(inv_items, craft_items)
end
--[[
Match inventory items with craft grid positions.
For every position select the matching inventory item with maximum
(total_count / (times_matched + 1)) value.
Match inventory items with craft grid positions.
For every position select the matching inventory item with maximum
(total_count / (times_matched + 1)) value.
If for some position matching item cannot be found or match count is 0
then return nil.
If for some position matching item cannot be found or match count is 0
then return nil.
Return a table of "matched item name" - "set of craft positions" pairs
and overall match count.
Return a table of "matched item name" - "set of craft positions" pairs
and overall match count.
Arguments:
inv_counts: table of inventory items counts from "count_items"
craft_positions: table of craft positions from "count_craft_positions"
Arguments:
inv_counts: table of inventory items counts from "count_items"
craft_positions: table of craft positions from "count_craft_positions"
Example output:
match_table = {
["wool:white"] = {[1] = true, [2] = true, [3] = true}
["default:acacia_wood"] = {[4] = true, [6] = true}
["default:pine_wood"] = {[5] = true}
}
match_count = 2
--]]
Example output:
match_table = {
["wool:white"] = {[1] = true, [2] = true, [3] = true}
["default:acacia_wood"] = {[4] = true, [6] = true}
["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 = {}
@ -227,17 +227,17 @@ function unified_inventory.match_items(inv_counts, craft_positions)
end
--[[
Remove item from inventory lists.
Return stack of actually removed items.
Remove item from inventory lists.
Return stack of actually removed items.
This function replicates the inv:remove_item function but can accept
multiple lists.
This function replicates the inv:remove_item function but can accept
multiple lists.
Arguments:
inv: minetest inventory reference
lists: names of inventory lists
stack: minetest item stack
--]]
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)
@ -256,17 +256,17 @@ function unified_inventory.remove_item(inv, lists, stack)
end
--[[
Add item to inventory lists.
Return leftover stack.
Add item to inventory lists.
Return leftover stack.
This function replicates the inv:add_item function but can accept
multiple lists.
This function replicates the inv:add_item function but can accept
multiple lists.
Arguments:
inv: minetest inventory reference
lists: names of inventory lists
stack: minetest item stack
--]]
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)
@ -282,15 +282,15 @@ function unified_inventory.add_item(inv, lists, stack)
end
--[[
Move items from source list to destination list if possible.
Skip positions specified in exclude set.
Move items from source list to destination list if possible.
Skip positions specified in exclude set.
Arguments:
inv: minetest inventory reference
src_list: name of source list
dst_list: name of destination list
exclude: set of positions to skip
--]]
Arguments:
inv: minetest inventory reference
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)
@ -312,21 +312,21 @@ function unified_inventory.swap_items(inv, src_list, dst_list, exclude)
end
--[[
Move matched items to the destination list.
Move matched items to the destination list.
If destination list position is already occupied with some other item
then function tries to (in that order):
1. Move it to the source list
2. Move it to some other unused position in destination list itself
3. Drop it to the ground if nothing else is possible.
If destination list position is already occupied with some other item
then function tries to (in that order):
1. Move it to the source list
2. Move it to some other unused position in destination list itself
3. Drop it to the ground if nothing else is possible.
Arguments:
player: minetest player object
src_list: name of source list
dst_list: name of destination list
match_table: table of matched items
amount: amount of items per every position
--]]
Arguments:
player: minetest player object
src_list: name of source list
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
@ -374,21 +374,21 @@ function unified_inventory.move_match(player, src_list, dst_list, match_table, a
end
--[[
Find craft match and move matched items to the destination list.
Find craft match and move matched items to the destination list.
If match cannot be found or match count is smaller than the desired
amount then do nothing.
If match cannot be found or match count is smaller than the desired
amount then do nothing.
If amount passed is -1 then amount is defined by match count itself.
This is used to indicate "craft All" case.
If amount passed is -1 then amount is defined by match count itself.
This is used to indicate "craft All" case.
Arguments:
player: minetest player object
src_list: name of source list
dst_list: name of destination list
craft: minetest craft recipe
amount: desired amount of output items
--]]
Arguments:
player: minetest player object
src_list: name of source list
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}

View File

@ -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})
@ -193,9 +193,9 @@ unified_inventory.register_page("craft", {
-- stack_image_button(): generate a form button displaying a stack of items
--
-- The specified item may be a group. In that case, the group will be
-- The specified item may be a group. In that case, the group will be
-- represented by some item in the group, along with a flag indicating
-- that it's a group. If the group contains only one item, it will be
-- that it's a group. If the group contains only one item, it will be
-- treated as if that item had been specified directly.
local function stack_image_button(x, y, w, h, buttonname_prefix, item)
@ -226,7 +226,7 @@ local function stack_image_button(x, y, w, h, buttonname_prefix, item)
end
grouptip = F(grouptip)
if andcount >= 1 then
button = button .. string.format("tooltip[%s;%s]", buttonname, grouptip)
button = button .. string.format("tooltip[%s;%s]", buttonname, grouptip)
end
end
return button
@ -260,8 +260,8 @@ local other_dir = {
unified_inventory.register_page("craftguide", {
get_formspec = function(player, perplayer_formspec)
local formspecy = perplayer_formspec.formspec_y
local formheadery = perplayer_formspec.form_header_y
local formspecy = perplayer_formspec.formspec_y
local formheadery = perplayer_formspec.form_header_y
local craftresultx = perplayer_formspec.craft_result_x
local craftresulty = perplayer_formspec.craft_result_y
@ -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

View File

@ -244,4 +244,3 @@ end)
minetest.register_on_leaveplayer(function(player)
waypoints_temp[player:get_player_name()] = nil
end)