forked from Ekdohibs/itest
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathscuba.lua
More file actions
61 lines (57 loc) · 1.6 KB
/
scuba.lua
File metadata and controls
61 lines (57 loc) · 1.6 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
--time in number of ticks
local cell_time = 4
minetest.register_tool("voltbuild:scuba_gear", {
description = "Scuba Gear",
inventory_image = "voltbuild_scuba_gear.png",
})
minetest.register_craftitem("voltbuild:air_cell", {
description = "Air Cell",
inventory_image = "voltbuild_air_cell.png",
})
local scuba
local cell_ticks = {}
scuba = function (player)
--to prevent a server crash from the minetest.after loop by making sure it
--runs only for players actually in the game
if cell_ticks[player] then
local inv = player:get_inventory()
local i, gear_equipped, cells_index
if player:get_breath() ~= 11 then
for i=1,inv:get_size("main") do
local stack = inv:get_stack("main",i)
if stack:get_name() == "voltbuild:scuba_gear" then
gear_equipped = true
if cells_index then
break
end
end
if stack:get_name() == "voltbuild:air_cell"
and not stack:is_empty() then
cells_index = i
if gear_equipped then
break
end
end
end
end
if cells_index and gear_equipped then
cell_ticks[player] = cell_ticks[player]+1
player:set_breath(10)
if cell_ticks[player] >= cell_time then
local cells = inv:get_stack("main",cells_index)
cells:take_item()
inv:set_stack("main",cells_index,cells)
cell_ticks[player] = cell_ticks[player] - cell_time
end
end
minetest.after(4,scuba,player)
end
end
minetest.register_on_joinplayer(function(player)
minetest.after(4,scuba,player)
cell_ticks[player] = 0
end)
--to prevent a server crash from the minetest.after loop
minetest.register_on_leaveplayer(function(player)
cell_ticks[player] = nil
end)