Implements registrations and removes unnecessary files

This commit is contained in:
AliasAlreadyTaken 2025-04-16 09:37:05 +02:00
parent 8adacd1d16
commit 403711da80
13 changed files with 143 additions and 249 deletions

151
api.lua
View File

@ -1,34 +1,135 @@
-- 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_rulesets.some_api_call(target, message, color)
local function get_data() return yl_rulesets.data end
if (type(target) ~= "string") then
return false, yl_rulesets.t("error_not_a_string", "target")
end
if (minetest.get_player_by_name(target) == nil) then
return false, yl_rulesets.t("error_player_not_online", target)
function yl_rulesets.get_data() return get_data() end
-- Create ruleset_group
--
local function register_ruleset_group(ruleset_group_name)
local data = yl_rulesets.get_data()
if type(ruleset_group_name) ~= "string" then
return false, yl_rulesets.t("error_not_a_string", "ruleset_group")
end
if (type(message) ~= "string") then
return false, yl_rulesets.t("error_not_a_string", "message")
if type(data) ~= "table" then
return false, yl_rulesets.t("error_not_a_table", "data")
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_rulesets.t("error_not_a_colorspec", "color")
if data[ruleset_group_name] ~= nil then
return false, yl_rulesets.t("error_already_exists", "ruleset_group",
ruleset_group_name)
end
if (minetest.colorize == nil) then
return false, yl_rulesets.t("error_function_not_available",
"minetest.colorize")
end
data[ruleset_group_name] = {}
local message_with_color = minetest.colorize(color, message)
minetest.chat_send_player(target, message_with_color)
return true, yl_rulesets.t("api_sent_x_to_y", message_with_color, target)
return true, ""
end
function yl_rulesets.register_ruleset_group(...)
return register_ruleset_group(...)
end
-- Create ruleset
--
local function register_ruleset(ruleset_name, ruleset_group_name)
local data = yl_rulesets.get_data()
if type(ruleset_name) ~= "string" then
return false, yl_rulesets.t("error_not_a_string", "ruleset_name")
end
if type(ruleset_group_name) ~= "string" then
return false, yl_rulesets.t("error_not_a_string", "ruleset_group_name")
end
if type(data) ~= "table" then
return false, yl_rulesets.t("error_not_a_table", "data")
end
if type(data[ruleset_group_name]) ~= "table" then
return false, yl_rulesets.t("error_not_a_table", "ruleset_group_name")
end
if data[ruleset_group_name][ruleset_name] ~= nil then
return false, yl_rulesets.t("error_already_exists", "ruleset_name",
ruleset_name)
end
data[ruleset_group_name][ruleset_name] = {}
return true, ""
end
function yl_rulesets.register_ruleset(...) return register_ruleset(...) end
-- Create rule
--
local function register_rule(rule_name, ruleset_name, ruleset_group_name,
function_activate, function_deactivate, options)
local data = yl_rulesets.get_data()
-- strings
if type(rule_name) ~= "string" then
return false, yl_rulesets.t("error_not_a_string", "rule_name")
end
if type(ruleset_name) ~= "string" then
return false, yl_rulesets.t("error_not_a_string", "ruleset_name")
end
if type(ruleset_group_name) ~= "string" then
return false, yl_rulesets.t("error_not_a_string", "ruleset_group_name")
end
-- functions
if type(function_activate) ~= "function" then
return false, yl_rulesets.t("error_not_a_function", "function_activate")
end
if type(function_deactivate) ~= "function" then
return false,
yl_rulesets.t("error_not_a_function", "function_deactivate")
end
-- data
if type(data) ~= "table" then
return false, yl_rulesets.t("error_not_a_table", "data")
end
if type(data[ruleset_group_name]) ~= "table" then
return false, yl_rulesets.t("error_not_a_table", "ruleset_group_name")
end
if data[ruleset_group_name][ruleset_name] ~= "table" then
return false, yl_rulesets.t("error_not_a_table", "ruleset_name")
end
if data[ruleset_group_name][ruleset_name][rule_name] ~= nil then
return false,
yl_rulesets.t("error_already_exists", "rule_name", rule_name)
end
local rule = {
rule_name = rule_name,
ruleset_name = ruleset_name,
ruleset_group_name = ruleset_group_name,
function_activate = function_activate,
function_deactivate = function_deactivate,
options = options or {}
}
data[ruleset_group_name][ruleset_name][rule_name] = rule
return true, ""
end
function yl_rulesets.register_rule(...) return register_rule(...) end
-- Enable ruleset
--

