make compatability stuff less complicated

This commit is contained in:
flux
2022-06-27 08:47:36 -07:00
parent 75969eb9d7
commit 9cc83ce5cc
9 changed files with 69 additions and 22 deletions

103
stairsplus/compat/i3.lua Normal file
View File

@ -0,0 +1,103 @@
-- luacheck: read globals i3
if not stairsplus.has.i3 then
return
end
local api = stairsplus.api
i3.register_craft_type("stairsplus:craft_schema", {
description = "Stairs+ craft schema",
icon = "stairsplus_saw_button.png",
})
i3.register_craft_type("stairsplus:circular_saw", {
description = "Stairs+ circular saw",
icon = "stairsplus_saw_button.png",
})
local function convert_schema_recipe_item(item)
if item == "" or item:match(":") then
return item
end
local name, count = item:match("^([^ ]+) (%d+)")
if not name then
name = item
end
count = tonumber(count)
if name == "node" then
name = "mapgen_stone"
if count then
return ("%s %s"):format(name, count)
else
return name
end
else
if count then
return ("group:shape_%s %s"):format(name, count)
else
return ("group:shape_%s"):format(name)
end
end
end
local function on_register_craft_schema(schema)
local recipe = table.copy(schema)
recipe.output = convert_schema_recipe_item(recipe.output)
if recipe.replacements then
for _, replacement in ipairs(recipe.replacements) do
for i, item in ipairs(replacement) do
replacement[i] = convert_schema_recipe_item(item)
end
end
end
if recipe.type == "shapeless" then
for i, item in ipairs(recipe.recipe) do
recipe.recipe[i] = convert_schema_recipe_item(item)
end
elseif recipe.type == "shaped" or recipe.type == nil then
local tmp = {}
for _, row in ipairs(recipe.recipe) do
for i, item in ipairs(row) do
table.insert(tmp, convert_schema_recipe_item(item))
end
end
recipe.recipe = tmp
end
i3.register_craft({
type = "stairsplus:craft_schema",
result = recipe.output,
items = recipe.recipe,
})
end
for _, schema in ipairs(api.registered_recipe_schemas) do
on_register_craft_schema(schema)
end
api.register_on_register_craft_schema(on_register_craft_schema)
local function on_register_single(node, shaped_name)
i3.register_craft({
type = "stairsplus:circular_saw",
result = shaped_name,
items = {node},
})
end
for _, single in ipairs(api.registered_singles) do
local node, shaped_name = unpack(single)
on_register_single(node, shaped_name)
end
api.register_on_register_single(on_register_single)

View File

@ -0,0 +1,25 @@
stairsplus.compat = {
is_legacy_drawtype = function(node)
local def = minetest.registered_nodes[node]
return (
def.drawtype == "mesh" or
def.drawtype == "plantlike" or
def.drawtype == "nodebox"
)
end,
is_legacy_paramtype2 = function(node)
local def = minetest.registered_nodes[node]
return (
def.paramtype2 == "color" or
def.paramtype2 == "colorwallmounted" or
def.paramtype2 == "glasslikeliquidlevel"
)
end,
}
stairsplus.dofile("compat", "i3")
stairsplus.dofile("compat", "unified_inventory")
stairsplus.dofile("compat", "stairs")
stairsplus.dofile("compat", "old_moreblocks")

View File

