From b7abc8df281390b1fb5def31866086029209aa67 Mon Sep 17 00:00:00 2001 From: Elias Fleckenstein Date: Tue, 11 May 2021 19:15:23 +0200 Subject: [PATCH] Add on_object_add callback --- builtin/client/register.lua | 1 + doc/client_lua_api.txt | 2 ++ src/client/content_cao.cpp | 3 +++ src/script/cpp_api/s_client.cpp | 19 +++++++++++++++++-- src/script/cpp_api/s_client.h | 1 + 5 files changed, 24 insertions(+), 2 deletions(-) diff --git a/builtin/client/register.lua b/builtin/client/register.lua index 6a6d8e13c..f17188b84 100644 --- a/builtin/client/register.lua +++ b/builtin/client/register.lua @@ -107,6 +107,7 @@ core.registered_on_play_sound, core.register_on_play_sound = make_registration() core.registered_on_spawn_particle, core.register_on_spawn_particle = make_registration() core.registered_on_object_properties_change, core.register_on_object_properties_change = make_registration() core.registered_on_object_hp_change, core.register_on_object_hp_change = make_registration() +core.registered_on_object_add, core.register_on_object_add = make_registration() core.registered_nodes = {} core.registered_items = {} diff --git a/doc/client_lua_api.txt b/doc/client_lua_api.txt index e33fe0e3a..a02a281f5 100644 --- a/doc/client_lua_api.txt +++ b/doc/client_lua_api.txt @@ -763,6 +763,8 @@ Call these functions only at load time! * Called when recieving a spawn particle command from server * Newest functions are called first * If any function returns true, the particle does not spawn +* `minetest.register_on_object_add(function(obj))` + * Called every time an object is added * `minetest.register_on_object_properties_change(function(obj))` * Called every time the properties of an object are changed server-side * May modify the object's properties without the fear of infinite recursion diff --git a/src/client/content_cao.cpp b/src/client/content_cao.cpp index 84d200a73..5e9060fc8 100644 --- a/src/client/content_cao.cpp +++ b/src/client/content_cao.cpp @@ -829,6 +829,9 @@ void GenericCAO::addToScene(ITextureSource *tsrc) setNodeLight(m_last_light); updateMeshCulling(); + if (m_client->modsLoaded()) + m_client->getScript()->on_object_add(m_id); + if (m_client->modsLoaded()) m_client->getScript()->on_object_properties_change(m_id); } diff --git a/src/script/cpp_api/s_client.cpp b/src/script/cpp_api/s_client.cpp index 7971e4081..5990c4df2 100644 --- a/src/script/cpp_api/s_client.cpp +++ b/src/script/cpp_api/s_client.cpp @@ -297,7 +297,7 @@ void ScriptApiClient::on_object_properties_change(s16 id) { SCRIPTAPI_PRECHECKHEADER - // Get core.on_object_properties_change + // Get core.registered_on_object_properties_change lua_getglobal(L, "core"); lua_getfield(L, -1, "registered_on_object_properties_change"); @@ -312,7 +312,7 @@ void ScriptApiClient::on_object_hp_change(s16 id) { SCRIPTAPI_PRECHECKHEADER - // Get core.on_object_hp_change + // Get core.registered_on_object_hp_change lua_getglobal(L, "core"); lua_getfield(L, -1, "registered_on_object_hp_change"); @@ -323,6 +323,21 @@ void ScriptApiClient::on_object_hp_change(s16 id) runCallbacks(1, RUN_CALLBACKS_MODE_FIRST); } +void ScriptApiClient::on_object_add(s16 id) +{ + SCRIPTAPI_PRECHECKHEADER + + // Get core.registered_on_object_add + lua_getglobal(L, "core"); + lua_getfield(L, -1, "registered_on_object_add"); + + // Push data + push_objectRef(L, id); + + // Call functions + runCallbacks(1, RUN_CALLBACKS_MODE_FIRST); +} + bool ScriptApiClient::on_inventory_open(Inventory *inventory) { SCRIPTAPI_PRECHECKHEADER diff --git a/src/script/cpp_api/s_client.h b/src/script/cpp_api/s_client.h index a2babfac8..12d96d81e 100644 --- a/src/script/cpp_api/s_client.h +++ b/src/script/cpp_api/s_client.h @@ -65,6 +65,7 @@ public: bool on_spawn_particle(struct ParticleParameters param); void on_object_properties_change(s16 id); void on_object_hp_change(s16 id); + void on_object_add(s16 id); bool on_inventory_open(Inventory *inventory); void open_enderchest();