[chatHandler] Function release.

Share your gamemodes and snippets here
Post Reply
#Dav
Posts: 3
Joined: Sun Jun 02, 2019 2:42 am
Location: Greece

[chatHandler] Function release.

Post by #Dav » Sat Jun 22, 2019 5:11 pm

Hello, in this release you will be able to enable local chat functions for your gamemode,
the function includes commands for /l, /s, /w, /me and /em.

First you have to put this code to utilities.lua

Code: Select all

function getDistanceBetweenElements(x1, y1, x2, y2)
	local distance = math.sqrt( (x2-x1)^2 + (y2-y1)^2 )
	return distance
end
This is the function for the usage of the chat commands.

Code: Select all

function localChat(playerid, text)
	--local cmd = string.find(text, "/l")
	if(string.sub(text, 1, 3) == "/l ") then
		if (string.sub ~= nil) then
		local msg = string.sub(text, 3)
		if (logged_players[playerid] == nil) then
			sendPlayerMsg(playerid, "You must be logged in to use local chat.", 0xFFFE6776)
			return
		end
		local lx, ly, lz = getPlayerPos(playerid)

		local players = getPlayers()
		for k, v in ipairs(players) do
			local x, y, z = getPlayerPos(v)
			local distance = getDistanceBetweenElements(lx, ly, x, y)
			if (distance <= 50) then
				sendPlayerMsg(v, getPlayerName(playerid).."("..playerid..") said:"..msg, 0xFFD5AAFF)
				end
			end
		end
	end
end
registerEvent("localChat", "onPlayerCommand")

function localChatMe(playerid, text)
	--local cmd = string.find(text, "/me")
	if(string.sub(text, 1, 4) == "/me ") then
		local msg = string.sub(text, 4)
		if (logged_players[playerid] == nil) then
			sendPlayerMsg(playerid, "You must be logged in to use /me command.", 0xFFFE6776)
			return
		end
		local lx, ly, lz = getPlayerPos(playerid)

		local players = getPlayers()
		for k, v in ipairs(players) do
			local x, y, z = getPlayerPos(v)
			local distance = getDistanceBetweenElements(lx, ly, x, y)
			if (distance <= 50) then
				sendPlayerMsg(v, getPlayerName(playerid).."("..playerid..")"..msg, 0xFFD5AAFF)	
			end
		end
	end
end
registerEvent("localChatMe", "onPlayerCommand")

function localChatShout(playerid, text)
	--local cmd = string.find(text, "/s")
	if(string.sub(text, 1, 3) == "/s ") then
		local msg = string.sub(text, 3)
		if (logged_players[playerid] == nil) then
			sendPlayerMsg(playerid, "You must be logged in to use shout command.", 0xFFFE6776)
			return
		end
		
		local lx, ly, lz = getPlayerPos(playerid)

		local players = getPlayers()
		for k, v in ipairs(players) do
			local x, y, z = getPlayerPos(v)
			local distance = getDistanceBetweenElements(lx, ly, x, y)
			if (distance <= 100) then
				sendPlayerMsg(v, getPlayerName(playerid).."("..playerid..") shouted:"..msg, 0xFFD5AAFF)	
			end
		end
	end
end
registerEvent("localChatShout", "onPlayerCommand")

function localChatWhisper(playerid, text)
	--local cmd = string.find(text, "/w")
	if(string.sub(text, 1, 3) == "/w ") then
		local msg = string.sub(text, 3)
		if (logged_players[playerid] == nil) then
			sendPlayerMsg(playerid, "You must be logged in to use whisper command.", 0xFFFE6776)
			return
		end
		
		local lx, ly, lz = getPlayerPos(playerid)

		local players = getPlayers()
		for k, v in ipairs(players) do
			local x, y, z = getPlayerPos(v)
			local distance = getDistanceBetweenElements(lx, ly, x, y)
			if (distance <= 10) then
				sendPlayerMsg(v, getPlayerName(playerid).."("..playerid..") whispered:"..msg, 0xFFD5AAFF)		
			end
		end
	end
end
registerEvent("localChatWhisper", "onPlayerCommand")

function localChatEm(playerid, text)
	--local cmd = string.find(text, "/em")
	if(string.sub(text, 1, 4) == "/em ") then
		local msg = string.sub(text, 4)
		if (logged_players[playerid] == nil) then
			sendPlayerMsg(playerid, "You must be logged in to use /em command.", 0xFFFE6776)
			return
		end
		
		local lx, ly, lz = getPlayerPos(playerid)

		local players = getPlayers()
		for k, v in ipairs(players) do
			local x, y, z = getPlayerPos(v)
			local distance = getDistanceBetweenElements(lx, ly, x, y)
			if (distance <= 50) then
				sendPlayerMsg(v, "[OOC]:"..msg.." ("..getPlayerName(playerid)..")", 0xFFD5AAFF)		
			end
		end
	end
end
registerEvent("localChatEm", "onPlayerCommand")
(The code is writed to enable only logged in players to make use of it, this is the next release
comming in the next couple of days)
"Many humans do not care about what is "unfair,"
for it varies so much between them."

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests