From 8fdb82fd9413a0280d4aac4caaaadcae9308946a Mon Sep 17 00:00:00 2001 From: IamPyu Date: Sun, 8 Dec 2024 16:13:47 -0600 Subject: [PATCH] More changes --- mods/CORE/pyutest/util.lua | 66 ------------------------ mods/ENTITIES/pyutest_fireworks/init.lua | 2 +- mods/ITEMS/pyutest_electricity/mod.conf | 2 +- mods/PLAYER/pyutest_cmds/api.lua | 66 ++++++++++++++++++++++++ mods/PLAYER/pyutest_cmds/init.lua | 29 ++++++++++- 5 files changed, 95 insertions(+), 70 deletions(-) create mode 100644 mods/PLAYER/pyutest_cmds/api.lua diff --git a/mods/CORE/pyutest/util.lua b/mods/CORE/pyutest/util.lua index 2001cee..726d402 100644 --- a/mods/CORE/pyutest/util.lua +++ b/mods/CORE/pyutest/util.lua @@ -317,69 +317,3 @@ PyuTest.drop_item = function(pos, name) o:add_velocity(vector.new(math.random(-2, 2), 5, math.random(-2, 2))) end end - -PyuTest.chatcommand_entity_selector = function(caller, param) - local p_caller = core.get_player_by_name(caller) - - if core.get_player_by_name(param) ~= nil then - return {param} - end - - if param == "@self" or param == "@s" then - return {caller} - elseif param == "@random" or param == "@r" then - local players = core.get_connected_players() - return {players[math.random(#players)]:get_player_name()} - elseif param == "@nearest" or param == "@n" then - local players = core.get_connected_players() - local pos = p_caller:get_pos() - local cd = math.huge - local nearest - - for _, v in pairs(players) do - local dist = vector.distance(pos, v:get_pos()) - - if dist < cd and v ~= p_caller then - cd = dist - nearest = v - end - end - - if nearest then - return {nearest:get_player_name()} - end - elseif param == "@all" or param == "@a" then - local list = {} - - for _, v in pairs(core.get_connected_players()) do - list[#list+1] = v:get_player_name() - end - - return list - end - - return {caller} -end - -PyuTest.execute_as = function(runner, command) - local pos = command:find(" ") - local cmd, param = command, "" - if pos then - cmd = command:sub(1, pos - 1) - param = command:sub(pos + 1) - end - local cmddef = core.chatcommands[cmd] - if not cmddef then - core.chat_send_player(runner, "The command "..cmd.." does not exist") - return - end - local has_privs, missing_privs = core.check_player_privs(runner, cmddef.privs) - if not has_privs then - core.chat_send_player(runner, "You don't have permission " - .."to run "..cmd - .." (missing privileges: " - ..table.concat(missing_privs, ", ")..")") - return - end - cmddef.func(runner, param) -end diff --git a/mods/ENTITIES/pyutest_fireworks/init.lua b/mods/ENTITIES/pyutest_fireworks/init.lua index 76ac2a6..921de14 100644 --- a/mods/ENTITIES/pyutest_fireworks/init.lua +++ b/mods/ENTITIES/pyutest_fireworks/init.lua @@ -64,7 +64,7 @@ core.register_entity("pyutest_fireworks:firework", { local pos = self.object:get_pos() - for v in pairs(core.objects_inside_radius(pos, 3)) do + for v in core.objects_inside_radius(pos, 3) do if v ~= self.object then PyuTest.deal_damage(v, 9, PyuTest.DAMAGE_TYPES.fireworks()) end diff --git a/mods/ITEMS/pyutest_electricity/mod.conf b/mods/ITEMS/pyutest_electricity/mod.conf index c43aded..f103bb4 100644 --- a/mods/ITEMS/pyutest_electricity/mod.conf +++ b/mods/ITEMS/pyutest_electricity/mod.conf @@ -1 +1 @@ -depends = pyutest_blocks \ No newline at end of file +depends = pyutest_blocks,pyutest_cmds \ No newline at end of file diff --git a/mods/PLAYER/pyutest_cmds/api.lua b/mods/PLAYER/pyutest_cmds/api.lua new file mode 100644 index 0000000..21eef31 --- /dev/null +++ b/mods/PLAYER/pyutest_cmds/api.lua @@ -0,0 +1,66 @@ +PyuTest.chatcommand_entity_selector = function(caller, param) + local p_caller = core.get_player_by_name(caller) + + if core.get_player_by_name(param) ~= nil then + return {param} + end + + if param == "@self" or param == "@s" then + return {caller} + elseif param == "@random" or param == "@r" then + local players = core.get_connected_players() + return {players[math.random(#players)]:get_player_name()} + elseif param == "@nearest" or param == "@n" then + local players = core.get_connected_players() + local pos = p_caller:get_pos() + local cd = math.huge + local nearest + + for _, v in pairs(players) do + local dist = vector.distance(pos, v:get_pos()) + + if dist < cd and v ~= p_caller then + cd = dist + nearest = v + end + end + + if nearest then + return {nearest:get_player_name()} + end + elseif param == "@all" or param == "@a" then + local list = {} + + for _, v in pairs(core.get_connected_players()) do + list[#list+1] = v:get_player_name() + end + + return list + end + + return {caller} +end + +PyuTest.execute_as = function(runner, command) + local pos = command:find(" ") + local cmd, param = command, "" + if pos then + cmd = command:sub(1, pos - 1) + param = command:sub(pos + 1) + end + local cmddef = core.chatcommands[cmd] + if not cmddef then + core.chat_send_player(runner, "The command "..cmd.." does not exist") + return + end + local has_privs, missing_privs = core.check_player_privs(runner, cmddef.privs) + if not has_privs then + core.chat_send_player(runner, "You don't have permission " + .."to run "..cmd + .." (missing privileges: " + ..table.concat(missing_privs, ", ")..")") + return + end + + return cmddef.func(runner, param) +end diff --git a/mods/PLAYER/pyutest_cmds/init.lua b/mods/PLAYER/pyutest_cmds/init.lua index 90da7a5..2bfec1b 100644 --- a/mods/PLAYER/pyutest_cmds/init.lua +++ b/mods/PLAYER/pyutest_cmds/init.lua @@ -1,19 +1,44 @@ local modpath = core.get_modpath("pyutest_cmds") +dofile(modpath .. "/api.lua") + core.register_chatcommand("say", { params = "", description = [[Say There are some text replacement macros you can use. Here is a list of them: -- %s: Replaced with your username]], +- %n: Replaced with your username +- %r: Replaced with some random player's name]], func = function(name, param) local str = param - str = str:gsub("%%s", name) + str = str:gsub("%%n", name) + str = str:gsub("%%r", function () + return PyuTest.chatcommand_entity_selector(name, "@r")[1] + end) + str = str:gsub("$embed{([%w%p]+)}", function(input) + return PyuTest.execute_as(name, input) or "" + end) core.chat_send_all(str) end }) +core.register_chatcommand("getpos", { + params = "", + description = "Return the position of ", + func = function(name, param) + local target = PyuTest.chatcommand_entity_selector(name, param) + local player = core.get_player_by_name(target[1]) + + if not player then + return "" + end + local pos = player:get_pos() + + return string.format("%f %f %f", pos.x, pos.y, pos.z) + end +}) + core.register_chatcommand("kill", { params = "", description = "Kill ",