Skip to content

Commit

Permalink
Switching Surrender to RegisterPlayerCommand
Browse files Browse the repository at this point in the history
- Switches !surrender to use SAM RegisterPlayerCommand API (moves hooks to a centralized location)
  • Loading branch information
data-bomb committed Mar 17, 2024
1 parent d9ce28e commit 9f0ba0c
Showing 1 changed file with 29 additions and 51 deletions.
80 changes: 29 additions & 51 deletions Si_SurrenderCommand/Si_SurrenderCommand.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
Silica Surrender Command Mod
Copyright (C) 2023 by databomb
Copyright (C) 2023-2024 by databomb
* Description *
For Silica listen servers, provides a command (!surrender) which
Expand Down Expand Up @@ -31,7 +31,7 @@ You should have received a copy of the GNU General Public License
using System;
using UnityEngine;

[assembly: MelonInfo(typeof(SurrenderCommand), "Surrender Command", "1.2.1", "databomb", "https://github.com/data-bomb/Silica")]
[assembly: MelonInfo(typeof(SurrenderCommand), "Surrender Command", "1.2.2", "databomb", "https://github.com/data-bomb/Silica")]
[assembly: MelonGame("Bohemia Interactive", "Silica")]
[assembly: MelonOptionalDependencies("Admin Mod")]

Expand Down Expand Up @@ -63,61 +63,39 @@ public static bool IsCommander(Player thePlayer)
return false;
}

#if NET6_0
[HarmonyPatch(typeof(Il2CppSilica.UI.Chat), nameof(Il2CppSilica.UI.Chat.MessageReceived))]
#else
[HarmonyPatch(typeof(Silica.UI.Chat), "MessageReceived")]
#endif
private static class ApplyChatReceiveSurrenderPatch
public override void OnLateInitializeMelon()
{
#if NET6_0
public static void Postfix(Il2CppSilica.UI.Chat __instance, Player __0, string __1, bool __2)
#else
public static void Postfix(Silica.UI.Chat __instance, Player __0, string __1, bool __2)
#endif
{
try
{
// each faction has its own chat manager but by looking at alien and only global messages this catches commands only once
if (!__instance.ToString().Contains("alien") || __2 == true)
{
return;
}
HelperMethods.CommandCallback surrenderCallback = Command_Surrender;
HelperMethods.RegisterPlayerCommand("surrender", surrenderCallback, true);
}

bool isSurrenderCommand = String.Equals(__1, "!surrender", StringComparison.OrdinalIgnoreCase);
if (!isSurrenderCommand)
{
return;
}
public static void Command_Surrender(Player callerPlayer, String args)
{
// check if we are actually a commander
bool isCommander = IsCommander(callerPlayer);

// check if we are actually a commander
bool isCommander = IsCommander(__0);
if (!isCommander)
{
// notify player on invalid usage
HelperMethods.ReplyToCommand_Player(callerPlayer, ": only commanders can use !surrender");
return;
}

if (!isCommander)
{
// notify player on invalid usage
HelperMethods.ReplyToCommand_Player(__0, ": only commanders can use !surrender");
return;
}
// check if game on-going
if (!GameMode.CurrentGameMode.GameOngoing)
{
HelperMethods.SendChatMessageToPlayer(callerPlayer, HelperMethods.chatPrefix, " Can't surrender. Game not started.");
return;
}

// is there a game currently started?
if (GameMode.CurrentGameMode.GameOngoing)
{
// destroy all structures on team that's surrendering
Team SurrenderTeam = __0.Team;
for (int i = 0; i < SurrenderTeam.Structures.Count; i++)
{
SurrenderTeam.Structures[i].DamageManager.SetHealth01(0.0f);
}
// notify all players
HelperMethods.ReplyToCommand_Player(callerPlayer, "used !surrender to end");

// notify all players
HelperMethods.ReplyToCommand_Player(__0, "used !surrender to end");
}
}
catch (Exception error)
{
HelperMethods.PrintError(error, "Failed to run Chat::MessageReceived");
}
// destroy all structures on team that's surrendering
Team SurrenderTeam = callerPlayer.Team;
for (int i = 0; i < SurrenderTeam.Structures.Count; i++)
{
SurrenderTeam.Structures[i].DamageManager.SetHealth01(0.0f);
}
}
}
Expand Down

0 comments on commit 9f0ba0c

Please sign in to comment.