@ -0,0 +1,234 @@
-- legacy: export old API for mods which depend on it
-- provide a configuration option to *disable* legacy. it must be enabled by default, to prevent breaking
-- existing servers
local api = stairsplus.api
local is_legacy_drawtype = stairsplus.compat.is_legacy_drawtype
local is_legacy_paramtype2 = stairsplus.compat.is_legacy_paramtype2
local legacy_mode = stairsplus.settings.legacy_mode
local function clean_legacy_fields(fields)
fields = fields or {}
fields.drawtype = nil
fields.light_source = nil
fields.inventory_image = nil
fields.inventory_overlay = nil
fields.wield_image = nil
fields.wield_overlay = nil
fields.wield_scale = nil
fields.tool_capabilities = nil
fields.node_placement_prediction = nil
fields.node_dig_prediction = nil
fields.on_place = nil
fields.on_secondary_use = nil
fields.on_drop = nil
fields.on_use = nil
fields.after_use = nil
fields.paramtype2 = nil
fields.node_box = nil
fields.mesh = nil
fields.connects_to = nil
fields.connect_sides = nil
fields.selection_box = nil
fields.collision_box = nil
fields.legacy_facedir_simple = nil
fields.legacy_wallmounted = nil
if type(fields.drop) ~= "string" then
fields.drop = nil
end
fields.on_construct = nil
fields.on_destruct = nil
fields.after_destruct = nil
fields.after_place_node = nil
fields.after_dig_node = nil
fields.can_dig = nil
fields.on_punch = nil
fields.on_rightclick = nil
fields.on_dig = nil
fields.on_timer = nil
fields.on_receive_fields = nil
return fields
end
local function register_group(modname, subname, recipeitem, fields, group)
if not minetest.registered_nodes[recipeitem] then
error(("cannot register stairs for %s before the node is defined"):format(recipeitem))
end
fields = clean_legacy_fields(fields)
local meta = {}
if is_legacy_drawtype(recipeitem) then
meta.ignore_drawtype = true
end
if is_legacy_paramtype2(recipeitem) then
meta.ignore_paramtype2 = true
end
api.register_group(recipeitem, group, fields, meta)
local old_name = ("%s:%s"):format(modname, subname)
if old_name ~= recipeitem then
api.register_alias_group(old_name, recipeitem, group)
end
end
function stairsplus:register_all(modname, subname, recipeitem, fields)
if legacy_mode then
register_group(modname, subname, recipeitem, fields, "legacy")
else
register_group(modname, subname, recipeitem, fields, "common")
end
end
function stairsplus:register_micro(modname, subname, recipeitem, fields)
register_group(modname, subname, recipeitem, fields, "micro")
end
function stairsplus:register_panel(modname, subname, recipeitem, fields)
register_group(modname, subname, recipeitem, fields, "panel")
end
function stairsplus:register_slab(modname, subname, recipeitem, fields)
register_group(modname, subname, recipeitem, fields, "slab")
end
function stairsplus:register_slope(modname, subname, recipeitem, fields)
register_group(modname, subname, recipeitem, fields, "slope")
end
function stairsplus:register_stair(modname, subname, recipeitem, fields)
register_group(modname, subname, recipeitem, fields, "stair")
end
local fix_shape_table = {
micro = "micro_8",
panel = "panel_8",
slab = "slab_8",
slab_quarter = "slab_4",
slab_three_quarter = "slab_12",
stair_alt = "stair_alt_8",
}
local function interpret_subset(subset)
local shapes = {}
for _, v in ipairs(subset) do
local shape = table.concat(v, "")
shape = fix_shape_table[shape] or shape
table.insert(shapes, shape)
end
return shapes
end
function stairsplus:register_custom_subset(subset, modname, subname, recipeitem, fields)
local shapes = interpret_subset(subset)
fields = clean_legacy_fields(fields)
local meta = {}
if is_legacy_drawtype(recipeitem) then
meta.ignore_drawtype = true
end
api.register_singles(recipeitem, shapes, fields, meta)
local old_name = ("%s:%s"):format(modname, subname)
if old_name ~= recipeitem then
api.register_alias_shapes(old_name, recipeitem, shapes)
end
end
function stairsplus:register_alias_all(modname_old, subname_old, modname_new, subname_new)
local old_node = ("%s:%s"):format(modname_old, subname_old)
local new_node = ("%s:%s"):format(modname_new, subname_new)
if legacy_mode then
api.register_alias_group(old_node, new_node, "legacy")
else
api.register_alias_group(old_node, new_node, "common")
end
end
function stairsplus:register_alias_micro(modname_old, subname_old, modname_new, subname_new)
local old_node = ("%s:%s"):format(modname_old, subname_old)
local new_node = ("%s:%s"):format(modname_new, subname_new)
api.register_alias_group(old_node, new_node, "micro")
end
function stairsplus:register_alias_panel(modname_old, subname_old, modname_new, subname_new)
local old_node = ("%s:%s"):format(modname_old, subname_old)
local new_node = ("%s:%s"):format(modname_new, subname_new)
api.register_alias_group(old_node, new_node, "panel")
end
function stairsplus:register_alias_slab(modname_old, subname_old, modname_new, subname_new)
local old_node = ("%s:%s"):format(modname_old, subname_old)
local new_node = ("%s:%s"):format(modname_new, subname_new)
api.register_alias_group(old_node, new_node, "slab")
end
function stairsplus:register_alias_slope(modname_old, subname_old, modname_new, subname_new)
local old_node = ("%s:%s"):format(modname_old, subname_old)
local new_node = ("%s:%s"):format(modname_new, subname_new)
api.register_alias_group(old_node, new_node, "slope")
end
function stairsplus:register_alias_stair(modname_old, subname_old, modname_new, subname_new)
local old_node = ("%s:%s"):format(modname_old, subname_old)
local new_node = ("%s:%s"):format(modname_new, subname_new)
api.register_alias_group(old_node, new_node, "stair")
end
function stairsplus:register_custom_subset_alias(subset, modname_old, subname_old, modname_new, subname_new)
local shapes = interpret_subset(subset)
local old_node = ("%s:%s"):format(modname_old, subname_old)
local new_node = ("%s:%s"):format(modname_new, subname_new)
api.register_alias_shapes(old_node, new_node, shapes)
end
function stairsplus:register_alias_force_all(modname_old, subname_old, modname_new, subname_new)
local old_node = ("%s:%s"):format(modname_old, subname_old)
local new_node = ("%s:%s"):format(modname_new, subname_new)
if legacy_mode then
api.register_alias_force_group(old_node, new_node, "legacy")
else
api.register_alias_force_group(old_node, new_node, "common")
end
end
function stairsplus:register_alias_force_micro(modname_old, subname_old, modname_new, subname_new)
local old_node = ("%s:%s"):format(modname_old, subname_old)
local new_node = ("%s:%s"):format(modname_new, subname_new)
api.register_alias_force_group(old_node, new_node, "micro")
end
function stairsplus:register_alias_force_panel(modname_old, subname_old, modname_new, subname_new)
local old_node = ("%s:%s"):format(modname_old, subname_old)
local new_node = ("%s:%s"):format(modname_new, subname_new)
api.register_alias_force_group(old_node, new_node, "panel")
end
function stairsplus:register_alias_force_slab(modname_old, subname_old, modname_new, subname_new)
local old_node = ("%s:%s"):format(modname_old, subname_old)
local new_node = ("%s:%s"):format(modname_new, subname_new)
api.register_alias_force_group(old_node, new_node, "slab")
end
function stairsplus:register_alias_force_slope(modname_old, subname_old, modname_new, subname_new)
local old_node = ("%s:%s"):format(modname_old, subname_old)
local new_node = ("%s:%s"):format(modname_new, subname_new)
api.register_alias_force_group(old_node, new_node, "slope")
end
function stairsplus:register_alias_force_stair(modname_old, subname_old, modname_new, subname_new)
local old_node = ("%s:%s"):format(modname_old, subname_old)
local new_node = ("%s:%s"):format(modname_new, subname_new)
api.register_alias_force_group(old_node, new_node, "stair")
end
function stairsplus:register_custom_subset_alias(subset, modname_old, subname_old, modname_new, subname_new)
local shapes = interpret_subset(subset)
local old_node = ("%s:%s"):format(modname_old, subname_old)
local new_node = ("%s:%s"):format(modname_new, subname_new)
api.register_alias_force_shapes(old_node, new_node, shapes)
end

