Hello. This can be used for several things... E.g. you could rewrite guild system to be in Lua or make anti-bot system in a channel without using a command in front of each word (that means if you'd be using talkactions). onWriteToChannel does not react on keywords, as fast as you write to a channel it will call this function!
So, open up creatureevent.h, and under:
Add:
And under:
Add:
Now, open creatureevent.cpp.
Under:
Add:
Under:
Add:
Under:
Add:
Under function:
Add:
Now finally open up chat.cpp, find function Chat::talkToChannel, above:
Add:
Now, in creaturescripts.xml you write:
And script (example):
Then we're done! Enjoy!
Now, if you want function doPlayerOpenChannel, keep reading!
In luascript.h, find:
Under it add:
Now go to luascript.cpp and find:
Under it add:
Now find function:
Under it add:
So, open up creatureevent.h, and under:
Code:
CREATURE_EVENT_CHANNEL_LEAVE,
Add:
Code:
CREATURE_EVENT_CHANNEL_WRITE,
And under:
Code:
uint32_t executeChannelLeave(Player* player, uint16_t channelId, UsersMap usersMap);
Add:
Code:
uint32_t executeChannelWrite(Player* player, uint16_t channelId, std::string text);
Now, open creatureevent.cpp.
Under:
Code:
else if(tmpStr == "joinchannel")
m_type = CREATURE_EVENT_CHANNEL_JOIN;
Add:
Code:
else if(tmpStr == "writetochannel")
m_type = CREATURE_EVENT_CHANNEL_WRITE;
Under:
Code:
case CREATURE_EVENT_CHANNEL_LEAVE:
return "onLeaveChannel";
Add:
Code:
case CREATURE_EVENT_CHANNEL_WRITE:
return "onWriteToChannel";
Under:
Code:
case CREATURE_EVENT_CHANNEL_LEAVE:
return "cid, channel, users";
Add:
Code:
case CREATURE_EVENT_CHANNEL_WRITE:
return "cid, channel, text";
Under function:
Code:
uint32_t CreatureEvent::executeChannelLeave(Player* player, uint16_t channelId, UsersMap usersMap)
Add:
Code:
uint32_t CreatureEvent::executeChannelWrite(Player* player, uint16_t channelId, std::string text)
{
//onWriteToChannel(cid, channel, text)
if(m_scriptInterface->reserveScriptEnv())
{
ScriptEnviroment* env = m_scriptInterface->getScriptEnv();
if(m_scripted == EVENT_SCRIPT_BUFFER)
{
env->setRealPos(player->getPosition());
std::stringstream scriptstream;
scriptstream << "local cid = " << env->addThing(player) << std::endl;
scriptstream << "local channel = " << channelId << std::endl;
scriptstream << "local text = " << text << std::endl;
scriptstream << m_scriptData;
bool result = true;
if(m_scriptInterface->loadBuffer(scriptstream.str()) != -1)
{
lua_State* L = m_scriptInterface->getLuaState();
result = m_scriptInterface->getGlobalBool(L, "_result", true);
}
m_scriptInterface->releaseScriptEnv();
return result;
}
else
{
#ifdef __DEBUG_LUASCRIPTS__
char desc[35];
sprintf(desc, "%s", player->getName().c_str());
env->setEventDesc(desc);
#endif
env->setScriptId(m_scriptId, m_scriptInterface);
env->setRealPos(player->getPosition());
lua_State* L = m_scriptInterface->getLuaState();
m_scriptInterface->pushFunction(m_scriptId);
lua_pushnumber(L, env->addThing(player));
lua_pushnumber(L, channelId);
lua_pushstring(L, text.c_str());
bool result = m_scriptInterface->callFunction(3);
m_scriptInterface->releaseScriptEnv();
return result;
}
}
else
{
std::cout << "[Error - CreatureEvent::executeChannelWrite] Call stack overflow." << std::endl;
return 0;
}
}
Now finally open up chat.cpp, find function Chat::talkToChannel, above:
Code:
if(channelId != CHANNEL_GUILD || !g_config.getBool(ConfigManager::INGAME_GUILD_MANAGEMENT)
|| (text[0] != '!' && text[0] != '/'))
Add:
Code:
CreatureEventList writeEvents = player->getCreatureEvents(CREATURE_EVENT_CHANNEL_WRITE);
for(CreatureEventList::iterator it = writeEvents.begin(); it != writeEvents.end(); ++it)
if(!(*it)->executeChannelWrite(player, channelId, text))
return true;
Now, in creaturescripts.xml you write:
Code:
<event type="writetochannel" name="PlayerWriteToChannel" event="script" value="playerWrite.lua"/>
And script (example):
Lua:
function onWriteToChannel(cid, channelId, text)
if(text:lower() == 'fuck') then
doPlayerSendChannelMessage(cid, "", "Swearings are not allowed here!", TALKTYPE_CHANNEL_RN, channelId)
return false -- this will make his text dont appear!
end
return true
end
Then we're done! Enjoy!
Now, if you want function doPlayerOpenChannel, keep reading!
In luascript.h, find:
Code:
static int32_t luaGetChannelUsers(lua_State* L);
Under it add:
Code:
static int32_t luaDoPlayerOpenChannel(lua_State* L);
Now go to luascript.cpp and find:
Code:
//getChannelUsers(channelId)
lua_register(m_luaState, "getChannelUsers", LuaScriptInterface::luaGetChannelUsers);
Under it add:
Code:
//doPlayerOpenChannel(cid, channelId)
lua_register(m_luaState, "doPlayerOpenChannel", LuaScriptInterface::luaDoPlayerOpenChannel);
Now find function:
Code:
int32_t LuaScriptInterface::luaGetChannelUsers(lua_State* L)
Under it add:
Code:
int32_t LuaScriptInterface::luaDoPlayerOpenChannel(lua_State* L)
{
//doPlayerOpenChannel(cid, channelId)
uint32_t channelId = popNumber(L);
uint32_t cid = popNumber(L);
ScriptEnviroment* env = getScriptEnv();
Player* player = env->getPlayerByUID(cid);
if(player)
lua_pushnumber(L, g_game.playerOpenChannel(cid, channelId) ? true : false);
else
{
reportErrorFunc(getErrorDesc(LUA_ERROR_PLAYER_NOT_FOUND));
lua_pushnumber(L, false);
}
return 1;
}
Last edited: