From c65bde260eebc3fdfe672bfda5bf69f3489e927d Mon Sep 17 00:00:00 2001 From: Coder12a <38924418+Coder12a@users.noreply.github.com> Date: Tue, 22 Jan 2019 16:16:56 -0600 Subject: [PATCH] Use: safe_file_write --- factions.lua | 37 ++++++++++++++----------------------- ip.lua | 11 +++-------- misc_mod_data.lua | 7 +------ 3 files changed, 18 insertions(+), 37 deletions(-) diff --git a/factions.lua b/factions.lua index eef0b86..9cfcb28 100644 --- a/factions.lua +++ b/factions.lua @@ -341,6 +341,8 @@ end function factions.Faction.remove_player(self, player) self.players[player] = nil + self.onlineplayers[player] = nil + self.offlineplayers[player] = nil factions.players[player] = nil self:on_player_leave(player) self:check_players_in_faction(self) @@ -368,7 +370,7 @@ function factions.Faction.remove_player(self, player) self.onlineplayers[player] = nil factions.bulk_save() end - +local parcel_size = factions_config.parcel_size --! @param parcelpos position of the wanted parcel --! @return whether this faction can claim a parcelpos function factions.Faction.can_claim_parcel(self, parcelpos) @@ -599,7 +601,7 @@ end function factions.Faction.tp_spawn(self, playername) player = minetest.get_player_by_name(playername) if player then - player:moveto(self.spawn, false) + player:set_pos(self.spawn) end end @@ -926,7 +928,6 @@ function factions.Faction.on_revoke_invite(self, player) minetest.chat_send_player(player, "You are no longer invited to faction "..self.name) end -local parcel_size = factions_config.parcel_size function factions.get_parcel_pos(pos) if factions_config.protection_style == "2d" then return math.floor(pos.x / parcel_size) * parcel_size .. "," .. math.floor(pos.z / parcel_size) * parcel_size @@ -1023,17 +1024,12 @@ function factions.save() --due to figuring out which data to save and which is temporary only --all data is saved here --this implies data needs to be cleant up on load - - local file,error = io.open(factions_worldid .. "/" .. "factions.conf","w") - - if file ~= nil then - file:write(minetest.serialize(factions.factions)) - file:close() - else + saving = false + if not minetest.safe_file_write(factions_worldid .. "/" .. "factions.conf",minetest.serialize(factions.factions)) then minetest.log("error","MOD factions: unable to save factions world specific data!: " .. error) + factions.bulk_save() end factions_ip.save() - saving = false end function factions.bulk_save() @@ -1239,11 +1235,8 @@ function factions.faction_tick() end end -local player_count = 0 - minetest.register_on_joinplayer( function(player) - player_count = player_count + 1 local name = player:get_player_name() minetest.after(5,createHudfactionLand,player) local faction = factions.get_player_faction(name) @@ -1252,7 +1245,7 @@ function(player) minetest.after(5,createHudFactionName,player,faction.name) minetest.after(5,createHudPower,player,faction) faction.offlineplayers[name] = nil - faction.onlineplayers[name] = 1 + faction.onlineplayers[name] = true if faction.no_parcel ~= -1 then local now = os.time() - faction.no_parcel local l = factions_config.maximum_parcelless_faction_time @@ -1282,7 +1275,6 @@ end minetest.register_on_leaveplayer( function(player) - player_count = player_count - 1 local name = player:get_player_name() local faction = factions.get_player_faction(name) local id_name1 = name .. "factionLand" @@ -1298,14 +1290,9 @@ minetest.register_on_leaveplayer( if hud_ids[id_name3] then hud_ids[id_name3] = nil end - faction.offlineplayers[name] = 1 + faction.offlineplayers[name] = true faction.onlineplayers[name] = nil end - if player_count > 0 then - factions.bulk_save() - else - factions.save() - end end ) @@ -1325,7 +1312,11 @@ minetest.register_on_respawnplayer( end ) - +minetest.register_on_shutdown( + function() + factions.save() + saving = false +end) local default_is_protected = minetest.is_protected minetest.is_protected = function(pos, player) diff --git a/ip.lua b/ip.lua index 29dd8bf..d50893a 100644 --- a/ip.lua +++ b/ip.lua @@ -5,14 +5,9 @@ factions_ip.player_ips = {} local factions_worldid = minetest.get_worldpath() function factions_ip.save() - local file,error = io.open(factions_worldid .. "/" .. "factions_iplist.txt","w") - - if file ~= nil then - file:write(minetest.serialize(factions_ip.player_ips)) - file:close() - else - minetest.log("error","MOD factions: unable to save faction player ips!: " .. error) - end + if not minetest.safe_file_write(factions_worldid .. "/" .. "factions_iplist.txt", minetest.serialize(factions_ip.player_ips)) then + minetest.log("error","MOD factions: unable to save faction player ips!: " .. error) + end end function factions_ip.load() diff --git a/misc_mod_data.lua b/misc_mod_data.lua index 8c959e2..045b6ba 100644 --- a/misc_mod_data.lua +++ b/misc_mod_data.lua @@ -5,12 +5,7 @@ misc_mod_data.data = {factions_version = "0.8.1",config = factions_config} local factions_worldid = minetest.get_worldpath() function misc_mod_data.save() - local file,error = io.open(factions_worldid .. "/" .. "factions_misc_mod_data.txt","w") - - if file ~= nil then - file:write(minetest.serialize(misc_mod_data.data)) - file:close() - else + if not minetest.safe_file_write(factions_worldid .. "/" .. "factions_misc_mod_data.txt", minetest.serialize(misc_mod_data.data)) then minetest.log("error","MOD factions: unable to save factions misc mod data!: " .. error) end end