From ef55a33f6ea7686f9b3ddad95d7e043ec173cd16 Mon Sep 17 00:00:00 2001 From: Blockhead Date: Wed, 3 Jul 2024 13:50:40 +1000 Subject: [PATCH] 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. --- api.lua | 98 ++++++++++++++++++++++++++++++++++++++++++++++++ farming.lua | 6 +-- init.lua | 95 ++-------------------------------------------- internal_api.lua | 7 ++++ mcl_farming.lua | 6 +-- 5 files changed, 113 insertions(+), 99 deletions(-) create mode 100644 api.lua create mode 100644 internal_api.lua diff --git a/api.lua b/api.lua new file mode 100644 index 0000000..4610ce7 --- /dev/null +++ b/api.lua @@ -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, +} diff --git a/farming.lua b/farming.lua index 746b36d..c250b8a 100644 --- a/farming.lua +++ b/farming.lua @@ -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 diff --git a/init.lua b/init.lua index d42e07f..12b8b2b 100644 --- a/init.lua +++ b/init.lua @@ -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 diff --git a/internal_api.lua b/internal_api.lua new file mode 100644 index 0000000..a8dff13 --- /dev/null +++ b/internal_api.lua @@ -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 diff --git a/mcl_farming.lua b/mcl_farming.lua index 694c32f..35152aa 100644 --- a/mcl_farming.lua +++ b/mcl_farming.lua @@ -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