From c31cdb0deb4070bcc1e6b6e77e857e01c4464a59 Mon Sep 17 00:00:00 2001 From: ThePython <87204246+ThePython10110@users.noreply.github.com> Date: Sun, 12 May 2024 16:51:51 -0700 Subject: [PATCH] Added /clear group: support --- API/API.lua | 2 ++ API/parsing.lua | 4 ++-- API/storage.lua | 2 +- COMMANDS/COMMANDS.lua | 4 +++- COMMANDS/clear.lua | 38 ++++++++++++++++++++++---------------- 5 files changed, 30 insertions(+), 20 deletions(-) diff --git a/API/API.lua b/API/API.lua index 65f0507..d1b5646 100644 --- a/API/API.lua +++ b/API/API.lua @@ -35,8 +35,10 @@ function better_commands.register_on_update(func) table.insert(better_commands.registered_on_update, func) end +better_commands.paused = false local timer = 0 minetest.register_globalstep(function(dtime) + if better_commands.paused then return end timer = timer + dtime if timer > better_commands.settings.save_interval then timer = 0 diff --git a/API/parsing.lua b/API/parsing.lua index 0e6b411..0bbfaed 100644 --- a/API/parsing.lua +++ b/API/parsing.lua @@ -22,7 +22,7 @@ function better_commands.parse_params(str) -- items/entities with extra data i = 1 repeat - -- modname:id[data] (modname optional) + -- modname:id[data] count wear (everything but id and data optional) tmp = {str:find("%s([_%w]*:?[_%w]+)%s*(%[.-%])%s*(%d*)%s*(%d*)", i)} if tmp[1] then tmp[1] = tmp[1] + 1 -- ignore the space @@ -53,6 +53,7 @@ function better_commands.parse_params(str) -- items/entities without extra data i = 1 repeat + -- modname:id count wear (everything but id optional) tmp = {str:find("(%s[_%w]*:?[_%w]+)%s*(%d*)%s*(%d*)", i)} if tmp[1] then tmp[1] = tmp[1] + 1 -- ignore the space @@ -498,7 +499,6 @@ end ---@return string? err ---@nodiscard function better_commands.parse_item(item_data, ignore_count) - minetest.log(dump(item_data)) if not better_commands.handle_alias(item_data[3]) then return nil, S("Invalid item: @1", item_data[3]) end diff --git a/API/storage.lua b/API/storage.lua index fa3876c..c2a6603 100644 --- a/API/storage.lua +++ b/API/storage.lua @@ -14,7 +14,7 @@ function better_commands.save(key) end better_commands.load("teams", {teams = {}}) -better_commands.load("scoreboard", {objectives = {}, players = {}, displays = {}}) +better_commands.load("scoreboard", {objectives = {}, players = {}, displays = {colors = {}}}) better_commands.load("spawnpoints", {}) better_commands.register_on_update(function () diff --git a/COMMANDS/COMMANDS.lua b/COMMANDS/COMMANDS.lua index 35cfcea..aa9a8fd 100644 --- a/COMMANDS/COMMANDS.lua +++ b/COMMANDS/COMMANDS.lua @@ -30,4 +30,6 @@ better_commands.register_command("?", table.copy(minetest.registered_chatcommand -- Temporary commands for testing better_commands.register_command("dumpscore",{func=function()minetest.log(dump(better_commands.scoreboard)) return true, nil, 1 end}) better_commands.register_command("dumpteam",{func=function()minetest.log(dump(better_commands.teams)) return true, nil, 1 end}) --- ]] \ No newline at end of file +-- ]] +better_commands.register_command("pause", {func=function()better_commands.paused=true return true, nil, 1 end,privs={server=true}}) +better_commands.register_command("resume", {func=function()better_commands.paused=false return true, nil, 1 end,privs={server=true}}) \ No newline at end of file diff --git a/COMMANDS/clear.lua b/COMMANDS/clear.lua index 53839ef..b3e2253 100644 --- a/COMMANDS/clear.lua +++ b/COMMANDS/clear.lua @@ -16,18 +16,23 @@ better_commands.register_command("clear", { targets, err = better_commands.parse_selector(selector, context) if err or not targets then return false, err, 0 end end - local filter - if split_param[2] and split_param[2][3] == "*" then - filter = "*" - elseif split_param[2] then + local filter, group + if split_param[2] then if split_param[2][5] then split_param[3] = split_param[2][5] end split_param[2][5] = nil split_param[2][6] = nil - filter, err = better_commands.parse_item(split_param[2], true) - if err or not filter then return false, err, 0 end - minetest.log(dump(filter:get_name())) + + if split_param[2][3] == "*" then + filter = "*" + elseif split_param[2][3]:sub(1,6) == "group:" then + group = true + filter = split_param[2][3]:sub(7, -1) + else + filter, err = better_commands.parse_item(split_param[2], true) + if err or not filter then return false, err, 0 end + end end local remove_max = tonumber(split_param[3] and split_param[3][3]) if split_param[3] and not remove_max then @@ -42,30 +47,28 @@ better_commands.register_command("clear", { local last local count = 0 local match_total = 0 - minetest.log(dump(split_param)) for _, target in ipairs(targets) do if target.is_player and target:is_player() then - local found = false local match_count = 0 local inv = target:get_inventory() for _, list in ipairs(better_commands.settings.clear_lists) do if inv:get_list(list) then if all and remove_max == -1 then inv:set_list(list, {}) - found = true elseif remove_max == 0 then for _, stack in ipairs(inv:get_list(list)) do if all then - found = true match_count = match_count + stack:get_count() + elseif group then + if minetest.get_item_group(stack:get_name(), filter) then + match_count = match_count + stack:get_count() + end elseif split_param[2].extra_data then if stack:peek_item(1):equals(filter) then - found = true match_count = match_count + stack:get_count() end ---@diagnostic disable-next-line: param-type-mismatch elseif stack:get_name() == filter:get_name() then - found = true match_count = match_count + stack:get_count() end end @@ -74,6 +77,10 @@ better_commands.register_command("clear", { local matches = false if all then matches = true + elseif group then + if minetest.get_item_group(stack:get_name(), filter) then + matches = true + end elseif split_param[2].extra_data then if stack:peek_item(1):equals(filter) then matches = true @@ -83,7 +90,6 @@ better_commands.register_command("clear", { matches = true end if matches then - found = true local count = stack:get_count() local to_remove = count if remove_max > 0 then @@ -111,10 +117,10 @@ better_commands.register_command("clear", { break end end - match_total = match_total + match_count end end - if found then + if match_count > 0 or (all and remove_max == -1) then + match_total = match_total + match_count count = count + 1 last = better_commands.get_entity_name(target) end