View File

@ -0,0 +1,117 @@
-- stairs compat: override what stairs does
-- in stairsplus_legacy, "fix" any stairs which were already registered
if not stairsplus.has.stairs then
return
end
local api = stairsplus.api
local S = stairsplus.S
local is_legacy_drawtype = stairsplus.compat.is_legacy_drawtype
local is_legacy_paramtype2 = stairsplus.compat.is_legacy_paramtype2
local default_align_style = stairsplus.settings.default_align_style
function stairs.register_stair(subname, node, groups, tiles, description, sounds, worldaligntex)
local meta = {
align_style = worldaligntex and "world" or default_align_style
}
if is_legacy_drawtype(node) then
meta.ignore_drawtype = true
end
if is_legacy_paramtype2(node) then
meta.ignore_paramtype2 = true
end
api.register_single(node, "stair", {
groups = groups,
tiles = tiles,
description = description,
sounds = sounds,
}, meta)
minetest.register_alias(("stairs:stair_%s"):format(subname), api.format_name(node, "stair"))
end
function stairs.register_slab(subname, node, groups, images, description, sounds, worldaligntex)
local meta = {
align_style = worldaligntex and "world" or default_align_style
}
if is_legacy_drawtype(node) then
meta.ignore_drawtype = true
end
if is_legacy_paramtype2(node) then
meta.ignore_paramtype2 = true
end
api.register_single(node, "slab_8", {
groups = groups,
tiles = images,
description = description,
sounds = sounds,
}, meta)
minetest.register_alias(("stairs:slab_%s"):format(subname), api.format_name(node, "slab_8"))
end
function stairs.register_stair_inner(subname, node, groups, tiles, description, sounds, worldaligntex, full_description)
local meta = {
align_style = worldaligntex and "world" or default_align_style
}
if is_legacy_drawtype(node) then
meta.ignore_drawtype = true
end
if is_legacy_paramtype2(node) then
meta.ignore_paramtype2 = true
end
api.register_single(node, "stair_inner", {
groups = groups,
tiles = tiles,
description = full_description or S("Inner @1", description),
sounds = sounds,
}, meta)
minetest.register_alias(("stairs:stair_inner_%s"):format(subname), api.format_name(node, "stair_inner"))
end
function stairs.register_stair_outer(subname, node, groups, tiles, description, sounds, worldaligntex, full_description)
local meta = {
align_style = worldaligntex and "world" or default_align_style
}
if is_legacy_drawtype(node) then
meta.ignore_drawtype = true
end
if is_legacy_paramtype2(node) then
meta.ignore_paramtype2 = true
end
api.register_single(node, "stair_outer", {
groups = groups,
tiles = tiles,
description = full_description or S("Outer @1", description),
sounds = sounds,
}, meta)
minetest.register_alias(("stairs:stair_outer_%s"):format(subname), api.format_name(node, "stair_outer"))
end
local stair_name_formats = {
stair = "stairs:stair_%s",
slab_8 = "stairs:slab_%s",
stair_inner = "stairs:stair_inner_%s",
stair_outer = "stairs:stair_outer_%s",
}
function stairsplus.compat.override_stairs(name, node, overrides, meta)
for shape, name_format in pairs(stair_name_formats) do
local stair_name = name_format:format(name)
if minetest.registered_nodes[stair_name] then
api.register_single(node, shape, overrides, meta)
local shaped_name = api.format_name(node, shape)
minetest.register_alias_force(stair_name, shaped_name)
end
end
end

