-- minetest.register_on_newplayer(func(ObjectRef))
-- minetest.register_on_respawnplayer(func(ObjectRef))
-- ^ return true in func to disable regular player placement
+-- minetest.register_on_chat_message(func(name, message))
-- minetest.setting_get(name)
-- minetest.setting_getbool(name)
+-- minetest.chat_send_all(text)
+-- minetest.chat_send_player(name, text)
--
-- Global objects:
-- minetest.env - environment reference
print("setting max_users = " .. dump(minetest.setting_get("max_users")))
print("setting asdf = " .. dump(minetest.setting_get("asdf")))
+minetest.register_on_chat_message(function(name, message)
+ print("on_chat_message: name="..dump(name).." message="..dump(message))
+ local cmd = "/testcommand"
+ if message:sub(0, #cmd) == cmd then
+ print(cmd.." invoked")
+ return true
+ end
+ local cmd = "/help"
+ if message:sub(0, #cmd) == cmd then
+ print("script-overridden help command")
+ minetest.chat_send_all("script-overridden help command")
+ return true
+ end
+end)
+
--
-- Done, print some random stuff
--
return 1;
}
+// chat_send_all(text)
+static int l_chat_send_all(lua_State *L)
+{
+ const char *text = luaL_checkstring(L, 1);
+ // Get server from registry
+ lua_getfield(L, LUA_REGISTRYINDEX, "minetest_server");
+ Server *server = (Server*)lua_touserdata(L, -1);
+ // Send
+ server->notifyPlayers(narrow_to_wide(text));
+ return 0;
+}
+
+// chat_send_player(name, text)
+static int l_chat_send_player(lua_State *L)
+{
+ const char *name = luaL_checkstring(L, 1);
+ const char *text = luaL_checkstring(L, 2);
+ // Get server from registry
+ lua_getfield(L, LUA_REGISTRYINDEX, "minetest_server");
+ Server *server = (Server*)lua_touserdata(L, -1);
+ // Send
+ server->notifyPlayer(name, narrow_to_wide(text));
+ return 0;
+}
+
static const struct luaL_Reg minetest_f [] = {
{"register_nodedef_defaults", l_register_nodedef_defaults},
{"register_entity", l_register_entity},
{"register_on_respawnplayer", l_register_on_respawnplayer},
{"setting_get", l_setting_get},
{"setting_getbool", l_setting_getbool},
+ {"chat_send_all", l_chat_send_all},
+ {"chat_send_player", l_chat_send_player},
{NULL, NULL}
};
lua_settable(L, objectstable);
}
+bool scriptapi_on_chat_message(lua_State *L, const std::string &name,
+ const std::string &message)
+{
+ realitycheck(L);
+ assert(lua_checkstack(L, 20));
+ StackUnroller stack_unroller(L);
+
+ // Get minetest.on_chat_message builtin function
+ 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;
+}
/*
misc
{
realitycheck(L);
assert(lua_checkstack(L, 20));
- infostream<<"scriptapi_environment_on_generated"<<std::endl;
+ //infostream<<"scriptapi_environment_on_generated"<<std::endl;
StackUnroller stack_unroller(L);
// Get minetest.registered_on_generateds