tidy code

This commit is contained in:
tenplus1 2024-08-16 15:48:59 +01:00
parent d77891d4d9
commit f47816b932

View File

@ -1,21 +1,24 @@
-- define global -- global
hopper = {version = "20230815"}
hopper = {version = "20240816"}
-- Translation and mod check
-- Translation support
local S = minetest.get_translator("hopper") local S = minetest.get_translator("hopper")
local mod_screwdriver = minetest.get_modpath("screwdriver")
-- creative check -- creative check
local creative_mode_cache = minetest.settings:get_bool("creative_mode") local creative_mode_cache = minetest.settings:get_bool("creative_mode")
local function check_creative(name) local function check_creative(name)
return creative_mode_cache or minetest.check_player_privs(name, {creative = true}) return creative_mode_cache or minetest.check_player_privs(name, {creative = true})
end end
-- screwdriver mod
local mod_screwdriver = minetest.get_modpath("screwdriver")
-- containers ( { where, node, inventory, run_callbacks }) -- containers ( { where, node, inventory, run_callbacks })
-- run_callbacks is false to suppress logging during transfer. (be quiet like pipeworks) -- run_callbacks is false to suppress logging during transfer. (be quiet like pipeworks)
local containers = { local containers = {
{"top", "hopper:hopper", "main", true}, {"top", "hopper:hopper", "main", true},
{"bottom", "hopper:hopper", "main", true}, {"bottom", "hopper:hopper", "main", true},
@ -104,8 +107,8 @@ elseif cb_default == true then
} }
end end
-- global function to add new containers -- global function to add new containers
function hopper:add_container(list) function hopper:add_container(list)
for n = 1, #list do for n = 1, #list do
@ -128,8 +131,8 @@ function hopper:add_container(list)
end end
end end
-- default containers -- default containers
hopper:add_container({ hopper:add_container({
{"top", "default:chest", "main"}, {"top", "default:chest", "main"},
{"bottom", "default:chest", "main"}, {"bottom", "default:chest", "main"},
@ -161,8 +164,8 @@ hopper:add_container({
{"void", "default:furnace_active", "src"} {"void", "default:furnace_active", "src"}
}) })
-- protector redo mod support -- protector redo mod support
if minetest.get_modpath("protector") then if minetest.get_modpath("protector") then
hopper:add_container({ hopper:add_container({
@ -173,8 +176,8 @@ if minetest.get_modpath("protector") then
}) })
end end
-- wine mod support -- wine mod support
if minetest.get_modpath("wine") then if minetest.get_modpath("wine") then
hopper:add_container({ hopper:add_container({
@ -185,8 +188,8 @@ if minetest.get_modpath("wine") then
}) })
end end
-- formspec -- formspec
local function get_hopper_formspec(pos) local function get_hopper_formspec(pos)
local spos = pos.x .. "," .. pos.y .. "," .. pos.z local spos = pos.x .. "," .. pos.y .. "," .. pos.z
@ -204,8 +207,8 @@ local function get_hopper_formspec(pos)
return formspec return formspec
end end
-- only log actions of real players -- only log actions of real players
local function log_action(player, pos, message) local function log_action(player, pos, message)
if player and not player.is_fake_player and player:is_player() then if player and not player.is_fake_player and player:is_player() then
@ -215,8 +218,8 @@ local function log_action(player, pos, message)
end end
end end
-- check where pointing and set normal or side-hopper -- check where pointing and set normal or side-hopper
local hopper_place = function(itemstack, placer, pointed_thing) local hopper_place = function(itemstack, placer, pointed_thing)
local pos = pointed_thing.above local pos = pointed_thing.above
@ -232,9 +235,8 @@ local hopper_place = function(itemstack, placer, pointed_thing)
-- make sure we aren't replacing something we shouldnt -- make sure we aren't replacing something we shouldnt
local node = minetest.get_node_or_nil(pos) local node = minetest.get_node_or_nil(pos)
local def = node and minetest.registered_nodes[node.name] local def = node and minetest.registered_nodes[node.name]
if def and not def.buildable_to then
return itemstack if def and not def.buildable_to then return itemstack end
end
if pointed_thing.type == "node" if pointed_thing.type == "node"
and placer and not placer:get_player_control().sneak then and placer and not placer:get_player_control().sneak then
@ -267,7 +269,7 @@ local hopper_place = function(itemstack, placer, pointed_thing)
itemstack:take_item() itemstack:take_item()
end end
-- set metadata -- get and set metadata
local meta = minetest.get_meta(pos) local meta = minetest.get_meta(pos)
local inv = meta:get_inventory() local inv = meta:get_inventory()
@ -278,8 +280,8 @@ local hopper_place = function(itemstack, placer, pointed_thing)
return itemstack return itemstack
end end
-- hopper node
-- hopper
minetest.register_node("hopper:hopper", { minetest.register_node("hopper:hopper", {
description = S("Hopper (Place onto sides for side-hopper)"), description = S("Hopper (Place onto sides for side-hopper)"),
groups = {cracky = 3}, groups = {cracky = 3},
@ -344,8 +346,8 @@ minetest.register_node("hopper:hopper", {
on_blast = function() end on_blast = function() end
}) })
-- side hopper node
-- side hopper
minetest.register_node("hopper:hopper_side", { minetest.register_node("hopper:hopper_side", {
description = S("Side Hopper (Place into crafting to return normal Hopper)"), description = S("Side Hopper (Place into crafting to return normal Hopper)"),
groups = {cracky = 3, not_in_creative_inventory = 1}, groups = {cracky = 3, not_in_creative_inventory = 1},
@ -415,10 +417,10 @@ minetest.register_node("hopper:hopper_side", {
on_blast = function() end on_blast = function() end
}) })
-- void hopper node
local player_void = {} local player_void = {}
-- void hopper
minetest.register_node("hopper:hopper_void", { minetest.register_node("hopper:hopper_void", {
description = S("Void Hopper (Use first to set destination container)"), description = S("Void Hopper (Use first to set destination container)"),
groups = {cracky = 3}, groups = {cracky = 3},
@ -442,9 +444,7 @@ minetest.register_node("hopper:hopper_void", {
on_use = function(itemstack, player, pointed_thing) on_use = function(itemstack, player, pointed_thing)
if pointed_thing.type ~= "node" then if pointed_thing.type ~= "node" then return end
return
end
local pos = pointed_thing.under local pos = pointed_thing.under
local name = player:get_player_name() local name = player:get_player_name()
@ -562,8 +562,8 @@ minetest.register_node("hopper:hopper_void", {
on_blast = function() end on_blast = function() end
}) })
-- transfer function -- transfer function
local transfer = function(src, srcpos, dst, dstpos, allowed, finished) local transfer = function(src, srcpos, dst, dstpos, allowed, finished)
-- source inventory -- source inventory
@ -573,9 +573,7 @@ local transfer = function(src, srcpos, dst, dstpos, allowed, finished)
local inv2 = minetest.get_meta(dstpos):get_inventory() local inv2 = minetest.get_meta(dstpos):get_inventory()
-- check for empty source or no inventory -- check for empty source or no inventory
if not inv or not inv2 or inv:is_empty(src) == true then if not inv or not inv2 or inv:is_empty(src) == true then return end
return
end
local stack, item, max local stack, item, max
@ -586,8 +584,7 @@ local transfer = function(src, srcpos, dst, dstpos, allowed, finished)
item = stack:get_name() item = stack:get_name()
-- if slot not empty and room for item in destination -- if slot not empty and room for item in destination
if item ~= "" if item ~= "" and inv2:room_for_item(dst, item) then
and inv2:room_for_item(dst, item) then
local take = stack:take_item(1) local take = stack:take_item(1)
@ -604,6 +601,7 @@ local transfer = function(src, srcpos, dst, dstpos, allowed, finished)
end end
end end
-- lazy container setting and function
local lazy = minetest.settings:get_bool("lazy_container_support") local lazy = minetest.settings:get_bool("lazy_container_support")
@ -626,8 +624,8 @@ local function add_container_lazy(meta, where, node_name, inv_names)
end end
end end
-- hopper workings -- hopper workings
minetest.register_abm({ minetest.register_abm({
label = "Hopper suction and transfer", label = "Hopper suction and transfer",
@ -643,16 +641,13 @@ minetest.register_abm({
for _,object in pairs(minetest.get_objects_inside_radius(pos, 1)) do for _,object in pairs(minetest.get_objects_inside_radius(pos, 1)) do
if not object:is_player() if not object:is_player()
and object:get_luaentity() and object:get_luaentity() and object:get_luaentity().name == "__builtin:item"
and object:get_luaentity().name == "__builtin:item" and inv and inv:room_for_item("main",
and inv
and inv:room_for_item("main",
ItemStack(object:get_luaentity().itemstring)) then ItemStack(object:get_luaentity().itemstring)) then
if object:get_pos().y - pos.y > 0.25 then if object:get_pos().y - pos.y > 0.25 then
inv:add_item("main", inv:add_item("main", ItemStack(object:get_luaentity().itemstring))
ItemStack(object:get_luaentity().itemstring))
object:get_luaentity().itemstring = "" object:get_luaentity().itemstring = ""
object:remove() object:remove()
@ -667,17 +662,13 @@ minetest.register_abm({
local face = node.param2 local face = node.param2
if face == 0 then if face == 0 then dst_pos = {x = pos.x - 1, y = pos.y, z = pos.z}
dst_pos = {x = pos.x - 1, y = pos.y, z = pos.z}
elseif face == 1 then elseif face == 1 then dst_pos = {x = pos.x, y = pos.y, z = pos.z + 1}
dst_pos = {x = pos.x, y = pos.y, z = pos.z + 1}
elseif face == 2 then elseif face == 2 then dst_pos = {x = pos.x + 1, y = pos.y, z = pos.z}
dst_pos = {x = pos.x + 1, y = pos.y, z = pos.z}
elseif face == 3 then elseif face == 3 then dst_pos = {x = pos.x, y = pos.y, z = pos.z - 1}
dst_pos = {x = pos.x, y = pos.y, z = pos.z - 1}
else else
return return
end end
@ -720,13 +711,9 @@ minetest.register_abm({
end end
local to local to
if node.name == "hopper:hopper" then if node.name == "hopper:hopper" then to = "bottom"
to = "bottom" elseif node.name == "hopper:hopper_side" then to = "side"
elseif node.name == "hopper:hopper_side" then elseif node.name == "hopper:hopper_void" then to = "void" end
to = "side"
elseif node.name == "hopper:hopper_void" then
to = "void"
end
local where, name, inv, run_cb, src_inv, dst_inv, src_cb, dst_cb local where, name, inv, run_cb, src_inv, dst_inv, src_cb, dst_cb
@ -825,8 +812,8 @@ minetest.register_abm({
end end
}) })
-- hopper recipe -- hopper recipe
minetest.register_craft({ minetest.register_craft({
output = "hopper:hopper", output = "hopper:hopper",
recipe = { recipe = {
@ -836,13 +823,16 @@ minetest.register_craft({
}) })
-- side hopper to hopper recipe -- side hopper to hopper recipe
minetest.register_craft({ minetest.register_craft({
output = "hopper:hopper", output = "hopper:hopper",
recipe = {{"hopper:hopper_side"}} recipe = {{"hopper:hopper_side"}}
}) })
-- void hopper recipe -- void hopper recipe
if minetest.get_modpath("teleport_potion") then if minetest.get_modpath("teleport_potion") then
minetest.register_craft({ minetest.register_craft({
output = "hopper:hopper_void", output = "hopper:hopper_void",
recipe = { recipe = {
@ -862,6 +852,7 @@ end
-- add lucky blocks -- add lucky blocks
if minetest.get_modpath("lucky_block") then if minetest.get_modpath("lucky_block") then
lucky_block:add_blocks({ lucky_block:add_blocks({