-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgui.lua
209 lines (179 loc) · 4.83 KB
/
gui.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
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
gui = {}
-- Chat command for opening gui
minetest.register_chatcommand("privareas", {
params = "",
description = "PrivAreas: access a formspec from the privilegeareas mod",
privs = {
privs = true,
},
func = function(name, param)
add_gui(name)
end,
})
-- The add gui
function add_gui(name)
print("Showing add area form to "..name)
gui[name]={
type="box"
}
minetest.show_formspec(name, "privilegeareas:gui_add", "size[6,5]"..
"button[2,0;2,1;type;box]"..
"field[2,2;1,1;x;X;0]"..
"field[3,2;1,1;y;Y;0]"..
"field[4,2;1,1;z;Z;0]"..
"field[2,3;1,1;x2;X;0]"..
"field[3,3;1,1;y2;Y;0]"..
"field[4,3;1,1;z2;Z;0]"..
"button_exit[2,4;2,1;submit;Add]")
end
minetest.register_on_player_receive_fields(function(player, formname, fields)
local name = player:get_player_name()
if (formname~="privilegeareas:gui_add") then
return false
end
if not minetest.check_player_privs(player, "privs") then
return false
end
if fields.submit then
-- Do addition stuff
print("submitting")
if gui[name].type == "box" then
table.insert(privilegeareas.areas,{
type = gui[name].type,
location = {
x=math.floor(fields.x),
y=math.floor(fields.y),
z=math.floor(fields.z),
},
location2 = {
x=math.floor(fields.x2),
y=math.floor(fields.y2),
z=math.floor(fields.z2),
},
actions = {
on_enter = {
},
on_leave = {
}
}
})
else
table.insert(privilegeareas.areas,{
type = gui[name].type,
location = {
x=math.floor(fields.x),
y=math.floor(fields.y),
z=math.floor(fields.z),
radius=math.floor(fields.rad)
},
actions = {
on_enter = {
grant = {},
take = {}
},
on_leave = {
grant = {},
take = {}
}
}
})
end
gui[name]=nil
act_gui(name,#privilegeareas.areas)
return
end
if fields.type then
-- Do toggle stuff
print("toggling")
if gui[name].type == "box" then
gui[name].type = "radius"
minetest.show_formspec(name, "privilegeareas:gui_add", "size[6,5]"..
"button[2,0;2,1;type;"..gui[name].type.."]"..
"field[2,2;1,1;x;X;"..fields.x.."]"..
"field[3,2;1,1;y;Y;"..fields.y.."]"..
"field[4,2;1,1;z;Z;"..fields.z.."]"..
"field[2,3;1,1;rad;R;0]"..
"button_exit[2,4;2,1;submit;Add]")
else
gui[name].type = "box"
minetest.show_formspec(name, "privilegeareas:gui_add", "size[6,5]"..
"button[2,0;2,1;type;"..gui[name].type.."]"..
"field[2,2;1,1;x;X;"..fields.x.."]"..
"field[3,2;1,1;y;Y;"..fields.y.."]"..
"field[4,2;1,1;z;Z;"..fields.z.."]"..
"field[2,3;1,1;x2;X;0]"..
"field[3,3;1,1;y2;Y;0]"..
"field[4,3;1,1;z2;Z;0]"..
"button_exit[2,4;2,1;submit;Add]")
end
end
end)
function act_gui(name,id)
print("Showing add action form for area "..id)
print(dump(privilegeareas.areas[id]))
if not gui[name] or not gui[name].trigger or not gui[name].type or not gui[name].value then
gui[name]={
trigger = "on_enter",
type = "grant",
value = "",
}
end
gui[name].id = id
minetest.show_formspec(name, "privilegeareas:gui_act", "size[7,4]"..
"field[2,1;4,1;name;Data;"..gui[name].value.."]"..
"button[1,2;2,1;trigger;"..gui[name].trigger.."]"..
"button[4,2;2,1;type;"..gui[name].type.."]"..
"button[1,3;2,1;submit;Add]"..
"button_exit[4,3;2,1;close;Close]")
end
minetest.register_on_player_receive_fields(function(player, formname, fields)
if (formname~="privilegeareas:gui_act") then
return false
end
local name = player:get_player_name()
gui[name].value = fields.name
if fields.close then
return
end
if fields.submit then
-- Do addition stuff
print("submitting")
if (gui[name].id and gui[name].trigger and fields.name) then
-- this will doubtlessly be buggy
local id = gui[name].id
local trigger = gui[name].trigger
local action_type = gui[name].type
local trigger_action_types = privilegeareas.areas[id].actions[trigger]
if not trigger_action_types[action_type] then
trigger_action_types[action_type] = {}
end
table.insert(trigger_action_types[action_type],fields.name)
-- Alert user of success
minetest.chat_send_player(name, "Added data '"..fields.name.."' to '"..gui[name].type.."' in trigger "..gui[name].trigger.." in area "..gui[name].id)
-- Delete field
gui[name] = nil
-- Update
act_gui(name,id)
return
end
end
if fields.type then
-- Do toggle stuff
print("toggling type")
if gui[player:get_player_name()].type == "grant" then
gui[player:get_player_name()].type = "take"
else
gui[player:get_player_name()].type = "grant"
end
end
if fields.trigger then
-- Do toggle stuff
print("toggling trigger")
if gui[player:get_player_name()].trigger == "on_enter" then
gui[player:get_player_name()].trigger = "on_leave"
else
gui[player:get_player_name()].trigger = "on_enter"
end
end
act_gui(name,gui[name].id)
end)