Ir para conteúdo

[Creaturescript] Onwritetochannel(Cid, Channelid, Text)


Posts Recomendados

Nome: onWriteToChannel(cid, channelId, text)

Tipo: Código C++

Autor: Colandus, Oneshot (Adaptação 0.4 + Default Channel)

 

 

Fala, galera.

 

Esse é um código C++ que serve como add-on para o seu servidor, ele adiciona um novo tipo de função para os creaturescripts, que é executado toda vez que um jogador envia uma mensagem ao servidor.

 

Foi criado por Colandus e estou trazendo ao XTibia por ser um código muito útil e interessante de se usar nos servidores.

 

Lembrando que os passos abaixo devem ser seguidos em um IDE de sua preferência para posterior compilação.

 

 

Em creatureevent.h, procure por:

 

CREATURE_EVENT_CHANNEL_LEAVE,

 

Adicione logo abaixo:

 

CREATURE_EVENT_CHANNEL_WRITE,

 

Ainda em creatureevent.h, procure por:

 

uint32_t executeChannelLeave(Player* player, uint16_t channelId, UsersMap usersMap);

 

Adicione abaixo:

 

uint32_t executeChannelWrite(Player* player, uint16_t channelId, std::string text);

 

Agora em creaturescripts.cpp, logo abaixo de:

 


else if(tmpStr == "joinchannel")
	m_type = CREATURE_EVENT_CHANNEL_JOIN;

 

Adicione:

 


else if(tmpStr == "writechannel")
	m_type = CREATURE_EVENT_CHANNEL_WRITE;

 

Abaixo de:

 


	case CREATURE_EVENT_CHANNEL_LEAVE:
		return "onLeaveChannel";

 

Adicione:

 


	case CREATURE_EVENT_CHANNEL_WRITE:
		return "onWriteToChannel";

 

Abaixo de:

 


	case CREATURE_EVENT_CHANNEL_LEAVE:
		return "cid, channel, users";

 

Adicione:

 


	case CREATURE_EVENT_CHANNEL_WRITE:
		return "cid, channel, text";

 

Logo após o fim da função:

 

uint32_t CreatureEvent::executeChannelLeave(Player* player, uint16_t channelId, UsersMap usersMap)

 

Adicione:

 


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;
}
}

 

0.4

 

 


uint32_t CreatureEvent::executeChannelWriteTo(Player* player, uint16_t channelId, std::string text)
{
//onWriteToChannel(cid, channel, text)
if(m_interface->reserveEnv())
{
	ScriptEnviroment* env = m_interface->getEnv();
	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_interface->loadBuffer(scriptstream.str()) != -1)
		{
			lua_State* L = m_interface->getState();
			result = m_interface->getGlobalBool(L, "_result", true);
		}

		m_interface->releaseEnv();
		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_interface);
		env->setRealPos(player->getPosition());

		lua_State* L = m_interface->getState();
		m_interface->pushFunction(m_scriptId);

		lua_pushnumber(L, env->addThing(player));
		lua_pushnumber(L, channelId);
		lua_pushstring(L, text.c_str());

		bool result = m_interface->callFunction(3);
		m_interface->releaseEnv();
		return result;
	}
}
else
{
	std::cout << "[Error - CreatureEvent::executeChannelWrite] Call stack overflow." << std::endl;
	return 0;
}
}

 

 

 

Em game.cpp, procure pela função:

 

Game::playerSay

 

Acima de:

 


if(player->isAccountManager())
{
	if(mute)
		player->removeMessageBuffer();

	return internalCreatureSay(player, SPEAK_SAY, text, false);
}

 

Adicione:

 

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;

 

Finalmente no chat.cpp, procure pela função:

 

Chat::talkToChannel

 

Acima de:

 


if(channelId != CHANNEL_GUILD || !g_config.getBool(ConfigManager::INGAME_GUILD_MANAGEMENT)
	|| (text[0] != '!' && text[0] != '/'))

 

Adicione:

 


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;

 

 

Exemplo de Uso:

 


function onWriteToChannel(cid, channelId, text)
if text:lower():find(".servegame.com") then
	doPlayerSendTextMessage(cid, MESSAGE_STATUS_WARNING, "É proibido divulgar servidores aqui.")
	return false
end
return true
end

 

<event type="writechannel" name="WriteToChannel" event="script" value="writetochannel.lua"/>

 

São várias as possibilidades de uso.

 

 

Abraços.

Editado por Oneshot
Link para o comentário
Compartilhar em outros sites

daora o.O

 

bem útil mesmo, rep+

 

será que funciona no tfs 0.2 '-'?

 

----------

 

acho que pode ser util pro negocio que eu tava fazendo, de mandar msgs em cores..

 

function onWriteToChannel(cid, channelId, text)
   local acess = {
       [1] = "branca",
       [2] = "azul",
       [3] = "vermelho"
   }

   for _, k in pairs(getPlayersOnline()) do
       return doPlayerSendTextMessage(k, acess[getPlayerAcess(cid)], text)
   end
end

Editado por fireelement
Link para o comentário
Compartilhar em outros sites

×
×
  • Criar Novo...