function PVPSound_DataOnLoad()
	PVPSoundDataFrame:RegisterEvent("CHAT_MSG_ADDON")
	PVPSoundDataFrame:RegisterEvent("CHAT_MSG_EMOTE")
end

local CheckLimit = 0.5000

-- Names
local PlayerName
local PlayerNameSpree
local KillerName
local KillerNameServer

-- Sprees
local Spree
local KSpree
local DeathSpree
local MessageSpree

-- Data Share
local SentTime
local ReceivedTime
local WasOutOfRange

-- PVPSound Addon Data Sharing
function PVPSound_DataOnEvent(self, event, prefix, message, channel, sender, ...)
	if select(4, GetBuildInfo()) >= 40200 then
		arg1, eventType, hideCaster, sourceGUID, sourceName, sourceFlags, sourceRaidFlags, destGUID, destName, destFlags, destRaidFlags, spellId, swingOverkill, _, _, spellOverkill = select(1, ...)
	elseif select(4, GetBuildInfo()) >= 40100 then
		arg1, eventType, hideCaster, sourceGUID, sourceName, sourceFlags, destGUID, destName, destFlags, spellId, swingOverkill, _, _, spellOverkill = select(1, ...)
	elseif select(4, GetBuildInfo()) >= 40000 then
		arg1, eventType, sourceGUID, sourceName, sourceFlags, destGUID, destName, destFlags, spellId, swingOverkill, _, _, spellOverkill = select(1, ...)
	elseif select(4, GetBuildInfo()) >= 20400 then
		_, eventType, sourceGUID, sourceName, sourceFlags, destGUID, destName, destFlags, spellId, swingOverkill, _, _, spellOverkill = select(1, ...)
	end

	if PS_DataShare == true then
		if event == "CHAT_MSG_EMOTE" then
			-- If the sent data is in the time limit
			if ReceivedTime ~= nil and SentTime ~= nil and PlayerNameSpree ~= nil then
				if string.find(arg1,PlayerNameSpree) then
					if (ReceivedTime - SentTime) <= CheckLimit then
						WasOutOfRange = false
					else
						WasOutOfRange = false
					end
				else
					WasOutOfRange = true
				end
			end
		elseif event == "CHAT_MSG_ADDON" then
			-- If the Sent Data is from the registered prefix
			if prefix == "PVPSound" then
				-- If the sender is not nil and its not the player
				if sender ~= nil and sender ~= UnitName("player") then
					-- If the message is not nil
					if message ~= nil then
						-- Kill Data Share
						if string.find(message,"!") then
							KSpree = tostring(string.match(message, "(.+)!"))
							-- If sender is from CrossRealm
							if string.find(sender,"-") and PS_HideServerName ~= false then
								PlayerName = tostring(string.match(sender, "(.+)-"))
								-- If from -Azjol-Nerub
								if string.find(PlayerName,"-") and PS_HideServerName ~= false then
									PlayerName = tostring(string.match(PlayerName, "(.+)-"))
									if PlayerName ~= nil and KSpree ~= nil then
										PVPSound_PrintKill(PlayerName, KSpree)
									end
								else
									if PlayerName ~= nil and KSpree ~= nil then
										PVPSound_PrintKill(PlayerName, KSpree)
									end
								end
							elseif string.find(sender,"-") and PS_HideServerName == false then
								PlayerName = tostring(sender)
								if PlayerName ~= nil and KSpree ~= nil then
									PVPSound_PrintKill(PlayerName, KSpree)
								end
							 -- If the sender is not from CrossRealm and his/her Emote is toggled off or Emote is on but in Chat Message mode
							else
								PlayerName = tostring(sender)
								if PlayerName ~= nil and KSpree ~= nil then
									PVPSound_PrintKill(PlayerName, KSpree)
								end
							end
						 -- If the sender is not from CrossRealm and his/her Emote is toggled on and Emote mode in on Emote mode too, but out of Emote range
						elseif string.find(message,"?") then
							-- Nil if from CrossRealm
							if string.find(sender,"-") then
								ReceivedTime = nil
								SentTime = nil
								PlayerName = nil
								Spree = nil
								PlayerNameSpree = nil
							else
								ReceivedTime = tonumber(GetTime())
								SentTime = tonumber(string.match(message, "(.+)?"))
								PlayerName = tostring(sender)
								Spree = tostring(string.match(message, "?(.+)"))
								PlayerNameSpree = tostring(""..PlayerName.." "..Spree.."")
							end
						elseif string.find(message,"+") then
							-- Nil if from CrossRealm
							if string.find(sender,"-") then
								PlayerName = nil
								MessageSpree = nil
								WasOutOfRange = nil
							else
								PlayerName = tostring(sender)
								MessageSpree = tostring(string.match(message, "(.+)+"))
								if WasOutOfRange == true then
									if PlayerName ~= nil and MessageSpree ~= nil then
										PVPSound_PrintKill(PlayerName, MessageSpree)
									end
								end
								WasOutOfRange = nil
							end
						 -- Death Data Share
						elseif string.find(message,":") then
							DeathSpree = tostring(string.match(message, "(.+):"))
							KillerNameServer = tostring(string.match(message, ":(.+)"))
							if KillerNameServer ~= nil then
								-- If sender is from CrossRealm
								if string.find(KillerNameServer,"-") and PS_HideServerName ~= false then
									KillerName = tostring(string.match(KillerNameServer, "(.+)-"))
									-- If from -Azjol-Nerub
									if string.find(KillerNameServer,"-") and PS_HideServerName ~= false then
										KillerName = tostring(string.match(KillerNameServer, "(.+)-"))
									end
								elseif string.find(KillerNameServer,"-") and PS_HideServerName == false then
									KillerName = tostring(string.match(message, ":(.+)"))
								else
									KillerName = tostring(KillerNameServer)
								end
								if DeathSpree ~= nil then
									if KillerName ~= nil and KillerName ~= UnitName("player") then
										-- If sender is from CrossRealm
										if string.find(sender,"-") and PS_HideServerName ~= false then
											SenderName = tostring(string.match(sender, "(.+)-"))
											-- If from -Azjol-Nerub
											if string.find(SenderName,"-") and PS_HideServerName ~= false then
												SenderName = tostring(string.match(SenderName, "(.+)-"))
												if SenderName ~= nil and DeathSpree ~= nil and KillerName ~= nil then
													PVPSound_PrintDeath(SenderName, DeathSpree, KillerName)
												end
											else
												if SenderName ~= nil and DeathSpree ~= nil and KillerName ~= nil then
													PVPSound_PrintDeath(SenderName, DeathSpree, KillerName)
												end
											end
										elseif string.find(sender,"-") and PS_HideServerName == false then
											SenderName = tostring(sender)
											if SenderName ~= nil and DeathSpree ~= nil and KillerName ~= nil then
												PVPSound_PrintDeath(SenderName, DeathSpree, KillerName)
											end
										else
											SenderName = tostring(sender)
											if SenderName ~= nil and DeathSpree ~= nil and KillerName ~= nil then
												PVPSound_PrintDeath(SenderName, DeathSpree, KillerName)
											end
										end
									end
								end
							end
						 -- Nil everything
						else
							Spree = nil
							KSpree = nil
							DeathSpree = nil
							MessageSpree = nil
							PlayerName = nil
							PlayerNameSpree = nil
							KillerName = nil
							KillerNameServer = nil
							WasOutOfRange = nil
						end
					end
				end
			end
		end
	end
