From 7429b0c782f5caed1a9ade5092092dfc6f024e01 Mon Sep 17 00:00:00 2001 From: data-bomb Date: Sat, 21 Dec 2024 18:21:11 -0800 Subject: [PATCH] Adding Anti-Grief Infinite Unit Block - Block players griefing by creating infinite units (changes game behavior to remove starting units (Crab, Horned Crab, or Wasp) when an Alien takeover is processed) --- Si_AntiGrief/Si_AntiGrief.cs | 59 +++++++++++++++++++++++++++--------- 1 file changed, 45 insertions(+), 14 deletions(-) diff --git a/Si_AntiGrief/Si_AntiGrief.cs b/Si_AntiGrief/Si_AntiGrief.cs index d9e9ce0..aa830bb 100644 --- a/Si_AntiGrief/Si_AntiGrief.cs +++ b/Si_AntiGrief/Si_AntiGrief.cs @@ -35,7 +35,7 @@ You should have received a copy of the GNU General Public License using System.Linq; using UnityEngine; -[assembly: MelonInfo(typeof(AntiGrief), "Anti-Grief", "1.3.8", "databomb", "https://github.com/data-bomb/Silica")] +[assembly: MelonInfo(typeof(AntiGrief), "Anti-Grief", "1.4.0", "databomb", "https://github.com/data-bomb/Silica")] [assembly: MelonGame("Bohemia Interactive", "Silica")] [assembly: MelonOptionalDependencies("Admin Mod")] @@ -177,11 +177,6 @@ public void OnRequestEnterUnit(object? sender, OnRequestEnterUnitArgs args) return; } - if (!_BlockShrimpControllers.Value) - { - return; - } - Player player = args.Player; Unit unit = args.Unit; @@ -190,18 +185,29 @@ public void OnRequestEnterUnit(object? sender, OnRequestEnterUnitArgs args) return; } - // if the player isn't on the alien team, we can skip this check - if (player.Team.Index != (int)SiConstants.ETeam.Alien) + if (_BlockShrimpControllers.Value) { - return; + // if the player isn't on the alien team, we can skip this check + if (player.Team.Index != (int)SiConstants.ETeam.Alien) + { + return; + } + + // is it a shrimp? + if (unit.IsResourceHolder) + { + MelonLogger.Msg("Found " + player.PlayerName + " trying to use an Alien shrimp."); + HelperMethods.SendChatMessageToPlayer(player, HelperMethods.chatPrefix, " use of shrimp is not permitted on this server."); + args.Block = true; + return; + } } - // is it a shrimp? - if (unit.IsResourceHolder) + // check if the unit the player is in right now needs to get deleted + if (ShouldDeletePriorUnit(player)) { - MelonLogger.Msg("Found " + player.PlayerName + " trying to use an Alien shrimp."); - HelperMethods.SendChatMessageToPlayer(player, HelperMethods.chatPrefix, " use of shrimp is not permitted on this server."); - args.Block = true; + MelonLogger.Msg("Player " + player.PlayerName + " found switching from spawn unit. Taking action to prevent inf creatures."); + DeletePriorUnit(player, player.ControlledUnit); } } catch (Exception error) @@ -210,6 +216,31 @@ public void OnRequestEnterUnit(object? sender, OnRequestEnterUnitArgs args) } } + public static void DeletePriorUnit(Player player, Unit unit) + { + for (int i = NetworkComponent.NetworkComponents.Count - 1; i >= 0; i--) + { + NetworkComponent networkComponent = NetworkComponent.NetworkComponents[i]; + if (networkComponent != null && networkComponent.OwnerPlayer == player && networkComponent.IsValid && networkComponent == unit.NetworkComponent) + { + MelonLogger.Msg("Removing old unit (" + unit.name + ") from " + player.PlayerName + " before allowing transfer."); + UnityEngine.Object.Destroy(networkComponent.gameObject); + } + } + } + + public static bool ShouldDeletePriorUnit(Player player) + { + Unit? currentUnit = player.ControlledUnit; + Team? playerTeam = player.Team; + if (currentUnit == null || playerTeam == null) + { + return false; + } + + return GameMode.CurrentGameMode.GetCanDeletePlayerControlledUnit(player, currentUnit); + } + // hook DestroyAllUnitsForPlayer pre and override game code to prevent aliens from dying public void OnRoleChanged(object? sender, OnRoleChangedArgs args) {