From 37ca0efe05b84e64d1ebf810d936b4d6fbbdc977 Mon Sep 17 00:00:00 2001 From: ancientmarinerdev Date: Thu, 9 Mar 2023 23:46:14 +0000 Subject: [PATCH 1/4] New, improved, safe clearmobs command --- mods/ENTITIES/mcl_mobs/api.lua | 110 ++++++++++++++++++++++++++++++--- 1 file changed, 101 insertions(+), 9 deletions(-) diff --git a/mods/ENTITIES/mcl_mobs/api.lua b/mods/ENTITIES/mcl_mobs/api.lua index cfa02c941..eeee823b0 100644 --- a/mods/ENTITIES/mcl_mobs/api.lua +++ b/mods/ENTITIES/mcl_mobs/api.lua @@ -534,20 +534,112 @@ minetest.register_globalstep(function(dtime) timer = 0 end) + minetest.register_chatcommand("clearmobs",{ privs={maphack=true}, - params = "||", - description=S("Removes all spawned mobs except nametagged and tamed ones. all removes all mobs, nametagged only nametagged ones and with the range paramter all mobs in a distance of the current player are removed."), + params = "|| |", + description=S("Removes specified spawned mobs except nametagged and tamed ones. For param2, use nametagged to remove those with a nametag, or range for all mobs in a distance of the current player to be removed."), func=function(n,param) + if not param or param == "" then + minetest.log("Incorrect usage. For more information please type: /help clearmobs") + return + else + minetest.log("param: " .. dump(param)) + end + + local mob, unsafe = param:match("^([%w]+)[ ]?([%w%d]*)$") + + local all = false + local nametagged = false + + local mob_name, mob_type, range + + if mob and mob ~= "" then + if mob == "all" then + all = true + elseif mob == "passive" or mob == "monster" then + mob_type = mob + elseif mob then + mob_name = mob + end + minetest.log ("mob: [" .. mob .. "]") + else + minetest.log("No valid second param") + return + end + + if unsafe and unsafe ~= "" then + minetest.log ("unsafe: [" .. unsafe .. "]") + if unsafe == "nametagged" then + nametagged = true + end + + local num = tonumber(unsafe) + if num then + minetest.log("valid number") + range = num + else + minetest.log("not a number") + end + end + local p = minetest.get_player_by_name(n) - local num=tonumber(param) + + minetest.log("Range: ".. tostring(range)) + for _,o in pairs(minetest.luaentities) do - if o.is_mob then - if param == "all" or - ( param == "nametagged" and o.nametag ) or - ( param == "" and ( not o.nametag or o.nametag == "" ) and not o.tamed ) or - ( num and num > 0 and vector.distance(p:get_pos(),o.object:get_pos()) <= num ) then - o.object:remove() + if o and o.is_mob then + local mob_match = false + + if all then + minetest.log("Match - All mobs specified") + mob_match = true + elseif mob_type then + + minetest.log("Match - o.type: ".. tostring(o.type)) + minetest.log("mob_type: ".. tostring(mob_type)) + if mob_type == "monster" and o.type == mob_type then + minetest.log("Match - monster") + mob_match = true + elseif mob_type == "passive" and o.type ~= "monster" and o.type ~= "npc" then + minetest.log("Match - passive") + mob_match = true + else + minetest.log("No match for type.") + end + + elseif mob_name and (o.name == mob_name or string.find(o.name, mob_name)) then + minetest.log("Match - mob_name = ".. tostring(o.name)) + mob_match = true + else + --minetest.log("No match - o.type = ".. tostring(o.type)) + --minetest.log("No match - mob_name = ".. tostring(o.name)) + --minetest.log("No match - mob_type = ".. tostring(mob_name)) + end + + if mob_match then + local in_range = true + if (not range or range <= 0 ) then + in_range = true + else + if ( vector.distance(p:get_pos(),o.object:get_pos()) <= range ) then + in_range = true + else + minetest.log("Out of range") + in_range = false + end + end + + minetest.log("o.nametag: ".. tostring(o.nametag)) + if in_range and ( (not o.nametag or o.nametag == "" ) and not o.tamed ) then + minetest.log("No nametag or tamed. Kill it") + --o.object:remove() + elseif nametagged and o.nametag then + minetest.log("Namedtagged and it has a name tag. Kill it") + --o.object:remove() + else + minetest.log("Tamed or out of range, do not kill") + end end end end From c97fef03b9f3136fcdc9cbe58862caa3f2a0eae4 Mon Sep 17 00:00:00 2001 From: ancientmarinerdev Date: Thu, 9 Mar 2023 23:49:26 +0000 Subject: [PATCH 2/4] Comment out logging, and add in the destruction --- mods/ENTITIES/mcl_mobs/api.lua | 42 ++++++++++++++-------------------- 1 file changed, 17 insertions(+), 25 deletions(-) diff --git a/mods/ENTITIES/mcl_mobs/api.lua b/mods/ENTITIES/mcl_mobs/api.lua index eeee823b0..b6024e426 100644 --- a/mods/ENTITIES/mcl_mobs/api.lua +++ b/mods/ENTITIES/mcl_mobs/api.lua @@ -543,10 +543,7 @@ minetest.register_chatcommand("clearmobs",{ if not param or param == "" then minetest.log("Incorrect usage. For more information please type: /help clearmobs") return - else - minetest.log("param: " .. dump(param)) end - local mob, unsafe = param:match("^([%w]+)[ ]?([%w%d]*)$") local all = false @@ -554,6 +551,7 @@ minetest.register_chatcommand("clearmobs",{ local mob_name, mob_type, range + -- Param 1 resolve if mob and mob ~= "" then if mob == "all" then all = true @@ -562,12 +560,13 @@ minetest.register_chatcommand("clearmobs",{ elseif mob then mob_name = mob end - minetest.log ("mob: [" .. mob .. "]") + --minetest.log ("mob: [" .. mob .. "]") else - minetest.log("No valid second param") + --minetest.log("No valid second param") return end + -- Param 2 resolve if unsafe and unsafe ~= "" then minetest.log ("unsafe: [" .. unsafe .. "]") if unsafe == "nametagged" then @@ -575,37 +574,30 @@ minetest.register_chatcommand("clearmobs",{ end local num = tonumber(unsafe) - if num then - minetest.log("valid number") - range = num - else - minetest.log("not a number") - end + if num then range = num end end local p = minetest.get_player_by_name(n) - minetest.log("Range: ".. tostring(range)) - for _,o in pairs(minetest.luaentities) do if o and o.is_mob then local mob_match = false if all then - minetest.log("Match - All mobs specified") + --minetest.log("Match - All mobs specified") mob_match = true elseif mob_type then - minetest.log("Match - o.type: ".. tostring(o.type)) - minetest.log("mob_type: ".. tostring(mob_type)) + --minetest.log("Match - o.type: ".. tostring(o.type)) + --minetest.log("mob_type: ".. tostring(mob_type)) if mob_type == "monster" and o.type == mob_type then - minetest.log("Match - monster") + --minetest.log("Match - monster") mob_match = true elseif mob_type == "passive" and o.type ~= "monster" and o.type ~= "npc" then - minetest.log("Match - passive") + --minetest.log("Match - passive") mob_match = true else - minetest.log("No match for type.") + --minetest.log("No match for type.") end elseif mob_name and (o.name == mob_name or string.find(o.name, mob_name)) then @@ -625,20 +617,20 @@ minetest.register_chatcommand("clearmobs",{ if ( vector.distance(p:get_pos(),o.object:get_pos()) <= range ) then in_range = true else - minetest.log("Out of range") + --minetest.log("Out of range") in_range = false end end minetest.log("o.nametag: ".. tostring(o.nametag)) if in_range and ( (not o.nametag or o.nametag == "" ) and not o.tamed ) then - minetest.log("No nametag or tamed. Kill it") - --o.object:remove() + --minetest.log("No nametag or tamed. Kill it") + o.object:remove() elseif nametagged and o.nametag then - minetest.log("Namedtagged and it has a name tag. Kill it") - --o.object:remove() + --minetest.log("Namedtagged and it has a name tag. Kill it") + o.object:remove() else - minetest.log("Tamed or out of range, do not kill") + --minetest.log("Tamed or out of range, do not kill") end end end From fc7eb135e6b30951d0137c9c36116c2ec10cab6a Mon Sep 17 00:00:00 2001 From: ancientmarinerdev Date: Fri, 10 Mar 2023 00:08:12 +0000 Subject: [PATCH 3/4] Comment out leftover logging --- mods/ENTITIES/mcl_mobs/api.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/mods/ENTITIES/mcl_mobs/api.lua b/mods/ENTITIES/mcl_mobs/api.lua index b6024e426..282aab2b3 100644 --- a/mods/ENTITIES/mcl_mobs/api.lua +++ b/mods/ENTITIES/mcl_mobs/api.lua @@ -568,7 +568,7 @@ minetest.register_chatcommand("clearmobs",{ -- Param 2 resolve if unsafe and unsafe ~= "" then - minetest.log ("unsafe: [" .. unsafe .. "]") + --minetest.log ("unsafe: [" .. unsafe .. "]") if unsafe == "nametagged" then nametagged = true end @@ -601,7 +601,7 @@ minetest.register_chatcommand("clearmobs",{ end elseif mob_name and (o.name == mob_name or string.find(o.name, mob_name)) then - minetest.log("Match - mob_name = ".. tostring(o.name)) + --minetest.log("Match - mob_name = ".. tostring(o.name)) mob_match = true else --minetest.log("No match - o.type = ".. tostring(o.type)) @@ -622,7 +622,7 @@ minetest.register_chatcommand("clearmobs",{ end end - minetest.log("o.nametag: ".. tostring(o.nametag)) + --minetest.log("o.nametag: ".. tostring(o.nametag)) if in_range and ( (not o.nametag or o.nametag == "" ) and not o.tamed ) then --minetest.log("No nametag or tamed. Kill it") o.object:remove() From 270820125df0a247c47425eefe3bc9e56bf96686 Mon Sep 17 00:00:00 2001 From: ancientmarinerdev Date: Sat, 11 Mar 2023 23:10:48 +0000 Subject: [PATCH 4/4] Add in a default for improved clearmobs command. --- mods/ENTITIES/mcl_mobs/api.lua | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/mods/ENTITIES/mcl_mobs/api.lua b/mods/ENTITIES/mcl_mobs/api.lua index 282aab2b3..8342a037d 100644 --- a/mods/ENTITIES/mcl_mobs/api.lua +++ b/mods/ENTITIES/mcl_mobs/api.lua @@ -537,12 +537,14 @@ end) minetest.register_chatcommand("clearmobs",{ privs={maphack=true}, - params = "|| |", + params = "[||] [|]", description=S("Removes specified spawned mobs except nametagged and tamed ones. For param2, use nametagged to remove those with a nametag, or range for all mobs in a distance of the current player to be removed."), - func=function(n,param) + func=function(player, param) + local default = false if not param or param == "" then - minetest.log("Incorrect usage. For more information please type: /help clearmobs") - return + default = true + minetest.chat_send_player(player, + S("Default usage. Clearing hostile mobs. For more options please type: /help clearmobs")) end local mob, unsafe = param:match("^([%w]+)[ ]?([%w%d]*)$") @@ -562,8 +564,12 @@ minetest.register_chatcommand("clearmobs",{ end --minetest.log ("mob: [" .. mob .. "]") else - --minetest.log("No valid second param") - return + --minetest.log("No valid first param") + if default then + --minetest.log("Use default") + mob_type = "monster" + end + --return end -- Param 2 resolve @@ -577,7 +583,7 @@ minetest.register_chatcommand("clearmobs",{ if num then range = num end end - local p = minetest.get_player_by_name(n) + local p = minetest.get_player_by_name(player) for _,o in pairs(minetest.luaentities) do if o and o.is_mob then