forked from ynong123/password_chest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.lua
More file actions
355 lines (299 loc) · 9.72 KB
/
Copy pathinit.lua
File metadata and controls
355 lines (299 loc) · 9.72 KB
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
-- Mod: Password Chest
-- Created by ynong123
dofile(minetest.get_modpath("password_chest") .. "/account.lua")
local FORMNAME_DATABASE = "password_chest:database"
local FORMNAME_SETUP = "password_chest:setup"
local FORMNAME_UNLOCK = "password_chest:unlock"
local FORMNAME_CHEST = "password_chest:password_chest_formspec"
local FORMNAME_CHANGE_PASSWORD = "password_chest:change_password"
local player_sessions = {}
local function copy_pos(pos)
return {
x = pos.x,
y = pos.y,
z = pos.z
}
end
local function same_pos(a, b)
return a
and b
and a.x == b.x
and a.y == b.y
and a.z == b.z
end
local function set_player_session(player_name, pos, formname, authenticated)
player_sessions[player_name] = {
authenticated = authenticated or false,
formname = formname,
pos = copy_pos(pos)
}
end
local function clear_player_session(player_name)
player_sessions[player_name] = nil
end
local function show_setup_formspec(player_name, pos)
set_player_session(player_name, pos, FORMNAME_SETUP, false)
minetest.show_formspec(
player_name,
FORMNAME_SETUP,
"size[8,4]" ..
"label[0,0;Password Chest Setup]" ..
"pwdfield[0.5,2;7.0,1;password;Password:]" ..
"button[0,3;3.9,1;lock;Lock]" ..
"button_exit[4.1,3;3.9,1;cancel;Cancel]"
)
end
local function show_unlock_formspec(player_name, pos)
set_player_session(player_name, pos, FORMNAME_UNLOCK, false)
minetest.show_formspec(
player_name,
FORMNAME_UNLOCK,
"size[4,4]" ..
"label[0,0;Unlock Chest]" ..
"pwdfield[0.5,2;3.0,1;password;Password:]" ..
"button[0,3;1.9,1;open;Open]" ..
"button_exit[2.1,3;1.9,1;cancel;Cancel]"
)
end
local function password_chest_formspec(pos)
local spos = pos.x .. "," .. pos.y .. "," .. pos.z
local formspec =
"size[8,10]" ..
default.gui_bg ..
default.gui_bg_img ..
default.gui_slots ..
"list[nodemeta:" .. spos .. ";main;0,0.3;8,4;]" ..
"list[current_player;main;0,4.85;8,1;]" ..
"list[current_player;main;0,6.08;8,3;8]" ..
"listring[nodemeta:" .. spos .. ";main]" ..
"listring[current_player;main]" ..
default.get_hotbar_bg(0, 4.85) ..
"button[0,9;8,1;change_password;Change Password]"
return formspec
end
local function show_password_chest_formspec(player_name, pos)
set_player_session(player_name, pos, FORMNAME_CHEST, true)
minetest.show_formspec(player_name, FORMNAME_CHEST, password_chest_formspec(pos))
end
local function show_change_password_formspec(player_name, pos)
set_player_session(player_name, pos, FORMNAME_CHANGE_PASSWORD, true)
minetest.show_formspec(
player_name,
FORMNAME_CHANGE_PASSWORD,
"size[5,8]" ..
"label[0,0;Change Password]" ..
"pwdfield[0.5,2;4.0,1;old_password;Old Password:]" ..
"pwdfield[0.5,4;4.0,1;new_password;New Password:]" ..
"pwdfield[0.5,6;4.0,1;confirm_password;Confirm Password:]" ..
"button[0,7;2,1;save;Save]" ..
"button_exit[3,7;2,1;cancel;Cancel]"
)
end
local function can_access_chest(pos, player)
if not player or not player:is_player() then
return false
end
local session = player_sessions[player:get_player_name()]
local meta = minetest.get_meta(pos)
return session
and session.authenticated
and session.formname == FORMNAME_CHEST
and same_pos(session.pos, pos)
and meta:get_int("id") ~= 0
and meta:get_string("owner") ~= ""
end
minetest.register_chatcommand("password_chest", {
params = "[reset|database]",
description = "Use \"/password_chest reset\" to reset all of the password chests and \"/password_chest database\" to get the password database.",
privs = {
server = true
},
func = function(name, param)
if param == "reset" then
for i = 1, account.counter, 1 do
local meta = minetest.get_meta(account.pos[i])
meta:set_string("infotext", "Password Chest (unconfigured)")
meta:set_string("owner", "")
meta:set_int("id", 0)
end
account.reset()
return true, "Password Chest Database has been reset successfully. All password chests will become unconfigured."
elseif param == "database" then
minetest.show_formspec(name, FORMNAME_DATABASE, account.formspec())
return true, ""
end
return false, "Usage: /password_chest [reset|database]"
end
})
minetest.register_node("password_chest:password_chest", {
description = "Password Chest",
tiles = {
"password_chest_top.png",
"password_chest_top.png",
"password_chest_side.png",
"password_chest_side.png",
"password_chest_side.png",
"password_chest_front.png"
},
groups = {choppy = 2, oddly_breakable_by_hand = 2},
paramtype2 = "facedir",
legacy_facedir_simple = true,
is_ground_content = false,
on_construct = function(pos)
local meta = minetest.get_meta(pos)
meta:set_string("infotext", "Password Chest (unconfigured)")
meta:set_int("id", 0)
meta:set_string("owner", "")
local inv = meta:get_inventory()
inv:set_size("main", 8 * 4)
end,
on_rightclick = function(pos, node, player, itemstack, pointed_thing)
local meta = minetest.get_meta(pos)
local player_name = player:get_player_name()
if meta:get_int("id") == 0 then
show_setup_formspec(player_name, pos)
else
show_unlock_formspec(player_name, pos)
end
end,
can_dig = function(pos, player)
local meta = minetest.get_meta(pos)
local inv = meta:get_inventory()
local owner = meta:get_string("owner")
if not inv:is_empty("main") then
return false
end
if owner == "" then
return true
end
return player and player:get_player_name() == owner
end,
allow_metadata_inventory_move = function(pos, from_list, from_index, to_list, to_index, count, player)
if can_access_chest(pos, player) then
return count
end
return 0
end,
allow_metadata_inventory_put = function(pos, listname, index, stack, player)
if can_access_chest(pos, player) then
return stack:get_count()
end
return 0
end,
allow_metadata_inventory_take = function(pos, listname, index, stack, player)
if can_access_chest(pos, player) then
return stack:get_count()
end
return 0
end,
after_dig_node = function(pos, oldnode, oldmetadata, digger)
account.delete(tonumber(oldmetadata.fields.id))
return true
end
})
minetest.register_on_player_receive_fields(function(player, formname, fields)
local player_name = player:get_player_name()
local session = player_sessions[player_name]
if formname == FORMNAME_DATABASE then
return fields.quit or fields.close
end
if not session or session.formname ~= formname then
return false
end
local pos = session.pos
local meta = minetest.get_meta(pos)
if formname == FORMNAME_SETUP then
if fields.lock then
local password = fields.password or ""
if password == "" then
minetest.chat_send_player(player_name, "Password field is empty.")
return true
end
if meta:get_int("id") ~= 0 then
minetest.chat_send_player(player_name, "This chest has already been configured.")
show_unlock_formspec(player_name, pos)
return true
end
meta:set_string("infotext", "Password Chest (owned by " .. player_name .. ")")
meta:set_int("id", account.new(password, pos))
meta:set_string("owner", player_name)
clear_player_session(player_name)
minetest.chat_send_player(player_name, "Your chest has been locked.")
return true
end
if fields.quit then
clear_player_session(player_name)
return true
end
return false
end
if formname == FORMNAME_UNLOCK then
if fields.open then
local password = fields.password or ""
if password == "" then
minetest.chat_send_player(player_name, "Password field is empty.")
return true
end
if account.check(meta:get_int("id"), password) then
show_password_chest_formspec(player_name, pos)
else
minetest.chat_send_player(player_name, "Your password is incorrect. Please type again.")
end
return true
end
if fields.quit then
clear_player_session(player_name)
return true
end
return false
end
if formname == FORMNAME_CHEST then
if fields.change_password then
show_change_password_formspec(player_name, pos)
return true
end
if fields.quit then
clear_player_session(player_name)
return true
end
return false
end
if formname == FORMNAME_CHANGE_PASSWORD then
if fields.save then
local old_password = fields.old_password or ""
local new_password = fields.new_password or ""
local confirm_password = fields.confirm_password or ""
if old_password == "" or new_password == "" then
minetest.chat_send_player(player_name, "Password field is empty.")
return true
end
if new_password ~= confirm_password then
minetest.chat_send_player(player_name, "Password confirmation mismatch.")
return true
end
if account.change(meta:get_int("id"), old_password, new_password) then
minetest.chat_send_player(player_name, "Updated password chest.")
show_password_chest_formspec(player_name, pos)
else
minetest.chat_send_player(player_name, "Your password is incorrect. Please type again.")
end
return true
end
if fields.quit then
clear_player_session(player_name)
return true
end
end
return false
end)
minetest.register_on_leaveplayer(function(player)
clear_player_session(player:get_player_name())
end)
minetest.register_craft({
output = "password_chest:password_chest",
recipe = {
{"group:wood", "default:mese", "group:wood"},
{"group:wood", "default:gold_ingot", "group:wood"},
{"group:wood", "group:wood", "group:wood"}
}
})