diff --git a/api.lua b/api.lua index c73ab03..9f7a7ad 100644 --- a/api.lua +++ b/api.lua @@ -1,10 +1,26 @@ --- Use this file for functions that can be called from other mods --- Make sure the functions are well defended against wrong input and --- document them on the readme, what they do, what types and values --- they expect as parameters and what types and values they return. --- If you ever change those, consider adding backwards compatibility, --- since other mods may rely on them. -function yl_api_food.some_api_call(target, message, color) +function yl_api_food.register_food(modname, itemname, table_stages) + + yl_api_food.log("modname=" .. dump(modname)) + yl_api_food.log("itemname=" .. dump(itemname)) + yl_api_food.log("table_stages=" .. dump(table_stages)) + + for n = 1, #table_stages, 1 do + local node_definition = table.copy(table_stages[n] + .node_definition_overwrite) + local name = ":" .. modname .. ":" .. itemname .. "_" .. + table_stages[n].stage + node_definition.description = table_stages[n].description + node_definition.tiles = table_stages[n].tiles + node_definition.stage = table_stages[n] + + node_definition.on_timer = yl_api_food.on_timer + node_definition.on_construct = yl_api_food.on_construct + node_definition.on_destruct = yl_api_food.remove_timer + + minetest.register_node(name, node_definition) + end + + --[[ if (type(target) ~= "string") then return false, yl_api_food.t("error_not_a_string", "target") @@ -31,4 +47,5 @@ function yl_api_food.some_api_call(target, message, color) minetest.chat_send_player(target, message_with_color) return true, yl_api_food.t("api_sent_x_to_y", message_with_color, target) + ]] -- end diff --git a/config.lua b/config.lua index 949cfe3..3679437 100644 --- a/config.lua +++ b/config.lua @@ -3,6 +3,6 @@ yl_api_food.settings = {} -yl_api_food.settings.debug = minetest.settings:get("yl_api_food.debug") or false +yl_api_food.settings.debug = minetest.settings:get("yl_api_food.debug") or true yl_api_food.settings.maximum_stages = minetest.settings:get("yl_api_food.maximum_stages") or 8 diff --git a/dev/whatdowedo.txt b/dev/whatdowedo.txt index 5ca6f6f..311e82b 100644 --- a/dev/whatdowedo.txt +++ b/dev/whatdowedo.txt @@ -1,4 +1,26 @@ -This file holds a couple of editors notes regarding the mod. -Todo's can be noted here, known issues, the path to ressources or general annotations. +This mod allows to register "growable" food, which progresses through stages even while the mapblock is not loaded. +register_food(modname, itemname, table_stages) + +Example: + +local modname = canned_food +local itemname = melon_jam +local table_stages = { + { + stage = "1", + next_stage = "2", + duration = 120, + -- All chances will be added and rolled on, only one result is possible. If "next_stage_chance" is present, if overrides "next_stage" + -- In the example below, a roll of 1 to 55 means result 1, 56 to 154 means result 3 and 155 to 161 means result 4 + next_stage_chance = {["2"] = 55, ["3"] = 99, ["4"] = 7}, + tiles = {"canned_food_melon_jam_1.png", ... }, -- Required by node definition + description = "Awesome melon jam", -- Required by node definition + node_definition_overwrite = { -- Optional "Node definition" aka "Used by minetest.register_node" + drawtype = "normal", + ... + } + }, + +} \ No newline at end of file diff --git a/information.lua b/information.lua index bb83a8a..4627e22 100644 --- a/information.lua +++ b/information.lua @@ -4,4 +4,4 @@ yl_api_food.information.author = "AliasAlreadyTaken" yl_api_food.information.license = "MIT" yl_api_food.information.name = "yl_api_food" yl_api_food.information.source = "https://gitea.your-land.de/your-land/yl_api_food" -yl_api_food.information.additional = yl_api_food.t("YL Food API") +yl_api_food.information.additional = yl_api_food.t("information_additional") diff --git a/internal.lua b/internal.lua index df667d6..bf3065c 100644 --- a/internal.lua +++ b/internal.lua @@ -7,3 +7,34 @@ local function log(text) end function yl_api_food.log(text) return log(text) end + +local function remove_timer(pos) + local t = minetest.get_node_timer(pos) + t:stop() +end + +function yl_api_food.remove_timer(pos) + return remove_timer(pos) +end + +local function on_construct(pos) + local t = minetest.get_node_timer(pos) + local node = minetest.get_node(pos) + local nodename = node.name + local duration = minetest.registered_nodes[nodename].stage.duration + core.log("action", "duration:" ..tostring(duration)) + t:start(duration) +end + + +function yl_api_food.on_construct(pos) + return on_construct(pos) +end + +local function on_timer(pos, elapsed) + core.chat_send_all(core.pos_to_string(pos).. ":" .. tostring(elapsed)) +end + +function yl_api_food.on_timer(pos, elapsed) + return on_timer(pos, elapsed) +end \ No newline at end of file diff --git a/mod.conf b/mod.conf index 99ae6d3..8f3388b 100644 --- a/mod.conf +++ b/mod.conf @@ -1,6 +1,4 @@ name = yl_api_food description = A template with best practices -depends = default -optional_depends = moreblocks author = AliasAlreadyTaken title = Template \ No newline at end of file diff --git a/texts.lua b/texts.lua index d34783d..295486a 100644 --- a/texts.lua +++ b/texts.lua @@ -2,18 +2,19 @@ local S = minetest.get_translator(yl_api_food.modname) local texts = {} -function yl_api_food.t(key, ...) return S(texts[key], ...) or "" end +--function yl_api_food.t(key, ...) return S(texts[key], ...) or "" end +function yl_api_food.t(key, ...) return string.format(texts[key], ...) or "" end -- Fixed texts -texts["log_prefix"] = "[MOD] @1 : @2" +texts["log_prefix"] = "[MOD] %s : %s" texts["get_savepath"] = "get_savepath : @1" texts["get_filepath"] = "get_filepath : @1" -- Translateable texts -texts["information_additional"] = "" +texts["information_additional"] = "YL Food API" texts["api_sent_x_to_y"] = "Sent @1 to @2"