summaryrefslogtreecommitdiff
path: root/callbacks.lua
diff options
context:
space:
mode:
Diffstat (limited to 'callbacks.lua')
-rw-r--r--callbacks.lua78
1 files changed, 41 insertions, 37 deletions
diff --git a/callbacks.lua b/callbacks.lua
index 3a41c32..c0f9d04 100644
--- a/callbacks.lua
+++ b/callbacks.lua
@@ -1,7 +1,9 @@
local function default_refill(stack)
stack:set_count(stack:get_stack_max())
local itemdef = minetest.registered_items[stack:get_name()]
- if itemdef and (itemdef.wear_represents or "mechanical_wear") == "mechanical_wear" and stack:get_wear() ~= 0 then
+ if itemdef
+ and (itemdef.wear_represents or "mechanical_wear") == "mechanical_wear"
+ and stack:get_wear() ~= 0 then
stack:set_wear(0)
end
return stack
@@ -52,7 +54,8 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
local player_name = player:get_player_name()
-- always take new search text, even if not searching on it yet
- if fields.searchbox ~= nil and fields.searchbox ~= unified_inventory.current_searchbox[player_name] then
+ if fields.searchbox
+ and fields.searchbox ~= unified_inventory.current_searchbox[player_name] then
unified_inventory.current_searchbox[player_name] = fields.searchbox
unified_inventory.set_inventory_formspec(player, unified_inventory.current_page[player_name])
end
@@ -98,7 +101,7 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
if start_i > pagemax then
start_i = pagemax
end
- if not (start_i == start) then
+ if start_i ~= start then
minetest.sound_play("paperflip1",
{to_player=player_name, gain = 1.0})
unified_inventory.current_index[player_name] = (start_i - 1) * unified_inventory.items_per_page + 1
@@ -106,7 +109,7 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
unified_inventory.current_page[player_name])
end
- local clicked_item = nil
+ local clicked_item
for name, value in pairs(fields) do
if string.sub(name, 1, 12) == "item_button_" then
local new_dir, mangled_item = string.match(name, "^item_button_([a-z]+)_(.*)$")
@@ -116,7 +119,8 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
unified_inventory.apply_filter(player, clicked_item, new_dir)
return
end
- if new_dir == "recipe" or new_dir == "usage" then
+ if new_dir == "recipe"
+ or new_dir == "usage" then
unified_inventory.current_craft_direction[player_name] = new_dir
end
break
@@ -126,22 +130,20 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
minetest.sound_play("click",
{to_player=player_name, gain = 0.1})
local page = unified_inventory.current_page[player_name]
- if not unified_inventory.is_creative(player_name) then
+ local player_creative = unified_inventory.is_creative(player_name)
+ if not player_creative then
page = "craftguide"
end
if page == "craftguide" then
unified_inventory.current_item[player_name] = clicked_item
unified_inventory.alternate[player_name] = 1
- unified_inventory.set_inventory_formspec(player,
- "craftguide")
- else
- if unified_inventory.is_creative(player_name) then
- local inv = player:get_inventory()
- local stack = ItemStack(clicked_item)
- stack:set_count(stack:get_stack_max())
- if inv:room_for_item("main", stack) then
- inv:add_item("main", stack)
- end
+ unified_inventory.set_inventory_formspec(player, "craftguide")
+ elseif player_creative then
+ local inv = player:get_inventory()
+ local stack = ItemStack(clicked_item)
+ stack:set_count(stack:get_stack_max())
+ if inv:room_for_item("main", stack) then
+ inv:add_item("main", stack)
end
end
end
@@ -156,27 +158,29 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
end
-- alternate button
- if fields.alternate then
- minetest.sound_play("click",
- {to_player=player_name, gain = 0.1})
- local item_name = unified_inventory.current_item[player_name]
- if item_name then
- local alternates = 0
- local alternate = unified_inventory.alternate[player_name]
- local crafts = unified_inventory.crafts_for[unified_inventory.current_craft_direction[player_name]][item_name]
- if crafts ~= nil then
- alternates = #crafts
- end
- if alternates > 1 then
- alternate = alternate + 1
- if alternate > alternates then
- alternate = 1
- end
- unified_inventory.alternate[player_name] = alternate
- unified_inventory.set_inventory_formspec(player,
- unified_inventory.current_page[player_name])
- end
- end
+ if not fields.alternate then
+ return
end
+ minetest.sound_play("click",
+ {to_player=player_name, gain = 0.1})
+ local item_name = unified_inventory.current_item[player_name]
+ if not item_name then
+ return
+ end
+ local crafts = unified_inventory.crafts_for[unified_inventory.current_craft_direction[player_name]][item_name]
+ if not crafts then
+ return
+ end
+ local alternates = #crafts
+ if alternates <= 1 then
+ return
+ end
+ local alternate = unified_inventory.alternate[player_name] + 1
+ if alternate > alternates then
+ alternate = 1
+ end
+ unified_inventory.alternate[player_name] = alternate
+ unified_inventory.set_inventory_formspec(player,
+ unified_inventory.current_page[player_name])
end)