pcall(function()PlayerBlockedEvent=StarterGui:GetCore("\080\108\097\121\101\114\066\108\111\099\107\101\100\069\118\101\110\116")PlayerMutedEvent=StarterGui:GetCore("\080\108\097\121\101\114\077\117\116\101\100\069\118\101\110\116")PlayerUnBlockedEvent=StarterGui:GetCore("\080\108\097\121\101\114\085\110\098\108\111\099\107\101\100\069\118\101\110\116")PlayerUnMutedEvent=StarterGui:GetCore("\080\108\097\121\101\114\085\110\109\117\116\101\100\069\118\101\110\116")end)function SendSystemMessageToSelf(n)local I=ChatWindow:GetCurrentChannel()if I then local X={ID=-1;FromSpeaker=nil,SpeakerUserId=0,OriginalChannel=I.Name,IsFiltered=true,MessageLength=string.len(n);MessageLengthUtf8=utf8.len(utf8.nfcnormalize(n));Message=trimTrailingSpaces(n);MessageType=ChatConstants.MessageTypeSystem,Time=os.time(),ExtraData=nil}I:AddMessageToChannel(X)end end function MutePlayer(n)local I=DefaultChatSystemChatEvents:FindFirstChild("\077\117\116\101\080\108\097\121\101\114\082\101\113\117\101\115\116")if I then return I:InvokeServer(n.Name)end return false end if PlayerBlockedEvent then PlayerBlockedEvent.Event:connect(function(n)if MutePlayer(n)then local I if ChatSettings.PlayerDisplayNamesEnabled then I=n.DisplayName else I=n.Name end SendSystemMessageToSelf(ChatLocalization:Get("\071\097\109\101\067\104\097\116\095\067\104\097\116\077\097\105\110\095\083\112\101\097\107\101\114\072\097\115\066\101\101\110\066\108\111\099\107\101\100",string.format("\083\112\101\097\107\101\114 \'\037\115\' \104\097\115 \098\101\101\110 \098\108\111\099\107\101\100\046",I),{RBX_NAME=I}))end end)end if FFlagUserHandleFriendJoinNotifierOnClient then local function n()if ChatSettings.ShowFriendJoinNotification~=nil then return ChatSettings.ShowFriendJoinNotification end return false end if n()then Players.PlayerAdded:Connect(function(n)local I,X=pcall(function()return n:IsFriendsWith(LocalPlayer.UserId)end)if I and X then local I=n.Name if ChatSettings.PlayerDisplayNamesEnabled then I=n.DisplayName end local X=ChatLocalization:FormatMessageToSend("\071\097\109\101\067\104\097\116\095\070\114\105\101\110\100\067\104\097\116\078\111\116\105\102\105\101\114\095\074\111\105\110\077\101\115\115\097\103\101",string.format("\089\111\117\114 \102\114\105\101\110\100 \037\115 \104\097\115 \106\111\105\110\101\100 \116\104\101 \103\097\109\101\046",I),"\082\066\088\095\078\065\077\069",I)SendSystemMessageToSelf(X)end end)end end if PlayerMutedEvent then PlayerMutedEvent.Event:connect(function(n)if MutePlayer(n)then local I if ChatSettings.PlayerDisplayNamesEnabled then I=n.DisplayName else I=n.Name end SendSystemMessageToSelf(ChatLocalization:Get("\071\097\109\101\067\104\097\116\095\067\104\097\116\077\097\105\110\095\083\112\101\097\107\101\114\072\097\115\066\101\101\110\077\117\116\101\100",string.format("\083\112\101\097\107\101\114 \'\037\115\' \104\097\115 \098\101\101\110 \109\117\116\101\100\046",I),{RBX_NAME=I}))end end)end function UnmutePlayer(n)local I=DefaultChatSystemChatEvents:FindFirstChild("\085\110\077\117\116\101\080\108\097\121\101\114\082\101\113\117\101\115\116")if I then return I:InvokeServer(n.Name)end return false end if PlayerUnBlockedEvent then PlayerUnBlockedEvent.Event:connect(function(n)if UnmutePlayer(n)then local I if ChatSettings.PlayerDisplayNamesEnabled then I=n.DisplayName else I=n.Name end SendSystemMessageToSelf(ChatLocalization:Get("\071\097\109\101\067\104\097\116\095\067\104\097\116\077\097\105\110\095\083\112\101\097\107\101\114\072\097\115\066\101\101\110\085\110\066\108\111\099\107\101\100",string.format("\083\112\101\097\107\101\114 \'\037\115\' \104\097\115 \098\101\101\110 \117\110\098\108\111\099\107\101\100\046",I),{RBX_NAME=I}))end end)end if PlayerUnMutedEvent then PlayerUnMutedEvent.Event:connect(function(n)if UnmutePlayer(n)then local I if ChatSettings.PlayerDisplayNamesEnabled then I=n.DisplayName else I=n.Name end SendSystemMessageToSelf(ChatLocalization:Get("\071\097\109\101\067\104\097\116\095\067\104\097\116\077\097\105\110\095\083\112\101\097\107\101\114\072\097\115\066\101\101\110\085\110\077\117\116\101\100",string.format("\083\112\101\097\107\101\114 \'\037\115\' \104\097\115 \098\101\101\110 \117\110\109\117\116\101\100\046",I),{RBX_NAME=I}))end end)end
-- This is pcalled because the SetCore methods may not be released yet.
pcall(function()
	PlayerBlockedEvent = StarterGui:GetCore("PlayerBlockedEvent")
	PlayerMutedEvent = StarterGui:GetCore("PlayerMutedEvent")
	PlayerUnBlockedEvent = StarterGui:GetCore("PlayerUnblockedEvent")
	PlayerUnMutedEvent = StarterGui:GetCore("PlayerUnmutedEvent")
end)

