Skip to content

Commit 0d1c959

Browse files
orwell96Zeno-
authored andcommittedNov 22, 2016
Make supplying empty formspec strings close the formspec (#4737)
This will only happen if the formname matches or if formname is "".
1 parent dbeb322 commit 0d1c959

File tree

4 files changed

+38
-10
lines changed

4 files changed

+38
-10
lines changed
 

‎builtin/game/misc.lua

+4
Original file line numberDiff line numberDiff line change
@@ -239,3 +239,7 @@ else
239239

240240
end
241241

242+
function core.close_formspec(player_name, formname)
243+
return minetest.show_formspec(player_name, formname, "")
244+
end
245+

‎doc/lua_api.txt

+7
Original file line numberDiff line numberDiff line change
@@ -2316,6 +2316,13 @@ and `minetest.auth_reload` call the authetification handler.
23162316
* `formname`: name passed to `on_player_receive_fields` callbacks.
23172317
It should follow the `"modname:<whatever>"` naming convention
23182318
* `formspec`: formspec to display
2319+
* `minetest.close_formspec(playername, formname)`
2320+
* `playername`: name of player to close formspec
2321+
* `formname`: has to exactly match the one given in show_formspec, or the formspec will
2322+
not close.
2323+
* calling show_formspec(playername, formname, "") is equal to this expression
2324+
* to close a formspec regardless of the formname, call
2325+
minetest.close_formspec(playername, ""). USE THIS ONLY WHEN ABSOLUTELY NECESSARY!
23192326
* `minetest.formspec_escape(string)`: returns a string
23202327
* escapes the characters "[", "]", "\", "," and ";", which can not be used in formspecs
23212328
* `minetest.explode_table_event(string)`: returns a table

‎src/game.cpp

+21-8
Original file line numberDiff line numberDiff line change
@@ -1205,6 +1205,7 @@ static inline void create_formspec_menu(GUIFormSpecMenu **cur_formspec,
12051205
(*cur_formspec)->setFormSource(fs_src);
12061206
(*cur_formspec)->setTextDest(txt_dest);
12071207
}
1208+
12081209
}
12091210

12101211
#ifdef __ANDROID__
@@ -1753,6 +1754,8 @@ class Game {
17531754
ChatBackend *chat_backend;
17541755

17551756
GUIFormSpecMenu *current_formspec;
1757+
//default: "". If other than "", empty show_formspec packets will only close the formspec when the formname matches
1758+
std::string cur_formname;
17561759

17571760
EventManager *eventmgr;
17581761
QuicktuneShortcutter *quicktune;
@@ -1841,6 +1844,7 @@ Game::Game() :
18411844
soundmaker(NULL),
18421845
chat_backend(NULL),
18431846
current_formspec(NULL),
1847+
cur_formname(""),
18441848
eventmgr(NULL),
18451849
quicktune(NULL),
18461850
gui_chat_console(NULL),
@@ -3005,6 +3009,7 @@ void Game::openInventory()
30053009

30063010
create_formspec_menu(&current_formspec, client, gamedef, texture_src,
30073011
device, &input->joystick, fs_src, txt_dst, client);
3012+
cur_formname = "";
30083013

30093014
InventoryLocation inventoryloc;
30103015
inventoryloc.setCurrentPlayer();
@@ -3484,14 +3489,21 @@ void Game::processClientEvents(CameraOrientation *cam, float *damage_flash)
34843489
player->hurt_tilt_strength = 0;
34853490

34863491
} else if (event.type == CE_SHOW_FORMSPEC) {
3487-
FormspecFormSource *fs_src =
3488-
new FormspecFormSource(*(event.show_formspec.formspec));
3489-
TextDestPlayerInventory *txt_dst =
3490-
new TextDestPlayerInventory(client, *(event.show_formspec.formname));
3491-
3492-
create_formspec_menu(&current_formspec, client, gamedef,
3493-
texture_src, device, &input->joystick,
3494-
fs_src, txt_dst, client);
3492+
if (*(event.show_formspec.formspec) == "") {
3493+
if (current_formspec && ( *(event.show_formspec.formname) == "" || *(event.show_formspec.formname) == cur_formname) ){
3494+
current_formspec->quitMenu();
3495+
}
3496+
} else {
3497+
FormspecFormSource *fs_src =
3498+
new FormspecFormSource(*(event.show_formspec.formspec));
3499+
TextDestPlayerInventory *txt_dst =
3500+
new TextDestPlayerInventory(client, *(event.show_formspec.formname));
3501+
3502+
create_formspec_menu(&current_formspec, client, gamedef,
3503+
texture_src, device, &input->joystick,
3504+
fs_src, txt_dst, client);
3505+
cur_formname = *(event.show_formspec.formname);
3506+
}
34953507

34963508
delete(event.show_formspec.formspec);
34973509
delete(event.show_formspec.formname);
@@ -3955,6 +3967,7 @@ void Game::handlePointingAtNode(GameRunData *runData,
39553967

39563968
create_formspec_menu(&current_formspec, client, gamedef,
39573969
texture_src, device, &input->joystick, fs_src, txt_dst, client);
3970+
cur_formname = "";
39583971

39593972
current_formspec->setFormSpec(meta->getString("formspec"), inventoryloc);
39603973
} else {

‎src/server.cpp

+6-2
Original file line numberDiff line numberDiff line change
@@ -1647,8 +1647,12 @@ void Server::SendShowFormspecMessage(u16 peer_id, const std::string &formspec,
16471647
DSTACK(FUNCTION_NAME);
16481648

16491649
NetworkPacket pkt(TOCLIENT_SHOW_FORMSPEC, 0 , peer_id);
1650-
1651-
pkt.putLongString(FORMSPEC_VERSION_STRING + formspec);
1650+
if (formspec == "" ){
1651+
//the client should close the formspec
1652+
pkt.putLongString("");
1653+
} else {
1654+
pkt.putLongString(FORMSPEC_VERSION_STRING + formspec);
1655+
}
16521656
pkt << formname;
16531657

16541658
Send(&pkt);

0 commit comments

Comments
 (0)
Please sign in to comment.