aboutsummaryrefslogtreecommitdiff
path: root/builtin/common
diff options
context:
space:
mode:
authorElias Fleckenstein <eliasfleckenstein@web.de>2022-05-17 22:12:00 +0200
committerElias Fleckenstein <eliasfleckenstein@web.de>2022-05-17 22:12:00 +0200
commit21df26984da91143c15587f5a03c98d68c3adc4e (patch)
treeaaa707a628ad331f67890023dffe1b4f60dd01d3 /builtin/common
parentb09fc5de5cdb021f43ad32b7e3f50dc75c0bc622 (diff)
parenteabf05758e3ba5f6f4bb1b8d1d1f02179b84e410 (diff)
downloaddragonfireclient-21df26984da91143c15587f5a03c98d68c3adc4e.tar.xz
Merge branch 'master' of https://github.com/minetest/minetest
Diffstat (limited to 'builtin/common')
-rw-r--r--builtin/common/after.lua9
-rw-r--r--builtin/common/chatcommands.lua89
-rw-r--r--builtin/common/information_formspecs.lua32
-rw-r--r--builtin/common/misc_helpers.lua2
-rw-r--r--builtin/common/strict.lua11
-rw-r--r--builtin/common/tests/misc_helpers_spec.lua1
-rw-r--r--builtin/common/tests/serialize_spec.lua1
-rw-r--r--builtin/common/tests/vector_spec.lua11
-rw-r--r--builtin/common/vector.lua16
9 files changed, 109 insertions, 63 deletions
diff --git a/builtin/common/after.lua b/builtin/common/after.lua
index e20f292f0..bce262537 100644
--- a/builtin/common/after.lua
+++ b/builtin/common/after.lua
@@ -37,7 +37,14 @@ function core.after(after, func, ...)
arg = {...},
mod_origin = core.get_last_run_mod(),
}
+
jobs[#jobs + 1] = new_job
time_next = math.min(time_next, expire)
- return { cancel = function() new_job.func = function() end end }
+
+ return {
+ cancel = function()
+ new_job.func = function() end
+ new_job.args = {}
+ end
+ }
end
diff --git a/builtin/common/chatcommands.lua b/builtin/common/chatcommands.lua
index 62f9eacd0..817f1f526 100644
--- a/builtin/common/chatcommands.lua
+++ b/builtin/common/chatcommands.lua
@@ -6,6 +6,42 @@ local S = core.get_translator("__builtin")
core.registered_chatcommands = {}
+-- Interpret the parameters of a command, separating options and arguments.
+-- Input: command, param
+-- command: name of command
+-- param: parameters of command
+-- Returns: opts, args
+-- opts is a string of option letters, or false on error
+-- args is an array with the non-option arguments in order, or an error message
+-- Example: for this command line:
+-- /command a b -cd e f -g
+-- the function would receive:
+-- a b -cd e f -g
+-- and it would return:
+-- "cdg", {"a", "b", "e", "f"}
+-- Negative numbers are taken as arguments. Long options (--option) are
+-- currently rejected as reserved.
+local function getopts(command, param)
+ local opts = ""
+ local args = {}
+ for match in param:gmatch("%S+") do
+ if match:byte(1) == 45 then -- 45 = '-'
+ local second = match:byte(2)
+ if second == 45 then
+ return false, S("Invalid parameters (see /help @1).", command)
+ elseif second and (second < 48 or second > 57) then -- 48 = '0', 57 = '9'
+ opts = opts .. match:sub(2)
+ else
+ -- numeric, add it to args
+ args[#args + 1] = match
+ end
+ else
+ args[#args + 1] = match
+ end
+ end
+ return opts, args
+end
+
function core.register_chatcommand(cmd, def)
def = def or {}
def.params = def.params or ""
@@ -78,22 +114,30 @@ if INIT == "client" then
end
end
+local function format_help_line(cmd, def)
+ local cmd_marker = INIT == "client" and "." or "/"
+ local msg = core.colorize("#00ffff", cmd_marker .. cmd)
+ if def.params and def.params ~= "" then
+ msg = msg .. " " .. def.params
+ end
+ if def.description and def.description ~= "" then
+ msg = msg .. ": " .. def.description
+ end
+ return msg
+end
+
local function do_help_cmd(name, param)
- local function format_help_line(cmd, def)
- local cmd_marker = "/"
- if INIT == "client" then
- cmd_marker = "."
- end
- local msg = core.colorize("#00ffff", cmd_marker .. cmd)
- if def.params and def.params ~= "" then
- msg = msg .. " " .. def.params
- end
- if def.description and def.description ~= "" then
- msg = msg .. ": " .. def.description
- end
- return msg
+ local opts, args = getopts("help", param)
+ if not opts then
+ return false, args
+ end
+ if #args > 1 then
+ return false, S("Too many arguments, try using just /help <command>")
end
- if param == "" then
+ local use_gui = INIT ~= "client" and core.get_player_by_name(name)
+ use_gui = use_gui and not opts:find("t")
+
+ if #args == 0 and not use_gui then
local cmds = {}
for cmd, def in pairs(core.registered_chatcommands) do
if INIT == "client" or core.check_player_privs(name, def.privs) then
@@ -116,7 +160,10 @@ local function do_help_cmd(name, param)
.. "everything.")
end
return true, msg
- elseif param == "all" then
+ elseif #args == 0 or (args[1] == "all" and use_gui) then
+ core.show_general_help_formspec(name)
+ return true
+ elseif args[1] == "all" then
local cmds = {}
for cmd, def in pairs(core.registered_chatcommands) do
if INIT == "client" or core.check_player_privs(name, def.privs) then
@@ -131,7 +178,11 @@ local function do_help_cmd(name, param)
msg = core.gettext("Available commands:")
end
return true, msg.."\n"..table.concat(cmds, "\n")
- elseif INIT == "game" and param == "privs" then
+ elseif INIT == "game" and args[1] == "privs" then
+ if use_gui then
+ core.show_privs_help_formspec(name)
+ return true
+ end
local privs = {}
for priv, def in pairs(core.registered_privileges) do
privs[#privs + 1] = priv .. ": " .. def.description
@@ -139,7 +190,7 @@ local function do_help_cmd(name, param)
table.sort(privs)
return true, S("Available privileges:").."\n"..table.concat(privs, "\n")
else
- local cmd = param
+ local cmd = args[1]
local def = core.registered_chatcommands[cmd]
if not def then
local msg
@@ -165,8 +216,8 @@ if INIT == "client" then
})
else
core.register_chatcommand("help", {
- params = S("[all | privs | <cmd>]"),
- description = S("Get help for commands or list privileges"),
+ params = S("[all | privs | <cmd>] [-t]"),
+ description = S("Get help for commands or list privileges (-t: output in chat)"),
func = do_help_cmd,
})
end
diff --git a/builtin/common/information_formspecs.lua b/builtin/common/information_formspecs.lua
index e814b4c43..3405263bf 100644
--- a/builtin/common/information_formspecs.lua
+++ b/builtin/common/information_formspecs.lua
@@ -125,30 +125,12 @@ core.register_on_player_receive_fields(function(player, formname, fields)
end
end)
-
-local help_command = core.registered_chatcommands["help"]
-local old_help_func = help_command.func
-
-help_command.func = function(name, param)
- local admin = core.settings:get("name")
-
- -- If the admin ran help, put the output in the chat buffer as well to
- -- work with the server terminal
- if param == "privs" then
- core.show_formspec(name, "__builtin:help_privs",
- build_privs_formspec(name))
- if name ~= admin then
- return true
- end
- end
- if param == "" or param == "all" then
- core.show_formspec(name, "__builtin:help_cmds",
- build_chatcommands_formspec(name))
- if name ~= admin then
- return true
- end
- end
-
- return old_help_func(name, param)
+function core.show_general_help_formspec(name)
+ core.show_formspec(name, "__builtin:help_cmds",
+ build_chatcommands_formspec(name))
end
+function core.show_privs_help_formspec(name)
+ core.show_formspec(name, "__builtin:help_privs",
+ build_privs_formspec(name))
+end
diff --git a/builtin/common/misc_helpers.lua b/builtin/common/misc_helpers.lua
index e4bc056d9..542b2040d 100644
--- a/builtin/common/misc_helpers.lua
+++ b/builtin/common/misc_helpers.lua
@@ -543,7 +543,7 @@ if INIT == "mainmenu" then
end
end
-if INIT == "client" or INIT == "mainmenu" then
+if core.gettext then -- for client and mainmenu
function fgettext_ne(text, ...)
text = core.gettext(text)
local arg = {n=select('#', ...), ...}
diff --git a/builtin/common/strict.lua b/builtin/common/strict.lua
index ccde9676b..f46234dc6 100644
--- a/builtin/common/strict.lua
+++ b/builtin/common/strict.lua
@@ -1,8 +1,3 @@
-
--- Always warn when creating a global variable, even outside of a function.
--- This ignores mod namespaces (variables with the same name as the current mod).
-local WARN_INIT = false
-
local getinfo = debug.getinfo
function core.global_exists(name)
@@ -33,11 +28,6 @@ function meta:__newindex(name, value)
end
declared[name] = true
end
- -- Ignore mod namespaces
- if WARN_INIT and name ~= core.get_current_modname() then
- core.log("warning", ("Global variable %q created at %s.")
- :format(name, desc))
- end
rawset(self, name, value)
end
@@ -54,4 +44,3 @@ function meta:__index(name)
end
setmetatable(_G, meta)
-
diff --git a/builtin/common/tests/misc_helpers_spec.lua b/builtin/common/tests/misc_helpers_spec.lua
index b16987f0b..b11236860 100644
--- a/builtin/common/tests/misc_helpers_spec.lua
+++ b/builtin/common/tests/misc_helpers_spec.lua
@@ -1,4 +1,5 @@
_G.core = {}
+_G.vector = {metatable = {}}
dofile("builtin/common/vector.lua")
dofile("builtin/common/misc_helpers.lua")
diff --git a/builtin/common/tests/serialize_spec.lua b/builtin/common/tests/serialize_spec.lua
index e46b7dcc5..69b2b567c 100644
--- a/builtin/common/tests/serialize_spec.lua
+++ b/builtin/common/tests/serialize_spec.lua
@@ -1,4 +1,5 @@
_G.core = {}
+_G.vector = {metatable = {}}
_G.setfenv = require 'busted.compatibility'.setfenv
diff --git a/builtin/common/tests/vector_spec.lua b/builtin/common/tests/vector_spec.lua
index 2a50e2889..6a0b81a89 100644
--- a/builtin/common/tests/vector_spec.lua
+++ b/builtin/common/tests/vector_spec.lua
@@ -1,4 +1,4 @@
-_G.vector = {}
+_G.vector = {metatable = {}}
dofile("builtin/common/vector.lua")
describe("vector", function()
@@ -128,6 +128,14 @@ describe("vector", function()
assert.equal(vector.new(4.1, 5.9, 5.5), a:apply(f))
end)
+ it("combine()", function()
+ local a = vector.new(1, 2, 3)
+ local b = vector.new(3, 2, 1)
+ assert.equal(vector.add(a, b), vector.combine(a, b, function(x, y) return x + y end))
+ assert.equal(vector.new(3, 2, 3), vector.combine(a, b, math.max))
+ assert.equal(vector.new(1, 2, 1), vector.combine(a, b, math.min))
+ end)
+
it("equals()", function()
local function assertE(a, b)
assert.is_true(vector.equals(a, b))
@@ -300,6 +308,7 @@ describe("vector", function()
it("from_string()", function()
local v = vector.new(1, 2, 3.14)
+ assert.is_true(vector.check(vector.from_string("(1, 2, 3.14)")))
assert.same({v, 13}, {vector.from_string("(1, 2, 3.14)")})
assert.same({v, 12}, {vector.from_string("(1,2 ,3.14)")})
assert.same({v, 12}, {vector.from_string("(1,2,3.14,)")})
diff --git a/builtin/common/vector.lua b/builtin/common/vector.lua
index 02fc1bdee..a08472e32 100644
--- a/builtin/common/vector.lua
+++ b/builtin/common/vector.lua
@@ -6,10 +6,8 @@ Note: The vector.*-functions must be able to accept old vectors that had no meta
-- localize functions
local setmetatable = setmetatable
-vector = {}
-
-local metatable = {}
-vector.metatable = metatable
+-- vector.metatable is set by C++.
+local metatable = vector.metatable
local xyz = {"x", "y", "z"}
@@ -61,7 +59,7 @@ function vector.from_string(s, init)
if not (x and y and z) then
return nil
end
- return {x = x, y = y, z = z}, np
+ return fast_new(x, y, z), np
end
function vector.to_string(v)
@@ -112,6 +110,14 @@ function vector.apply(v, func)
)
end
+function vector.combine(a, b, func)
+ return fast_new(
+ func(a.x, b.x),
+ func(a.y, b.y),
+ func(a.z, b.z)
+ )
+end
+
function vector.distance(a, b)
local x = a.x - b.x
local y = a.y - b.y