function SendSystemMessageToSelf(message)
	local currentChannel = ChatWindow:GetCurrentChannel()

	if currentChannel then
		local messageData = {
			ID = -1,
			FromSpeaker = nil,
			SpeakerUserId = 0,
			OriginalChannel = currentChannel.Name,
			IsFiltered = true,
			MessageLength = string.len(message),
			MessageLengthUtf8 = utf8.len(utf8.nfcnormalize(message)),
			Message = trimTrailingSpaces(message),
			MessageType = ChatConstants.MessageTypeSystem,
			Time = os.time(),
			ExtraData = nil,
		}

		currentChannel:AddMessageToChannel(messageData)
	end
end

function MutePlayer(player)
	local mutePlayerRequest = DefaultChatSystemChatEvents:FindFirstChild("MutePlayerRequest")
	if mutePlayerRequest then
		return mutePlayerRequest:InvokeServer(player.Name)
	end
	return false
end

if PlayerBlockedEvent then
	PlayerBlockedEvent.Event:connect(function(player)
		if MutePlayer(player) then
			local playerName

			if ChatSettings.PlayerDisplayNamesEnabled then
				playerName = player.DisplayName
			else
				playerName = player.Name
			end

			SendSystemMessageToSelf(
				ChatLocalization:Get(
					"GameChat_ChatMain_SpeakerHasBeenBlocked",
					string.format("Speaker '%s' has been blocked.", playerName),
					{ RBX_NAME = playerName }
				)
			)
		end
	end)
end

if FFlagUserHandleFriendJoinNotifierOnClient then
	local function ShowFriendJoinNotification()
		if ChatSettings.ShowFriendJoinNotification ~= nil then
			return ChatSettings.ShowFriendJoinNotification
		end
		return false
	end

	if ShowFriendJoinNotification() then
		Players.PlayerAdded:Connect(function(newPlayer)
			local success, isFriends = pcall(function()
				return newPlayer:IsFriendsWith(LocalPlayer.UserId)
			end)

			if success and isFriends then
				local joinedFriendName = newPlayer.Name
				if ChatSettings.PlayerDisplayNamesEnabled then
					joinedFriendName = newPlayer.DisplayName
				end

				local msg = ChatLocalization:FormatMessageToSend(
					"GameChat_FriendChatNotifier_JoinMessage",
					string.format("Your friend %s has joined the game.", joinedFriendName),
					"RBX_NAME",
					joinedFriendName
				)

				SendSystemMessageToSelf(msg)
			end
		end)
	end
end

if PlayerMutedEvent then
	PlayerMutedEvent.Event:connect(function(player)
		if MutePlayer(player) then
			local playerName

			if ChatSettings.PlayerDisplayNamesEnabled then
				playerName = player.DisplayName
			else
				playerName = player.Name
			end

			SendSystemMessageToSelf(
				ChatLocalization:Get(
					"GameChat_ChatMain_SpeakerHasBeenMuted",
					string.format("Speaker '%s' has been muted.", playerName),
					{ RBX_NAME = playerName }
				)
			)
		end
	end)
end

function UnmutePlayer(player)
	local unmutePlayerRequest = DefaultChatSystemChatEvents:FindFirstChild("UnMutePlayerRequest")
	if unmutePlayerRequest then
		return unmutePlayerRequest:InvokeServer(player.Name)
	end
	return false
end

if PlayerUnBlockedEvent then
	PlayerUnBlockedEvent.Event:connect(function(player)
		if UnmutePlayer(player) then
			local playerName

			if ChatSettings.PlayerDisplayNamesEnabled then
				playerName = player.DisplayName
			else
				playerName = player.Name
			end

			SendSystemMessageToSelf(
				ChatLocalization:Get(
					"GameChat_ChatMain_SpeakerHasBeenUnBlocked",
					string.format("Speaker '%s' has been unblocked.", playerName),
					{ RBX_NAME = playerName }
				)
			)
		end
	end)
end

if PlayerUnMutedEvent then
	PlayerUnMutedEvent.Event:connect(function(player)
		if UnmutePlayer(player) then
			local playerName

			if ChatSettings.PlayerDisplayNamesEnabled then
				playerName = player.DisplayName
			else
				playerName = player.Name
			end

			SendSystemMessageToSelf(
				ChatLocalization:Get(
					"GameChat_ChatMain_SpeakerHasBeenUnMuted",
					string.format("Speaker '%s' has been unmuted.", playerName),
					{ RBX_NAME = playerName }
				)
			)
		end
	end)
end
