Added /clear group:<groupname> support

This commit is contained in:
ThePython 2024-05-12 16:51:51 -07:00
parent e122cf0e17
commit c31cdb0deb
5 changed files with 30 additions and 20 deletions

View File

@ -35,8 +35,10 @@ function better_commands.register_on_update(func)
table.insert(better_commands.registered_on_update, func) table.insert(better_commands.registered_on_update, func)
end end
better_commands.paused = false
local timer = 0 local timer = 0
minetest.register_globalstep(function(dtime) minetest.register_globalstep(function(dtime)
if better_commands.paused then return end
timer = timer + dtime timer = timer + dtime
if timer > better_commands.settings.save_interval then if timer > better_commands.settings.save_interval then
timer = 0 timer = 0

View File

@ -22,7 +22,7 @@ function better_commands.parse_params(str)
-- items/entities with extra data -- items/entities with extra data
i = 1 i = 1
repeat 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)} tmp = {str:find("%s([_%w]*:?[_%w]+)%s*(%[.-%])%s*(%d*)%s*(%d*)", i)}
if tmp[1] then if tmp[1] then
tmp[1] = tmp[1] + 1 -- ignore the space tmp[1] = tmp[1] + 1 -- ignore the space
@ -53,6 +53,7 @@ function better_commands.parse_params(str)
-- items/entities without extra data -- items/entities without extra data
i = 1 i = 1
repeat repeat
-- modname:id count wear (everything but id optional)
tmp = {str:find("(%s[_%w]*:?[_%w]+)%s*(%d*)%s*(%d*)", i)} tmp = {str:find("(%s[_%w]*:?[_%w]+)%s*(%d*)%s*(%d*)", i)}
if tmp[1] then if tmp[1] then
tmp[1] = tmp[1] + 1 -- ignore the space tmp[1] = tmp[1] + 1 -- ignore the space
@ -498,7 +499,6 @@ end
---@return string? err ---@return string? err
---@nodiscard ---@nodiscard
function better_commands.parse_item(item_data, ignore_count) function better_commands.parse_item(item_data, ignore_count)
minetest.log(dump(item_data))
if not better_commands.handle_alias(item_data[3]) then if not better_commands.handle_alias(item_data[3]) then
return nil, S("Invalid item: @1", item_data[3]) return nil, S("Invalid item: @1", item_data[3])
end end

View File

@ -14,7 +14,7 @@ function better_commands.save(key)
end end
better_commands.load("teams", {teams = {}}) 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.load("spawnpoints", {})
better_commands.register_on_update(function () better_commands.register_on_update(function ()

View File

@ -31,3 +31,5 @@ better_commands.register_command("?", table.copy(minetest.registered_chatcommand
better_commands.register_command("dumpscore",{func=function()minetest.log(dump(better_commands.scoreboard)) return true, nil, 1 end}) 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}) better_commands.register_command("dumpteam",{func=function()minetest.log(dump(better_commands.teams)) return true, nil, 1 end})
-- ]] -- ]]
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}})

View File

@ -16,18 +16,23 @@ better_commands.register_command("clear", {
targets, err = better_commands.parse_selector(selector, context) targets, err = better_commands.parse_selector(selector, context)
if err or not targets then return false, err, 0 end if err or not targets then return false, err, 0 end
end end
local filter local filter, group
if split_param[2] and split_param[2][3] == "*" then if split_param[2] then
filter = "*"
elseif split_param[2] then
if split_param[2][5] then if split_param[2][5] then
split_param[3] = split_param[2][5] split_param[3] = split_param[2][5]
end end
split_param[2][5] = nil split_param[2][5] = nil
split_param[2][6] = nil split_param[2][6] = nil
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) filter, err = better_commands.parse_item(split_param[2], true)
if err or not filter then return false, err, 0 end if err or not filter then return false, err, 0 end
minetest.log(dump(filter:get_name())) end
end end
local remove_max = tonumber(split_param[3] and split_param[3][3]) local remove_max = tonumber(split_param[3] and split_param[3][3])
if split_param[3] and not remove_max then if split_param[3] and not remove_max then
@ -42,30 +47,28 @@ better_commands.register_command("clear", {
local last local last
local count = 0 local count = 0
local match_total = 0 local match_total = 0
minetest.log(dump(split_param))
for _, target in ipairs(targets) do for _, target in ipairs(targets) do
if target.is_player and target:is_player() then if target.is_player and target:is_player() then
local found = false
local match_count = 0 local match_count = 0
local inv = target:get_inventory() local inv = target:get_inventory()
for _, list in ipairs(better_commands.settings.clear_lists) do for _, list in ipairs(better_commands.settings.clear_lists) do
if inv:get_list(list) then if inv:get_list(list) then
if all and remove_max == -1 then if all and remove_max == -1 then
inv:set_list(list, {}) inv:set_list(list, {})
found = true
elseif remove_max == 0 then elseif remove_max == 0 then
for _, stack in ipairs(inv:get_list(list)) do for _, stack in ipairs(inv:get_list(list)) do
if all then if all then
found = true
match_count = match_count + stack:get_count() 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 elseif split_param[2].extra_data then
if stack:peek_item(1):equals(filter) then if stack:peek_item(1):equals(filter) then
found = true
match_count = match_count + stack:get_count() match_count = match_count + stack:get_count()
end end
---@diagnostic disable-next-line: param-type-mismatch ---@diagnostic disable-next-line: param-type-mismatch
elseif stack:get_name() == filter:get_name() then elseif stack:get_name() == filter:get_name() then
found = true
match_count = match_count + stack:get_count() match_count = match_count + stack:get_count()
end end
end end
@ -74,6 +77,10 @@ better_commands.register_command("clear", {
local matches = false local matches = false
if all then if all then
matches = true 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 elseif split_param[2].extra_data then
if stack:peek_item(1):equals(filter) then if stack:peek_item(1):equals(filter) then
matches = true matches = true
@ -83,7 +90,6 @@ better_commands.register_command("clear", {
matches = true matches = true
end end
if matches then if matches then
found = true
local count = stack:get_count() local count = stack:get_count()
local to_remove = count local to_remove = count
if remove_max > 0 then if remove_max > 0 then
@ -111,10 +117,10 @@ better_commands.register_command("clear", {
break break
end end
end end
end
end
if match_count > 0 or (all and remove_max == -1) then
match_total = match_total + match_count match_total = match_total + match_count
end
end
if found then
count = count + 1 count = count + 1
last = better_commands.get_entity_name(target) last = better_commands.get_entity_name(target)
end end