Refactor API into own file and remove _foodblocks

I think it is better not to pollute the global namespace. I couldn't
contrive a way of hiding the wood sounds and node groups from the new
implementation detail of the API, so they're left public. Perhaps it's
better that way.
This commit is contained in:
Blockhead 2024-07-03 13:50:40 +10:00
parent 5bca5584ed
commit ef55a33f6e
5 changed files with 113 additions and 99 deletions

98
api.lua Normal file
View File

@ -0,0 +1,98 @@
local S = minetest.get_translator("foodblocks")
local drop_self = minetest.settings:get_bool("foodblocks_drop_selves", false)
local creative_mode = minetest.is_creative_enabled("") or false
local node_groups
local wood_sounds
-- Intended for internal use only
local function set_node_groups(grp)
node_groups = table.copy(grp)
end
-- Intended for internal use only
local function set_wood_sounds(snd)
wood_sounds = table.copy(snd)
end
--[[ regfoodblock
Register a whole cube food block.
By default it will have have a 1 wide x 3 high texture sheet.
In vertical order: top, then sides, then bottom.
name: the internal name for the block and prefix for its texture *_cube.png
desc: the displayed name for the block, which will be localised
ingredient: the food used to craft the block and that it drops
customtiles: use the provided tiles for a texture instead of the sheet
customdef: any other custom fields to put in the node definition. Will override
any defaults set by the foodblocks code, so use with care!
--]]
local function regfoodblock(name, desc, ingredient, customtiles, customdef)
local tile_sides = name..'_cube.png^[sheet:1x3:0,1'
local node_name = "foodblocks:"..name.."_cube"
local tiles
if customtiles then
tiles = customtiles
else
tiles = {
name..'_cube.png^[sheet:1x3:0,0', -- top
name..'_cube.png^[sheet:1x3:0,2', -- bottom
tile_sides,
tile_sides,
tile_sides,
tile_sides,
}
end
local drop
-- This wouldn't work when written as (drop_self or creative_mode).
-- The reason was I wasn't checking for and substituting false for nils
-- when assigning above!
if drop_self or creative_mode then
drop = node_name
else
drop = string.format('"%s" 9', ingredient)
end
minetest.log("warning", string.format("name = %s, drop = %s", name, drop))
local node_def = {
description = S("@1 Block", S(desc)),
drop = drop,
drawtype = 'normal',
groups = node_groups,
paramtype2 = "facedir",
sounds = wood_sounds,
tiles = tiles,
_mcl_hardness = 1,
}
if customdef ~= nil and type(customdef) == "table" then
for k,v in pairs(customdef) do
node_def[k] = v
end
end
minetest.register_node(node_name, node_def)
minetest.register_craft({
output = node_name,
recipe = {
{ingredient, ingredient, ingredient},
{ingredient, ingredient, ingredient},
{ingredient, ingredient, ingredient},
}
})
minetest.register_craft({
output = string.format('"%s" 9', ingredient),
type = "shapeless",
recipe = {node_name}
})
end
return {
regfoodblock = regfoodblock,
set_node_groups = set_node_groups,
set_wood_sounds = set_wood_sounds,
}

View File

@ -1,12 +1,10 @@
local regfoodblock = foodblocks.regfoodblock
if minetest.get_modpath("default") then
_foodblocks.wood_sounds = default.node_sound_wood_defaults()
_foodblocks.node_groups = {choppy = 3, oddly_breakable_by_hand = 2}
foodblocks.set_wood_sounds(default.node_sound_wood_defaults())
foodblocks.set_node_groups({choppy = 3, oddly_breakable_by_hand = 2})
end
local extradef = { groups = {choppy = 3, oddly_breakable_by_hand = 2}}
-- FORK DETECTION
-- farming_undo is a fork of redo with some more stuff
local ffork = 0

View File

@ -1,96 +1,9 @@
local S = minetest.get_translator("foodblocks")
local drop_self = minetest.settings:get_bool("foodblocks_drop_selves", false)
local creative_mode = minetest.is_creative_enabled("") or false
--[[ regfoodblock
Register a whole cube food block.
By default it will have have a 1 wide x 3 high texture sheet.
In vertical order: top, then sides, then bottom.
name: the internal name for the block and prefix for its texture *_cube.png
desc: the displayed name for the block, which will be localised
ingredient: the food used to craft the block and that it drops
customtiles: use the provided tiles for a texture instead of the sheet
customdef: any other custom fields to put in the node definition. Will override
any defaults set by the foodblocks code, so use with care!
--]]
local function regfoodblock(name, desc, ingredient, customtiles, customdef)
local tile_sides = name..'_cube.png^[sheet:1x3:0,1'
local node_name = "foodblocks:"..name.."_cube"
local tiles
if customtiles then
tiles = customtiles
else
tiles = {
name..'_cube.png^[sheet:1x3:0,0', -- top
name..'_cube.png^[sheet:1x3:0,2', -- bottom
tile_sides,
tile_sides,
tile_sides,
tile_sides,
}
end
local drop
-- This wouldn't work when written as (drop_self or creative_mode).
-- The reason was I wasn't checking for and substituting false for nils
-- when assigning above!
if drop_self or creative_mode then
drop = node_name
else
drop = string.format('"%s" 9', ingredient)
end
minetest.log("warning", string.format("name = %s, drop = %s", name, drop))
local node_def = {
description = S("@1 Block", S(desc)),
drop = drop,
drawtype = 'normal',
groups = _foodblocks.node_groups,
paramtype2 = "facedir",
sounds = _foodblocks.wood_sounds,
tiles = tiles,
_mcl_hardness = 1,
}
if customdef ~= nil and type(customdef) == "table" then
for k,v in pairs(customdef) do
node_def[k] = v
end
end
minetest.register_node(node_name, node_def)
minetest.register_craft({
output = node_name,
recipe = {
{ingredient, ingredient, ingredient},
{ingredient, ingredient, ingredient},
{ingredient, ingredient, ingredient},
}
})
minetest.register_craft({
output = string.format('"%s" 9', ingredient),
type = "shapeless",
recipe = {node_name}
})
end
-- Internal API
_foodblocks = {
wood_sounds = {},
node_groups = {},
}
-- External API
foodblocks = {
regfoodblock = regfoodblock,
}
local modpath = minetest.get_modpath("foodblocks")
local api = dofile(modpath.."/api.lua")
-- External API
foodblocks = api
if minetest.get_modpath("farming")
and minetest.global_exists("farming")
then

7
internal_api.lua Normal file
View File

@ -0,0 +1,7 @@
local function set_node_groups(grp)
node_groups = table.copy(grp)
end
local function set_wood_sounds(snd)
wood_sounds = table.copy(snd)
end

View File

@ -3,17 +3,15 @@ local regfoodblock = foodblocks.regfoodblock
local melondef = minetest.registered_nodes["mcl_farming:melon"]
if minetest.get_modpath("mcl_sounds") then
_foodblocks.wood_sounds = mcl_sounds.node_sound_wood_defaults()
foodblocks.set_wood_sounds(mcl_sounds.node_sound_wood_defaults())
end
_foodblocks.nodegroups = melondef.groups
foodblocks.set_node_groups(melondef.groups)
local extra_node_def = {
_mcl_blast_resistance = melondef._mcl_blast_resistance,
_mcl_hardness = melondef._mcl_hardness,
_mcl_silk_touch_drop = true,
}
_foodblocks.node_groups = minetest.registered_nodes["mcl_farming:melon"].groups
local gold_apple_textures = {
'(apple_cube.png^[sheet:1x3:0,0^[colorize:yellow:90^apple_top_overlay.png)', -- top