Skip to content

Commit

Permalink
Add on_joinplayer and on_leaveplayer callbacks.
Browse files Browse the repository at this point in the history
  • Loading branch information
red-001 authored and sofar committed Apr 9, 2017
1 parent 330b38d commit 139cd7e
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 0 deletions.
3 changes: 3 additions & 0 deletions builtin/client/register.lua
Expand Up @@ -69,3 +69,6 @@ core.registered_on_damage_taken, core.register_on_damage_taken = make_registrati
core.registered_on_formspec_input, core.register_on_formspec_input = make_registration()
core.registered_on_dignode, core.register_on_dignode = make_registration()
core.registered_on_punchnode, core.register_on_punchnode = make_registration()
core.registered_on_joinplayer, core.register_on_joinplayer = make_registration()
core.registered_on_leaveplayer, core.register_on_leaveplayer = make_registration()

9 changes: 9 additions & 0 deletions clientmods/preview/init.lua
Expand Up @@ -139,3 +139,12 @@ core.register_chatcommand("disconnect", {
core.disconnect()
end,
})
core.register_on_joinplayer(function(name)
print(name .. " joined the game.")
end)
core.register_on_leaveplayer(function(name)
print(name .. " left the game.")
end)
7 changes: 7 additions & 0 deletions doc/client_lua_api.md
Expand Up @@ -667,6 +667,13 @@ Call these functions only at load time!
* Called when the local player punches a node
* Newest functions are called first
* If any function returns true, the punch is ignored
* `minetest.register_on_joinplayer(func(name))`
* Called when a player joins the game.
* Gets called for all players currently on a server when joining a server.
* `minetest.register_on_leaveplayer(func(name))`
* Called when a player leaves the game.
* Not called if the client is exiting.

### Sounds
* `minetest.sound_play(spec, parameters)`: returns a handle
* `spec` is a `SimpleSoundSpec`
Expand Down
7 changes: 7 additions & 0 deletions src/content_cao.cpp
Expand Up @@ -44,6 +44,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "camera.h" // CameraModes
#include "wieldmesh.h"
#include "log.h"
#include "clientscripting.h"

class Settings;
struct ToolCapabilities;
Expand Down Expand Up @@ -623,6 +624,10 @@ void GenericCAO::initialize(const std::string &data)
m_is_visible = false;
player->setCAO(this);
}

if (m_client->moddingEnabled())
m_client->getScript()->on_joinplayer(m_name);

m_env->addPlayerName(m_name.c_str());
}
}
Expand Down Expand Up @@ -667,6 +672,8 @@ void GenericCAO::processInitData(const std::string &data)
GenericCAO::~GenericCAO()
{
if (m_is_player) {
if (m_client->moddingEnabled() && !m_client->isShutdown())
m_client->getScript()->on_leaveplayer(m_name);
m_env->removePlayerName(m_name.c_str());
}
removeFromScene(true);
Expand Down
24 changes: 24 additions & 0 deletions src/script/cpp_api/s_client.cpp
Expand Up @@ -189,6 +189,30 @@ bool ScriptApiClient::on_punchnode(v3s16 p, MapNode node)
return blocked;
}

void ScriptApiClient::on_joinplayer(const std::string &name)
{
SCRIPTAPI_PRECHECKHEADER

// Get core.registered_on_joinplayer
lua_getglobal(L, "core");
lua_getfield(L, -1, "registered_on_joinplayer");
// Call callbacks
lua_pushstring(L, name.c_str());
runCallbacks(1, RUN_CALLBACKS_MODE_LAST);
}

void ScriptApiClient::on_leaveplayer(const std::string &name)
{
SCRIPTAPI_PRECHECKHEADER

// Get core.registered_on_leaveplayer
lua_getglobal(L, "core");
lua_getfield(L, -1, "registered_on_leaveplayer");
// Call callbacks
lua_pushstring(L, name.c_str());
runCallbacks(1, RUN_CALLBACKS_MODE_LAST);
}

void ScriptApiClient::setEnv(ClientEnvironment *env)
{
ScriptApiBase::setEnv(env);
Expand Down
3 changes: 3 additions & 0 deletions src/script/cpp_api/s_client.h
Expand Up @@ -52,6 +52,9 @@ class ScriptApiClient : virtual public ScriptApiBase
bool on_dignode(v3s16 p, MapNode node);
bool on_punchnode(v3s16 p, MapNode node);

void on_joinplayer(const std::string &name);
void on_leaveplayer(const std::string &name);

void setEnv(ClientEnvironment *env);
};
#endif

0 comments on commit 139cd7e

Please sign in to comment.