Splits data into legacy, default and json

This commit is contained in:
AliasAlreadyTaken 2024-07-16 01:55:04 +02:00
parent cbc01d1435
commit f7755d2086
15 changed files with 120 additions and 16 deletions

View File

@ -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.

View File

@ -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"

View File

@ -1,2 +1,3 @@
dofile(yl_canned_food_mtg.modpath .. "data_legacy.lua")
dofile(yl_canned_food_mtg.modpath .. "data_recipes.lua")
dofile(yl_canned_food_mtg.modpath .. "data_recipes.lua")
dofile(yl_canned_food_mtg.modpath .. "data_json.lua")

6
data_json.lua Normal file
View File

@ -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 = {}

View File

@ -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",

View File

@ -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
}
}
}
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)

View File

@ -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"}]

View File

@ -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 = {

View File

@ -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

View File

@ -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",

View File

@ -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")

View File

@ -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()

View File

@ -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

View File

@ -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

19
setup.lua Normal file
View File

@ -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()