diff --git a/claim_events.lua b/claim_events.lua index c225af7..3249582 100644 --- a/claim_events.lua +++ b/claim_events.lua @@ -52,7 +52,7 @@ end --! @brief claim a parcel, update power and update global parcels table function factions.unclaim_parcel(name, parcelpos) - factions.parcels.remove(parcelpos) + factions.remove_key(factions.parcels, parcelpos, nil, "faction", true) local faction = factions.factions.get(name) diff --git a/databases.lua b/databases.lua index e40b478..7e2bbb8 100644 --- a/databases.lua +++ b/databases.lua @@ -99,7 +99,7 @@ end -- Create a empty player table. function factions.create_player_table() local table = { - faction = "" + faction = "", } return factions.on_create_player_table(table) end @@ -111,3 +111,30 @@ function factions.create_claim_table() } return factions.on_create_claim_table(table) end + +-- helper functions +function factions.db_is_empty(table) + for k, v in pairs(table) do + return false + end + return true +end + +function factions.remove_key(db, db_name, db_data, key, write) + if not db_data then + db_data = db.get(db_name) + end + + db_data[key] = nil + + if factions.db_is_empty(db_data) then + db.remove(db_name) + return nil + end + + if write then + db.set(db_name, db_data) + end + + return db_data +end diff --git a/factions.lua b/factions.lua index 0542277..a196038 100644 --- a/factions.lua +++ b/factions.lua @@ -220,7 +220,7 @@ function factions.remove_player(name, player) factions.factions.set(name, faction) - factions.players.remove(player) + factions.remove_key(factions.players, player, nil, "faction", true) factions.on_player_leave(name, player) if factions_config.enable_power_per_player then @@ -281,11 +281,11 @@ function factions.disband(name, reason) end for k, _ in pairs(faction.players) do -- remove players affiliation - factions.players.remove(k) + factions.remove_key(factions.players, k, nil, "faction", true) end for k, v in pairs(faction.land) do -- remove parcel claims - factions.parcels.remove(k) + factions.remove_key(factions.parcels, k, nil, "faction", true) end factions.on_disband(name, reason)