From f7755d20861d7b056258d88ff673ca3928688256 Mon Sep 17 00:00:00 2001 From: AliasAlreadyTaken Date: Tue, 16 Jul 2024 01:55:04 +0200 Subject: [PATCH] Splits data into legacy, default and json --- README.md | 10 ++++++++ config.lua | 5 ++++ data.lua | 3 ++- data_json.lua | 6 +++++ data_legacy.lua | 6 ++--- data_recipes.lua | 12 ++++++--- dev/yl_canned_food_mtg.json | 1 + feature_eat.lua | 2 +- feature_recipes.lua | 4 +-- feature_unified_inventory.lua | 2 +- init.lua | 3 ++- initialize.lua | 3 ++- internal.lua | 48 ++++++++++++++++++++++++++++++++++- settingtypes.txt | 12 ++++++++- setup.lua | 19 ++++++++++++++ 15 files changed, 120 insertions(+), 16 deletions(-) create mode 100644 data_json.lua create mode 100644 dev/yl_canned_food_mtg.json create mode 100644 setup.lua diff --git a/README.md b/README.md index b02cda6..2096ad8 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,16 @@ yl_canned_food_mtg.enable_unified_inventory = true ``` Set this to true if you want to enable unified_inventory integration +``` +yl_canned_food_mtg.data_source = "default" +``` +Set this to where the data shall be loaded from. Use "default" for a new world, "legacy" for a world where this mod replaces canned_food and "json" where you decide the data for yourself. Here is an example json: [yl_canned_food_mtg.json](dev/yl_canned_food_mtg.json) + +``` +yl_canned_food_mtg.save_path +``` +Set this to where in the worldfolder you want the JSON files stored. + ## Usage This mod can be used in singleplayer and multiplayer. It adds crafting recipes, nutritional values and unified inventory integration for minetest_game. If your game is a different one, use their canned_food integration instead. diff --git a/config.lua b/config.lua index d7599ca..5292791 100644 --- a/config.lua +++ b/config.lua @@ -13,3 +13,8 @@ yl_canned_food_mtg.settings.enable_eat = yl_canned_food_mtg.settings.enable_unified_inventory = minetest.settings:get_bool("yl_canned_food_mtg.enable_unified_inventory", true) + +yl_canned_food_mtg.settings.data_source = + minetest.settings:get("yl_canned_food_mtg.data_source") or "default" + +yl_canned_food_mtg.settings.save_path = minetest.settings:get("yl_scheduler.yl_canned_food_mtg") or "yl_canned_food_mtg" \ No newline at end of file diff --git a/data.lua b/data.lua index 144c290..4f45949 100644 --- a/data.lua +++ b/data.lua @@ -1,2 +1,3 @@ dofile(yl_canned_food_mtg.modpath .. "data_legacy.lua") -dofile(yl_canned_food_mtg.modpath .. "data_recipes.lua") \ No newline at end of file +dofile(yl_canned_food_mtg.modpath .. "data_recipes.lua") +dofile(yl_canned_food_mtg.modpath .. "data_json.lua") \ No newline at end of file diff --git a/data_json.lua b/data_json.lua new file mode 100644 index 0000000..ccee9f3 --- /dev/null +++ b/data_json.lua @@ -0,0 +1,6 @@ +if (yl_canned_food_mtg.settings.data_source ~= "json") then + return false +end + +-- filled by initialize +yl_canned_food_mtg.json_recipes = {} diff --git a/data_legacy.lua b/data_legacy.lua index 6da97cf..445d94e 100644 --- a/data_legacy.lua +++ b/data_legacy.lua @@ -1,8 +1,8 @@ -if (yl_canned_food.settings.legacy ~= true) then +if (yl_canned_food_mtg.settings.data_source ~= "legacy") then return false end -yl_canned_food_mtg.recipes = { +yl_canned_food_mtg.legacy_recipes = { { additives = {}, nutrition = 6, @@ -12,7 +12,7 @@ yl_canned_food_mtg.recipes = { base_amount = 5 }, { additives = {}, - nutrition = 21, + nutrition = 20, out = "canned_food:honey_jar", base_mod = "mobs_animal", base_item = "honey", diff --git a/data_recipes.lua b/data_recipes.lua index ed945c8..88ea3e7 100644 --- a/data_recipes.lua +++ b/data_recipes.lua @@ -1,8 +1,8 @@ -if (yl_canned_food.settings.legacy == true) then +if (yl_canned_food_mtg.settings.data_source ~= "default") then return false end -yl_canned_food_mtg.recipes = { +yl_canned_food_mtg.default_recipes = { { additives = {}, nutrition = 6, @@ -12,7 +12,7 @@ yl_canned_food_mtg.recipes = { base_amount = 5 }, { additives = {}, - nutrition = 21, + nutrition = 20, out = "yl_canned_food:honey_jar", base_mod = "mobs_animal", base_item = "honey", @@ -214,4 +214,8 @@ yl_canned_food_mtg.recipes = { base_item = "orange", base_amount = 3 } -} \ No newline at end of file +} + +local content = minetest.write_json(yl_canned_food_mtg.default_recipes) +core.log("action","meow") +minetest.safe_file_write(yl_canned_food_mtg.worldpath .. "yl_canned_food_mtg.json", content) \ No newline at end of file diff --git a/dev/yl_canned_food_mtg.json b/dev/yl_canned_food_mtg.json new file mode 100644 index 0000000..2ba3ac3 --- /dev/null +++ b/dev/yl_canned_food_mtg.json @@ -0,0 +1 @@ +[{"additives":null,"base_amount":5.0,"base_item":"mushroom_brown","base_mod":"flowers","nutrition":6.0,"out":"yl_canned_food:canned_mushrooms"},{"additives":null,"base_amount":4.0,"base_item":"honey","base_mod":"mobs_animal","nutrition":21.0,"out":"yl_canned_food:honey_jar"},{"additives":null,"base_amount":5.0,"base_item":"potato","base_mod":"farming","nutrition":6.0,"out":"yl_canned_food:canned_potato"},{"additives":null,"base_amount":5.0,"base_item":"beetroot","base_mod":"farming","nutrition":6.0,"out":"yl_canned_food:canned_beetroot"},{"additives":null,"base_amount":5.0,"base_item":"pineapple_ring","base_mod":"farming","nutrition":6.0,"out":"yl_canned_food:canned_pineapple"},{"additives":null,"base_amount":3.0,"base_item":"cucumber","base_mod":"farming","nutrition":15.0,"out":"yl_canned_food:canned_cucumber"},{"additives":null,"base_amount":8.0,"base_item":"pine_nuts","base_mod":"ethereal","nutrition":10.0,"out":"yl_canned_food:pine_nuts_jar"},{"additives":null,"base_amount":4.0,"base_item":"onion","base_mod":"farming","nutrition":5.0,"out":"yl_canned_food:canned_onion"},{"additives":["group:sugar"],"base_amount":5.0,"base_item":"strawberry","base_mod":"ethereal","nutrition":7.0,"out":"yl_canned_food:strawberry_jam"},{"additives":null,"base_amount":3.0,"base_item":"tomato","base_mod":"farming","nutrition":15.0,"out":"yl_canned_food:canned_tomato"},{"additives":null,"base_amount":8.0,"base_item":"garlic_clove","base_mod":"farming","nutrition":5.0,"out":"yl_canned_food:canned_garlic_cloves"},{"additives":["group:sugar"],"base_amount":6.0,"base_item":"blueberries","base_mod":"default","nutrition":8.0,"out":"yl_canned_food:blueberry_jam"},{"additives":["group:sugar"],"base_amount":6.0,"base_item":"blueberries","base_mod":"farming","nutrition":8.0,"out":"yl_canned_food:blueberry_jam"},{"additives":null,"base_amount":3.0,"base_item":"pumpkin_slice","base_mod":"farming","nutrition":7.0,"out":"yl_canned_food:canned_pumpkin"},{"additives":null,"base_amount":3.0,"base_item":"corn","base_mod":"farming","nutrition":11.0,"out":"yl_canned_food:canned_corn"},{"additives":["group:sugar"],"base_amount":6.0,"base_item":"blackberry","base_mod":"farming","nutrition":8.0,"out":"yl_canned_food:blackberry_jam"},{"additives":null,"base_amount":8.0,"base_item":"peas","base_mod":"farming","nutrition":10.0,"out":"yl_canned_food:canned_peas"},{"additives":null,"base_amount":6.0,"base_item":"beans","base_mod":"farming","nutrition":7.0,"out":"yl_canned_food:canned_beans"},{"additives":["group:sugar"],"base_amount":6.0,"base_item":"raspberries","base_mod":"farming","nutrition":8.0,"out":"yl_canned_food:raspberry_jam"},{"additives":null,"base_amount":3.0,"base_item":"apple","base_mod":"default","nutrition":7.0,"out":"yl_canned_food:apple_jam"},{"additives":null,"base_amount":6.0,"base_item":"chili_pepper","base_mod":"farming","nutrition":7.0,"out":"yl_canned_food:canned_chili_pepper"},{"additives":["group:sugar"],"base_amount":4.0,"base_item":"grapes","base_mod":"farming","nutrition":11.0,"out":"yl_canned_food:grape_jam"},{"additives":null,"base_amount":5.0,"base_item":"coconut_slice","base_mod":"ethereal","nutrition":6.0,"out":"yl_canned_food:canned_coconut"},{"additives":null,"base_amount":3.0,"base_item":"carrot","base_mod":"farming","nutrition":15.0,"out":"yl_canned_food:canned_carrot"},{"additives":["group:sugar"],"base_amount":6.0,"base_item":"rhubarb","base_mod":"farming","nutrition":8.0,"out":"yl_canned_food:rhubarb_jam"},{"additives":["group:sugar"],"base_amount":5.0,"base_item":"banana","base_mod":"ethereal","nutrition":7.0,"out":"yl_canned_food:banana_jam"},{"additives":null,"base_amount":5.0,"base_item":"dandelion_yellow","base_mod":"flowers","nutrition":6.0,"out":"yl_canned_food:dandelion_jam"},{"additives":["group:sugar"],"base_amount":3.0,"base_item":"melon_slice","base_mod":"farming","nutrition":8.0,"out":"yl_canned_food:melon_jam"},{"additives":null,"base_amount":5.0,"base_item":"rose","base_mod":"flowers","nutrition":6.0,"out":"yl_canned_food:rose_jam"},{"additives":["group:sugar"],"base_amount":3.0,"base_item":"orange","base_mod":"ethereal","nutrition":8.0,"out":"yl_canned_food:orange_jam"}] \ No newline at end of file diff --git a/feature_eat.lua b/feature_eat.lua index 5a03764..5c7182a 100644 --- a/feature_eat.lua +++ b/feature_eat.lua @@ -9,7 +9,7 @@ end local amount = 0 -for _, recipe in ipairs(yl_canned_food_mtg.recipes) do +for _, recipe in ipairs(yl_canned_food_mtg.data) do -- normal local redefinition = { diff --git a/feature_recipes.lua b/feature_recipes.lua index 6f2aa52..dde67aa 100644 --- a/feature_recipes.lua +++ b/feature_recipes.lua @@ -16,7 +16,7 @@ local vessel = "vessels:glass_bottle" itemcache[vessel] = true -for _, recipe in ipairs(yl_canned_food_mtg.recipes) do +for _, recipe in ipairs(yl_canned_food_mtg.data) do itemcache[recipe.out] = true local ingredient = recipe.base_mod .. ":" .. recipe.base_item @@ -36,7 +36,7 @@ for item, _ in pairs(itemcache) do end end -for _, recipe in ipairs(yl_canned_food_mtg.recipes) do +for _, recipe in ipairs(yl_canned_food_mtg.data) do local crafting = {} local success = true diff --git a/feature_unified_inventory.lua b/feature_unified_inventory.lua index 168cbb2..8309acf 100644 --- a/feature_unified_inventory.lua +++ b/feature_unified_inventory.lua @@ -26,7 +26,7 @@ unified_inventory.register_craft_type("yl_canned_food", { local amount = 0 -for _, recipe in ipairs(yl_canned_food_mtg.recipes) do +for _, recipe in ipairs(yl_canned_food_mtg.data) do unified_inventory.register_craft({ output = recipe.out .. "_plus", type = "yl_canned_food", diff --git a/init.lua b/init.lua index 2a0c989..22dc8ae 100644 --- a/init.lua +++ b/init.lua @@ -12,11 +12,12 @@ yl_canned_food_mtg.error = {} yl_canned_food_mtg.modname = minetest.get_current_modname() -- yl_canned_food_mtg.modstorage = minetest.get_mod_storage() yl_canned_food_mtg.modpath = minetest.get_modpath("yl_canned_food_mtg") .. DIR_DELIM --- yl_canned_food_mtg.worldpath = minetest.get_worldpath() .. DIR_DELIM +yl_canned_food_mtg.worldpath = minetest.get_worldpath() .. DIR_DELIM dofile(yl_canned_food_mtg.modpath .. "texts.lua") dofile(yl_canned_food_mtg.modpath .. "information.lua") dofile(yl_canned_food_mtg.modpath .. "config.lua") +dofile(yl_canned_food_mtg.modpath .. "setup.lua") dofile(yl_canned_food_mtg.modpath .. "data.lua") dofile(yl_canned_food_mtg.modpath .. "internal.lua") dofile(yl_canned_food_mtg.modpath .. "initialize.lua") diff --git a/initialize.lua b/initialize.lua index e1f8b5f..7c2bcbf 100644 --- a/initialize.lua +++ b/initialize.lua @@ -1,7 +1,8 @@ -- Use this file to initialize variables once after server start and check everything is in place local function run_each_serverstart() - yl_canned_food_mtg.data = yl_canned_food_mtg.recipes + assert(yl_canned_food_mtg.check_settings_match(),yl_canned_food_mtg.t("settings_dont_match")) + yl_canned_food_mtg.data = yl_canned_food_mtg.get_data() end run_each_serverstart() diff --git a/internal.lua b/internal.lua index fc1d176..b9f08a6 100644 --- a/internal.lua +++ b/internal.lua @@ -10,7 +10,7 @@ function yl_canned_food_mtg.log(text) return log(text) end local function get_savepath() local savepath = yl_canned_food_mtg.worldpath .. yl_canned_food_mtg.settings.save_path - log(yl_canned_food_mtg.t("log_prefix", dump(savepath))) + log(yl_canned_food_mtg.t("log_prefix", dump(savepath), "")) return savepath end @@ -47,8 +47,54 @@ end -- Public functions wrap the private ones, so they can be exchanged easily +function yl_canned_food_mtg.get_filepath(filename) + return get_filepath(filename) +end + function yl_canned_food_mtg.load(filename, ...) return load_json(filename, ...) end function yl_canned_food_mtg.save(filename, content, ...) return save_json(filename, content, ...) end + +-- Read data +-- + +local function get_data() + + local filename = yl_canned_food_mtg.settings.save_path + + local filepath = yl_canned_food_mtg.get_filepath(filename) + core.log("action","MEOW=".. dump(filepath)) + yl_canned_food_mtg.json_recipes = yl_canned_food_mtg.load(filepath) or {} + + return yl_canned_food_mtg.legacy_recipes or yl_canned_food_mtg.default_recipes or yl_canned_food_mtg.json_recipes +end + +function yl_canned_food_mtg.get_data() + return get_data() +end + +-- Check sttings match +-- + +local function check_settings_match() + core.log("action","yl_canned_food.legacy=".. dump(yl_canned_food.legacy)) + core.log("action","yl_canned_food_mtg.settings.data_source=".. dump(yl_canned_food_mtg.settings.data_source)) + if (yl_canned_food.settings.legacy == true) and (yl_canned_food_mtg.settings.data_source == "legacy") then + -- legacy: Nodenames looke like canned_food:apple_jam + return true + elseif (yl_canned_food.settings.legacy == false) and (yl_canned_food_mtg.settings.data_source == "default") then + -- default: Nodenames looke like yl_canned_food:apple_jam + return true + elseif (yl_canned_food_mtg.settings.data_source == "json") then + -- json: It's the user's responsibility to adjust the nodenames in the json + return true + else + return false + end +end + +function yl_canned_food_mtg.check_settings_match() + return check_settings_match() +end \ No newline at end of file diff --git a/settingtypes.txt b/settingtypes.txt index 5879690..19e807b 100644 --- a/settingtypes.txt +++ b/settingtypes.txt @@ -16,6 +16,16 @@ yl_canned_food_mtg.enable_recipes (Enable recipes) bool true yl_canned_food_mtg.enable_eat (Debug mode) bool true # Enable unified_inventory integration -Set this to true if you want to enable unified_inventory integration. Set this to false to disable the integration. +# Set this to true if you want to enable unified_inventory integration. Set this to false to disable the integration. # Optional, default is true yl_canned_food_mtg.enable_unified_inventory (Enable unified_inventory integration) bool true + +# Data source +# Set this to where the data will be loaded from. Use "default" for a new world, "legacy" for a world where this mod replaces canned_food and "json" where you decide the data for yourself. +# optional, default is "default" +yl_canned_food_mtg.data_source (Data source) enum default default,legacy,json + +# Save path +# Set this to where in the worldfolder you want the JSON files stored. +# Optional, default is yl_canned_food_mtg +yl_canned_food_mtg.save_path (Save path) string yl_canned_food_mtg \ No newline at end of file diff --git a/setup.lua b/setup.lua new file mode 100644 index 0000000..84625e4 --- /dev/null +++ b/setup.lua @@ -0,0 +1,19 @@ +if yl_canned_food_mtg.settings.data_source ~= "json" then + return false +end + +local mkdir = minetest.mkdir +local save_path = yl_canned_food_mtg.settings.save_path + +local function run_once() + local path = yl_canned_food_mtg.worldpath .. DIR_DELIM .. save_path + local file = io.open(path, "r") + if not file then + mkdir(path) + else + file:close() + end + +end + +run_once()