-
Notifications
You must be signed in to change notification settings - Fork 574
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
stairs: Add stairs.register_stair_type() to reduce duplicated code. #2641
Conversation
71331b0
to
cf7d9b6
Compare
ac09e13
to
e0934b7
Compare
This replaces stairs.register_stair(), stairs.register_stair_inner() and stairs.register_stair_outer(). v2: Set all elements in a single array.
I believe this is a small step that helps issue #2403. With fewer redundant functions in the stairs mod it will be potentially easier to move code around. |
There's much redundant parts in the code - good to see some cleanups. Much code-- Table containing templates to register a stair type
local mod_stair_types = {
-- sample entry
inner = {
recipe = {
-- replace 1 with supplied recipeitem
{"", 1, ""},
{1, "", 1},
{1, 1, 1},
},
-- either: mesh = "demo.b3d"
-- or: node_box:
node_box = {
-- etc
}
},
-- etc
}
local function register_stair(type, modname, nodename, description, def)
local typedef = mod_stair_types[type]
assert(typedef, "Unknown type")
-- Register node and crafting based on the mod_stair_types entry
-- modname:stair_type_nodename
minetest.register_node(("%s:stair_%s_%s"):format(
modname, type, nodename), {
description = description,
sounds = def.sounds,
-- etc
})
end
function stairs.register_stair(...)
local def = {...}
if #def >= 5 then
-- Old notation required at least 5 args
return legacy_stair_registration(...)
end
assert(#def == 3, "Invalid usage. 3 args expected")
local modname = def[1]
local nodename = def[2]
def = def[3] -- recycle name
for type, description in ipairs(def.types)
register_stair(type, modname, nodename, description, def)
end
end
-- register new kind of stair
function stairs.register_stair_type(typename, typedef)
assert(not mod_stair_types[typename], "Type already registered")
-- TODO: santity checks, defaults
mod_stair_types[typename] = typedef
end
-- Examples for usage in mods
stairs.register_stair_type("slope", {
recipe = {
-- etc
},
mesh = "example.b3d",
-- etc
})
stairs.register_stair("mymod", "barking_wood", {
types = {
inner = S("Inner Barking Wood Stair"),
outer = S("Outer Barking Wood Stair"),
slope = S("Barking Wood Slope"), -- is translation-friendly
},
recipeitem = "mymod:barking_wood_plank",
sound = -- etc
groups = -- etc
}) |
Thanks, you gave me some good ideas how to improve this, but I'm not sure I agree with this part. The problem I saw was both that there is a lot of redundant code and also that there are multiple redundant / similar functions. I'm not sure how that is an improvement to have 4 redundant functions instead of 3? This will just lead to greater confusion on which function to use for inexperienced modders and greater technical debt making sure they all stay in sync. I think this classic xkcd shows the problem. :) |
This is less than trivial cleanup, but should have no change in behavior and should be easy to update existing code whenever maintainers have time.
The problem is that
mods/stairs/init.lua
contains three large functions with largely duplicated code,stairs.register_stair()
,stairs.register_stair_inner()
andstairs.register_stair_outer()
for creating regular, inner and outer corner stairs respectively. This is very hard to read while spotting the relatively minor differences between functions.This adds a new function,
stairs.register_stair_type()
which combines all three functions with a single new argument,stairtype
. It can be set as"inner"
for inner corner stairs,"outer"
for outer corner stairs andnil
for regular stairs.I also updated the existing uses in
mods/stairs/init.lua
,mods/farming/nodes.lua
, updated the docs and added deprecation warnings for the old functions.Please let me know if there is anything I missed.