end

function PVPSound_PrintKill(sender, message)
	if message == "EmoteFirstBloodMale" then
		print("|cFFFFA500"..sender.." "..Msg_FirstBloodMale.."|cFFFFFFFF")
	elseif message == "EmoteFirstBloodFemale" then
		print("|cFFFFA500"..sender.." "..Msg_FirstBloodFemale.."|cFFFFFFFF")
	elseif message == "EmoteKillingSpree" then
		print("|cFFFFA500"..sender.." "..Msg_KillingSpree.."|cFFFFFFFF")
	elseif message == "EmoteRampage" then
		print("|cFFFFA500"..sender.." "..Msg_Rampage.."|cFFFFFFFF")
	elseif message == "EmoteDominating" then
		print("|cFFFFA500"..sender.." "..Msg_Dominating.."|cFFFFFFFF")
	elseif message == "EmoteUnstoppable" then
		print("|cFFFFA500"..sender.." "..Msg_Unstoppable.."|cFFFFFFFF")
	elseif message == "EmoteGodlike" then
		print("|cFFFFA500"..sender.." "..Msg_Godlike.."|cFFFFFFFF")
	elseif message == "EmoteMassacre" then
		print("|cFFFFA500"..sender.." "..Msg_Massacre.."|cFFFFFFFF")
	elseif message == "FirstBloodMale" then
		print("|cFFFFA500"..sender.." "..Msg_FirstBloodMale.."|cFFFFFFFF")
	elseif message == "FirstBloodFemale" then
		print("|cFFFFA500"..sender.." "..Msg_FirstBloodFemale.."|cFFFFFFFF")
	elseif message == "KillingSpree" then
		print("|cFFFFA500"..sender.." "..Msg_KillingSpree.."|cFFFFFFFF")
	elseif message == "Rampage" then
		print("|cFFFFA500"..sender.." "..Msg_Rampage.."|cFFFFFFFF")
	elseif message == "Dominating" then
		print("|cFFFFA500"..sender.." "..Msg_Dominating.."|cFFFFFFFF")
	elseif message == "Unstoppable" then
		print("|cFFFFA500"..sender.." "..Msg_Unstoppable.."|cFFFFFFFF")
	elseif message == "Godlike" then
		print("|cFFFFA500"..sender.." "..Msg_Godlike.."|cFFFFFFFF")
	elseif message == "Massacre" then
		print("|cFFFFA500"..sender.." "..Msg_Massacre.."|cFFFFFFFF")
	elseif message == "FirstBloodMaleEmote" then
		print("|cFFFFA500"..sender.." "..Msg_FirstBloodMale.."|cFFFFFFFF")
	elseif message == "FirstBloodFemaleEmote" then
		print("|cFFFFA500"..sender.." "..Msg_FirstBloodFemale.."|cFFFFFFFF")
	elseif message == "KillingSpreeEmote" then
		print("|cFFFFA500"..sender.." "..Msg_KillingSpree.."|cFFFFFFFF")
	elseif message == "RampageEmote" then
		print("|cFFFFA500"..sender.." "..Msg_Rampage.."|cFFFFFFFF")
	elseif message == "DominatingEmote" then
		print("|cFFFFA500"..sender.." "..Msg_Dominating.."|cFFFFFFFF")
	elseif message == "UnstoppableEmote" then
		print("|cFFFFA500"..sender.." "..Msg_Unstoppable.."|cFFFFFFFF")
	elseif message == "GodlikeEmote" then
		print("|cFFFFA500"..sender.." "..Msg_Godlike.."|cFFFFFFFF")
	elseif message == "MassacreEmote" then
		print("|cFFFFA500"..sender.." "..Msg_Massacre.."|cFFFFFFFF")
	end
end

function PVPSound_PrintDeath(sender, message, killer)
	if message == "FirstBlood" then
		print("|cFFFF4500"..sender..""..Msg_FirstBloodOver.." "..killer..".|cFFFFFFFF")
	elseif message == "KillingSpree" then
		print("|cFFFF4500"..sender..""..Msg_KillingSpreeOver.." "..killer..".|cFFFFFFFF")
	elseif message == "Rampage" then
		print("|cFFFF4500"..sender..""..Msg_RampageOver.." "..killer..".|cFFFFFFFF")
	elseif message == "Dominating" then
		print("|cFFFF4500"..sender..""..Msg_DominatingOver.." "..killer..".|cFFFFFFFF")
	elseif message == "Unstoppable" then
		print("|cFFFF4500"..sender..""..Msg_UnstoppableOver.." "..killer..".|cFFFFFFFF")
	elseif message == "Godlike" then
		print("|cFFFF4500"..sender..""..Msg_GodlikeOver.." "..killer..".|cFFFFFFFF")
	elseif message == "Massacre" then
		print("|cFFFF4500"..sender..""..Msg_MassacreOver.." "..killer..".|cFFFFFFFF")
	end
end