From f89780695e3f078e82db5edc023c52820815ec19 Mon Sep 17 00:00:00 2001 From: cora Date: Tue, 24 May 2022 17:41:18 +0200 Subject: [PATCH 1/3] Add /gamemode command for per player gamemode --- mods/HUD/mcl_inventory/init.lua | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/mods/HUD/mcl_inventory/init.lua b/mods/HUD/mcl_inventory/init.lua index f2cb2e523..fd3d39799 100644 --- a/mods/HUD/mcl_inventory/init.lua +++ b/mods/HUD/mcl_inventory/init.lua @@ -177,7 +177,35 @@ minetest.register_on_joinplayer(function(player) return_fields(player, "enchanting_lapis") end) -if minetest.is_creative_enabled("") then - dofile(minetest.get_modpath(minetest.get_current_modname()).."/creative.lua") + +dofile(minetest.get_modpath(minetest.get_current_modname()).."/creative.lua") + +local mt_is_creative_enabled = minetest.is_creative_enabled + +function minetest.is_creative_enabled(name) + if mt_is_creative_enabled(name) then return true end + local p = minetest.get_player_by_name(name) + if p then + return p:get_meta():get_string("gamemode_creative") == "true" + end + return false end +minetest.register_chatcommand("gamemode",{ + privs = { server = true }, + func = function(n,param) + local p = minetest.get_player_by_name(n) + if not p then return end + local m = p:get_meta() + local gm = "survival" + if param == "creative" then + m:set_string("gamemode_creative","true") + gm = "creative" + set_inventory(p) + elseif param == "survival" then + m:set_string("gamemode_creative","") + set_inventory(p) + end + minetest.chat_send_player(n,S("Gamemode for player ")..n..S(": "..gm)) + end +}) From 2499fe1e830ae6b11c2da4e8d3c27fb646ad2d29 Mon Sep 17 00:00:00 2001 From: cora Date: Tue, 24 May 2022 19:50:22 +0200 Subject: [PATCH 2/3] add complete input validation, better result msgs --- mods/HUD/mcl_inventory/init.lua | 56 ++++++++++++++++++++++++++------- 1 file changed, 45 insertions(+), 11 deletions(-) diff --git a/mods/HUD/mcl_inventory/init.lua b/mods/HUD/mcl_inventory/init.lua index fd3d39799..caafc5975 100644 --- a/mods/HUD/mcl_inventory/init.lua +++ b/mods/HUD/mcl_inventory/init.lua @@ -186,26 +186,60 @@ function minetest.is_creative_enabled(name) if mt_is_creative_enabled(name) then return true end local p = minetest.get_player_by_name(name) if p then - return p:get_meta():get_string("gamemode_creative") == "true" + return p:get_meta():get_string("gamemode") == "creative" end return false end +local function in_table(n,h) + for k,v in pairs(h) do + if v == n then return true end + end + return false +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 = { + "survival", + "creative" +} + +local function player_set_gamemode(p,g) + local m = p:get_meta() + m:set_string("gamemode",g) + set_inventory(p) +end + minetest.register_chatcommand("gamemode",{ privs = { server = true }, func = function(n,param) + -- Full input validation ( just for @erlehmann <3 ) local p = minetest.get_player_by_name(n) - if not p then return end - local m = p:get_meta() - local gm = "survival" - if param == "creative" then - m:set_string("gamemode_creative","true") - gm = "creative" - set_inventory(p) - elseif param == "survival" then - m:set_string("gamemode_creative","") - set_inventory(p) + local args = str_split(param) + if args[2] ~= nil then + p = minetest.get_player_by_name(args[2]) end + if not p then + minetest.chat_send_player(n,S("Player not online")) + return + end + 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 + elseif args[1] ~= nil then + player_set_gamemode(p,args[1]) + end + --Result message - show effective game mode + local gm = p:get_meta():get_string("gamemode") + if gm == "" then gm = gamemodes[1] end minetest.chat_send_player(n,S("Gamemode for player ")..n..S(": "..gm)) end }) From d863a6298b3d69f9046d4cd8abebd4d8b45336c8 Mon Sep 17 00:00:00 2001 From: cora Date: Wed, 25 May 2022 16:36:04 +0200 Subject: [PATCH 3/3] fix/simplifiy things from review --- mods/HUD/mcl_inventory/init.lua | 27 +++++++++------------------ 1 file changed, 9 insertions(+), 18 deletions(-) diff --git a/mods/HUD/mcl_inventory/init.lua b/mods/HUD/mcl_inventory/init.lua index caafc5975..d47478c9e 100644 --- a/mods/HUD/mcl_inventory/init.lua +++ b/mods/HUD/mcl_inventory/init.lua @@ -198,48 +198,39 @@ local function in_table(n,h) return false 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 = { "survival", "creative" } -local function player_set_gamemode(p,g) +function mcl_inventory.player_set_gamemode(p,g) local m = p:get_meta() m:set_string("gamemode",g) set_inventory(p) end -minetest.register_chatcommand("gamemode",{ +minetest.register_chatcommand("gamemode",{ + params = S("[] []"), + description = S("Change gamemode (survival/creative) for yourself or player"), privs = { server = true }, func = function(n,param) -- Full input validation ( just for @erlehmann <3 ) local p = minetest.get_player_by_name(n) - local args = str_split(param) + local args = param:split(" ") if args[2] ~= nil then p = minetest.get_player_by_name(args[2]) end if not p then - minetest.chat_send_player(n,S("Player not online")) - return + return false, S("Player not online") end 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 + return false, S("Gamemode " .. args[1] .. " does not exist.") elseif args[1] ~= nil then - player_set_gamemode(p,args[1]) + mcl_inventory.player_set_gamemode(p,args[1]) end --Result message - show effective game mode local gm = p:get_meta():get_string("gamemode") 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 })