Skip to content

Commit

Permalink
make kill command support an optional radius param
Browse files Browse the repository at this point in the history
  • Loading branch information
wsor4035 committed Aug 3, 2024
1 parent fc018f2 commit 4be29c3
Showing 1 changed file with 25 additions and 9 deletions.
34 changes: 25 additions & 9 deletions mods/fl_wildlife/commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,39 +22,55 @@ minetest.register_chatcommand("clearents", {

--override this core command to allow killing mobs
minetest.override_chatcommand("kill", {
params = "<playername|mobname>",
params = "<playername|mobname> [<radius>]",
description = "Kill entity(mob), player, or yourself",
func = function(name, param)
local player = minetest.get_player_by_name(param)
local split_param = param:split(" ")
local target = split_param[1]
local radius = split_param[2] and tonumber(split_param[2]) or nil
local player = minetest.get_player_by_name(target)

if player then
if minetest.settings:get_bool("enable_damage") == false then
return false, "You can not kill players as damage is disabled"
end

if player:get_hp() <= 0 then
return false, "Target player " .. param .. " is already dead"
return false, "Target player " .. target .. " is already dead"
end

player:set_hp(0)
return true, "Player " .. param .. " killed"
return true, "Player " .. target .. " killed"
end

local entity = minetest.registered_entities[param]
local entity = minetest.registered_entities[target]

if entity then
local removed_ent_count = 0
local command_player = minetest.get_player_by_name(name)

if radius and not command_player then
return false, "Only in game players can use the radius parameter"
end

--command player pos
--set this after the check for command player
local cp_pos = command_player:get_pos()

for _, ent in pairs(minetest.luaentities) do
if ent.name == param then
if ent.name == target then
--maybe consider adding a check to see if the entity is a mob
--and setting its hp to 0 so it can run its death callback
ent.object:remove()
removed_ent_count = removed_ent_count + 1

if (radius and vector.distance(ent.object:get_pos(), cp_pos) <= radius) or not radius then
ent.object:remove()
removed_ent_count = removed_ent_count + 1
end

end
end

return true, "Removed " .. removed_ent_count .. " entities of type " .. param
return true, "Removed " .. removed_ent_count .. " entities of type " .. target
end

return false, "Invalid entity name or player name"
Expand Down

0 comments on commit 4be29c3

Please sign in to comment.