mirror of
https://github.com/mt-mods/playerfactions.git
synced 2024-11-29 02:33:47 +01:00
Temporarily fixed bug with offline players
Player storage isn't available when players are offline, so get_player_faction wouldn't work. Fixed by adding a secondary data structure, which will be removed once a mechanism for offline player storage has been added to the engine
This commit is contained in:
parent
807cb6400e
commit
c255afbbf0
29
init.lua
29
init.lua
@ -12,6 +12,13 @@ if storage:get_string("facts") ~= "" then
|
|||||||
facts = minetest.deserialize(storage:get_string("facts"))
|
facts = minetest.deserialize(storage:get_string("facts"))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- Fix factions
|
||||||
|
for fname, fact in pairs(facts) do
|
||||||
|
if fact.members == nil then
|
||||||
|
fact.members = {}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
local function save_factions()
|
local function save_factions()
|
||||||
storage:set_string("facts", minetest.serialize(facts))
|
storage:set_string("facts", minetest.serialize(facts))
|
||||||
end
|
end
|
||||||
@ -19,8 +26,18 @@ end
|
|||||||
-- Data manipulation
|
-- Data manipulation
|
||||||
function factions.get_player_faction(name)
|
function factions.get_player_faction(name)
|
||||||
local player = minetest.get_player_by_name(name)
|
local player = minetest.get_player_by_name(name)
|
||||||
|
|
||||||
|
-- Look in faction objects if we can't do the shortcut
|
||||||
if player == nil then
|
if player == nil then
|
||||||
|
for fname, fact in pairs(facts) do
|
||||||
|
if fact.members[name] then
|
||||||
|
return fname
|
||||||
|
end
|
||||||
|
end
|
||||||
return nil
|
return nil
|
||||||
|
|
||||||
|
-- Player is online, we can take a shortcut using the player object
|
||||||
|
-- In the future, we can always do this; see https://github.com/minetest/minetest/issues/6193
|
||||||
else
|
else
|
||||||
local faction = player:get_meta():get_string("faction")
|
local faction = player:get_meta():get_string("faction")
|
||||||
if faction == "" then
|
if faction == "" then
|
||||||
@ -31,6 +48,11 @@ function factions.get_player_faction(name)
|
|||||||
factions.leave_faction(name)
|
factions.leave_faction(name)
|
||||||
return nil
|
return nil
|
||||||
else
|
else
|
||||||
|
local found = false
|
||||||
|
if facts[faction].members[name] == nil then
|
||||||
|
facts[faction].members[name] = true
|
||||||
|
save_factions()
|
||||||
|
end
|
||||||
return faction
|
return faction
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -53,7 +75,8 @@ function factions.register_faction(fname, founder, pw)
|
|||||||
facts[fname] = {
|
facts[fname] = {
|
||||||
name = fname,
|
name = fname,
|
||||||
owner = founder,
|
owner = founder,
|
||||||
password = pw
|
password = pw,
|
||||||
|
members = {}
|
||||||
}
|
}
|
||||||
save_factions()
|
save_factions()
|
||||||
factions.join_faction(fname, founder)
|
factions.join_faction(fname, founder)
|
||||||
@ -74,10 +97,14 @@ end
|
|||||||
|
|
||||||
function factions.join_faction(name, player)
|
function factions.join_faction(name, player)
|
||||||
minetest.get_player_by_name(player):get_meta():set_string("faction", name)
|
minetest.get_player_by_name(player):get_meta():set_string("faction", name)
|
||||||
|
facts[name].members[player] = true
|
||||||
end
|
end
|
||||||
|
|
||||||
function factions.leave_faction(name)
|
function factions.leave_faction(name)
|
||||||
minetest.get_player_by_name(name):get_meta():set_string("faction", "")
|
minetest.get_player_by_name(name):get_meta():set_string("faction", "")
|
||||||
|
if facts[name] ~= nil then
|
||||||
|
facts[name].members[player] = nil
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Chat commands
|
-- Chat commands
|
||||||
|
Loading…
Reference in New Issue
Block a user