Skip to content

Commit

Permalink
[CSM] Add event on_connect player API lua (#5540)
Browse files Browse the repository at this point in the history
* Add event on_connect player API lua
  • Loading branch information
Dumbeldor authored and nerzhul committed Apr 8, 2017
1 parent ff4fef5 commit f735346
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 0 deletions.
1 change: 1 addition & 0 deletions builtin/client/register.lua
Expand Up @@ -60,6 +60,7 @@ end

core.registered_globalsteps, core.register_globalstep = make_registration()
core.registered_on_shutdown, core.register_on_shutdown = make_registration()
core.registered_on_connect, core.register_on_connect = make_registration()
core.registered_on_receiving_chat_messages, core.register_on_receiving_chat_messages = make_registration()
core.registered_on_sending_chat_messages, core.register_on_sending_chat_messages = make_registration()
core.registered_on_death, core.register_on_death = make_registration()
Expand Down
4 changes: 4 additions & 0 deletions clientmods/preview/init.lua
Expand Up @@ -6,6 +6,10 @@ core.register_on_shutdown(function()
print("[PREVIEW] shutdown client")
end)

core.register_on_connect(function()
print("[PREVIEW] Player connection completed")
end)

-- This is an example function to ensure it's working properly, should be removed before merge
core.register_on_receiving_chat_messages(function(message)
print("[PREVIEW] Received message " .. message)
Expand Down
2 changes: 2 additions & 0 deletions doc/client_lua_api.md
Expand Up @@ -639,6 +639,8 @@ Call these functions only at load time!
* **Warning**: If the client terminates abnormally (i.e. crashes), the registered
callbacks **will likely not be run**. Data should be saved at
semi-frequent intervals as well as on server shutdown.
* `minetest.register_on_connect(func())`
* Called at the end of client connection (when player is loaded onto map)
* `minetest.register_on_receiving_chat_message(func(name, message))`
* Called always when a client receive a message
* Return `true` to mark the message as handled, which means that it will not be shown to chat
Expand Down
4 changes: 4 additions & 0 deletions src/client.cpp
Expand Up @@ -1864,6 +1864,10 @@ void Client::afterContentReceived(IrrlichtDevice *device)

m_state = LC_Ready;
sendReady();

if (g_settings->getBool("enable_client_modding"))
m_script->on_connect();

text = wgettext("Done!");
draw_load_screen(text, device, guienv, 0, 100);
infostream<<"Client::afterContentReceived() done"<<std::endl;
Expand Down
11 changes: 11 additions & 0 deletions src/script/cpp_api/s_client.cpp
Expand Up @@ -35,6 +35,17 @@ void ScriptApiClient::on_shutdown()
runCallbacks(0, RUN_CALLBACKS_MODE_FIRST);
}

void ScriptApiClient::on_connect()
{
SCRIPTAPI_PRECHECKHEADER

// get registered connect hooks
lua_getglobal(L, "core");
lua_getfield(L, -1, "registered_on_connect");
// Call callback
runCallbacks(0, RUN_CALLBACKS_MODE_FIRST);
}

bool ScriptApiClient::on_sending_message(const std::string &message)
{
SCRIPTAPI_PRECHECKHEADER
Expand Down
2 changes: 2 additions & 0 deletions src/script/cpp_api/s_client.h
Expand Up @@ -36,6 +36,8 @@ class ScriptApiClient : virtual public ScriptApiBase
public:
// Calls on_shutdown handlers
void on_shutdown();

void on_connect();

// Chat message handlers
bool on_sending_message(const std::string &message);
Expand Down

0 comments on commit f735346

Please sign in to comment.