mirror of
https://github.com/minetest/minetest.git
synced 2024-11-27 18:13:46 +01:00
For consistency, implement calling of on_chat_message callbacks in C
This commit is contained in:
parent
30648d1cc9
commit
19a1ac1f34
@ -166,14 +166,4 @@ minetest.registered_on_generateds, minetest.register_on_generated = make_registr
|
|||||||
minetest.registered_on_newplayers, minetest.register_on_newplayer = make_registration()
|
minetest.registered_on_newplayers, minetest.register_on_newplayer = make_registration()
|
||||||
minetest.registered_on_respawnplayers, minetest.register_on_respawnplayer = make_registration()
|
minetest.registered_on_respawnplayers, minetest.register_on_respawnplayer = make_registration()
|
||||||
|
|
||||||
minetest.on_chat_message = function(name, message)
|
|
||||||
for i,func in ipairs(minetest.registered_on_chat_messages) do
|
|
||||||
local ate = func(name, message)
|
|
||||||
if ate then
|
|
||||||
return true
|
|
||||||
end
|
|
||||||
end
|
|
||||||
return false
|
|
||||||
end
|
|
||||||
|
|
||||||
-- END
|
-- END
|
||||||
|
@ -1548,18 +1548,28 @@ bool scriptapi_on_chat_message(lua_State *L, const std::string &name,
|
|||||||
assert(lua_checkstack(L, 20));
|
assert(lua_checkstack(L, 20));
|
||||||
StackUnroller stack_unroller(L);
|
StackUnroller stack_unroller(L);
|
||||||
|
|
||||||
// Get minetest.on_chat_message builtin function
|
// Get minetest.registered_on_chat_messages
|
||||||
lua_getglobal(L, "minetest");
|
lua_getglobal(L, "minetest");
|
||||||
lua_getfield(L, -1, "on_chat_message");
|
lua_getfield(L, -1, "registered_on_chat_messages");
|
||||||
luaL_checktype(L, -1, LUA_TFUNCTION);
|
luaL_checktype(L, -1, LUA_TTABLE);
|
||||||
|
int table = lua_gettop(L);
|
||||||
// Call function
|
// Foreach
|
||||||
lua_pushstring(L, name.c_str());
|
lua_pushnil(L);
|
||||||
lua_pushstring(L, message.c_str());
|
while(lua_next(L, table) != 0){
|
||||||
if(lua_pcall(L, 2, 1, 0))
|
// key at index -2 and value at index -1
|
||||||
script_error(L, "error: %s\n", lua_tostring(L, -1));
|
luaL_checktype(L, -1, LUA_TFUNCTION);
|
||||||
bool ate = lua_toboolean(L, -1);
|
// Call function
|
||||||
return ate;
|
lua_pushstring(L, name.c_str());
|
||||||
|
lua_pushstring(L, message.c_str());
|
||||||
|
if(lua_pcall(L, 2, 1, 0))
|
||||||
|
script_error(L, "error: %s\n", lua_tostring(L, -1));
|
||||||
|
bool ate = lua_toboolean(L, -1);
|
||||||
|
lua_pop(L, 1);
|
||||||
|
if(ate)
|
||||||
|
return true;
|
||||||
|
// value removed, keep key for next iteration
|
||||||
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
Loading…
Reference in New Issue
Block a user