View File

@ -1,19 +0,0 @@
local chatcommand_cmd = "admin_example"
local chatcommand_definition = {
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_rulesets.t("chatcommand_admin_success_message")
else
return false, yl_rulesets.t("chatcommand_admin_fail_message")
end
end
-- Called when command is run. Returns boolean success and text output.
-- Special case: The help message is shown to the player if `func`
-- returns false without a text output.
}
minetest.register_chatcommand(chatcommand_cmd, chatcommand_definition)

View File

@ -1,19 +0,0 @@
local chatcommand_cmd = "player_example"
local chatcommand_definition = {
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_rulesets.t("chatcommand_player_parameters")
else
return false, yl_rulesets.t("chatcommand_player_parameters")
end
end
-- Called when command is run. Returns boolean success and text output.
-- Special case: The help message is shown to the player if `func`
-- returns false without a text output.
}
minetest.register_chatcommand(chatcommand_cmd, chatcommand_definition)

View File

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

View File

@ -4,9 +4,3 @@
yl_rulesets.settings = {}
yl_rulesets.settings.debug = minetest.settings:get_bool("yl_rulesets.debug", false)
yl_rulesets.settings.external_value = "mod_default" or minetest.settings:get("yl_rulesets.external_value") or "default"
yl_rulesets.settings.save_path = "yl_rulesets" or minetest.settings:get("yl_rulesets.save_path") or "default"
yl_rulesets.settings.admin_priv = "admin_priv" or minetest.settings:get("yl_rulesets.admin_priv") or "server"

View File

@ -1,13 +0,0 @@
local timer = 0
local gs = function(dtime)
timer = timer + dtime
if timer <= yl_rulesets.config.interval then
return
end
timer = 0
-- do stuff
end
minetest.register_globalstep(gs)

View File

@ -7,48 +7,3 @@ local function log(text)
end
function yl_rulesets.log(text) return log(text) end
local function get_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_rulesets.t("get_filepath", dump(filename), dump(path_to_file)))
return path_to_file
end
local function save_json(filename, content)
if type(filename) ~= "string" or type(content) ~= "table" then
return false
end
local savepath = get_filepath(filename)
local savecontent = minetest.write_json(content)
return minetest.safe_file_write(savepath, savecontent)
end
local function load_json(path)
local file = io.open(path, "r")
if not file then
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_rulesets.t("error_cannot_read_file", dump(path))
end
return true, minetest.parse_json(content)
end
-- Public functions wrap the private ones, so they can be exchanged easily
function yl_rulesets.load(filename, ...) return load_json(filename, ...) end
function yl_rulesets.save(filename, content, ...)
return save_json(filename, content, ...)
end

View File

@ -1,6 +1,4 @@
name = yl_rulesets
description = A template with best practices
depends = default
optional_depends = moreblocks
description = Allows creation of rulesets
author = AliasAlreadyTaken
title = Template
title = YL Rulesets

View File

