From 8d88ef70fd8ea60608a0de17e11b0c76be62bece Mon Sep 17 00:00:00 2001 From: Kamron Batman <3953314+kamronbatman@users.noreply.github.com> Date: Sun, 19 Jul 2026 09:26:27 -0700 Subject: [PATCH] fix: Eliminates double lookup with Contains->Remove (#2539) --- .../Engines/Factions/Core/FactionItem.cs | 5 +--- .../UOContent/Engines/Factions/Core/Town.cs | 24 ++----------------- .../Factions/Items/Traps/BaseFactionTrap.cs | 8 ++----- Projects/UOContent/Engines/Party/Party.cs | 3 +-- .../UOContent/Engines/Party/PartyCommands.cs | 2 +- Projects/UOContent/Gumps/AdminGump.cs | 13 ++++------ Projects/UOContent/Gumps/Base/Legacy/Gump.cs | 7 ++---- Projects/UOContent/Items/Guilds/Guildstone.cs | 4 +--- Projects/UOContent/Misc/Guild.cs | 11 +++------ Projects/UOContent/Misc/Notoriety.cs | 13 ++-------- .../Mobiles/Vendors/NPC/AnimalTrainer.cs | 3 ++- Projects/UOContent/Multis/Houses/BaseHouse.cs | 12 +++------- 12 files changed, 25 insertions(+), 80 deletions(-) diff --git a/Projects/UOContent/Engines/Factions/Core/FactionItem.cs b/Projects/UOContent/Engines/Factions/Core/FactionItem.cs index ad874e936..42b3adc00 100644 --- a/Projects/UOContent/Engines/Factions/Core/FactionItem.cs +++ b/Projects/UOContent/Engines/Factions/Core/FactionItem.cs @@ -87,10 +87,7 @@ public class FactionItem item.FactionItemState = null; } - if (Faction?.State.FactionItems.Contains(this) == true) - { - Faction.State.FactionItems.Remove(this); - } + Faction?.State.FactionItems.Remove(this); } public void Serialize(IGenericWriter writer) diff --git a/Projects/UOContent/Engines/Factions/Core/Town.cs b/Projects/UOContent/Engines/Factions/Core/Town.cs index 2dcca8c78..8aa136ff6 100644 --- a/Projects/UOContent/Engines/Factions/Core/Town.cs +++ b/Projects/UOContent/Engines/Factions/Core/Town.cs @@ -350,28 +350,8 @@ public abstract class Town : IComparable return true; } - public bool UnregisterGuard(BaseFactionGuard guard) - { - if (guard == null) - { - return false; - } - - var guardList = FindGuardList(guard.GetType()); - - if (guardList == null) - { - return false; - } - - if (!guardList.Guards.Contains(guard)) - { - return false; - } - - guardList.Guards.Remove(guard); - return true; - } + public bool UnregisterGuard(BaseFactionGuard guard) => + guard != null && FindGuardList(guard.GetType())?.Guards.Remove(guard) == true; public bool RegisterVendor(BaseFactionVendor vendor) { diff --git a/Projects/UOContent/Engines/Factions/Items/Traps/BaseFactionTrap.cs b/Projects/UOContent/Engines/Factions/Items/Traps/BaseFactionTrap.cs index 17b77d69c..932489008 100644 --- a/Projects/UOContent/Engines/Factions/Items/Traps/BaseFactionTrap.cs +++ b/Projects/UOContent/Engines/Factions/Items/Traps/BaseFactionTrap.cs @@ -252,11 +252,7 @@ public abstract class BaseFactionTrap : BaseTrap public override void OnDelete() { - if (Faction?.Traps.Contains(this) == true) - { - Faction.Traps.Remove(this); - } - + Faction?.Traps.Remove(this); base.OnDelete(); } @@ -286,4 +282,4 @@ public abstract class BaseFactionTrap : BaseTrap return faction != Faction; } -} \ No newline at end of file +} diff --git a/Projects/UOContent/Engines/Party/Party.cs b/Projects/UOContent/Engines/Party/Party.cs index 21c69c797..ab8d4e762 100644 --- a/Projects/UOContent/Engines/Party/Party.cs +++ b/Projects/UOContent/Engines/Party/Party.cs @@ -123,9 +123,8 @@ namespace Server.Engines.PartySystem { from.SendMessage("They are not in a party."); } - else if (p.m_Listeners.Contains(from)) + else if (p.m_Listeners.Remove(from)) { - p.m_Listeners.Remove(from); from.SendMessage("You are no longer listening to that party."); } else diff --git a/Projects/UOContent/Engines/Party/PartyCommands.cs b/Projects/UOContent/Engines/Party/PartyCommands.cs index ae249f963..520a07a0f 100644 --- a/Projects/UOContent/Engines/Party/PartyCommands.cs +++ b/Projects/UOContent/Engines/Party/PartyCommands.cs @@ -40,7 +40,7 @@ namespace Server.Engines.PartySystem from.SendLocalizedMessage(1005455); // Who would you like to remove from your party? from.Target = new RemovePartyTarget(); } - else if ((p.Leader == from || from == target) && p.Contains(target)) + else if (p.Leader == from || from == target) { p.Remove(target); } diff --git a/Projects/UOContent/Gumps/AdminGump.cs b/Projects/UOContent/Gumps/AdminGump.cs index df72593c3..6e7c3ac23 100644 --- a/Projects/UOContent/Gumps/AdminGump.cs +++ b/Projects/UOContent/Gumps/AdminGump.cs @@ -1865,17 +1865,14 @@ namespace Server.Gumps { var acct = (Account)m_List[v]; - if (info.IsSwitched(v)) - { - if (!rads.Contains(acct)) - { - rads.Add(acct); - } - } - else if (rads.Contains(acct)) + if (!info.IsSwitched(v)) { rads.Remove(acct); } + else if (!rads.Contains(acct)) + { + rads.Add(acct); + } } } } diff --git a/Projects/UOContent/Gumps/Base/Legacy/Gump.cs b/Projects/UOContent/Gumps/Base/Legacy/Gump.cs index 27144daa8..5cfb1f14a 100644 --- a/Projects/UOContent/Gumps/Base/Legacy/Gump.cs +++ b/Projects/UOContent/Gumps/Base/Legacy/Gump.cs @@ -226,13 +226,10 @@ public class Gump : BaseGump public void Remove(GumpEntry g) { - if (g == null || !Entries.Contains(g)) + if (g != null && Entries.Remove(g)) { - return; + g.Parent = null; } - - Entries.Remove(g); - g.Parent = null; } public int Intern(string value) diff --git a/Projects/UOContent/Items/Guilds/Guildstone.cs b/Projects/UOContent/Items/Guilds/Guildstone.cs index f4ff4720d..3ae67d00c 100644 --- a/Projects/UOContent/Items/Guilds/Guildstone.cs +++ b/Projects/UOContent/Items/Guilds/Guildstone.cs @@ -54,15 +54,13 @@ public partial class Guildstone : Item, IAddon, IChoppable var house = BaseHouse.FindHouseAt(this); - if (house?.IsOwner(from) == true && house.Addons.Contains(this)) + if (house?.IsOwner(from) == true && house.Addons.Remove(this)) { Effects.PlaySound(GetWorldLocation(), Map, 0x3B3); from.SendLocalizedMessage(500461); // You destroy the item. Delete(); - house.Addons.Remove(this); - var deed = Deed; if (deed != null) diff --git a/Projects/UOContent/Misc/Guild.cs b/Projects/UOContent/Misc/Guild.cs index faf9914ab..88ec44773 100644 --- a/Projects/UOContent/Misc/Guild.cs +++ b/Projects/UOContent/Misc/Guild.cs @@ -191,7 +191,7 @@ namespace Server.Guilds public void TurnToMember(Guild g) { - if (g.Alliance != this || !m_PendingMembers.Contains(g) || m_Members.Contains(g)) + if (g.Alliance != this || m_Members.Contains(g) || !m_PendingMembers.Remove(g)) { return; } @@ -199,21 +199,16 @@ namespace Server.Guilds g.GuildMessage(1070760, Name); // Your Guild has joined the ~1_ALLIANCENAME~ Alliance. AllianceMessage(1070761, g.Name); // A new Guild has joined your Alliance: ~1_GUILDNAME~ - m_PendingMembers.Remove(g); m_Members.Add(g); g.Alliance.InvalidateMemberProperties(); } public void RemoveGuild(Guild g) { - if (m_PendingMembers.Contains(g)) - { - m_PendingMembers.Remove(g); - } + m_PendingMembers.Remove(g); - if (m_Members.Contains(g)) // Sanity, just incase someone with a custom script adds a character to BOTH arrays + if (m_Members.Remove(g)) // Sanity, just incase someone with a custom script adds a character to BOTH arrays { - m_Members.Remove(g); g.InvalidateMemberProperties(); g.GuildMessage(1070763, Name); // Your Guild has been removed from the ~1_ALLIANCENAME~ Alliance. diff --git a/Projects/UOContent/Misc/Notoriety.cs b/Projects/UOContent/Misc/Notoriety.cs index df70639c0..cb9b1ddcb 100644 --- a/Projects/UOContent/Misc/Notoriety.cs +++ b/Projects/UOContent/Misc/Notoriety.cs @@ -477,17 +477,8 @@ namespace Server.Misc return Notoriety.Enemy; } - if (Stealing.ClassicMode && pmTarg?.PermaFlags.Contains(source) == true) - { - return Notoriety.CanBeAttacked; - } - - if (bcTarg?.AlwaysAttackable == true) - { - return Notoriety.CanBeAttacked; - } - - if (CheckHouseFlag(source, target, target.Location, target.Map)) + if (Stealing.ClassicMode && pmTarg?.PermaFlags.Contains(source) == true || + bcTarg?.AlwaysAttackable == true || CheckHouseFlag(source, target, target.Location, target.Map)) { return Notoriety.CanBeAttacked; } diff --git a/Projects/UOContent/Mobiles/Vendors/NPC/AnimalTrainer.cs b/Projects/UOContent/Mobiles/Vendors/NPC/AnimalTrainer.cs index 64ee7d45c..847209869 100644 --- a/Projects/UOContent/Mobiles/Vendors/NPC/AnimalTrainer.cs +++ b/Projects/UOContent/Mobiles/Vendors/NPC/AnimalTrainer.cs @@ -152,7 +152,8 @@ namespace Server.Mobiles public void EndClaimList(Mobile from, BaseCreature pet) { - if (pet?.Deleted != false || from.Map != Map || from is not PlayerMobile pm || pm.Stabled?.Contains(pet) != true || !from.CheckAlive()) + if (pet?.Deleted != false || from.Map != Map || from is not PlayerMobile pm + || pm.Stabled?.Contains(pet) != true || !from.CheckAlive()) { return; } diff --git a/Projects/UOContent/Multis/Houses/BaseHouse.cs b/Projects/UOContent/Multis/Houses/BaseHouse.cs index aea0cb8bc..19e9c020c 100644 --- a/Projects/UOContent/Multis/Houses/BaseHouse.cs +++ b/Projects/UOContent/Multis/Houses/BaseHouse.cs @@ -2642,10 +2642,8 @@ namespace Server.Multis return; } - if (Access.Contains(targ)) + if (Access.Remove(targ)) { - Access.Remove(targ); - if (!HasAccess(targ) && IsInside(targ)) { targ.Location = BanLocation; @@ -2800,10 +2798,8 @@ namespace Server.Multis return; } - if (CoOwners.Contains(targ)) + if (CoOwners.Remove(targ)) { - CoOwners.Remove(targ); - targ.Delta(MobileDelta.Noto); from.SendLocalizedMessage(501299); // Co-owner removed from list. @@ -2872,10 +2868,8 @@ namespace Server.Multis return; } - if (Friends.Contains(targ)) + if (Friends.Remove(targ)) { - Friends.Remove(targ); - targ.Delta(MobileDelta.Noto); from.SendLocalizedMessage(501298); // Friend removed from list.