From f93be5818ab9e3366fb580df83222537b86bf062 Mon Sep 17 00:00:00 2001 From: Athozus Date: Sat, 25 Mar 2023 16:02:26 +0100 Subject: [PATCH] Fix removing elements from tables --- storage.lua | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/storage.lua b/storage.lua index a407bd0..f4dcae8 100644 --- a/storage.lua +++ b/storage.lua @@ -351,13 +351,13 @@ end function mail.removePlayersFromMaillist(ml_id) local maillists_players = mail.getPlayersInMaillists() - local updated_players = {} - for _, playerInfo in ipairs(maillists_players) do - if playerInfo.id ~= ml_id then - table.insert(updated_players, playerInfo) + for i=#maillists_players,1,-1 do + local playerInfo = maillists_players[i] + if playerInfo.id == ml_id then + table.remove(maillists_players, i) end end - if mail.write_json_file(mail.maildir .. "/mail.maillists_players.json", updated_players) then + if mail.write_json_file(mail.maildir .. "/mail.maillists_players.json", maillists_players) then return true else minetest.log("error","[mail] Save failed!") @@ -464,9 +464,10 @@ end function mail.deleteContact(owner, name) local contacts = mail.getContacts() - for _, contact in ipairs(contacts) do + for i=#contacts,1,-1 do + local contact = contacts[i] if contact.owner == owner and contact.name == name then - table.remove(contacts, _) + table.remove(contacts, i) end end if mail.write_json_file(mail.maildir .. "/mail.contacts.json", contacts) then