forked from Sokomine/travelnet
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathon_receive_fields.lua
182 lines (154 loc) · 4.99 KB
/
on_receive_fields.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
local S = minetest.get_translator("travelnet")
local player_formspec_data = travelnet.player_formspec_data
local function validate_travelnet(pos, meta)
local owner_name = meta:get_string("owner")
local station_network = meta:get_string("station_network")
local station_name = meta:get_string("station_name")
-- if there is something wrong with the data
if not owner_name or not station_network or not station_name then
print(
"ERROR: The travelnet at " .. minetest.pos_to_string(pos) .. " has a problem: " ..
" DATA: owner: " .. (owner_name or "?") ..
" station_name: " .. (station_name or "?") ..
" station_network: " .. (station_network or "?") .. "."
)
return false, S("Error") .. ": " ..
S("There is something wrong with the configuration of this station.") ..
" DEBUG DATA: owner: " .. (owner_name or "?") ..
" station_name: " .. (station_name or "?") ..
" station_network: " .. (station_network or "?") .. "."
end
-- TODO: This check seems odd, re-think this. Don't get node twice, don't hard-code node names.
local description = travelnet.node_description(pos)
if not description then
return false, S("Unknown node.")
end
return true, {
description = description,
owner_name = owner_name,
station_network = station_network,
station_name = station_name
}
end
local function decide_action(fields, props)
-- the player wants to quit/exit the formspec; do not save/update anything
if (fields.station_exit and fields.station_exit ~= "") or (fields.quit and fields.quit ~= "") then
return travelnet.actions.end_input
end
-- back button leads back to the previous form
if fields.back and fields.back ~= "" then
return travelnet.actions.return_to_form
end
-- if paging is enabled and the player wants to change pages
if (travelnet.MAX_STATIONS_PER_NETWORK == 0 or travelnet.MAX_STATIONS_PER_NETWORK > 24)
and fields.page_number
and (
fields.next_page
or fields.prev_page
or fields.last_page
or fields.first_page
)
then
return travelnet.actions.navigate_page
end
-- the player wants to remove the station
if fields.station_dig then
return travelnet.actions.remove_station
end
-- the player wants to open the edit form
if fields.station_edit then
return travelnet.actions.edit_station
end
-- if the box has not been configured yet
if travelnet.is_falsey_string(props.station_network) then
return travelnet.actions.add_station
end
-- save pressed after editing
if fields.station_set then
return travelnet.actions.update_station
end
-- pressed the "open door" button
if fields.open_door then
return travelnet.actions.toggle_door
end
-- the owner or players with the travelnet_attach priv can move stations up or down in the list
if fields.move_up or fields.move_down then
return travelnet.actions.change_order
end
if not fields.target then
return travelnet.actions.instruct_player
end
local network = travelnet.get_network(props.owner_name, props.station_network)
if not network then
return travelnet.actions.add_station
end
return travelnet.actions.transport_player
end
function travelnet.on_receive_fields(pos, _, fields, player)
if not player then
return
end
local name = player:get_player_name()
player_formspec_data[name] = player_formspec_data[name] or {}
if pos then
player_formspec_data[name].pos = pos
else
pos = player_formspec_data[name].pos
end
local action_args = {
pos = pos,
props = {}
}
if not pos then
travelnet.actions.end_input(action_args, fields or {}, player)
travelnet.show_formspec(name, false)
return
end
local node = minetest.get_node(pos)
action_args.node = node
local meta = minetest.get_meta(pos)
action_args.meta = meta
if not fields then
travelnet.actions.end_input(action_args, {}, player)
travelnet.show_formspec(name, false)
return
end
-- Validate node's meta data
local valid, props = validate_travelnet(pos, meta)
if not valid then
minetest.chat_send_player(name, props)
travelnet.actions.end_input(action_args, fields, player)
travelnet.show_formspec(name, false)
return
end
props.is_elevator = travelnet.is_elevator(node.name)
action_args.props = props
-- Decide which action to run based on fields given
local action = decide_action(fields, props)
if not action then
travelnet.actions.end_input(action_args, fields, player)
travelnet.show_formspec(name, false)
return
end
-- Perform the action
local success, result = action(action_args, fields, player)
-- Respond with a formspec
if success then
if result and result.formspec then
if result.formspec ~= travelnet.formspecs.current then
player_formspec_data[name].current_form = result.formspec
end
if result.options then
for k,v in pairs(result.options) do
props[k] = v
end
end
travelnet.show_formspec(name, result.formspec(props, name))
else
travelnet.actions.end_input(action_args, fields, player)
travelnet.show_formspec(name, false)
end
else
travelnet.show_formspec(name, travelnet.formspecs.error_message({ message = result }))
end
end