Changes template to ruleset namespace, adds readme, adds examples

This commit is contained in:
AliasAlreadyTaken 2025-04-16 05:44:29 +02:00
parent db295f4157
commit 6ed7e97463
19 changed files with 217 additions and 110 deletions

View File

@ -5,8 +5,8 @@ Copyright (c) 2024 Developername, Styxcolor
Code:
* [MIT](https://gitea.your-land.de/your-land/yl_template/src/LICENSE-MIT) Developername
* [MIT](https://gitea.your-land.de/your-land/yl_rulesets/src/LICENSE-MIT) Developername
Media:
* screenshot.png [CC0](https://gitea.your-land.de/your-land/yl_template/src/LICENSE-CC0) Styxcolor
* screenshot.png [CC0](https://gitea.your-land.de/your-land/yl_rulesets/src/LICENSE-CC0) Styxcolor

View File

@ -1,56 +1,94 @@
# yl_template
# yl_rulesets
## Purpose
This mod is not meant to bring functionality by itself, but to serve as a template you can base your mod on. Remove the components you do not want and expand on those you want.
This mod is a library to create, apply and reset rulesets. Rulesets in the sense of this mod are a group of mechanics, settings or other ways to alter what the server does. One example of a ruleset may be "All mobs drop double the amount of items and award three times the xp, but are four times as dangerous as usual" or "All mobs drop half the amount of items and award half the xp, but are also only half as dangerous as usual"
## Terms and definitions
In the context of this mod a `rule` is a single alteration to the server. For example "mobs drops are halved" or "mobs drops are twice as common". To apply a number of rules at the same time, they are aggregated in a `ruleset`. Individual rules cannot be enabled or disabled on their own. They must be part of a ruleset, which is applied as a whole. The mod makes sure that within a `ruleset_group` only one ruleset is active at the same time.
## Download
Get it from https://gitea.your-land.de/your-land/yl_template
Get it from https://gitea.your-land.de/your-land/yl_rulesets
## Dependencies
`Delete the Dependency section if your mod has no dependencies`
See [mod.conf](mod.conf)
* [modname_of_dependency](https://gitea.your-land.de/link/to/git/of/dependency)
## Installation
1. Copy the "yl_template" folder to your mod directory.
1. Copy the "yl_rulesets" folder to your mod directory.
2. Make sure the [dependencies](#dependencies) are satisfied, if any.
3. Enable the mod in your world.mt file.
## Configuration
```
yl_template.debug = false
yl_rulesets.debug = false
```
Set to true to enable debug mode
```
yl_template.save_path
```
Set this to where in the worldfolder you want the JSON files stored.
## Usage
This mod targets `[servers|singleplayer|...]`, but should work in `[[servers|singleplayer|...]`, too. It comes with no direct content but exposes functions you can use in your mod.
This mod targets servers, but should work in singleplayer, too. It comes with no direct content but exposes functions you can use in your mod.
Define one or more `ruleset_group` per mod. Create `rulesets` between which you will switch and assign `rules` you create to those rulesets. Each rule needs a function that is executed when the rule is activated and another function that rolls back the rules once you switch to another ruleset. Then, during operation, switch between rulesets as you need. Make sure to cleanly separate rules from each other and rulesets, so that you don't overwrite one rule with a another rule from a different set. It makes sense to group the rules thematically.
### Chatcommands
None so far
### Modmakers
Use the following public functions to `[achieve your goals]`
Use the following public functions to create rules, rulesets and ruleset_groups and switch between them:
```
yl_rulesets.register_ruleset_group("ruleset_group_name")
```
Creates a `ruleset_group` of the name "ruleset_group_name". Returns `true, ""` if the ruleset group was successfully created. Returns `false, "errormessage"` if the ruleset group cannot be created.
```
yl_rulesets.register_ruleset("ruleset_name", "ruleset_group_name")
```
Creates a `ruleset` of the name "ruleset_name" as member of the "ruleset_group_name" ruleset group. Returns `true, ""` if the ruleset was successfully created. Returns `false, "errormessage"` if the ruleset cannot be created.
```
yl_rulesets.register_rule("rulename", "ruleset_name", "ruleset_group_name", function_activate, function_deactivate, {options})
```
Creates a `rule` of the name "rule_name" and adds it to the `ruleset` "ruleset_name" of group "ruleset_group_name". `function_activate` is the function that gets executed when this rule comes into effect. `function_deactivate` is the rule that rolls back what the activation changed. `options` is a table of options, see below. Returns `true, ""` if the rule was successfully created. Returns `false, "errormessage"` if the rule cannot be created.
```
yl_rulesets.enable("ruleset_group", "ruleset")
```
Enables a `ruleset` of a given `ruleset_group`. All deactivate-functions of the previous ruleset are executed and all activate functions of the given `ruleset` are executed. Returns `true, number_of_rules` if the ruleset was successfully activated. Returns `false, "errormessage"` if the ruleset was not successfully activated.
```
function_activate()
function_deactivate()
```
These are the functions that are executed, when a rule is activated or deactivated. Must return `true` on success and must return `false` on failure.
## Limitations
* This mod does not police what is in rules.
* It cannot make sure rulesets don't overwrite each other's or other ruleset_groups' rules
## Alternatives
* Apply and roll back the rules manually
## Supported versions
If you use yl_template, but something is wrong, please [file a bug](https://gitea.your-land.de/your-land/yl_template/issues/new). PRs also welcome.
If you use yl_rulesets, but something is wrong, please [file a bug](https://gitea.your-land.de/your-land/yl_rulesets/issues/new). PRs also welcome.
There is no reason to believe it doesn't work anywhere, but you never know.
@ -66,11 +104,9 @@ Mods that depend on it will cease to work, if the mod is removed without proper
## License
[Shorthand of your chosen license: MIT, LGPLv3+, GPLv3+, AGPL, ...]. Please choose one of the major license that is compatible with as much of the MT universe over some obscure and incompatible license. If you use the screenshot.png, keep Styxcolor in there because she made that screenshot.png, if you add your own, replace the line with whoever made it. Example for MIT and CC0:
See [LICENSE.md](https://gitea.your-land.de/your-land/yl_rulesets/src/LICENSE.md)
See [LICENSE.md](https://gitea.your-land.de/your-land/yl_template/src/LICENSE.md)
* Code MIT Developername
* Code MIT AliasAlreadyTaken
* Screenshot CC0 Styxcolor
## Thank you

14
api.lua
View File

@ -4,31 +4,31 @@
-- 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_template.some_api_call(target, message, color)
function yl_rulesets.some_api_call(target, message, color)
if (type(target) ~= "string") then
return false, yl_template.t("error_not_a_string", "target")
return false, yl_rulesets.t("error_not_a_string", "target")
end
if (minetest.get_player_by_name(target) == nil) then
return false, yl_template.t("error_player_not_online", target)
return false, yl_rulesets.t("error_player_not_online", target)
end
if (type(message) ~= "string") then
return false, yl_template.t("error_not_a_string", "message")
return false, yl_rulesets.t("error_not_a_string", "message")
end
-- is_color(color) does not exist, you need to implement it if you want to use it
if (is_color(color) == false) then
return false, yl_template.t("error_not_a_colorspec", "color")
return false, yl_rulesets.t("error_not_a_colorspec", "color")
end
if (minetest.colorize == nil) then
return false, yl_template.t("error_function_not_available",
return false, yl_rulesets.t("error_function_not_available",
"minetest.colorize")
end
local message_with_color = minetest.colorize(color, message)
minetest.chat_send_player(target, message_with_color)
return true, yl_template.t("api_sent_x_to_y", message_with_color, target)
return true, yl_rulesets.t("api_sent_x_to_y", message_with_color, target)
end

View File

@ -1,14 +1,14 @@
local chatcommand_cmd = "admin_example"
local chatcommand_definition = {
params = yl_template.t("chatcommand_admin_parameters"), -- Short parameter description
description = yl_template.t("chatcommand_admin_description"), -- Full description
privs = {[yl_template.settings.admin_priv] = true}, -- Require the "privs" privilege to run
params = yl_rulesets.t("chatcommand_admin_parameters"), -- Short parameter description
description = yl_rulesets.t("chatcommand_admin_description"), -- Full description
privs = {[yl_rulesets.settings.admin_priv] = true}, -- Require the "privs" privilege to run
func = function(name, param)
local success = true
if success then
return true, yl_template.t("chatcommand_admin_success_message")
return true, yl_rulesets.t("chatcommand_admin_success_message")
else
return false, yl_template.t("chatcommand_admin_fail_message")
return false, yl_rulesets.t("chatcommand_admin_fail_message")
end
end
-- Called when command is run. Returns boolean success and text output.

View File

@ -1,14 +1,14 @@
local chatcommand_cmd = "player_example"
local chatcommand_definition = {
params = yl_template.t("chatcommand_player_parameters"), -- Short parameter description
description = yl_template.t("chatcommand_player_parameters"), -- Full description
params = yl_rulesets.t("chatcommand_player_parameters"), -- Short parameter description
description = yl_rulesets.t("chatcommand_player_parameters"), -- Full description
privs = {privs = true}, -- Require the "privs" privilege to run
func = function(name, param)
local success = true
if success then
return true, yl_template.t("chatcommand_player_parameters")
return true, yl_rulesets.t("chatcommand_player_parameters")
else
return false, yl_template.t("chatcommand_player_parameters")
return false, yl_rulesets.t("chatcommand_player_parameters")
end
end
-- Called when command is run. Returns boolean success and text output.

View File

@ -1,2 +1,2 @@
dofile(yl_template.modpath .. "chatcommand_admin.lua")
dofile(yl_template.modpath .. "chatcommand_player.lua")
dofile(yl_rulesets.modpath .. "chatcommand_admin.lua")
dofile(yl_rulesets.modpath .. "chatcommand_player.lua")

View File

@ -1,12 +1,12 @@
-- Setting a configuration, switch the order in which the settings shall take precedence. First valid one taken.
yl_template.settings = {}
yl_rulesets.settings = {}
yl_template.settings.debug = minetest.settings:get_bool("yl_template.debug", false)
yl_rulesets.settings.debug = minetest.settings:get_bool("yl_rulesets.debug", false)
yl_template.settings.external_value = "mod_default" or minetest.settings:get("yl_template.external_value") or "default"
yl_rulesets.settings.external_value = "mod_default" or minetest.settings:get("yl_rulesets.external_value") or "default"
yl_template.settings.save_path = "yl_template" or minetest.settings:get("yl_template.save_path") or "default"
yl_rulesets.settings.save_path = "yl_rulesets" or minetest.settings:get("yl_rulesets.save_path") or "default"
yl_template.settings.admin_priv = "admin_priv" or minetest.settings:get("yl_template.admin_priv") or "server"
yl_rulesets.settings.admin_priv = "admin_priv" or minetest.settings:get("yl_rulesets.admin_priv") or "server"

View File

@ -2,3 +2,76 @@ 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.
Ruleset group "defense":
ruleset 1: rule 1: No one can join, rule 2: no one gets privs
ruleset 2: rule 1: Everyone can join, rule 2: everyone gets privs
Ruleset group "monsters":
ruleset 1: rule 1: mobs give 2x XP, rule 2: mobs 3x as dangerous
ruleset 2: rule 1: mobs give 0.5 xp, rule 2: mobs half as dangerous
yl_rules.register_ruleset_group("rulesetgroup")
yl_rules.register_ruleset("ruleset", "rulesetgroup")
yl_rules.register_rule("rulename", "ruleset", "rulesetgroup", function_activate, function_deactivate,{options})
yl_rules.register_rule("all-join", yl_defense.function_activate, default,{})
yl_rules.register_rule("no-join", yl_defense.no_join_activate, default,{})
yl_rules.register_rule("no-join-inpolite", yl_defense.no_join_inpolite_activate, default,{})
###
function default()
lockdown = false
lockdown_message = nil
end
function yl_defense.all_join_activate()
lockdown = false
end
function yl_defense.all_join_deactivate()
lockdown = nil
end
function yl_defense.no_join_activate()
lockdown = true
lockdown_message = "Sorry, we're not taking new players at the moment"
end
function yl_defense.no_join_deactivate()
lockdown = false
<----
end
function yl_defense.no_join_inpolite_activate()
lockdown = true
lockdown_message = "Sorry, we're not taking new players at the moment"
end
function yl_defense.no_join_inpolite_deactivate()
lockdown = false
lockdown_message = ""
end
##
local lockdown = nil
core.register_on_prejoinplayer(function(name, ip)
if lockdown == true then
return "Sorry, we're not taking new players at the moment"
end
end)

View File

@ -2,7 +2,7 @@ local timer = 0
local gs = function(dtime)
timer = timer + dtime
if timer <= yl_template.config.interval then
if timer <= yl_rulesets.config.interval then
return
end
timer = 0

View File

@ -1,7 +1,7 @@
yl_template.information = {}
yl_template.information.version = "0.0.1"
yl_template.information.author = "AliasAlreadyTaken"
yl_template.information.license = "MIT"
yl_template.information.name = "yl_template" -- Name of the mod
yl_template.information.source = "https://gitea.your-land.de/your-land/yl_template"
yl_template.information.additional = yl_template.t("information_additional")
yl_rulesets.information = {}
yl_rulesets.information.version = "0.0.1"
yl_rulesets.information.author = "AliasAlreadyTaken"
yl_rulesets.information.license = "MIT"
yl_rulesets.information.name = "yl_rulesets" -- Name of the mod
yl_rulesets.information.source = "https://gitea.your-land.de/your-land/yl_rulesets"
yl_rulesets.information.additional = yl_rulesets.t("information_additional")

View File

@ -5,27 +5,25 @@
-- Changelog
local mod_start_time = minetest.get_us_time()
minetest.log("action", "[MOD] yl_template loading")
minetest.log("action", "[MOD] yl_rulesets loading")
yl_template = {}
yl_template.error = {}
yl_template.modname = minetest.get_current_modname()
yl_template.modstorage = minetest.get_mod_storage()
yl_template.modpath = minetest.get_modpath("yl_template") .. DIR_DELIM
yl_template.worldpath = minetest.get_worldpath() .. DIR_DELIM
yl_rulesets = {}
yl_rulesets.error = {}
yl_rulesets.modname = minetest.get_current_modname()
yl_rulesets.modstorage = minetest.get_mod_storage()
yl_rulesets.modpath = minetest.get_modpath("yl_rulesets") .. DIR_DELIM
yl_rulesets.worldpath = minetest.get_worldpath() .. DIR_DELIM
dofile(yl_template.modpath .. "texts.lua")
dofile(yl_template.modpath .. "information.lua")
dofile(yl_template.modpath .. "config.lua")
dofile(yl_template.modpath .. "setup.lua")
dofile(yl_template.modpath .. "privs.lua")
dofile(yl_template.modpath .. "internal.lua")
dofile(yl_template.modpath .. "api.lua")
dofile(yl_template.modpath .. "initialize.lua")
dofile(yl_template.modpath .. "distinct_feature.lua")
dofile(yl_template.modpath .. "overwrite_feature.lua")
dofile(yl_template.modpath .. "globalsteps.lua")
dofile(yl_template.modpath .. "chatcommands.lua")
dofile(yl_rulesets.modpath .. "texts.lua")
dofile(yl_rulesets.modpath .. "information.lua")
dofile(yl_rulesets.modpath .. "config.lua")
-- dofile(yl_rulesets.modpath .. "setup.lua")
-- dofile(yl_rulesets.modpath .. "privs.lua")
dofile(yl_rulesets.modpath .. "internal.lua")
dofile(yl_rulesets.modpath .. "api.lua")
-- dofile(yl_rulesets.modpath .. "initialize.lua")
-- dofile(yl_rulesets.modpath .. "globalsteps.lua")
-- dofile(yl_rulesets.modpath .. "chatcommands.lua")
local mod_end_time = (minetest.get_us_time() - mod_start_time) / 1000000
minetest.log("action", "[MOD] yl_template loaded in [" .. mod_end_time .. "s]")
minetest.log("action", "[MOD] yl_rulesets loaded in [" .. mod_end_time .. "s]")

View File

@ -1,9 +1,9 @@
-- Use this file to initialize variables once after server start and check everything is in place
local function run_each_serverstart()
yl_template.data = {}
--minetest.on_mods_loaded(0.0, yl_template.on_mods_loaded)
--minetest.after(0.0, yl_template.after)
yl_rulesets.data = {}
--minetest.on_mods_loaded(0.0, yl_rulesets.on_mods_loaded)
--minetest.after(0.0, yl_rulesets.after)
end
run_each_serverstart()

View File

@ -1,22 +1,22 @@
-- The functions and variables in this file are only for use in the mod itself.
-- Those that do real work should be local and wrapped in public functions
local function log(text)
local logmessage = yl_template.t("log_prefix", yl_template.modname, text)
if yl_template.settings.debug then minetest.log("action", logmessage) end
local logmessage = yl_rulesets.t("log_prefix", yl_rulesets.modname, text)
if yl_rulesets.settings.debug then minetest.log("action", logmessage) end
return logmessage
end
function yl_template.log(text) return log(text) end
function yl_rulesets.log(text) return log(text) end
local function get_savepath()
local savepath = yl_template.worldpath .. yl_template.settings.save_path
log(yl_template.t("log_prefix", dump(savepath)))
local savepath = yl_rulesets.worldpath .. yl_rulesets.settings.save_path
log(yl_rulesets.t("log_prefix", dump(savepath)))
return savepath
end
local function get_filepath(filename)
local path_to_file = get_savepath() .. DIR_DELIM .. filename .. ".json"
log(yl_template.t("get_filepath", dump(filename), dump(path_to_file)))
log(yl_rulesets.t("get_filepath", dump(filename), dump(path_to_file)))
return path_to_file
end
@ -32,14 +32,14 @@ end
local function load_json(path)
local file = io.open(path, "r")
if not file then
return false, yl_template.t("error_cannot_open_file", dump(path))
return false, yl_rulesets.t("error_cannot_open_file", dump(path))
end
local content = file:read("*all")
file:close()
if not content then
return false, yl_template.t("error_cannot_read_file", dump(path))
return false, yl_rulesets.t("error_cannot_read_file", dump(path))
end
return true, minetest.parse_json(content)
@ -47,8 +47,8 @@ end
-- Public functions wrap the private ones, so they can be exchanged easily
function yl_template.load(filename, ...) return load_json(filename, ...) end
function yl_rulesets.load(filename, ...) return load_json(filename, ...) end
function yl_template.save(filename, content, ...)
function yl_rulesets.save(filename, content, ...)
return save_json(filename, content, ...)
end

View File

@ -1,4 +1,4 @@
name = yl_template
name = yl_rulesets
description = A template with best practices
depends = default
optional_depends = moreblocks

View File

@ -1,6 +1,6 @@
local priv_name = "example"
local priv_definition = {
description = yl_template.t("privs_example_description"),
description = yl_rulesets.t("privs_example_description"),
-- Privilege description
give_to_singleplayer = false,
@ -13,27 +13,27 @@ local priv_definition = {
on_grant = function(name, granter_name)
-- logging
if (type(granter_name) ~= "string") then
local errormessage = yl_template.t("error_name_not_found",
local errormessage = yl_rulesets.t("error_name_not_found",
dump(granter_name))
yl_template.log(errormessage)
yl_rulesets.log(errormessage)
return false, errormessage
end
if not name then
local errormessage = yl_template.t("error_name_not_found",
local errormessage = yl_rulesets.t("error_name_not_found",
dump(name))
yl_template.log(errormessage)
yl_rulesets.log(errormessage)
return false, errormessage
end
if not priv_name then
local errormessage = yl_template.t("error_priv_not_found",
local errormessage = yl_rulesets.t("error_priv_not_found",
dump(priv_name))
yl_template.log(errormessage)
yl_rulesets.log(errormessage)
return false, errormessage
end
local text = yl_template.t("privs_example_grant_logmessage",
local text = yl_rulesets.t("privs_example_grant_logmessage",
dump(granter_name), dump(priv_name),
dump(name))
yl_template.log(text)
yl_rulesets.log(text)
end,
-- Called when given to player 'name' by 'granter_name'.
-- 'granter_name' will be nil if the priv was granted by a mod.
@ -41,27 +41,27 @@ local priv_definition = {
on_revoke = function(name, revoker_name)
-- logging
if (type(revoker_name) ~= "string") then
local errormessage = yl_template.t("error_name_not_found",
local errormessage = yl_rulesets.t("error_name_not_found",
dump(revoker_name))
yl_template.log(errormessage)
yl_rulesets.log(errormessage)
return false, errormessage
end
if not name then
local errormessage = yl_template.t("error_name_not_found",
local errormessage = yl_rulesets.t("error_name_not_found",
dump(name))
yl_template.log(errormessage)
yl_rulesets.log(errormessage)
return false, errormessage
end
if not priv_name then
local errormessage = yl_template.t("error_priv_not_found",
local errormessage = yl_rulesets.t("error_priv_not_found",
dump(priv_name))
yl_template.log(errormessage)
yl_rulesets.log(errormessage)
return false, errormessage
end
local text = yl_template.t("privs_example_revoke_logmessage",
local text = yl_rulesets.t("privs_example_revoke_logmessage",
dump(revoker_name), dump(priv_name),
dump(name))
yl_template.log(text)
yl_rulesets.log(text)
end
-- Called when taken from player 'name' by 'revoker_name'.
-- 'revoker_name' will be nil if the priv was revoked by a mod.

View File

@ -1 +1 @@
dofile(yl_template.modpath .. "priv_example.lua")
dofile(yl_rulesets.modpath .. "priv_example.lua")

View File

@ -3,9 +3,9 @@
# Debug
# Set this to true to enable debug mode, it will output some more values to log
# Optional, default is false
yl_template.debug (Debug mode) bool false
yl_rulesets.debug (Debug mode) bool false
# First line: Name the settings
# Second line: Say what it does, how to use it, what it changes
# Third line: Is it optional? What's the default value? Does it expect a certain set of values?
yl_template.external_value (Description of the setting) string "default_value"
yl_rulesets.external_value (Description of the setting) string "default_value"

View File

@ -3,10 +3,10 @@
-- this code not to run again
local mkdir = minetest.mkdir
local save_path = yl_template.settings.save_path
local save_path = yl_rulesets.settings.save_path
local function run_once()
local path = yl_template.worldpath .. DIR_DELIM .. save_path
local path = yl_rulesets.worldpath .. DIR_DELIM .. save_path
local file = io.open(path, "r")
if not file then
mkdir(path)

View File

@ -1,8 +1,8 @@
local S = minetest.get_translator(yl_template.modname)
local S = minetest.get_translator(yl_rulesets.modname)
local texts = {}
function yl_template.t(key, ...) return S(texts[key], ...) or "" end
function yl_rulesets.t(key, ...) return S(texts[key], ...) or "" end
-- Fixed texts