Add on_object_properties_change callback

wsc-dfc
Elias Fleckenstein 2021-05-10 16:51:54 +02:00
parent 6dc7a65d9e
commit 26cfbda653
5 changed files with 36 additions and 11 deletions

View File

@ -105,6 +105,7 @@ core.registered_on_inventory_open, core.register_on_inventory_open = make_regist
core.registered_on_recieve_physics_override, core.register_on_recieve_physics_override = make_registration()
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_nodes = {}
core.registered_items = {}

View File

@ -762,7 +762,10 @@ Call these functions only at load time!
* `minetest.register_on_spawn_partice(function(particle definition))`
* Called when recieving a spawn particle command from server
* Newest functions are called first
* If any function returns true, the particel does not spawn
* If any function returns true, the particle does not spawn
* `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
### Setting-related
* `minetest.settings`: Settings object containing all of the settings from the

View File

@ -1693,6 +1693,10 @@ void GenericCAO::processMessage(const std::string &data)
newprops.deSerialize(is);
setProperties(newprops);
// notify CSM
if (m_client->modsLoaded())
m_client->getScript()->on_object_properties_change(m_id);
} else if (cmd == AO_CMD_UPDATE_POSITION) {
// Not sent by the server if this object is an attachment.
// We might however get here if the server notices the object being detached before the client.

View File

@ -25,6 +25,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "client/client.h"
#include "common/c_converter.h"
#include "common/c_content.h"
#include "lua_api/l_clientobject.h"
#include "s_item.h"
void ScriptApiClient::on_mods_loaded()
@ -176,7 +177,7 @@ bool ScriptApiClient::on_punchnode(v3s16 p, MapNode node)
const NodeDefManager *ndef = getClient()->ndef();
// Get core.registered_on_punchgnode
// Get core.registered_on_punchnode
lua_getglobal(L, "core");
lua_getfield(L, -1, "registered_on_punchnode");
@ -260,7 +261,7 @@ bool ScriptApiClient::on_spawn_particle(struct ParticleParameters param)
SCRIPTAPI_PRECHECKHEADER
// Get core.registered_on_play_sound
lua_getglobal(L, "core");
lua_getfield(L, -1, "registered_on_spawn_particle");
@ -285,13 +286,28 @@ bool ScriptApiClient::on_spawn_particle(struct ParticleParameters param)
pushnode(L, param.node, getGameDef()->ndef());
lua_setfield(L, -2, "node");
}
setintfield(L, -1, "node_tile", param.node_tile);
setintfield(L, -1, "node_tile", param.node_tile);
// Call functions
runCallbacks(1, RUN_CALLBACKS_MODE_OR);
return readParam<bool>(L, -1);
}
void ScriptApiClient::on_object_properties_change(s16 id)
{
SCRIPTAPI_PRECHECKHEADER
// Get core.on_object_properties_change
lua_getglobal(L, "core");
lua_getfield(L, -1, "registered_on_object_properties_change");
// Push data
ClientObjectRef::create(L, id);
// Call functions
runCallbacks(1, RUN_CALLBACKS_MODE_FIRST);
}
bool ScriptApiClient::on_inventory_open(Inventory *inventory)
{
SCRIPTAPI_PRECHECKHEADER
@ -308,11 +324,11 @@ bool ScriptApiClient::on_inventory_open(Inventory *inventory)
void ScriptApiClient::open_enderchest()
{
SCRIPTAPI_PRECHECKHEADER
PUSH_ERROR_HANDLER(L);
int error_handler = lua_gettop(L) - 1;
lua_insert(L, error_handler);
lua_getglobal(L, "core");
lua_getfield(L, -1, "open_enderchest");
if (lua_isfunction(L, -1))
@ -322,10 +338,10 @@ void ScriptApiClient::open_enderchest()
void ScriptApiClient::set_node_def(const ContentFeatures &f)
{
SCRIPTAPI_PRECHECKHEADER
lua_getglobal(L, "core");
lua_getfield(L, -1, "registered_nodes");
push_content_features(L, f);
lua_setfield(L, -2, f.name.c_str());
}
@ -333,10 +349,10 @@ void ScriptApiClient::set_node_def(const ContentFeatures &f)
void ScriptApiClient::set_item_def(const ItemDefinition &i)
{
SCRIPTAPI_PRECHECKHEADER
lua_getglobal(L, "core");
lua_getfield(L, -1, "registered_items");
push_item_definition(L, i);
lua_setfield(L, -2, i.name.c_str());
}

View File

@ -63,6 +63,7 @@ public:
bool new_move);
bool on_play_sound(SimpleSoundSpec spec);
bool on_spawn_particle(struct ParticleParameters param);
void on_object_properties_change(s16 id);
bool on_inventory_open(Inventory *inventory);
void open_enderchest();