Do not use players_in_nether as list but hashmap

Also, comment broken function
This commit is contained in:
HybridDog 2018-08-15 17:31:15 +02:00
parent 2f3522598c
commit 4a7dd247e7
1 changed files with 46 additions and 53 deletions

View File

@ -11,35 +11,34 @@ minetest.after(5, function()
abm_allowed = true abm_allowed = true
end) end)
table.icontains = table.icontains or function(t, v) local save_path = minetest.get_worldpath() .. "/nether_players"
for _,i in ipairs(t) do
if i == v then
return true
end
end
return false
end
local players_in_nether = {} local players_in_nether = {}
-- only get info from file if nether prisons -- only get info from file if nether prisons
if nether_prisons then if nether_prisons then
local file = io.open(minetest.get_worldpath()..'/nether_players', "r") local file = io.open(save_path, "r")
if file then if not file then
local contents = file:read('*all') return
io.close(file) end
if contents then local contents = file:read"*all"
players_in_nether = string.split(contents, " ") io.close(file)
end if not contents then
return
end
local playernames = string.split(contents, " ")
for i = 1,#playernames do
players_in_nether[playernames[i]] = true
end end
end end
local function save_nether_players() local function save_nether_players()
local output = '' local playernames,n = {},1
for _,name in ipairs(players_in_nether) do for name in pairs(players_in_nether) do
output = output..name..' ' playernames[n] = name
n = n+1
end end
local f = io.open(minetest.get_worldpath()..'/nether_players', "w") local f = io.open(save_path, "w")
f:write(output) assert(f, "Could not open nether_players file for writing.")
f:write(table.concat(playernames, " "))
io.close(f) io.close(f)
end end
@ -100,10 +99,10 @@ end
-- teleports players to nether or helps it -- teleports players to nether or helps it
local function player_to_nether(player, safe) local function player_to_nether(player, safe)
local pname = player:get_player_name() local pname = player:get_player_name()
if table.icontains(players_in_nether, pname) then if players_in_nether[pname] then
return return
end end
players_in_nether[#players_in_nether+1] = pname players_in_nether[pname] = true
save_nether_players() save_nether_players()
if not safe then if not safe then
minetest.chat_send_player(pname, "For any reason you arrived here. " .. minetest.chat_send_player(pname, "For any reason you arrived here. " ..
@ -118,14 +117,8 @@ end
local function player_from_nether(player) local function player_from_nether(player)
local pname = player:get_player_name() local pname = player:get_player_name()
local changes if players_in_nether[pname] then
for n,i in ipairs(players_in_nether) do players_in_nether[pname] = nil
if i == pname then
table.remove(players_in_nether, n)
changes = true
end
end
if changes then
save_nether_players() save_nether_players()
end end
update_background(player) update_background(player)
@ -133,8 +126,9 @@ end
local function player_exists(name) local function player_exists(name)
for _,player in pairs(minetest.get_connected_players()) do local players = minetest.get_connected_players()
if player:get_player_name() == name then for i = 1,#players do
if players[i]:get_player_name() == name then
return true return true
end end
end end
@ -191,7 +185,7 @@ if nether_prisons then
-- randomly set player position when he/she dies in nether -- randomly set player position when he/she dies in nether
minetest.register_on_respawnplayer(function(player) minetest.register_on_respawnplayer(function(player)
local pname = player:get_player_name() local pname = player:get_player_name()
if not table.icontains(players_in_nether, pname) then if not players_in_nether[pname] then
return return
end end
local target = get_player_died_target(player) local target = get_player_died_target(player)
@ -211,7 +205,7 @@ if nether_prisons then
for _,player in pairs(minetest.get_connected_players()) do for _,player in pairs(minetest.get_connected_players()) do
local pname = player:get_player_name() local pname = player:get_player_name()
local ppos = player:getpos() local ppos = player:getpos()
if table.icontains(players_in_nether, pname) then if players_in_nether[pname] then
if ppos.y > nether.start then if ppos.y > nether.start then
player:moveto({x=ppos.x, y=portal_target, z=ppos.z}) player:moveto({x=ppos.x, y=portal_target, z=ppos.z})
update_background(player, true) update_background(player, true)
@ -248,22 +242,21 @@ if nether_prisons then
end) end)
else else
-- test if player is in nether when he/she joins -- test if player is in nether when he/she joins
minetest.register_on_joinplayer(function(player) --~ minetest.register_on_joinplayer(function(player)
minetest.after(0, function(player) --~ print(dump(player:get_pos()))
if player:getpos().y < nether.start then --~ minetest.after(0, function(player)
if not table.icontains(players_in_nether, pname) then --~ local pname = player:get_player_name()
players_in_nether[#players_in_nether+1] = pname --~ if player:getpos().y < nether.start then
end --~ if not players_in_nether[pname] then
return --~ players_in_nether[pname] = true
end --~ end
for i,name in pairs(players_in_nether) do --~ return
if name == pname then --~ end
players_in_nether[i] = nil --~ if players_in_nether[pname] then
return --~ players_in_nether[pname] = nil
end --~ end
end --~ end, player)
end, player) --~ end)
end)
end end
-- removes the violet stuff from the obsidian portal -- removes the violet stuff from the obsidian portal
@ -298,7 +291,7 @@ local particledef = {
-- teleports player to neter (obsidian portal) -- teleports player to neter (obsidian portal)
local function obsi_teleport_player(player, pos, target) local function obsi_teleport_player(player, pos, target)
local pname = player:get_player_name() local pname = player:get_player_name()
if table.icontains(players_in_nether, pname) then if players_in_nether[pname] then
return return
end end
@ -313,7 +306,7 @@ local function obsi_teleport_player(player, pos, target)
return return
end end
players_in_nether[#players_in_nether+1] = pname players_in_nether[pname] = true
save_nether_players() save_nether_players()
update_background(player, true) update_background(player, true)