1
0
Fork 0

fix/simplifiy things from review

This commit is contained in:
cora 2022-05-25 16:36:04 +02:00
parent 2499fe1e83
commit d863a6298b
1 changed files with 9 additions and 18 deletions

View File

@ -198,48 +198,39 @@ local function in_table(n,h)
return false return false
end end
function str_split(s,d)
if d == nil then d = "%s" end
local t={}
for v in string.gmatch(s, "([^"..d.."]+)") do
table.insert(t, v)
end
return t
end
local gamemodes = { local gamemodes = {
"survival", "survival",
"creative" "creative"
} }
local function player_set_gamemode(p,g) function mcl_inventory.player_set_gamemode(p,g)
local m = p:get_meta() local m = p:get_meta()
m:set_string("gamemode",g) m:set_string("gamemode",g)
set_inventory(p) set_inventory(p)
end end
minetest.register_chatcommand("gamemode",{ minetest.register_chatcommand("gamemode",{
params = S("[<gamemode>] [<player>]"),
description = S("Change gamemode (survival/creative) for yourself or player"),
privs = { server = true }, privs = { server = true },
func = function(n,param) func = function(n,param)
-- Full input validation ( just for @erlehmann <3 ) -- Full input validation ( just for @erlehmann <3 )
local p = minetest.get_player_by_name(n) local p = minetest.get_player_by_name(n)
local args = str_split(param) local args = param:split(" ")
if args[2] ~= nil then if args[2] ~= nil then
p = minetest.get_player_by_name(args[2]) p = minetest.get_player_by_name(args[2])
end end
if not p then if not p then
minetest.chat_send_player(n,S("Player not online")) return false, S("Player not online")
return
end end
if args[1] ~= nil and not in_table(args[1],gamemodes) then if args[1] ~= nil and not in_table(args[1],gamemodes) then
minetest.chat_send_player(n,S("Gamemode "..tostring(args[1]).." does not exist." )) return false, S("Gamemode " .. args[1] .. " does not exist.")
return
elseif args[1] ~= nil then elseif args[1] ~= nil then
player_set_gamemode(p,args[1]) mcl_inventory.player_set_gamemode(p,args[1])
end end
--Result message - show effective game mode --Result message - show effective game mode
local gm = p:get_meta():get_string("gamemode") local gm = p:get_meta():get_string("gamemode")
if gm == "" then gm = gamemodes[1] end if gm == "" then gm = gamemodes[1] end
minetest.chat_send_player(n,S("Gamemode for player ")..n..S(": "..gm)) return true, S("Gamemode for player ")..n..S(": "..gm)
end end
}) })