View File

@ -0,0 +1,134 @@
-- luacheck: read globals unified_inventory
if not stairsplus.has.unified_inventory then
return
end
local api = stairsplus.api
unified_inventory.register_craft_type("stairsplus:craft_schema", {
-- ^ Unique identifier for `register_craft`
description = "Stairs+ craft schema",
-- ^ Text shown below the crafting arrow
icon = "stairsplus_saw_button.png",
-- ^ Image shown above the crafting arrow
width = 3,
height = 3,
-- ^ Maximal input dimensions of the recipes
uses_crafting_grid = true,
})
unified_inventory.register_craft_type("stairsplus:circular_saw", {
-- ^ Unique identifier for `register_craft`
description = "Stairs+ circular saw",
-- ^ Text shown below the crafting arrow
icon = "stairsplus_saw_button.png",
-- ^ Image shown above the crafting arrow
width = 1,
height = 1,
-- ^ Maximal input dimensions of the recipes
uses_crafting_grid = true,
})
unified_inventory.register_category("stairsplus:cuttable", {
symbol = "stairsplus:circular_saw",
label = "Cuttable in the circular saw",
index = 0,
items = {}
})
unified_inventory.register_category("stairsplus:cut_node", {
symbol = "stairsplus:circular_saw",
label = "Nodes cut in the circular saw",
index = 0,
items = {}
})
local function convert_schema_recipe_item(item)
if item == "" or item:match(":") then
return item
end
local name, count = item:match("^([^ ]+) (%d+)")
if not name then
name = item
end
count = tonumber(count)
if name == "node" then
name = "mapgen_stone"
if count then
return ("%s %s"):format(name, count)
else
return name
end
else
if count then
return ("group:shape_%s %s"):format(name, count)
else
return ("group:shape_%s"):format(name)
end
end
end
local function on_register_craft_schema(schema)
local recipe = table.copy(schema)
recipe.output = convert_schema_recipe_item(recipe.output)
if recipe.replacements then
for _, replacement in ipairs(recipe.replacements) do
for i, item in ipairs(replacement) do
replacement[i] = convert_schema_recipe_item(item)
end
end
end
if recipe.type == "shapeless" then
for i, item in ipairs(recipe.recipe) do
recipe.recipe[i] = convert_schema_recipe_item(item)
end
elseif recipe.type == "shaped" or recipe.type == nil then
for _, row in ipairs(recipe.recipe) do
for i, item in ipairs(row) do
row[i] = convert_schema_recipe_item(item)
end
end
end
unified_inventory.register_craft({
output = recipe.output,
type = "stairsplus:craft_schema",
items = recipe.recipe,
width = 3,
})
end
for _, schema in ipairs(api.registered_recipe_schemas) do
on_register_craft_schema(schema)
end
api.register_on_register_craft_schema(on_register_craft_schema)
local function on_register_single(node, shaped_name)
unified_inventory.register_craft({
output = shaped_name,
type = "stairsplus:circular_saw",
items = {node},
width = 1,
})
unified_inventory.add_category_item("stairsplus:cuttable", node)
unified_inventory.add_category_item("stairsplus:cut_node", shaped_name)
end
for _, single in ipairs(api.registered_singles) do
local node, shaped_name = unpack(single)
on_register_single(node, shaped_name)
end
api.register_on_register_single(on_register_single)