aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPerttu Ahola <celeron55@gmail.com>2011-11-28 01:13:55 +0200
committerPerttu Ahola <celeron55@gmail.com>2011-11-29 19:13:56 +0200
commit19a1ac1f34c8dba8ada039c916858e16e2637f36 (patch)
treea9020ccecb30122341186666d44d6a21f92dc37a /src
parent30648d1cc987b25ccd2e09e18ee483a6b92e5ec1 (diff)
downloadminetest-19a1ac1f34c8dba8ada039c916858e16e2637f36.tar.gz
minetest-19a1ac1f34c8dba8ada039c916858e16e2637f36.tar.bz2
minetest-19a1ac1f34c8dba8ada039c916858e16e2637f36.zip
For consistency, implement calling of on_chat_message callbacks in C
Diffstat (limited to 'src')
-rw-r--r--src/scriptapi.cpp32
1 files changed, 21 insertions, 11 deletions
diff --git a/src/scriptapi.cpp b/src/scriptapi.cpp
index fb1b58f5d..a91122543 100644
--- a/src/scriptapi.cpp
+++ b/src/scriptapi.cpp
@@ -1548,18 +1548,28 @@ bool scriptapi_on_chat_message(lua_State *L, const std::string &name,
assert(lua_checkstack(L, 20));
StackUnroller stack_unroller(L);
- // Get minetest.on_chat_message builtin function
+ // Get minetest.registered_on_chat_messages
lua_getglobal(L, "minetest");
- lua_getfield(L, -1, "on_chat_message");
- luaL_checktype(L, -1, LUA_TFUNCTION);
-
- // Call function
- 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);
- return ate;
+ lua_getfield(L, -1, "registered_on_chat_messages");
+ luaL_checktype(L, -1, LUA_TTABLE);
+ int table = lua_gettop(L);
+ // Foreach
+ lua_pushnil(L);
+ while(lua_next(L, table) != 0){
+ // key at index -2 and value at index -1
+ luaL_checktype(L, -1, LUA_TFUNCTION);
+ // Call function
+ 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;
}
/*