forked from minetest-mirrors/ethereal
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflight.lua
More file actions
187 lines (128 loc) · 4.45 KB
/
flight.lua
File metadata and controls
187 lines (128 loc) · 4.45 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
-- translation and settings
local S = core.get_translator("ethereal")
local flight_secs = core.settings:get("ethereal.flightpotion_duration") or (5 * 60)
local timer_check = 5 -- seconds per check
-- get player timer
local function get_timer(user)
local meta = user and user:get_meta()
return meta and meta:get_string("ethereal:fly_timer") or ""
end
-- do we have fly privs
local function has_fly(name)
return core.get_player_privs(name).fly
end
-- set player timer
local function set_timer(user, timer)
local meta = user:get_meta() ; if not meta then return end
meta:set_string("ethereal:fly_timer", timer)
end
-- give or revoke fly priv
local function set_flight(user, set)
local name = user and user:get_player_name() ; if not name then return end
local privs = core.get_player_privs(name)
privs.fly = set
core.set_player_privs(name, privs)
-- when flight removed set timer to temp position
if set ~= true then set_timer(user, "-99") end
end
-- after function
local function ethereal_set_flight(user)
local name = user and user:get_player_name() ; if not name then return end
local timer = tonumber(get_timer(user)) or 0
-- if timer ran out then remove 'fly' privelage
if timer <= 0 and timer ~= -99 then
set_flight(user, nil) ; return
end
local privs = core.get_player_privs(name)
-- have we already applied 'fly' privelage?
if not privs.fly then set_flight(user, true) end
-- handle timer
timer = timer - timer_check
-- show expiration message and play sound
if timer <= 10 then
core.chat_send_player(name, core.get_color_escape_sequence("#ff5500")
.. S("Flight timer about to expire!"))
core.sound_play("default_dig_dig_immediate", {to_player = name}, true)
end
set_timer(user, timer) -- set update timer
-- restart checks
core.after(timer_check, function()
ethereal_set_flight(user)
end)
end
-- on join / leave
core.register_on_joinplayer(function(player)
-- wait 2 seconds before doing flight checks on player
core.after(2.0, function(player)
-- get player name and timer
local name = player and player:get_player_name() ; if not name then return end
local timer = get_timer(player)
-- if timer is blank and player can already fly then default and return
if timer == "" and has_fly(name) then
set_timer(player, "-99") ; return
end
timer = tonumber(timer) or 0
-- if timer is set to default then return
if timer == -99 then return end
-- if we got this far and player is flying then start countdown check
if has_fly(name) then
core.after(timer_check, function()
ethereal_set_flight(player)
end)
end
end, player)
end)
-- potion item
core.register_node("ethereal:flight_potion", {
description = S("Flight Potion"),
drawtype = "plantlike",
tiles = {"ethereal_flight_potion.png"},
inventory_image = "ethereal_flight_potion.png",
wield_image = "ethereal_flight_potion.png",
paramtype = "light",
sunlight_propagates = true,
walkable = false,
selection_box = {
type = "fixed", fixed = {-0.2, -0.37, -0.2, 0.2, 0.31, 0.2}
},
groups = {dig_immediate = 3, vessel = 1},
sounds = default.node_sound_glass_defaults(),
on_use = function(itemstack, user, pointed_thing)
if user.is_fake_player then return end
-- get info
local name = user:get_player_name()
local privs = core.get_player_privs(name)
local timer = get_timer(user)
if privs.fly then
local msg = timer
if timer == "" or timer == "-99" then msg = S("unlimited") end
core.chat_send_player(name, core.get_color_escape_sequence("#ffff00")
.. S("Flight already granted, @1 seconds left!", msg))
return
end
set_timer(user, flight_secs) -- set flight timer
-- show time remaining
core.chat_send_player(name,
core.get_color_escape_sequence("#1eff00")
.. S("Flight granted, you have @1 seconds!", flight_secs))
ethereal_set_flight(user) -- start check
itemstack:take_item() -- take item
-- return empty bottle
local inv = user:get_inventory()
if inv:room_for_item("main", {name = "vessels:glass_bottle"}) then
user:get_inventory():add_item("main", "vessels:glass_bottle")
else
core.add_item(user:get_pos(), {name = "vessels:glass_bottle"})
end
return itemstack
end
})
-- recipe
core.register_craft({
output = "ethereal:flight_potion",
recipe = {
{"ethereal:etherium_dust", "ethereal:etherium_dust", "ethereal:etherium_dust"},
{"ethereal:etherium_dust", "ethereal:fire_dust", "ethereal:etherium_dust"},
{"ethereal:birch_leaves4", "vessels:glass_bottle", "ethereal:birch_leaves4"}
}
})