2013-05-25 00:51:02 +02:00
|
|
|
/*
|
|
|
|
Minetest
|
|
|
|
Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2.1 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public License along
|
|
|
|
with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "cpp_api/s_player.h"
|
2013-08-11 04:09:45 +02:00
|
|
|
#include "cpp_api/s_internal.h"
|
2013-12-12 07:51:35 +01:00
|
|
|
#include "util/string.h"
|
2013-05-25 00:51:02 +02:00
|
|
|
|
|
|
|
void ScriptApiPlayer::on_newplayer(ServerActiveObject *player)
|
|
|
|
{
|
|
|
|
SCRIPTAPI_PRECHECKHEADER
|
|
|
|
|
|
|
|
// Get minetest.registered_on_newplayers
|
|
|
|
lua_getglobal(L, "minetest");
|
|
|
|
lua_getfield(L, -1, "registered_on_newplayers");
|
|
|
|
// Call callbacks
|
|
|
|
objectrefGetOrCreate(player);
|
2013-08-11 04:09:45 +02:00
|
|
|
script_run_callbacks(L, 1, RUN_CALLBACKS_MODE_FIRST);
|
2013-05-25 00:51:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptApiPlayer::on_dieplayer(ServerActiveObject *player)
|
|
|
|
{
|
|
|
|
SCRIPTAPI_PRECHECKHEADER
|
|
|
|
|
|
|
|
// Get minetest.registered_on_dieplayers
|
|
|
|
lua_getglobal(L, "minetest");
|
|
|
|
lua_getfield(L, -1, "registered_on_dieplayers");
|
|
|
|
// Call callbacks
|
|
|
|
objectrefGetOrCreate(player);
|
2013-08-11 04:09:45 +02:00
|
|
|
script_run_callbacks(L, 1, RUN_CALLBACKS_MODE_FIRST);
|
2013-05-25 00:51:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ScriptApiPlayer::on_respawnplayer(ServerActiveObject *player)
|
|
|
|
{
|
|
|
|
SCRIPTAPI_PRECHECKHEADER
|
|
|
|
|
|
|
|
// Get minetest.registered_on_respawnplayers
|
|
|
|
lua_getglobal(L, "minetest");
|
|
|
|
lua_getfield(L, -1, "registered_on_respawnplayers");
|
|
|
|
// Call callbacks
|
|
|
|
objectrefGetOrCreate(player);
|
2013-08-11 04:09:45 +02:00
|
|
|
script_run_callbacks(L, 1, RUN_CALLBACKS_MODE_OR);
|
2013-05-25 00:51:02 +02:00
|
|
|
bool positioning_handled_by_some = lua_toboolean(L, -1);
|
|
|
|
return positioning_handled_by_some;
|
|
|
|
}
|
|
|
|
|
2013-12-12 07:51:35 +01:00
|
|
|
bool ScriptApiPlayer::on_prejoinplayer(std::string name, std::string ip, std::string &reason)
|
|
|
|
{
|
|
|
|
SCRIPTAPI_PRECHECKHEADER
|
|
|
|
|
|
|
|
// Get minetest.registered_on_prejoinplayers
|
|
|
|
lua_getglobal(L, "minetest");
|
|
|
|
lua_getfield(L, -1, "registered_on_prejoinplayers");
|
|
|
|
lua_pushstring(L, name.c_str());
|
|
|
|
lua_pushstring(L, ip.c_str());
|
|
|
|
script_run_callbacks(L, 2, RUN_CALLBACKS_MODE_OR);
|
|
|
|
if (lua_isstring(L, -1)) {
|
|
|
|
reason.assign(lua_tostring(L, -1));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-05-25 00:51:02 +02:00
|
|
|
void ScriptApiPlayer::on_joinplayer(ServerActiveObject *player)
|
|
|
|
{
|
|
|
|
SCRIPTAPI_PRECHECKHEADER
|
|
|
|
|
|
|
|
// Get minetest.registered_on_joinplayers
|
|
|
|
lua_getglobal(L, "minetest");
|
|
|
|
lua_getfield(L, -1, "registered_on_joinplayers");
|
|
|
|
// Call callbacks
|
|
|
|
objectrefGetOrCreate(player);
|
2013-08-11 04:09:45 +02:00
|
|
|
script_run_callbacks(L, 1, RUN_CALLBACKS_MODE_FIRST);
|
2013-05-25 00:51:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ScriptApiPlayer::on_leaveplayer(ServerActiveObject *player)
|
|
|
|
{
|
|
|
|
SCRIPTAPI_PRECHECKHEADER
|
|
|
|
|
|
|
|
// Get minetest.registered_on_leaveplayers
|
|
|
|
lua_getglobal(L, "minetest");
|
|
|
|
lua_getfield(L, -1, "registered_on_leaveplayers");
|
|
|
|
// Call callbacks
|
|
|
|
objectrefGetOrCreate(player);
|
2013-08-11 04:09:45 +02:00
|
|
|
script_run_callbacks(L, 1, RUN_CALLBACKS_MODE_FIRST);
|
2013-05-25 00:51:02 +02:00
|
|
|
}
|
|
|
|
|
2013-08-03 23:45:49 +02:00
|
|
|
void ScriptApiPlayer::on_cheat(ServerActiveObject *player,
|
|
|
|
const std::string &cheat_type)
|
|
|
|
{
|
|
|
|
SCRIPTAPI_PRECHECKHEADER
|
|
|
|
|
|
|
|
// Get minetest.registered_on_cheats
|
|
|
|
lua_getglobal(L, "minetest");
|
|
|
|
lua_getfield(L, -1, "registered_on_cheats");
|
|
|
|
// Call callbacks
|
|
|
|
objectrefGetOrCreate(player);
|
|
|
|
lua_newtable(L);
|
|
|
|
lua_pushlstring(L, cheat_type.c_str(), cheat_type.size());
|
|
|
|
lua_setfield(L, -2, "type");
|
2013-08-11 04:09:45 +02:00
|
|
|
script_run_callbacks(L, 2, RUN_CALLBACKS_MODE_FIRST);
|
2013-08-03 23:45:49 +02:00
|
|
|
}
|
|
|
|
|
2013-05-25 00:51:02 +02:00
|
|
|
void ScriptApiPlayer::on_playerReceiveFields(ServerActiveObject *player,
|
|
|
|
const std::string &formname,
|
|
|
|
const std::map<std::string, std::string> &fields)
|
|
|
|
{
|
|
|
|
SCRIPTAPI_PRECHECKHEADER
|
|
|
|
|
|
|
|
// Get minetest.registered_on_chat_messages
|
|
|
|
lua_getglobal(L, "minetest");
|
|
|
|
lua_getfield(L, -1, "registered_on_player_receive_fields");
|
|
|
|
// Call callbacks
|
|
|
|
// param 1
|
|
|
|
objectrefGetOrCreate(player);
|
|
|
|
// param 2
|
|
|
|
lua_pushstring(L, formname.c_str());
|
|
|
|
// param 3
|
|
|
|
lua_newtable(L);
|
|
|
|
for(std::map<std::string, std::string>::const_iterator
|
|
|
|
i = fields.begin(); i != fields.end(); i++){
|
|
|
|
const std::string &name = i->first;
|
|
|
|
const std::string &value = i->second;
|
|
|
|
lua_pushstring(L, name.c_str());
|
|
|
|
lua_pushlstring(L, value.c_str(), value.size());
|
|
|
|
lua_settable(L, -3);
|
|
|
|
}
|
2013-08-11 04:09:45 +02:00
|
|
|
script_run_callbacks(L, 3, RUN_CALLBACKS_MODE_OR_SC);
|
2013-05-25 00:51:02 +02:00
|
|
|
}
|
|
|
|
ScriptApiPlayer::~ScriptApiPlayer() {
|
|
|
|
}
|
|
|
|
|
|
|
|
|