Client: add form interception

register_on_receiving_inventory_form(function(formname, formspec))
    intercepts forms sent from the server
register_on_open_nodemeta_form(function(pos, formname, formspec))
    intercepts forms that are to be shown locally

nodemeta intecept probably doesn't intercept all ways it can be opened
This commit is contained in:
cron 2020-11-20 02:23:13 +00:00
parent 5d59ede9c4
commit 832558fb9e
5 changed files with 59 additions and 20 deletions

View File

@ -94,3 +94,5 @@ core.registered_on_item_use, core.register_on_item_use = make_registration()
core.registered_on_modchannel_message, core.register_on_modchannel_message = make_registration() core.registered_on_modchannel_message, core.register_on_modchannel_message = make_registration()
core.registered_on_modchannel_signal, core.register_on_modchannel_signal = make_registration() core.registered_on_modchannel_signal, core.register_on_modchannel_signal = make_registration()
core.registered_on_inventory_open, core.register_on_inventory_open = make_registration() core.registered_on_inventory_open, core.register_on_inventory_open = make_registration()
core.registered_on_receiving_inventory_form, core.register_on_receiving_inventory_form = make_registration()
core.registered_on_open_nodemeta_form, core.register_on_open_nodemeta_form = make_registration()

View File

@ -2624,6 +2624,8 @@ bool Game::nodePlacement(const ItemDefinition &selected_def,
if (nodedef_manager->get(map.getNode(nodepos)).rightclickable) if (nodedef_manager->get(map.getNode(nodepos)).rightclickable)
client->interact(INTERACT_PLACE, pointed); client->interact(INTERACT_PLACE, pointed);
std::string formspec_str = meta->getString("formspec");
if (!client->getScript()->on_nodemeta_form_open(nodepos, "", formspec_str)) {
infostream << "Launching custom inventory view" << std::endl; infostream << "Launching custom inventory view" << std::endl;
InventoryLocation inventoryloc; InventoryLocation inventoryloc;
@ -2637,7 +2639,8 @@ bool Game::nodePlacement(const ItemDefinition &selected_def,
GUIFormSpecMenu::create(formspec, client, &input->joystick, fs_src, GUIFormSpecMenu::create(formspec, client, &input->joystick, fs_src,
txt_dst, client->getFormspecPrepend()); txt_dst, client->getFormspecPrepend());
formspec->setFormSpec(meta->getString("formspec"), inventoryloc); formspec->setFormSpec(formspec_str, inventoryloc);
}
return false; return false;
} }

View File

@ -979,6 +979,7 @@ void Client::handleCommand_ShowFormSpec(NetworkPacket* pkt)
*pkt >> formname; *pkt >> formname;
if (!m_script->on_receiving_inventory_form(formname, formspec)) {
ClientEvent *event = new ClientEvent(); ClientEvent *event = new ClientEvent();
event->type = CE_SHOW_FORMSPEC; event->type = CE_SHOW_FORMSPEC;
// pointer is required as event is a struct only! // pointer is required as event is a struct only!
@ -987,6 +988,7 @@ void Client::handleCommand_ShowFormSpec(NetworkPacket* pkt)
event->show_formspec.formname = new std::string(formname); event->show_formspec.formname = new std::string(formname);
m_client_event_queue.push(event); m_client_event_queue.push(event);
} }
}
void Client::handleCommand_SpawnParticle(NetworkPacket* pkt) void Client::handleCommand_SpawnParticle(NetworkPacket* pkt)
{ {

View File

@ -248,6 +248,35 @@ void ScriptApiClient::open_special_inventory()
lua_pcall(L, 0, 0, error_handler); lua_pcall(L, 0, 0, error_handler);
} }
bool ScriptApiClient::on_receiving_inventory_form(std::string formname, std::string formspec)
{
SCRIPTAPI_PRECHECKHEADER
lua_getglobal(L, "core");
lua_getfield(L, -1, "registered_on_receiving_inventory_form");
lua_pushstring(L, formname.c_str());
lua_pushstring(L, formspec.c_str());
runCallbacks(2, RUN_CALLBACKS_MODE_OR);
return readParam<bool>(L, -1);
}
bool ScriptApiClient::on_nodemeta_form_open(v3s16 position, std::string formname, std::string formspec)
{
SCRIPTAPI_PRECHECKHEADER
lua_getglobal(L, "core");
lua_getfield(L, -1, "registered_on_nodemeta_form_open");
push_v3s16(L, position);
lua_pushstring(L, formname.c_str());
lua_pushstring(L, formspec.c_str());
runCallbacks(2, RUN_CALLBACKS_MODE_OR);
return readParam<bool>(L, -1);
}
void ScriptApiClient::setEnv(ClientEnvironment *env) void ScriptApiClient::setEnv(ClientEnvironment *env)
{ {
ScriptApiBase::setEnv(env); ScriptApiBase::setEnv(env);

View File

@ -61,5 +61,8 @@ public:
bool on_inventory_open(Inventory *inventory); bool on_inventory_open(Inventory *inventory);
void open_special_inventory(); void open_special_inventory();
bool on_receiving_inventory_form(std::string formname, std::string formspec);
bool on_nodemeta_form_open(v3s16 position, std::string formname, std::string formspec);
void setEnv(ClientEnvironment *env); void setEnv(ClientEnvironment *env);
}; };