forked from ac-minetest/oldplayer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.lua
167 lines (135 loc) · 4.88 KB
/
init.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
-- OLD PLAYER : keep only serious players data on server
--(c) 2015-2016 rnd
oldplayer = {}
-- SETTINGS
oldplayer.requirement = {"default:dirt 1", "default:steel_ingot 1"};
oldplayer.welcome = "*** IMPORTANT *** please have at least 1 dirt and 1 steel ingot in your inventory when leaving to register as serious player. If not, your player data will be deleted.";
-- END OF SETTINGS
oldplayer.players = {};
local worldpath = minetest.get_worldpath();
minetest.register_on_joinplayer(function(player)
local name = player:get_player_name(); if name == nil then return end
-- read player inventory data
local inv = player:get_inventory();
local isoldplayer = inv:get_stack("oldplayer", 1):get_count();
inv:set_size("oldplayer", 2);
local ip = minetest.get_player_ip(name); if not ip then return end
inv:set_stack("oldplayer", 2, ItemStack("IP".. ip)) -- string.gsub(ip,".","_")));
if isoldplayer > 0 then
oldplayer.players[name] = 1
minetest.chat_send_player(name, "#OLDPLAYER: welcome back");
else
local privs = minetest.get_player_privs(name);
if privs.kick then
inv:set_stack("oldplayer", 1, ItemStack("oldplayer"));
minetest.chat_send_player(name, "#OLDPLAYER: welcome moderator. setting as old player.");
oldplayer.players[name] = 1
else
oldplayer.players[name] = 0
local form = "size [6,2] textarea[0,0;6.6,3.5;help;OLDPLAYER WELCOME;".. oldplayer.welcome.."]"
minetest.show_formspec(name, "oldplayer:welcome", form)
-- minetest.chat_send_player(name, oldplayer.welcome);
end
end
end)
minetest.register_on_leaveplayer(function(player, timed_out)
local name = player:get_player_name(); if name == nil then return end
if oldplayer.players[name] == 1 then return end -- already old, do nothing
local delete = false; -- should we delete player?
-- read player inventory data
local inv = player:get_inventory();
-- does player have all the required items in inventory?
for _,item in pairs(oldplayer.requirement) do
if not inv:contains_item("main", item) then
delete = true
end
end
if not delete then -- set up oldplayer inventory so we know player is old next time
inv:set_size("oldplayer", 2);
inv:set_stack("oldplayer", 1, ItemStack("oldplayer"));
else -- delete player profile
local filename = worldpath .. "\\players\\" .. name;
-- PROBLEM: deleting doesnt always work? seems minetest itself is saving stuff.
-- so we wait a little and then delete
minetest.after(10,function()
print("[oldplayer] removing player filename " .. filename)
local err,msg = os.remove(filename)
if err==nil then
print ("[oldplayer] error removing player data " .. filename .. " error message: " .. msg)
end
-- TO DO: how to remove players from auth.txt easily without editing file manually like below
end);
end
end
)
-- delete file if not old player
local function remove_non_old_player_file(name)
local filename = worldpath.."\\players\\"..name;
local f=io.open(filename,"r")
local s = f:read("*all"); f:close();
if string.find(s,"Item oldplayer") then return false else os.remove(filename) return true end
end
-- deletes data with no corresponding playerfiles from auth.txt and creates auth_new.txt
local function player_file_exists(name)
local f=io.open(worldpath.."\\players\\"..name,"r")
if f~=nil then io.close(f) return true else return false end
end
local function remove_missing_players_from_auth()
local playerfilelist = minetest.get_dir_list(worldpath.."\\players", false);
local f = io.open(worldpath.."\\auth.txt", "r");
if not f then return end
local s = f:read("*a");f:close();
local p1,p2;
f = io.open(worldpath.."\\auth_new.txt", "w");
if not f then return end
local playerlist = {};
for _,name in ipairs(playerfilelist) do
playerlist[name]=true;
end
local i=0;
local j=0; local k=0;
local name;
local count = 0;
-- parse through auth and remove missing players data
while j do
j=string.find(s,":",i);
if j then
if i ~= 1 then
name = string.sub(s,i+1,j-1)
else
name = string.sub(s,1,j-1)
end
if j then
k=string.find(s,"\n",i+1);
if not k then
j = nil
if playerlist[name] then
f:write(string.sub(s,i+1))
else
count = count+1
end
else
if playerlist[name] then
f:write(string.sub(s,i+1,k))
else
count = count + 1
end
i=k;
end
end
end
end
f:close();
print("#OLD PLAYER : removed " .. count .. " entries from auth.txt. Replace auth.txt with auth_new.txt");
end
local function remove_non_old_player_files()
local playerfilelist = minetest.get_dir_list(worldpath.."\\players", false);
local count = 0;
for _,name in ipairs(playerfilelist) do
if remove_non_old_player_file(name) then
count = count + 1
end
end
print("#OLD PLAYER: removed " .. count .. " non oldplayer player files");
end
minetest.register_on_shutdown(function() remove_non_old_player_files();remove_missing_players_from_auth() end)