@ -1,76 +0,0 @@
local priv_name = "example"
local priv_definition = {
description = yl_rulesets.t("privs_example_description"),
-- Privilege description
give_to_singleplayer = false,
-- Whether to grant the privilege to singleplayer.
give_to_admin = true,
-- Whether to grant the privilege to the server admin.
-- Uses value of 'give_to_singleplayer' by default.
on_grant = function(name, granter_name)
-- logging
if (type(granter_name) ~= "string") then
local errormessage = yl_rulesets.t("error_name_not_found",
dump(granter_name))
yl_rulesets.log(errormessage)
return false, errormessage
end
if not name then
local errormessage = yl_rulesets.t("error_name_not_found",
dump(name))
yl_rulesets.log(errormessage)
return false, errormessage
end
if not priv_name then
local errormessage = yl_rulesets.t("error_priv_not_found",
dump(priv_name))
yl_rulesets.log(errormessage)
return false, errormessage
end
local text = yl_rulesets.t("privs_example_grant_logmessage",
dump(granter_name), dump(priv_name),
dump(name))
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.
on_revoke = function(name, revoker_name)
-- logging
if (type(revoker_name) ~= "string") then
local errormessage = yl_rulesets.t("error_name_not_found",
dump(revoker_name))
yl_rulesets.log(errormessage)
return false, errormessage
end
if not name then
local errormessage = yl_rulesets.t("error_name_not_found",
dump(name))
yl_rulesets.log(errormessage)
return false, errormessage
end
if not priv_name then
local errormessage = yl_rulesets.t("error_priv_not_found",
dump(priv_name))
yl_rulesets.log(errormessage)
return false, errormessage
end
local text = yl_rulesets.t("privs_example_revoke_logmessage",
dump(revoker_name), dump(priv_name),
dump(name))
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.
-- Note that the above two callbacks will be called twice if a player is
-- responsible, once with the player name, and then with a nil player
-- name.
-- Return true in the above callbacks to stop register_on_priv_grant or
-- revoke being called.
}
minetest.register_privilege(priv_name, priv_definition)

View File

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

View File

@ -1,11 +1,6 @@
[category]
[yl_rulesets]
# Debug
# Set this to true to enable debug mode, it will output some more values to log
# Optional, default is 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_rulesets.external_value (Description of the setting) string "default_value"

View File

@ -1,19 +0,0 @@
-- Use this file to set up folder and variables once after installation of the mod
-- Afterwards you could disable this part of the code or set a variable that tells
-- this code not to run again
local mkdir = minetest.mkdir
local save_path = yl_rulesets.settings.save_path
local function run_once()
local path = yl_rulesets.worldpath .. DIR_DELIM .. save_path
local file = io.open(path, "r")
if not file then
mkdir(path)
else
file:close()
end
end
run_once()

View File

@ -2,7 +2,14 @@ local S = minetest.get_translator(yl_rulesets.modname)
local texts = {}
function yl_rulesets.t(key, ...) return S(texts[key], ...) or "" end
function yl_rulesets.t(key, ...)
if (texts[key] == nil) then
minetest.log("warning", "[" .. yl_rulesets.modname .. "] key " ..
(key or "") .. " does not exist")
return key
end
return S(texts[key], ...) or ""
end
-- Fixed texts
@ -13,19 +20,7 @@ texts["get_filepath"] = "get_filepath : @1"
-- Translateable texts
texts["information_additional"] = "Additional information"
texts["chatcommand_admin_description"] = "Admin Chatcommand description"
texts["chatcommand_admin_parameters"] = "<name> <privilege>"
texts["chatcommand_admin_success_message"] = "Sucess message"
texts["chatcommand_admin_fail_message"] = "Fail message"
texts["chatcommand_player_description"] = "Player Chatcommand description"
texts["chatcommand_player_parameters"] = "<name> <privilege>"
texts["chatcommand_player_success_message"] = "Sucess message"
texts["chatcommand_player_fail_message"] = "Fail message"
texts["api_sent_x_to_y"] = "Sent @1 to @2"
texts["information_additional"] = "Allows creation of rulesets"
texts["privs_example_description"] = "Can do example task"
texts["privs_example_grant_logmessage"] = "User @1 granted priv @2 to user @3"
@ -35,6 +30,8 @@ texts["privs_example_revoke_logmessage"] =
-- Errormessages
texts["error_not_a_string"] = "@1 not a string"
texts["error_not_a_table"] = "@1 not a table"
texts["error_not_a_function"] = "@1 not a function"
texts["error_player_not_online"] = "@1 not an online player"
texts["error_not_a_colorspec"] = "@1 not a ColorSpec"
texts["error_not_a_position"] = "@1 not a position"
@ -45,3 +42,6 @@ texts["error_cannot_read_file"] = "Error reading file: @1"
texts["error_name_not_found"] = "Name not found: @1"
texts["error_priv_not_found"] = "Priv not found: @1"
texts["error_already_exists"] = "@1 already exists @2"
texts["error_doesnt_exist"] = "@1 does not exist @2"