winstxnhdw / lc-hax

A powerful, feature-rich and highly performant internal cheat for the co-op indie horror video game, Lethal Company.
77 stars 25 forks source link

[FEATURE REQUEST]: Mute Chat Code #333

Closed D1GQ closed 4 months ago

D1GQ commented 5 months ago

I've been messing around with the mute player code it seems that I got it working, but there is a issue I'm running into that I have not been able to find a way to fix rn, the issue is it works perfectly fine on my end but it seems that it's not working for other players in the server, when I first suggested this future I was thinking that this would work server wide instead of client-sided. Also it's pretty buggy to do on yourself.

Code:

using System;
using System.Collections.Generic;
using System.Linq;
using GameNetcodeStuff;
using Hax;
using UnityEngine;

[DebugCommand("/mute")]
public class MuteCommand : ICommand {

    // Dictionary to store muted players, with username as key and TransientBehaviour as value
    static Dictionary<string, TransientBehaviour> MutedPlayers { get; } = new Dictionary<string, TransientBehaviour>();

    // Method to check if a muted player has sent any messages
    bool MutedPlayerHasMessaged(string playerUsername) =>
        Helper.HUDManager?.ChatMessageHistory.Any(message =>
            message.StartsWith($"<color=#FF0000>{playerUsername}</color>: <color=#FFFF00>'") &&
            !message.StartsWith("SYSTEM:")
        ) ?? false;

    // Method to execute the /mute command
    public void Execute(StringArray args) {
        // Check if arguments are provided
        if (args.Length == 0) {
            Chat.Print("Usage: /mute <player>");
            return;
        }

        // Check if HUDManager is available
        if (Helper.HUDManager == null) {
            Chat.Print("HUDManager not available.");
            return;
        }

        // Get the player object based on username provided in arguments
        if (Helper.GetPlayer(args[0]) is not PlayerControllerB player) {
            Chat.Print("Player is not alive or found!");
            return;
        }

        // Check if the player is already muted, if so, unmute them
        if (MutedPlayers.TryGetValue(player.playerUsername, out TransientBehaviour existingMuterObject)) {
            UnityEngine.Object.Destroy(existingMuterObject);
            MutedPlayers.Remove(player.playerUsername);
            Chat.Print($"Unmuted {player.playerUsername}!");
            return;
        }

        // Clear the chat to remove messages from muted player
        if (Chat.Clear != null) {
            Chat.Clear();
        }
        else {
            // If Chat.Clear is not available, try sending empty messages multiple times
            for (int i = 0; i < 25; i++) {
                Chat.Announce("", true);
            }
        }

        // Rebuild the chat without including messages from muted players and "SYSTEM:" messages
        string newChatText = string.Join('\n', Helper.HUDManager?.ChatMessageHistory
            .Where(message => !message.StartsWith($"<color=#FF0000>{player.playerUsername}</color>: <color=#FFFF00>'") &&
                               !message.StartsWith("SYSTEM:")));

        Chat.Announce(newChatText, true);

        // Mute the player by creating a TransientBehaviour object
        TransientBehaviour playerMuterObject = Helper.CreateComponent<TransientBehaviour>().Init(_ => {
            if (!MutedPlayerHasMessaged(player.playerUsername)) return;

            string newChatText = string.Join('\n', Helper.HUDManager?.ChatMessageHistory
                .Where(message => !message.StartsWith($"<color=#FF0000>{player.playerUsername}</color>: <color=#FFFF00>'") &&
                                   !message.StartsWith("SYSTEM:")));

            Chat.Announce(newChatText, true);
        }, 10000.0f);

        // Add the player to the dictionary of muted players
        MutedPlayers.Add(player.playerUsername, playerMuterObject);
        Chat.Print($"Muted {player.playerUsername}!");
    }
}
winstxnhdw commented 5 months ago

Weird. It shouldn’t be client-sided.

winstxnhdw commented 4 months ago

Unfortunately, this is not fixable. Taking a look at their RPC code.

        if (chatMessage.Length > 50)
        {
            return;
        }
        this.AddPlayerChatMessageClientRpc(chatMessage, playerId);

They block any message above 50.