Add on_object_add callback

pull/49/head 2021.05
Elias Fleckenstein 2021-05-11 19:15:23 +02:00
parent 4f613bbf51
commit b7abc8df28
5 changed files with 24 additions and 2 deletions

View File

@ -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 = {}

View File

@ -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

View File

@ -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);
}

View File

@ -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

View File

@ -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();