Remplement and improve /setting in Lua, now called /set

master
Perttu Ahola 2012-04-01 13:08:52 +03:00
parent b54178bbbd
commit 8ecfd88d92
4 changed files with 81 additions and 66 deletions

View File

@ -1,7 +1,7 @@
-- Minetest: builtin/chatcommands.lua -- Minetest: builtin/chatcommands.lua
-- --
-- Chat commands -- Chat command handler
-- --
minetest.chatcommands = {} minetest.chatcommands = {}
@ -13,7 +13,43 @@ function minetest.register_chatcommand(cmd, def)
minetest.chatcommands[cmd] = def minetest.chatcommands[cmd] = def
end end
-- Register the help command minetest.register_on_chat_message(function(name, message)
local cmd, param = string.match(message, "/([^ ]+) *(.*)")
if not param then
param = ""
end
local cmd_def = minetest.chatcommands[cmd]
if cmd_def then
if not cmd_def.func then
-- This is a C++ command
return false
else
local has_privs, missing_privs = minetest.check_player_privs(name, cmd_def.privs)
if has_privs then
cmd_def.func(name, param)
else
minetest.chat_send_player(name, "You don't have permission to run this command (missing privileges: "..table.concat(missing_privs, ", ")..")")
end
return true -- handled chat message
end
end
return false
end)
--
-- Chat commands
--
-- Register C++ commands without functions
minetest.register_chatcommand("me", {params = nil, description = "chat action (eg. /me orders a pizza)"})
minetest.register_chatcommand("status", {description = "print server status line"})
minetest.register_chatcommand("shutdown", {params = "", description = "shutdown server", privs = {server=true}})
minetest.register_chatcommand("clearobjects", {params = "", description = "clear all objects in world", privs = {server=true}})
minetest.register_chatcommand("time", {params = "<0...24000>", description = "set time of day", privs = {settime=true}})
minetest.register_chatcommand("ban", {params = "<name>", description = "ban IP of player", privs = {ban=true}})
minetest.register_chatcommand("unban", {params = "<name/ip>", description = "remove IP ban", privs = {ban=true}})
-- Register other commands
minetest.register_chatcommand("help", { minetest.register_chatcommand("help", {
privs = {}, privs = {},
params = "(nothing)/all/privs/<cmd>", params = "(nothing)/all/privs/<cmd>",
@ -58,18 +94,6 @@ minetest.register_chatcommand("help", {
end end
end, end,
}) })
-- Register C++ commands without functions
minetest.register_chatcommand("me", {params = nil, description = "chat action (eg. /me orders a pizza)"})
minetest.register_chatcommand("status", {description = "print server status line"})
minetest.register_chatcommand("shutdown", {params = "", description = "shutdown server", privs = {server=true}})
minetest.register_chatcommand("setting", {params = "<name> = <value>", description = "set line in configuration file", privs = {server=true}})
minetest.register_chatcommand("clearobjects", {params = "", description = "clear all objects in world", privs = {server=true}})
minetest.register_chatcommand("time", {params = "<0...24000>", description = "set time of day", privs = {settime=true}})
minetest.register_chatcommand("ban", {params = "<name>", description = "ban IP of player", privs = {ban=true}})
minetest.register_chatcommand("unban", {params = "<name/ip>", description = "remove IP ban", privs = {ban=true}})
-- Register some other commands
minetest.register_chatcommand("privs", { minetest.register_chatcommand("privs", {
params = "<name>", params = "<name>",
description = "print out privileges of player", description = "print out privileges of player",
@ -272,31 +296,37 @@ minetest.register_chatcommand("teleport", {
end, end,
}) })
-- minetest.register_chatcommand("set", {
-- Builtin chat handler params = "[-n] <name> <value> | <name>",
-- description = "set or read server configuration setting",
privs = {server=true},
minetest.register_on_chat_message(function(name, message) func = function(name, param)
local cmd, param = string.match(message, "/([^ ]+) *(.*)") local arg, setname, setvalue = string.match(param, "(-[n]) ([^ ]+) (.+)")
if not param then if arg and arg == "n" and setname and setvalue then
param = "" minetest.setting_set(setname, setvalue)
end minetest.chat_send_player(name, setname.." = "..setvalue)
local cmd_def = minetest.chatcommands[cmd] return
if cmd_def then end
if not cmd_def.func then local setname, setvalue = string.match(param, "([^ ]+) (.+)")
-- This is a C++ command if setname and setvalue then
return false if not minetest.setting_get(setname) then
else minetest.chat_send_player(name, "Failed. Use '/set -n <name> <value>' to create a new setting.")
local has_privs, missing_privs = minetest.check_player_privs(name, cmd_def.privs) return
if has_privs then end
cmd_def.func(name, param) minetest.setting_set(setname, setvalue)
else minetest.chat_send_player(name, setname.." = "..setvalue)
minetest.chat_send_player(name, "You don't have permission to run this command (missing privileges: "..table.concat(missing_privs, ", ")..")") return
end end
return true -- handled chat message local setname = string.match(param, "([^ ]+)")
end if setname then
end local setvalue = minetest.setting_get(setname)
return false if not setvalue then
end) setvalue = "<not set>"
end
minetest.chat_send_player(name, setname.." = "..setvalue)
return
end
minetest.chat_send_player(name, "Invalid parameters (see /help set)")
end,
})

View File

@ -516,6 +516,7 @@ minetest.register_authentication_handler(handler)
^ See minetest.builtin_auth_handler in builtin.lua for reference ^ See minetest.builtin_auth_handler in builtin.lua for reference
Setting-related: Setting-related:
minetest.setting_set(name, value)
minetest.setting_get(name) -> string or nil minetest.setting_get(name) -> string or nil
minetest.setting_getbool(name) -> boolean value or nil minetest.setting_getbool(name) -> boolean value or nil
minetest.add_to_creative_inventory(itemstring) minetest.add_to_creative_inventory(itemstring)

View File

@ -3800,6 +3800,15 @@ static int l_register_craft(lua_State *L)
return 0; /* number of results */ return 0; /* number of results */
} }
// setting_set(name, value)
static int l_setting_set(lua_State *L)
{
const char *name = luaL_checkstring(L, 1);
const char *value = luaL_checkstring(L, 2);
g_settings->set(name, value);
return 0;
}
// setting_get(name) // setting_get(name)
static int l_setting_get(lua_State *L) static int l_setting_get(lua_State *L)
{ {
@ -4006,6 +4015,7 @@ static const struct luaL_Reg minetest_f [] = {
{"register_item_raw", l_register_item_raw}, {"register_item_raw", l_register_item_raw},
{"register_alias_raw", l_register_alias_raw}, {"register_alias_raw", l_register_alias_raw},
{"register_craft", l_register_craft}, {"register_craft", l_register_craft},
{"setting_set", l_setting_set},
{"setting_get", l_setting_get}, {"setting_get", l_setting_get},
{"setting_getbool", l_setting_getbool}, {"setting_getbool", l_setting_getbool},
{"chat_send_all", l_chat_send_all}, {"chat_send_all", l_chat_send_all},

View File

@ -79,30 +79,6 @@ void cmd_shutdown(std::wostringstream &os,
ctx->flags |= SEND_TO_OTHERS; ctx->flags |= SEND_TO_OTHERS;
} }
void cmd_setting(std::wostringstream &os,
ServerCommandContext *ctx)
{
if(!ctx->server->checkPriv(ctx->player->getName(), "server"))
{
os<<L"-!- You don't have permission to do that";
return;
}
/*std::string confline = wide_to_narrow(
ctx->parms[1] + L" = " + ctx->params[2]);*/
std::string confline = wide_to_narrow(ctx->paramstring);
actionstream<<ctx->player->getName()
<<" sets: "<<confline<<std::endl;
g_settings->parseConfigLine(confline);
ctx->server->saveConfig();
os<< L"-!- Setting changed and configuration saved.";
}
void cmd_banunban(std::wostringstream &os, ServerCommandContext *ctx) void cmd_banunban(std::wostringstream &os, ServerCommandContext *ctx)
{ {
if(!ctx->server->checkPriv(ctx->player->getName(), "ban")) if(!ctx->server->checkPriv(ctx->player->getName(), "ban"))
@ -194,8 +170,6 @@ std::wstring processServerCommand(ServerCommandContext *ctx)
cmd_time(os, ctx); cmd_time(os, ctx);
else if(ctx->parms[0] == L"shutdown") else if(ctx->parms[0] == L"shutdown")
cmd_shutdown(os, ctx); cmd_shutdown(os, ctx);
else if(ctx->parms[0] == L"setting")
cmd_setting(os, ctx);
else if(ctx->parms[0] == L"ban" || ctx->parms[0] == L"unban") else if(ctx->parms[0] == L"ban" || ctx->parms[0] == L"unban")
cmd_banunban(os, ctx); cmd_banunban(os, ctx);
else if(ctx->parms[0] == L"me") else if(ctx->parms[0] == L"me")