Skip to content

Commit

Permalink
Adds Team Messaging Helper Methods
Browse files Browse the repository at this point in the history
- Adds overload for FindBroadcastPlayer(Team) to find first player on specified team
- Adds SendChatMessageToTeam helper method for mods
- Separates network code into NetworkSendChat method
  • Loading branch information
data-bomb committed Jun 24, 2024
1 parent 1deee24 commit 58cbd9f
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 6 deletions.
58 changes: 53 additions & 5 deletions Si_AdminMod/HelperMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,25 @@ public static void AlertAdminAction(Player? adminPlayer, string action)
broadcastPlayer.SendChatMessage(chatPrefix + GetAdminColor() + adminName + defaultColor + " " + action, false);
}

public static void SendChatMessageToTeam(Team team, params string[] messages)
{
Player broadcastPlayer = FindBroadcastPlayer(team);

for (int i = 0; i < Player.Players.Count; i++)
{
Player? player = Player.Players[i];
if (player == null)
{
continue;
}

if (player.Team == team)
{
NetworkSendChat(player, broadcastPlayer, messages);
}
}
}

public static void SendChatMessageToPlayer(Player? player, params string[] messages)
{
// send to server console if null
Expand All @@ -109,15 +128,19 @@ public static void SendChatMessageToPlayer(Player? player, params string[] messa
}

Player broadcastPlayer = FindBroadcastPlayer();

GameByteStreamWriter gameByteStreamWriter = GameByteStreamWriter.GetGameByteStreamWriter(0U, "Si_AdminMod::SendChatMessageToPlayer", true);
NetworkSendChat(player, broadcastPlayer, messages);
}

public static void NetworkSendChat(Player recipient, Player sender, params string[] messages)
{
GameByteStreamWriter gameByteStreamWriter = GameByteStreamWriter.GetGameByteStreamWriter(0U, "Si_AdminMod::NetworkSendChat", true);
gameByteStreamWriter.WriteByte((byte)ENetworkPacketType.ChatMessage);
gameByteStreamWriter.WriteUInt64((ulong)broadcastPlayer.PlayerID);
gameByteStreamWriter.WriteByte((byte)broadcastPlayer.PlayerChannel);
gameByteStreamWriter.WriteUInt64((ulong)sender.PlayerID);
gameByteStreamWriter.WriteByte((byte)sender.PlayerChannel);
gameByteStreamWriter.WriteString(String.Concat(messages));
gameByteStreamWriter.WriteBool(false);

SteamGameServerNetworking.SendP2PPacket(player.PlayerID, gameByteStreamWriter.GetByteData(), (uint)gameByteStreamWriter.GetByteDataSize(), EP2PSend.k_EP2PSendReliable, player.PlayerChannel);
SteamGameServerNetworking.SendP2PPacket(recipient.PlayerID, gameByteStreamWriter.GetByteData(), (uint)gameByteStreamWriter.GetByteDataSize(), EP2PSend.k_EP2PSendReliable, recipient.PlayerChannel);
}

public static Player FindBroadcastPlayer()
Expand All @@ -136,6 +159,31 @@ public static Player FindBroadcastPlayer()
return NetworkGameServer.GetServerPlayer();
}

public static Player FindBroadcastPlayer(Team team)
{
if (!NetworkGameServer.GetServerDedicated())
{
return NetworkGameServer.GetServerPlayer();
}

// not ideal but funnel messages through the first player on the team for now
for (int i = 0; i < Player.Players.Count; i++)
{
Player? player = Player.Players[i];
if (player == null)
{
continue;
}

if (player.Team == team)
{
return player;
}
}

return NetworkGameServer.GetServerPlayer();
}

public static void PrintError(Exception exception, string? message = null)
{
if (message != null)
Expand Down
2 changes: 1 addition & 1 deletion Si_AdminMod/ModAttributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ You should have received a copy of the GNU General Public License
using SilicaAdminMod;
using System.Drawing;

[assembly: MelonInfo(typeof(SiAdminMod), "Admin Mod", "2.0.819", "databomb", "https://github.com/data-bomb/Silica")]
[assembly: MelonInfo(typeof(SiAdminMod), "Admin Mod", "2.0.823", "databomb", "https://github.com/data-bomb/Silica")]
[assembly: MelonGame("Bohemia Interactive", "Silica")]

// Color.Cyan
Expand Down

0 comments on commit 58cbd9f

Please sign in to comment.