From a48ebb3a5f78fc3cd0e9abcf024c9e9a84ff7fbc Mon Sep 17 00:00:00 2001 From: Kamron Batman <3953314+kamronbatman@users.noreply.github.com> Date: Sat, 15 Sep 2018 15:50:35 -0700 Subject: [PATCH] Fixes FindItemsByType and FindItemByType and new override of IsLockedDown and IsSecure by renaming their methods. --- Scripts/Commands/Generic/Commands/Commands.cs | 2 +- .../Generic/Extensions/BaseExtension.cs | 2 +- .../Generic/Extensions/LimitExtension.cs | 2 +- .../Generic/Extensions/SortExtension.cs | 2 +- .../Generic/Extensions/WhereExtension.cs | 2 +- .../ContainedCommandImplementor.cs | 62 +++-- Scripts/Commands/Properties.cs | 7 +- Scripts/Engines/ConPVP/Games/BombingRun.cs | 34 +-- Scripts/Engines/ConPVP/Games/CTF.cs | 23 +- Scripts/Engines/Craft/Core/CraftItem.cs | 26 +- Scripts/Engines/Factions/Core/Faction.cs | 101 +++----- Scripts/Engines/Factions/Items/Sigil.cs | 4 +- .../Factions/Mobiles/Guards/GuardAI.cs | 17 +- Scripts/Engines/Harvest/Fishing.cs | 24 +- .../MLQuests/Items/PrismaticCrystal.cs | 2 +- Scripts/Engines/MLQuests/Items/Teleporters.cs | 33 +-- Scripts/Engines/Plants/MainPlantGump.cs | 25 +- Scripts/Engines/Plants/PlantSystem.cs | 26 +- .../Quests/Dark Tides/DarkTidesQuest.cs | 2 +- .../EminosUndertakingQuest.cs | 4 +- .../Emino's Undertaking/Mobiles/Emino.cs | 2 +- .../Haochi's Trials/HaochisTrialsQuest.cs | 2 +- .../Quests/Haochi's Trials/Mobiles/Haochi.cs | 25 +- .../Uzeraan Turmoil/UzeraanTurmoilQuest.cs | 8 +- .../Halloween/2006/Engines/TrickOrTreat.cs | 7 +- Scripts/Items/Aquarium/Aquarium.cs | 24 +- Scripts/Items/Containers/Container.cs | 8 +- Scripts/Items/Containers/SalvageBag.cs | 27 +- Scripts/Items/Food/Beverage.cs | 2 +- Scripts/Items/Maps/TreasureMap.cs | 9 +- Scripts/Items/Misc/InteriorDecorator.cs | 2 +- Scripts/Items/Misc/PlayerBulletinBoards.cs | 8 +- Scripts/Items/Misc/Teleporter.cs | 3 +- .../Skill Items/Magical/Potions/BasePotion.cs | 5 +- .../Tailor Items/Dyetubs/DyeTub.cs | 2 +- .../Special/House Raffle/HouseRaffleRegion.cs | 9 +- .../Items/Special/Veteran Rewards/Cannon.cs | 17 +- .../Veteran Rewards/WeaponEngravingTool.cs | 88 ++++--- Scripts/Mobiles/AI/ArcherAI.cs | 2 +- Scripts/Mobiles/AI/ThiefAI.cs | 10 +- Scripts/Mobiles/PlayerMobile.cs | 80 +++--- Scripts/Mobiles/Townfolk/Banker.cs | 14 +- .../Mobiles/Vendors/NPC/RealEstateBroker.cs | 4 +- Scripts/Multis/BaseHouse.cs | 30 +-- Scripts/Multis/HousePlacementTool.cs | 15 +- Scripts/Regions/HouseRegion.cs | 6 +- Scripts/Skills/RemoveTrap.cs | 11 +- Scripts/Spells/Ninjitsu/AnimalForm.cs | 2 +- Scripts/Spells/Spellweaving/ArcanistSpell.cs | 4 +- Server/Items/Container.cs | 233 +++++++----------- 50 files changed, 416 insertions(+), 643 deletions(-) diff --git a/Scripts/Commands/Generic/Commands/Commands.cs b/Scripts/Commands/Generic/Commands/Commands.cs index cef65354f..6e008d91d 100644 --- a/Scripts/Commands/Generic/Commands/Commands.cs +++ b/Scripts/Commands/Generic/Commands/Commands.cs @@ -1108,7 +1108,7 @@ namespace Server.Commands.Generic } foreach (BaseHouse house in BaseHouse.AllHouses) - if (house.IsSecure(item) || house.IsLockedDown(item)) + if (house.HasSecureItem(item) || house.HasLockedDownItem(item)) { e.Mobile.SendGump(new PropertiesGump(e.Mobile, house)); return; diff --git a/Scripts/Commands/Generic/Extensions/BaseExtension.cs b/Scripts/Commands/Generic/Extensions/BaseExtension.cs index e481b2560..ab1a40976 100644 --- a/Scripts/Commands/Generic/Extensions/BaseExtension.cs +++ b/Scripts/Commands/Generic/Extensions/BaseExtension.cs @@ -82,7 +82,7 @@ namespace Server.Commands.Generic size = i; } - parsed.Sort(delegate(BaseExtension a, BaseExtension b) { return a.Order - b.Order; }); + parsed.Sort((a, b) => a.Order - b.Order); AssemblyEmitter emitter = null; diff --git a/Scripts/Commands/Generic/Extensions/LimitExtension.cs b/Scripts/Commands/Generic/Extensions/LimitExtension.cs index 9aa8eed91..37c88bc34 100644 --- a/Scripts/Commands/Generic/Extensions/LimitExtension.cs +++ b/Scripts/Commands/Generic/Extensions/LimitExtension.cs @@ -5,7 +5,7 @@ namespace Server.Commands.Generic { public sealed class LimitExtension : BaseExtension { - public static ExtensionInfo ExtInfo = new ExtensionInfo(80, "Limit", 1, delegate { return new LimitExtension(); }); + public static ExtensionInfo ExtInfo = new ExtensionInfo(80, "Limit", 1, () => new LimitExtension()); public override ExtensionInfo Info => ExtInfo; diff --git a/Scripts/Commands/Generic/Extensions/SortExtension.cs b/Scripts/Commands/Generic/Extensions/SortExtension.cs index d6fb3e604..1652f652f 100644 --- a/Scripts/Commands/Generic/Extensions/SortExtension.cs +++ b/Scripts/Commands/Generic/Extensions/SortExtension.cs @@ -6,7 +6,7 @@ namespace Server.Commands.Generic { public sealed class SortExtension : BaseExtension { - public static ExtensionInfo ExtInfo = new ExtensionInfo(40, "Order", -1, delegate { return new SortExtension(); }); + public static ExtensionInfo ExtInfo = new ExtensionInfo(40, "Order", -1, () => new SortExtension()); private IComparer m_Comparer; diff --git a/Scripts/Commands/Generic/Extensions/WhereExtension.cs b/Scripts/Commands/Generic/Extensions/WhereExtension.cs index f72b31236..1330fdd96 100644 --- a/Scripts/Commands/Generic/Extensions/WhereExtension.cs +++ b/Scripts/Commands/Generic/Extensions/WhereExtension.cs @@ -4,7 +4,7 @@ namespace Server.Commands.Generic { public sealed class WhereExtension : BaseExtension { - public static ExtensionInfo ExtInfo = new ExtensionInfo(20, "Where", -1, delegate { return new WhereExtension(); }); + public static ExtensionInfo ExtInfo = new ExtensionInfo(20, "Where", -1, () => new WhereExtension()); public override ExtensionInfo Info => ExtInfo; diff --git a/Scripts/Commands/Generic/Implementors/ContainedCommandImplementor.cs b/Scripts/Commands/Generic/Implementors/ContainedCommandImplementor.cs index 82a294cc2..234d12236 100644 --- a/Scripts/Commands/Generic/Implementors/ContainedCommandImplementor.cs +++ b/Scripts/Commands/Generic/Implementors/ContainedCommandImplementor.cs @@ -1,5 +1,7 @@ using System; using System.Collections; +using System.Collections.Generic; +using System.Linq; using Server.Items; using Server.Targeting; @@ -39,42 +41,36 @@ namespace Server.Commands.Generic if (command.ObjectTypes == ObjectTypes.Mobiles) return; // sanity check - if (!(targeted is Container)) + if (!(targeted is Container cont)) + { from.SendMessage("That is not a container."); - else - try + return; + } + + try + { + Extensions ext = Extensions.Parse(from, ref args); + + if (!CheckObjectTypes(from, command, ext, out bool items, out bool _)) + return; + + if (!items) { - Extensions ext = Extensions.Parse(from, ref args); - - bool items, mobiles; - - if (!CheckObjectTypes(from, command, ext, out items, out mobiles)) - return; - - if (!items) - { - from.SendMessage("This command only works on items."); - return; - } - - Container cont = (Container)targeted; - - Item[] found = cont.FindItemsByType(typeof(Item), true); - - ArrayList list = new ArrayList(); - - for (int i = 0; i < found.Length; ++i) - if (ext.IsValid(found[i])) - list.Add(found[i]); - - ext.Filter(list); - - RunCommand(from, list, command, args); - } - catch (Exception e) - { - from.SendMessage(e.Message); + from.SendMessage("This command only works on items."); + return; } + + List list = cont.FindItemsByType().Where(item => ext.IsValid(item)).ToList(); + + // TODO: Is there a way to avoid using ArrayList? + ext.Filter(new ArrayList(list)); + + RunCommand(from, list, command, args); + } + catch (Exception e) + { + from.SendMessage(e.Message); + } } } } \ No newline at end of file diff --git a/Scripts/Commands/Properties.cs b/Scripts/Commands/Properties.cs index f8580acef..f73ed389a 100644 --- a/Scripts/Commands/Properties.cs +++ b/Scripts/Commands/Properties.cs @@ -304,12 +304,7 @@ namespace Server.Commands return "The properties have been decreased."; } - private static string InternalGetValue(object o, PropertyInfo p) - { - return InternalGetValue(o, p, null); - } - - private static string InternalGetValue(object o, PropertyInfo p, PropertyInfo[] chain) + private static string InternalGetValue(object o, PropertyInfo p, PropertyInfo[] chain = null) { Type type = p.PropertyType; diff --git a/Scripts/Engines/ConPVP/Games/BombingRun.cs b/Scripts/Engines/ConPVP/Games/BombingRun.cs index 027e8c1b6..4e6727021 100644 --- a/Scripts/Engines/ConPVP/Games/BombingRun.cs +++ b/Scripts/Engines/ConPVP/Games/BombingRun.cs @@ -924,9 +924,7 @@ namespace Server.Engines.ConPVP else Hue = 0x84C; - if (m.Backpack.FindItemByType(typeof(BRBomb), true) is BRBomb b) - b.CheckScore(this, m, 7); - + m.Backpack.FindItemByType()?.CheckScore(this, m, 7); return true; } } @@ -1487,12 +1485,7 @@ namespace Server.Engines.ConPVP if (mob?.Backpack == null || GetTeamInfo(mob) == null) return false; - Item bomb = mob.Backpack.FindItemByType(typeof(BRBomb), true); - - if (bomb != null) - return true; - - return false; + return mob.Backpack.FindItemByType() != null; } public void ReturnBomb() @@ -1606,22 +1599,17 @@ namespace Server.Engines.ConPVP bool hadBomb = false; - Item[] bombs = corpse.FindItemsByType(typeof(BRBomb), false); - - for (int i = 0; i < bombs.Length; ++i) - (bombs[i] as BRBomb)?.DropTo(mob, killer); - - hadBomb = bombs.Length > 0; - - if (mob.Backpack != null) + corpse.FindItemsByType(false).ForEach(bomb => { - bombs = mob.Backpack.FindItemsByType(typeof(BRBomb), false); + hadBomb = true; + bomb.DropTo(mob, killer); + }); - for (int i = 0; i < bombs.Length; ++i) - (bombs[i] as BRBomb)?.DropTo(mob, killer); - - hadBomb = hadBomb || bombs.Length > 0; - } + mob.Backpack?.FindItemsByType(false).ForEach(bomb => + { + hadBomb = true; + bomb.DropTo(mob, killer); + }); if (killer != null && killer.Player) { diff --git a/Scripts/Engines/ConPVP/Games/CTF.cs b/Scripts/Engines/ConPVP/Games/CTF.cs index e32b46da2..2296b9ab3 100644 --- a/Scripts/Engines/ConPVP/Games/CTF.cs +++ b/Scripts/Engines/ConPVP/Games/CTF.cs @@ -965,22 +965,17 @@ namespace Server.Engines.ConPVP bool hadFlag = false; - Item[] flags = corpse.FindItemsByType(typeof(CTFFlag), false); - - for (int i = 0; i < flags.Length; ++i) - (flags[i] as CTFFlag).DropTo(mob, killer); - - hadFlag = hadFlag || flags.Length > 0; - - if (mob.Backpack != null) + corpse.FindItemsByType(false).ForEach(flag => { - flags = mob.Backpack.FindItemsByType(typeof(CTFFlag), false); + hadFlag = true; + flag.DropTo(mob, killer); + }); - for (int i = 0; i < flags.Length; ++i) - (flags[i] as CTFFlag).DropTo(mob, killer); - - hadFlag = hadFlag || flags.Length > 0; - } + mob.Backpack?.FindItemsByType(false).ForEach(flag => + { + hadFlag = true; + flag.DropTo(mob, killer); + }); if (killer != null && killer.Player) { diff --git a/Scripts/Engines/Craft/Core/CraftItem.cs b/Scripts/Engines/Craft/Core/CraftItem.cs index 4ac07323b..8ad9a8236 100644 --- a/Scripts/Engines/Craft/Core/CraftItem.cs +++ b/Scripts/Engines/Craft/Core/CraftItem.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Linq; using Server.Commands; using Server.Factions; using Server.Items; @@ -337,7 +338,7 @@ namespace Server.Engines.Craft for (int i = 0; i < types.Length; ++i) { - items[i] = cont.FindItemsByType(types[i], true); + items[i] = cont.FindItemsByType(types[i]); for (int j = 0; j < items[i].Length; ++j) if (!(items[i][j] is IHasQuantity hq)) @@ -405,7 +406,7 @@ namespace Server.Engines.Craft public int GetQuantity(Container cont, Type[] types) { - Item[] items = cont.FindItemsByType(types, true); + Item[] items = cont.FindItemsByType(types); int amount = 0; @@ -530,24 +531,12 @@ namespace Server.Engines.Craft else maxAmount = -1; - Item consumeExtra = null; + RecallRune consumeExtra = null; if (NameNumber == 1041267) { // Runebooks are a special case, they need a blank recall rune - - List runes = ourPack.FindItemsByType(); - - for (int i = 0; i < runes.Count; ++i) - { - RecallRune rune = runes[i]; - - if (rune != null && !rune.Marked) - { - consumeExtra = rune; - break; - } - } + consumeExtra = ourPack.FindItemsByType().Find(rune => !rune.Marked); if (consumeExtra == null) { @@ -556,7 +545,7 @@ namespace Server.Engines.Craft } } - int index = 0; + int index; // Consume ALL if (consumeType == ConsumeType.All) @@ -621,7 +610,8 @@ namespace Server.Engines.Craft if (index == -1) { - if (consumeType != ConsumeType.None) consumeExtra?.Delete(); + if (consumeType != ConsumeType.None) + consumeExtra?.Delete(); return true; } diff --git a/Scripts/Engines/Factions/Core/Faction.cs b/Scripts/Engines/Factions/Core/Faction.cs index a26eb905e..33623d81d 100644 --- a/Scripts/Engines/Factions/Core/Faction.cs +++ b/Scripts/Engines/Factions/Core/Faction.cs @@ -337,15 +337,9 @@ namespace Server.Factions int killPoints = pl.KillPoints; - if (mob.Backpack != null) - { - //Ordinarily, through normal faction removal, this will never find any sigils. - //Only with a leave delay less than the ReturnPeriod or a Faction Kick/Ban, will this ever do anything - Item[] sigils = mob.Backpack.FindItemsByType(typeof(Sigil)); - - for (int i = 0; i < sigils.Length; ++i) - ((Sigil)sigils[i]).ReturnHome(); - } + //Ordinarily, through normal faction removal, this will never find any sigils. + //Only with a leave delay less than the ReturnPeriod or a Faction Kick/Ban, will this ever do anything + mob.Backpack?.FindItemsByType().ForEach(sigil => sigil.ReturnHome()); if (pl.RankIndex != -1) { @@ -936,43 +930,32 @@ namespace Server.Factions killer = victim.FindMostRecentDamager(true); PlayerState killerState = PlayerState.Find(killer); - - Container pack = victim.Backpack; - - if (pack != null) + Container killerPack = killer?.Backpack; + victim.Backpack?.FindItemsByType().ForEach(sigil => { - Container killerPack = killer?.Backpack; - Item[] sigils = pack.FindItemsByType(typeof(Sigil)); - - for (int i = 0; i < sigils.Length; ++i) + if (killerState == null || killerPack == null) { - Sigil sigil = (Sigil)sigils[i]; - - if (killerState != null && killerPack != null) - { - if (killer.GetDistanceToSqrt(victim) > 64) - { - sigil.ReturnHome(); - killer.SendLocalizedMessage(1042230); // The sigil has gone back to its home location. - } - else if (Sigil.ExistsOn(killer)) - { - sigil.ReturnHome(); - killer.SendLocalizedMessage( - 1010258); // The sigil has gone back to its home location because you already have a sigil. - } - else if (!killerPack.TryDropItem(killer, sigil, false)) - { - sigil.ReturnHome(); - killer.SendLocalizedMessage(1010259); // The sigil has gone home because your backpack is full. - } - } - else - { - sigil.ReturnHome(); - } + sigil.ReturnHome(); + return; } - } + + if (killer.GetDistanceToSqrt(victim) > 64) + { + sigil.ReturnHome(); + killer.SendLocalizedMessage(1042230); // The sigil has gone back to its home location. + } + else if (Sigil.ExistsOn(killer)) + { + sigil.ReturnHome(); + killer.SendLocalizedMessage( + 1010258); // The sigil has gone back to its home location because you already have a sigil. + } + else if (!killerPack.TryDropItem(killer, sigil, false)) + { + sigil.ReturnHome(); + killer.SendLocalizedMessage(1010259); // The sigil has gone home because your backpack is full. + } + }); if (killerState == null) return; @@ -1072,9 +1055,7 @@ namespace Server.Factions if (1 > Utility.Random(3)) killerState.IsActive = true; - int silver = 0; - - silver = killerState.Faction.AwardSilver(killer, award * 40); + int silver = killerState.Faction.AwardSilver(killer, award * 40); if (silver > 0) killer.SendLocalizedMessage(1042736, @@ -1129,17 +1110,7 @@ namespace Server.Factions private static void EventSink_Logout(LogoutEventArgs e) { - Mobile mob = e.Mobile; - - Container pack = mob.Backpack; - - if (pack == null) - return; - - Item[] sigils = pack.FindItemsByType(typeof(Sigil)); - - for (int i = 0; i < sigils.Length; ++i) - ((Sigil)sigils[i]).ReturnHome(); + e.Mobile.Backpack?.FindItemsByType().ForEach(sigil => sigil.ReturnHome()); } private static void EventSink_Login(LoginEventArgs e) @@ -1166,17 +1137,7 @@ namespace Server.Factions return null; } - public static Faction Find(Mobile mob) - { - return Find(mob, false, false); - } - - public static Faction Find(Mobile mob, bool inherit) - { - return Find(mob, inherit, false); - } - - public static Faction Find(Mobile mob, bool inherit, bool creatureAllegiances) + public static Faction Find(Mobile mob, bool inherit = false, bool creatureAllegiances = false) { PlayerState pl = PlayerState.Find(mob); @@ -1186,9 +1147,9 @@ namespace Server.Factions if (inherit && mob is BaseCreature bc) { if (bc.Controlled) - return Find(bc.ControlMaster, false); + return Find(bc.ControlMaster); if (bc.Summoned) - return Find(bc.SummonMaster, false); + return Find(bc.SummonMaster); if (creatureAllegiances && bc is BaseFactionGuard guard) return guard.Faction; if (creatureAllegiances) diff --git a/Scripts/Engines/Factions/Items/Sigil.cs b/Scripts/Engines/Factions/Items/Sigil.cs index 3035de911..916ab3be2 100644 --- a/Scripts/Engines/Factions/Items/Sigil.cs +++ b/Scripts/Engines/Factions/Items/Sigil.cs @@ -223,9 +223,7 @@ namespace Server.Factions public static bool ExistsOn(Mobile mob) { - Container pack = mob.Backpack; - - return pack?.FindItemByType(typeof(Sigil)) != null; + return mob.Backpack?.FindItemByType() != null; } private void BeginCorrupting(Faction faction) diff --git a/Scripts/Engines/Factions/Mobiles/Guards/GuardAI.cs b/Scripts/Engines/Factions/Mobiles/Guards/GuardAI.cs index f8c45ffbb..0608b3e4e 100644 --- a/Scripts/Engines/Factions/Mobiles/Guards/GuardAI.cs +++ b/Scripts/Engines/Factions/Mobiles/Guards/GuardAI.cs @@ -168,25 +168,16 @@ namespace Server.Factions public bool EquipWeapon() { - Container pack = m_Guard.Backpack; - - Item weapon = pack?.FindItemByType(typeof(BaseWeapon)); - - if (weapon == null) - return false; - - return m_Guard.EquipItem(weapon); + Item weapon = m_Guard.Backpack?.FindItemByType(); + + return weapon != null && m_Guard.EquipItem(weapon); } public bool StartBandage() { m_Bandage = null; - Container pack = m_Guard.Backpack; - - Item bandage = pack?.FindItemByType(typeof(Bandage)); - - if (bandage == null) + if (m_Guard.Backpack?.FindItemByType() == null) return false; m_Bandage = BandageContext.BeginHeal(m_Guard, m_Guard); diff --git a/Scripts/Engines/Harvest/Fishing.cs b/Scripts/Engines/Harvest/Fishing.cs index 013f905db..e99c6e5c9 100644 --- a/Scripts/Engines/Harvest/Fishing.cs +++ b/Scripts/Engines/Harvest/Fishing.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Linq; using Server.Engines.Quests; using Server.Engines.Quests.Collector; using Server.Items; @@ -198,22 +199,9 @@ namespace Server.Engines.Harvest public override bool CheckResources(Mobile from, Item tool, HarvestDefinition def, Map map, Point3D loc, bool timed) { - Container pack = from.Backpack; - - if (pack != null) - { - List messages = pack.FindItemsByType(); - - for (int i = 0; i < messages.Count; ++i) - { - SOS sos = messages[i]; - - if ((from.Map == Map.Felucca || from.Map == Map.Trammel) && from.InRange(sos.TargetLocation, 60)) - return true; - } - } - - return base.CheckResources(from, tool, def, map, loc, timed); + return from?.Backpack?.FindItemsByType().Any(sos => + (from.Map == Map.Felucca || from.Map == Map.Trammel) && from.InRange(sos.TargetLocation, 60)) ?? + base.CheckResources(from, tool, def, map, loc, timed); } public override Item Construct(Type type, Mobile from) @@ -320,9 +308,7 @@ namespace Server.Engines.Harvest if (preLoot != null) { - if (preLoot is IShipwreckedItem shipwreckedItem) - shipwreckedItem.IsShipwreckedItem = true; - + ((IShipwreckedItem)preLoot).IsShipwreckedItem = true; return preLoot; } diff --git a/Scripts/Engines/MLQuests/Items/PrismaticCrystal.cs b/Scripts/Engines/MLQuests/Items/PrismaticCrystal.cs index 4756aae7e..0195acdff 100644 --- a/Scripts/Engines/MLQuests/Items/PrismaticCrystal.cs +++ b/Scripts/Engines/MLQuests/Items/PrismaticCrystal.cs @@ -28,7 +28,7 @@ namespace Server.Items if (pm.InRange(GetWorldLocation(), 2)) { if (MLQuestSystem.GetContext(pm)?.IsDoingQuest(typeof(UnfadingMemoriesPartOne)) == true && - pm.Backpack.FindItemByType(typeof(PrismaticAmber), false) == null) + pm.Backpack.FindItemByType(false) == null) { Item amber = new PrismaticAmber(); diff --git a/Scripts/Engines/MLQuests/Items/Teleporters.cs b/Scripts/Engines/MLQuests/Items/Teleporters.cs index 136f79c88..8ebc8b5e5 100644 --- a/Scripts/Engines/MLQuests/Items/Teleporters.cs +++ b/Scripts/Engines/MLQuests/Items/Teleporters.cs @@ -152,31 +152,20 @@ namespace Server.Engines.MLQuests.Items if (!base.CanTeleport(m)) return false; - if (m_TicketType != null) + if (m_TicketType == null) + return true; + + Container pack = m.Backpack; + Item ticket = pack?.FindItemByType(m_TicketType, false) ?? m.Items.Find(item => m_TicketType.IsInstanceOfType(item)); + + if (ticket == null) { - Item ticket = null; - Container pack = m.Backpack; - - if (pack != null) - ticket = pack.FindItemByType(m_TicketType, false); // Check (top level) backpack - - if (ticket == null) - foreach (Item item in m.Items) // Check paperdoll - if (m_TicketType.IsInstanceOfType(item)) - { - ticket = item; - break; - } - - if (ticket == null) - { - TextDefinition.SendMessageTo(m, Message); - return false; - } - - (ticket as ITicket)?.OnTicketUsed(m); + TextDefinition.SendMessageTo(m, Message); + return false; } + (ticket as ITicket)?.OnTicketUsed(m); + return true; } diff --git a/Scripts/Engines/Plants/MainPlantGump.cs b/Scripts/Engines/Plants/MainPlantGump.cs index 2beb81dea..b7e97ae40 100644 --- a/Scripts/Engines/Plants/MainPlantGump.cs +++ b/Scripts/Engines/Plants/MainPlantGump.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using Server.Gumps; using Server.Items; using Server.Network; @@ -310,30 +311,20 @@ namespace Server.Engines.Plants } case 6: // Water { - Item[] item = from.Backpack.FindItemsByType(typeof(BaseBeverage)); + BaseBeverage bev = from.Backpack.FindItemsByType().Find(beverage => + beverage.IsEmpty && beverage.Pourable && beverage.Content == BeverageType.Water); - bool foundUsableWater = false; - - if (item != null && item.Length > 0) - for (int i = 0; i < item.Length; ++i) - { - BaseBeverage beverage = (BaseBeverage)item[i]; - - if (!beverage.IsEmpty && beverage.Pourable && beverage.Content == BeverageType.Water) - { - foundUsableWater = true; - m_Plant.Pour(from, beverage); - break; - } - } - - if (!foundUsableWater) + if (bev == null) { from.Target = new PlantPourTarget(m_Plant); from.SendLocalizedMessage(1060808, "#" + m_Plant .GetLocalizedPlantStatus()); // Target the container you wish to use to water the ~1_val~. } + else + { + m_Plant.Pour(from, bev); + } from.SendGump(new MainPlantGump(m_Plant)); diff --git a/Scripts/Engines/Plants/PlantSystem.cs b/Scripts/Engines/Plants/PlantSystem.cs index bcedd984d..7e49a696d 100644 --- a/Scripts/Engines/Plants/PlantSystem.cs +++ b/Scripts/Engines/Plants/PlantSystem.cs @@ -413,25 +413,17 @@ namespace Server.Engines.Plants { Mobile from = args.Mobile; - if (from.Backpack != null) + from.Backpack?.FindItemsByType().ForEach(plant => { - List plants = from.Backpack.FindItemsByType(); + if (plant.IsGrowable) + plant.PlantSystem.DoGrowthCheck(); + }); - foreach (PlantItem plant in plants) - if (plant.IsGrowable) - plant.PlantSystem.DoGrowthCheck(); - } - - BankBox bank = from.FindBankNoCreate(); - - if (bank != null) + from.FindBankNoCreate()?.FindItemsByType().ForEach(plant => { - List plants = bank.FindItemsByType(); - - foreach (PlantItem plant in plants) - if (plant.IsGrowable) - plant.PlantSystem.DoGrowthCheck(); - } + if (plant.IsGrowable) + plant.PlantSystem.DoGrowthCheck(); + }); } public static void GrowAll() @@ -443,7 +435,7 @@ namespace Server.Engines.Plants { PlantItem plant = (PlantItem)plants[i]; - if (plant.IsGrowable && plant.RootParent as Mobile == null && now >= plant.PlantSystem.NextGrowth) + if (plant.IsGrowable && !(plant.RootParent is Mobile) && now >= plant.PlantSystem.NextGrowth) plant.PlantSystem.DoGrowthCheck(); } } diff --git a/Scripts/Engines/Quests/Dark Tides/DarkTidesQuest.cs b/Scripts/Engines/Quests/Dark Tides/DarkTidesQuest.cs index a40936e2d..8faecadfd 100644 --- a/Scripts/Engines/Quests/Dark Tides/DarkTidesQuest.cs +++ b/Scripts/Engines/Quests/Dark Tides/DarkTidesQuest.cs @@ -90,7 +90,7 @@ namespace Server.Engines.Quests.Necro if (qs.IsObjectiveInProgress(typeof(FindMardothAboutKronusObjective)) || qs.IsObjectiveInProgress(typeof(FindWellOfTearsObjective)) || qs.IsObjectiveInProgress(typeof(UseCallingScrollObjective))) - return from.Backpack?.FindItemByType(typeof(KronusScroll)) == null; + return from.Backpack?.FindItemByType() == null; return false; } diff --git a/Scripts/Engines/Quests/Emino's Undertaking/EminosUndertakingQuest.cs b/Scripts/Engines/Quests/Emino's Undertaking/EminosUndertakingQuest.cs index 79396cb2b..e254814d2 100644 --- a/Scripts/Engines/Quests/Emino's Undertaking/EminosUndertakingQuest.cs +++ b/Scripts/Engines/Quests/Emino's Undertaking/EminosUndertakingQuest.cs @@ -106,7 +106,7 @@ namespace Server.Engines.Quests.Ninja if (qs is EminosUndertakingQuest) if (qs.IsObjectiveInProgress(typeof(GiveZoelNoteObjective))) - return from.Backpack?.FindItemByType(typeof(NoteForZoel)) == null; + return from.Backpack?.FindItemByType() == null; return false; } @@ -120,7 +120,7 @@ namespace Server.Engines.Quests.Ninja if (qs is EminosUndertakingQuest) if (qs.IsObjectiveInProgress(typeof(GiveEminoSwordObjective))) - return from.Backpack?.FindItemByType(typeof(EminosKatana)) == null; + return from.Backpack?.FindItemByType() == null; return false; } diff --git a/Scripts/Engines/Quests/Emino's Undertaking/Mobiles/Emino.cs b/Scripts/Engines/Quests/Emino's Undertaking/Mobiles/Emino.cs index 40aae7830..c81675c92 100644 --- a/Scripts/Engines/Quests/Emino's Undertaking/Mobiles/Emino.cs +++ b/Scripts/Engines/Quests/Emino's Undertaking/Mobiles/Emino.cs @@ -147,7 +147,7 @@ namespace Server.Engines.Quests.Ninja Item katana = null; if (player.Backpack != null) - katana = player.Backpack.FindItemByType(typeof(EminosKatana)); + katana = player.Backpack.FindItemByType(); if (katana != null) { diff --git a/Scripts/Engines/Quests/Haochi's Trials/HaochisTrialsQuest.cs b/Scripts/Engines/Quests/Haochi's Trials/HaochisTrialsQuest.cs index 913126a66..8645ecb24 100644 --- a/Scripts/Engines/Quests/Haochi's Trials/HaochisTrialsQuest.cs +++ b/Scripts/Engines/Quests/Haochi's Trials/HaochisTrialsQuest.cs @@ -109,7 +109,7 @@ namespace Server.Engines.Quests.Samurai if (qs is HaochisTrialsQuest) if (qs.IsObjectiveInProgress(typeof(FifthTrialReturnObjective))) - return from.Backpack?.FindItemByType(typeof(HaochisKatana)) == null; + return from.Backpack?.FindItemByType() == null; return false; } diff --git a/Scripts/Engines/Quests/Haochi's Trials/Mobiles/Haochi.cs b/Scripts/Engines/Quests/Haochi's Trials/Mobiles/Haochi.cs index ae6d0b04a..ccd9ee459 100644 --- a/Scripts/Engines/Quests/Haochi's Trials/Mobiles/Haochi.cs +++ b/Scripts/Engines/Quests/Haochi's Trials/Mobiles/Haochi.cs @@ -117,21 +117,18 @@ namespace Server.Engines.Quests.Samurai if (obj != null && !obj.Completed) { - Container pack = player.Backpack; - Item katana = pack?.FindItemByType(typeof(HaochisKatana)); - if (katana != null) - { - katana.Delete(); - obj.Complete(); + HaochisKatana katana = player.Backpack?.FindItemByType(); + if (katana == null) + return; + + katana.Delete(); + obj.Complete(); - obj = qs.FindObjective(typeof(FifthTrialIntroObjective)); - if (obj != null && ((FifthTrialIntroObjective)obj).StolenTreasure) - qs.AddConversation(new SixthTrialIntroConversation(true)); - else - qs.AddConversation(new SixthTrialIntroConversation(false)); - } - - return; + obj = qs.FindObjective(typeof(FifthTrialIntroObjective)); + if (obj != null && ((FifthTrialIntroObjective)obj).StolenTreasure) + qs.AddConversation(new SixthTrialIntroConversation(true)); + else + qs.AddConversation(new SixthTrialIntroConversation(false)); } obj = qs.FindObjective(typeof(SixthTrialReturnObjective)); diff --git a/Scripts/Engines/Quests/Uzeraan Turmoil/UzeraanTurmoilQuest.cs b/Scripts/Engines/Quests/Uzeraan Turmoil/UzeraanTurmoilQuest.cs index 9b754f8e0..0f604e0bd 100644 --- a/Scripts/Engines/Quests/Uzeraan Turmoil/UzeraanTurmoilQuest.cs +++ b/Scripts/Engines/Quests/Uzeraan Turmoil/UzeraanTurmoilQuest.cs @@ -117,7 +117,7 @@ namespace Server.Engines.Quests.Haven if (qs is UzeraanTurmoilQuest) if (qs.IsObjectiveInProgress(typeof(ReturnScrollOfPowerObjective))) - return from.Backpack?.FindItemByType(typeof(SchmendrickScrollOfPower)) == null; + return from.Backpack?.FindItemByType() == null; return false; } @@ -131,7 +131,7 @@ namespace Server.Engines.Quests.Haven if (qs is UzeraanTurmoilQuest) if (qs.IsObjectiveInProgress(typeof(ReturnFertileDirtObjective))) - return from.Backpack?.FindItemByType(typeof(QuestFertileDirt)) == null; + return from.Backpack?.FindItemByType() == null; return false; } @@ -145,7 +145,7 @@ namespace Server.Engines.Quests.Haven if (qs is UzeraanTurmoilQuest) if (qs.IsObjectiveInProgress(typeof(ReturnDaemonBloodObjective))) - return from.Backpack?.FindItemByType(typeof(QuestDaemonBlood)) == null; + return from.Backpack?.FindItemByType() == null; return false; } @@ -159,7 +159,7 @@ namespace Server.Engines.Quests.Haven if (qs is UzeraanTurmoilQuest) if (qs.IsObjectiveInProgress(typeof(ReturnDaemonBoneObjective))) - return from.Backpack?.FindItemByType(typeof(QuestDaemonBone)) == null; + return from.Backpack?.FindItemByType() == null; return false; } diff --git a/Scripts/Holiday Stuff/Halloween/2006/Engines/TrickOrTreat.cs b/Scripts/Holiday Stuff/Halloween/2006/Engines/TrickOrTreat.cs index 6c3e7ee87..63fd02d25 100644 --- a/Scripts/Holiday Stuff/Halloween/2006/Engines/TrickOrTreat.cs +++ b/Scripts/Holiday Stuff/Halloween/2006/Engines/TrickOrTreat.cs @@ -273,12 +273,7 @@ namespace Server.Engines.Events { typeof(WrappedCandy), typeof(Lollipops), typeof(NougatSwirl), typeof(Taffy), typeof(JellyBeans) }; if (TrickOrTreat.CheckMobile(target)) - for (int i = 0; i < types.Length; i++) - { - Item item = target.Backpack.FindItemByType(types[i]); - - if (item != null) return item; - } + return target.Backpack.FindItemByType(types); return null; } diff --git a/Scripts/Items/Aquarium/Aquarium.cs b/Scripts/Items/Aquarium/Aquarium.cs index 7cc5b6895..f3df612f3 100644 --- a/Scripts/Items/Aquarium/Aquarium.cs +++ b/Scripts/Items/Aquarium/Aquarium.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Linq; using Server.ContextMenus; using Server.Multis; using Server.Network; @@ -503,11 +504,12 @@ namespace Server.Items private void RecountLiveCreatures() { LiveCreatures = 0; - List fish = FindItemsByType(); - foreach (BaseFish f in fish) - if (!f.Dead) + FindItemsByType().ForEach(fish => + { + if (!fish.Dead) ++LiveCreatures; + }); } public void Validate() @@ -895,21 +897,7 @@ namespace Server.Items public static FishBowl GetEmptyBowl(Mobile from) { - if (from?.Backpack == null) - return null; - - Item[] items = from.Backpack.FindItemsByType(typeof(FishBowl)); - - for (int i = 0; i < items.Length; i++) - if (items[i] is FishBowl) - { - FishBowl bowl = (FishBowl)items[i]; - - if (bowl.Empty) - return bowl; - } - - return null; + return from?.Backpack?.FindItemsByType().Find(bowl => bowl.Empty); } private static Type[] m_Decorations = diff --git a/Scripts/Items/Containers/Container.cs b/Scripts/Items/Containers/Container.cs index faf1b2f0c..0cc191977 100644 --- a/Scripts/Items/Containers/Container.cs +++ b/Scripts/Items/Containers/Container.cs @@ -64,10 +64,10 @@ namespace Server.Items BaseHouse house = BaseHouse.FindHouseAt(this); - if (house?.IsLockedDown(this) == true) + if (house?.HasLockedDownItem(this) == true) { if (dropped is VendorRentalContract || dropped is Container container && - container.FindItemByType(typeof(VendorRentalContract)) != null) + container.FindItemByType() != null) { from.SendLocalizedMessage(1062492); // You cannot place a rental contract in a locked down container. return false; @@ -99,10 +99,10 @@ namespace Server.Items BaseHouse house = BaseHouse.FindHouseAt(this); - if (house?.IsLockedDown(this) == true) + if (house?.HasLockedDownItem(this) == true) { if (item is VendorRentalContract || item is Container container && - container.FindItemByType(typeof(VendorRentalContract)) != null) + container.FindItemByType() != null) { from.SendLocalizedMessage(1062492); // You cannot place a rental contract in a locked down container. return false; diff --git a/Scripts/Items/Containers/SalvageBag.cs b/Scripts/Items/Containers/SalvageBag.cs index 7f8d8703b..4be389398 100644 --- a/Scripts/Items/Containers/SalvageBag.cs +++ b/Scripts/Items/Containers/SalvageBag.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Linq; using Server.ContextMenus; using Server.Engines.Craft; using Server.Network; @@ -174,14 +175,7 @@ namespace Server.Items private void SalvageIngots(Mobile from) { - Item[] tools = from.Backpack.FindItemsByType(typeof(BaseTool)); - - bool ToolFound = false; - foreach (Item tool in tools) - if (tool is BaseTool baseTool && baseTool.CraftSystem == DefBlacksmithy.CraftSystem) - ToolFound = true; - - if (!ToolFound) + if (from.Backpack.FindItemsByType().All(tool => tool.CraftSystem != DefBlacksmithy.CraftSystem)) { from.SendLocalizedMessage(1079822); // You need a blacksmithing tool in order to salvage ingots. return; @@ -229,7 +223,9 @@ namespace Server.Items private void SalvageCloth(Mobile from) { - if (!(from.Backpack.FindItemByType(typeof(Scissors)) is Scissors scissors)) + Scissors scissors = from.Backpack.FindItemByType(); + + if (scissors == null) { from.SendLocalizedMessage(1079823); // You need scissors in order to salvage cloth. return; @@ -257,11 +253,16 @@ namespace Server.Items from.SendLocalizedMessage(1079974, $"{salvaged}\t{salvaged + notSalvaged}"); // Salvaged: ~1_COUNT~/~2_NUM~ tailored items + + Item[] items = FindItemsByType(new[]{ + typeof(Leather), typeof(Cloth), typeof(SpinedLeather), typeof(HornedLeather), typeof(BarbedLeather), + typeof(Bandage), typeof(Bone) + }); - foreach (Item i in FindItemsByType(typeof(Item), true)) - if (i is Leather || i is Cloth || i is SpinedLeather || i is HornedLeather || i is BarbedLeather || - i is Bandage || i is Bone) - from.AddToBackpack(i); + for (int i = 0; i < items.Length; i++) + { + from.AddToBackpack(items[i]); + } } private void SalvageAll(Mobile from) diff --git a/Scripts/Items/Food/Beverage.cs b/Scripts/Items/Food/Beverage.cs index 51be7d48c..3d155aa55 100644 --- a/Scripts/Items/Food/Beverage.cs +++ b/Scripts/Items/Food/Beverage.cs @@ -697,7 +697,7 @@ namespace Server.Items { BaseHouse house = BaseHouse.FindHouseAt(this); - if (house == null || !house.IsLockedDown(this)) + if (house == null || !house.HasLockedDownItem(this)) { if (message) from.SendLocalizedMessage(502946, "", 0x59); // That belongs to someone else. diff --git a/Scripts/Items/Maps/TreasureMap.cs b/Scripts/Items/Maps/TreasureMap.cs index b33610756..05d271d06 100644 --- a/Scripts/Items/Maps/TreasureMap.cs +++ b/Scripts/Items/Maps/TreasureMap.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.IO; +using System.Linq; using Server.ContextMenus; using Server.Engines.Harvest; using Server.Mobiles; @@ -301,13 +302,7 @@ namespace Server.Items if (m.Backpack == null) return false; - List items = m.Backpack.FindItemsByType(); - - foreach (BaseHarvestTool tool in items) - if (tool.HarvestSystem == Mining.System) - return true; - - return false; + return m.Backpack.FindItemsByType().Any(tool => tool.HarvestSystem == Mining.System); } public void OnBeginDig(Mobile from) diff --git a/Scripts/Items/Misc/InteriorDecorator.cs b/Scripts/Items/Misc/InteriorDecorator.cs index d89053ebb..3bc18b5d9 100644 --- a/Scripts/Items/Misc/InteriorDecorator.cs +++ b/Scripts/Items/Misc/InteriorDecorator.cs @@ -212,7 +212,7 @@ namespace Server.Items { from.SendLocalizedMessage(1042270); // That is not in your house. } - else if (!house.IsLockedDown(item) && !house.IsSecure(item) && !isDecorableComponent) + else if (!house.HasLockedDownItem(item) && !house.HasSecureItem(item) && !isDecorableComponent) { if (item is AddonComponent && m_Decorator.Command == DecorateCommand.Up) from.SendLocalizedMessage(1042274); // You cannot raise it up any higher. diff --git a/Scripts/Items/Misc/PlayerBulletinBoards.cs b/Scripts/Items/Misc/PlayerBulletinBoards.cs index 97dcdd38c..b60970067 100644 --- a/Scripts/Items/Misc/PlayerBulletinBoards.cs +++ b/Scripts/Items/Misc/PlayerBulletinBoards.cs @@ -168,7 +168,7 @@ namespace Server.Items { BaseHouse house = BaseHouse.FindHouseAt( this ); - if ( house == null || !house.IsLockedDown( this ) ) + if ( house == null || !house.HasLockedDownItem( this ) ) from.SendLocalizedMessage( 1062396 ); // This bulletin board must be locked down in a house to be usable. else if ( !from.InRange( GetWorldLocation(), 2 ) || !from.InLOS( this ) ) from.LocalOverheadMessage( MessageType.Regular, 0x3B2, 1019045 ); // I can't reach that. @@ -202,7 +202,7 @@ namespace Server.Items BaseHouse house = m_House; BasePlayerBB board = m_Board; - if ( house == null || !house.IsLockedDown( board ) ) + if ( house == null || !house.HasLockedDownItem( board ) ) { from.SendLocalizedMessage( 1062396 ); // This bulletin board must be locked down in a house to be usable. return; @@ -278,7 +278,7 @@ namespace Server.Items BaseHouse house = m_House; BasePlayerBB board = m_Board; - if ( house == null || !house.IsLockedDown( board ) ) + if ( house == null || !house.HasLockedDownItem( board ) ) { from.SendLocalizedMessage( 1062396 ); // This bulletin board must be locked down in a house to be usable. return; @@ -369,7 +369,7 @@ namespace Server.Items BaseHouse house = m_House; BasePlayerBB board = m_Board; - if ( house == null || !house.IsLockedDown( board ) ) + if ( house == null || !house.HasLockedDownItem( board ) ) { from.SendLocalizedMessage( 1062396 ); // This bulletin board must be locked down in a house to be usable. return; diff --git a/Scripts/Items/Misc/Teleporter.cs b/Scripts/Items/Misc/Teleporter.cs index f26849eb9..c8efaff8f 100644 --- a/Scripts/Items/Misc/Teleporter.cs +++ b/Scripts/Items/Misc/Teleporter.cs @@ -1067,8 +1067,7 @@ namespace Server.Items } if (GetFlag(ConditionFlag.DenyPackEthereals) && - (pack.FindItemByType(typeof(EtherealMount)) != null || - pack.FindItemByType(typeof(BaseImprisonedMobile)) != null)) + pack.FindItemByType(new []{typeof(EtherealMount), typeof(BaseImprisonedMobile)}) != null) { m.SendMessage("You must empty your backpack of ethereal mounts before proceeding."); return false; diff --git a/Scripts/Items/Skill Items/Magical/Potions/BasePotion.cs b/Scripts/Items/Skill Items/Magical/Potions/BasePotion.cs index 34f4b6980..aca9c46c0 100644 --- a/Scripts/Items/Skill Items/Magical/Potions/BasePotion.cs +++ b/Scripts/Items/Skill Items/Magical/Potions/BasePotion.cs @@ -91,8 +91,9 @@ namespace Server.Items { PotionKeg keg = kegs[i]; - if (keg == null) - continue; + // Should never happen +// if (keg == null) +// continue; if (keg.Held <= 0 || keg.Held >= 100) continue; diff --git a/Scripts/Items/Skill Items/Tailor Items/Dyetubs/DyeTub.cs b/Scripts/Items/Skill Items/Tailor Items/Dyetubs/DyeTub.cs index 5f22a828f..598ee3e56 100644 --- a/Scripts/Items/Skill Items/Tailor Items/Dyetubs/DyeTub.cs +++ b/Scripts/Items/Skill Items/Tailor Items/Dyetubs/DyeTub.cs @@ -167,7 +167,7 @@ namespace Server.Items { BaseHouse house = BaseHouse.FindHouseAt(item); - if (house == null || !house.IsLockedDown(item) && !house.IsSecure(item)) + if (house == null || !house.HasLockedDownItem(item) && !house.HasSecureItem(item)) from.SendLocalizedMessage(501022); // Furniture must be locked down to paint it. else if (!house.IsCoOwner(from)) from.SendLocalizedMessage(501023); // You must be the owner to use this item. diff --git a/Scripts/Items/Special/House Raffle/HouseRaffleRegion.cs b/Scripts/Items/Special/House Raffle/HouseRaffleRegion.cs index da58c7c60..954427ce2 100644 --- a/Scripts/Items/Special/House Raffle/HouseRaffleRegion.cs +++ b/Scripts/Items/Special/House Raffle/HouseRaffleRegion.cs @@ -1,4 +1,5 @@ using System.Collections.Generic; +using System.Linq; using Server.Items; using Server.Spells.Sixth; using Server.Targeting; @@ -41,13 +42,7 @@ namespace Server.Regions private bool ContainsDeed(Container cont) { - List deeds = cont.FindItemsByType(); - - for (int i = 0; i < deeds.Count; ++i) - if (deeds[i] == m_Stone.Deed) - return true; - - return false; + return cont.FindItemsByType().Any(deed => deed == m_Stone.Deed); } public override bool OnTarget(Mobile m, Target t, object o) diff --git a/Scripts/Items/Special/Veteran Rewards/Cannon.cs b/Scripts/Items/Special/Veteran Rewards/Cannon.cs index 11215654f..3cc4cabaf 100644 --- a/Scripts/Items/Special/Veteran Rewards/Cannon.cs +++ b/Scripts/Items/Special/Veteran Rewards/Cannon.cs @@ -157,7 +157,7 @@ namespace Server.Items { if (from.Backpack != null) { - PotionKeg keg = from.Backpack.FindItemByType(typeof(PotionKeg)) as PotionKeg; + PotionKeg keg = from.Backpack.FindItemByType(); if (Validate(keg) > 0) from.SendGump(new InternalGump(this, keg)); @@ -175,17 +175,20 @@ namespace Server.Items public int Validate(PotionKeg keg) { - if (keg != null && !keg.Deleted && keg.Held == 100) + if (keg == null || keg.Deleted || keg.Held != 100) + return 0; + + switch (keg.Type) { - if (keg.Type == PotionEffect.ExplosionLesser) + case PotionEffect.ExplosionLesser: return 5; - if (keg.Type == PotionEffect.Explosion) + case PotionEffect.Explosion: return 10; - if (keg.Type == PotionEffect.ExplosionGreater) + case PotionEffect.ExplosionGreater: return 15; + default: + return 0; } - - return 0; } public void Fill(Mobile from, PotionKeg keg) diff --git a/Scripts/Items/Special/Veteran Rewards/WeaponEngravingTool.cs b/Scripts/Items/Special/Veteran Rewards/WeaponEngravingTool.cs index 4a600067d..c5f3c9422 100644 --- a/Scripts/Items/Special/Veteran Rewards/WeaponEngravingTool.cs +++ b/Scripts/Items/Special/Veteran Rewards/WeaponEngravingTool.cs @@ -84,9 +84,7 @@ namespace Server.Items } else { - Item diamond = from.Backpack.FindItemByType(typeof(BlueDiamond)); - - if (diamond != null) + if (from.Backpack.FindItemByType() != null) from.SendGump(new ConfirmGump(this, null)); else from.SendLocalizedMessage( @@ -130,71 +128,71 @@ namespace Server.Items public virtual void Recharge(Mobile from, Mobile guildmaster) { - if (from.Backpack != null) - { - Item diamond = from.Backpack.FindItemByType(typeof(BlueDiamond)); + if (from.Backpack == null) + return; + + BlueDiamond diamond = from.Backpack.FindItemByType(); - if (guildmaster != null) + if (guildmaster != null) + { + if (m_UsesRemaining <= 0) { - if (m_UsesRemaining <= 0) + if (diamond != null && Banker.Withdraw(from, 100000)) { - if (diamond != null && Banker.Withdraw(from, 100000)) - { - diamond.Consume(); - UsesRemaining = 10; - guildmaster.Say(1076165); // Your weapon engraver should be good as new! - } - else - { - guildmaster.Say( - 1076167); // You need a 100,000 gold and a blue diamond to recharge the weapon engraver. - } + diamond.Consume(); + UsesRemaining = 10; + guildmaster.Say(1076165); // Your weapon engraver should be good as new! } else { guildmaster.Say( - 1076164); // I can only help with this if you are carrying an engraving tool that needs repair. + 1076167); // You need a 100,000 gold and a blue diamond to recharge the weapon engraver. } } else { - if (from.Skills.Tinkering.Value == 0) - { - from.SendLocalizedMessage( - 1076179); // Since you have no tinkering skill, you will need to find an NPC tinkerer to repair this for you. - } - else if (from.Skills.Tinkering.Value < 75.0) - { - from.SendLocalizedMessage( - 1076178); // Your tinkering skill is too low to fix this yourself. An NPC tinkerer can help you repair this for a fee. - } - else if (diamond != null) - { - diamond.Consume(); + guildmaster.Say( + 1076164); // I can only help with this if you are carrying an engraving tool that needs repair. + } + } + else + { + if (from.Skills.Tinkering.Value == 0) + { + from.SendLocalizedMessage( + 1076179); // Since you have no tinkering skill, you will need to find an NPC tinkerer to repair this for you. + } + else if (from.Skills.Tinkering.Value < 75.0) + { + from.SendLocalizedMessage( + 1076178); // Your tinkering skill is too low to fix this yourself. An NPC tinkerer can help you repair this for a fee. + } + else if (diamond != null) + { + diamond.Consume(); - if (Utility.RandomDouble() < from.Skills.Tinkering.Value / 100) - { - UsesRemaining = 10; - from.SendLocalizedMessage(1076165); // Your weapon engraver should be good as new! ????? - } - else - { - from.SendLocalizedMessage( - 1076175); // You cracked the diamond attempting to fix the weapon engraver. - } + if (Utility.RandomDouble() < from.Skills.Tinkering.Value / 100) + { + UsesRemaining = 10; + from.SendLocalizedMessage(1076165); // Your weapon engraver should be good as new! ????? } else { from.SendLocalizedMessage( - 1076166); // You do not have a blue diamond needed to recharge the engraving tool. + 1076175); // You cracked the diamond attempting to fix the weapon engraver. } } + else + { + from.SendLocalizedMessage( + 1076166); // You do not have a blue diamond needed to recharge the engraving tool. + } } } public static WeaponEngravingTool Find(Mobile from) { - return from.Backpack?.FindItemByType(typeof(WeaponEngravingTool)) as WeaponEngravingTool; + return from.Backpack?.FindItemByType(); } private class TargetWeapon : Target diff --git a/Scripts/Mobiles/AI/ArcherAI.cs b/Scripts/Mobiles/AI/ArcherAI.cs index 3783ef2ff..b6842f62d 100644 --- a/Scripts/Mobiles/AI/ArcherAI.cs +++ b/Scripts/Mobiles/AI/ArcherAI.cs @@ -68,7 +68,7 @@ namespace Server.Mobiles // When we have no ammo, we flee Container pack = m_Mobile.Backpack; - if (pack?.FindItemByType(typeof(Arrow)) == null) + if (pack?.FindItemByType() == null) { Action = ActionType.Flee; return true; diff --git a/Scripts/Mobiles/AI/ThiefAI.cs b/Scripts/Mobiles/AI/ThiefAI.cs index b6547700f..7e6c6ca5c 100644 --- a/Scripts/Mobiles/AI/ThiefAI.cs +++ b/Scripts/Mobiles/AI/ThiefAI.cs @@ -81,7 +81,7 @@ namespace Server.Mobiles if (cpack != null) { - Item steala = cpack.FindItemByType(typeof(Bandage)); + Item steala = cpack.FindItemByType(); if (steala != null) { m_Mobile.DebugSay("Trying to steal from combatant."); @@ -89,7 +89,7 @@ namespace Server.Mobiles m_Mobile.Target?.Invoke(m_Mobile, steala); } - Item stealb = cpack.FindItemByType(typeof(Nightshade)); + Item stealb = cpack.FindItemByType(); if (stealb != null) { m_Mobile.DebugSay("Trying to steal from combatant."); @@ -97,7 +97,7 @@ namespace Server.Mobiles m_Mobile.Target?.Invoke(m_Mobile, stealb); } - Item stealc = cpack.FindItemByType(typeof(BlackPearl)); + Item stealc = cpack.FindItemByType(); if (stealc != null) { m_Mobile.DebugSay("Trying to steal from combatant."); @@ -105,14 +105,14 @@ namespace Server.Mobiles m_Mobile.Target?.Invoke(m_Mobile, stealc); } - Item steald = cpack.FindItemByType(typeof(MandrakeRoot)); + Item steald = cpack.FindItemByType(); if (steald != null) { m_Mobile.DebugSay("Trying to steal from combatant."); m_Mobile.UseSkill(SkillName.Stealing); m_Mobile.Target?.Invoke(m_Mobile, steald); } - else if (steala == null && stealb == null && stealc == null && steald == null) + else if (steala == null && stealb == null && stealc == null) { m_Mobile.DebugSay("I am going to flee from {0}", combatant.Name); diff --git a/Scripts/Mobiles/PlayerMobile.cs b/Scripts/Mobiles/PlayerMobile.cs index e6ff74c6a..0cec8bfe3 100644 --- a/Scripts/Mobiles/PlayerMobile.cs +++ b/Scripts/Mobiles/PlayerMobile.cs @@ -1778,9 +1778,7 @@ namespace Server.Mobiles if (Core.AOS && Backpack != null && !Backpack.Deleted) { - List ilist = Backpack.FindItemsByType(FindItems_Callback); - - for (int i = 0; i < ilist.Count; i++) Backpack.AddItem(ilist[i]); + Backpack.FindItemsByType(FindItems_Callback).ForEach(item => Backpack.AddItem(item)); } EquipSnapshot = new List(Items); @@ -1813,53 +1811,52 @@ namespace Server.Mobiles private bool CheckInsuranceOnDeath(Item item) { - if (InsuranceEnabled && item.Insured) + if (!InsuranceEnabled || !item.Insured) + return false; + + #region Dueling + + if (m_DuelPlayer != null && DuelContext != null && DuelContext.Registered && DuelContext.Started && + !m_DuelPlayer.Eliminated) + return true; + + #endregion + + if (AutoRenewInsurance) { - #region Dueling + int cost = GetInsuranceCost(item); - if (m_DuelPlayer != null && DuelContext != null && DuelContext.Registered && DuelContext.Started && - !m_DuelPlayer.Eliminated) - return true; + if (m_InsuranceAward != null) + cost /= 2; - #endregion - - if (AutoRenewInsurance) + if (Banker.Withdraw(this, cost)) { - int cost = GetInsuranceCost(item); - - if (m_InsuranceAward != null) - cost /= 2; - - if (Banker.Withdraw(this, cost)) - { - m_InsuranceCost += cost; - item.PaidInsurance = true; - SendLocalizedMessage(1060398, - cost.ToString()); // ~1_AMOUNT~ gold has been withdrawn from your bank box. - } - else - { - SendLocalizedMessage(1061079, "", 0x23); // You lack the funds to purchase the insurance - item.PaidInsurance = false; - item.Insured = false; - m_NonAutoreinsuredItems++; - } + m_InsuranceCost += cost; + item.PaidInsurance = true; + SendLocalizedMessage(1060398, + cost.ToString()); // ~1_AMOUNT~ gold has been withdrawn from your bank box. } else { + SendLocalizedMessage(1061079, "", 0x23); // You lack the funds to purchase the insurance item.PaidInsurance = false; item.Insured = false; + m_NonAutoreinsuredItems++; } - - if (m_InsuranceAward != null) - if (Banker.Deposit(m_InsuranceAward, 300)) - if (m_InsuranceAward is PlayerMobile mobile) - mobile.m_InsuranceBonus += 300; - - return true; + } + else + { + item.PaidInsurance = false; + item.Insured = false; } - return false; + if (m_InsuranceAward != null) + if (Banker.Deposit(m_InsuranceAward, 300)) + if (m_InsuranceAward is PlayerMobile mobile) + mobile.m_InsuranceBonus += 300; + + return true; + } public override DeathMoveResult GetParentMoveResultFor(Item item) @@ -2014,7 +2011,8 @@ namespace Server.Mobiles if (!buff.RetainThroughDeath) list.Add(buff); - for (int i = 0; i < list.Count; i++) RemoveBuff(list[i]); + for (int i = 0; i < list.Count; i++) + RemoveBuff(list[i]); } } @@ -2129,7 +2127,7 @@ namespace Server.Mobiles { Type type = talisman.Protection.Type; - if (type.IsAssignableFrom(from.GetType())) + if (type.IsInstanceOfType(from)) amount = (int)(amount * (1 - (double)talisman.Protection.Amount / 100)); } @@ -3532,7 +3530,7 @@ namespace Server.Mobiles Container pack = Backpack; if (pack != null) - items.AddRange(pack.FindItemsByType(true, DisplayInItemInsuranceGump)); + items.AddRange(pack.FindItemsByType(DisplayInItemInsuranceGump)); // TODO: Investigate item sorting diff --git a/Scripts/Mobiles/Townfolk/Banker.cs b/Scripts/Mobiles/Townfolk/Banker.cs index 9e3a01a3b..6edf3ac54 100644 --- a/Scripts/Mobiles/Townfolk/Banker.cs +++ b/Scripts/Mobiles/Townfolk/Banker.cs @@ -48,7 +48,8 @@ namespace Server.Mobiles List checks = bank.FindItemsByType(); balance += gold.Aggregate(0L, (c, t) => c + t.Amount); - if (balance >= int.MaxValue) return int.MaxValue; + if (balance >= int.MaxValue) + return int.MaxValue; balance += checks.Aggregate(0L, (c, t) => c + t.Worth); } @@ -94,8 +95,7 @@ namespace Server.Mobiles // If for whatever reason the TOL checks fail, we should still try old methods for withdrawing currency. if (AccountGold.Enabled && from.Account != null && from.Account.WithdrawGold(amount)) return true; - Item[] gold, checks; - int balance = GetBalance(from, out gold, out checks); + int balance = GetBalance(from, out Item[] gold, out Item[] checks); if (balance < amount) return false; @@ -278,11 +278,9 @@ namespace Server.Mobiles if (split.Length >= 2) { - int amount; - Container pack = e.Mobile.Backpack; - if (!int.TryParse(split[1], out amount)) + if (!int.TryParse(split[1], out int amount)) break; if (!Core.ML && amount > 5000 || Core.ML && amount > 60000) @@ -363,9 +361,7 @@ namespace Server.Mobiles if (split.Length >= 2) { - int amount; - - if (!int.TryParse(split[1], out amount)) + if (!int.TryParse(split[1], out int amount)) break; if (amount < 5000) diff --git a/Scripts/Mobiles/Vendors/NPC/RealEstateBroker.cs b/Scripts/Mobiles/Vendors/NPC/RealEstateBroker.cs index a988e1b08..2f9171cbc 100644 --- a/Scripts/Mobiles/Vendors/NPC/RealEstateBroker.cs +++ b/Scripts/Mobiles/Vendors/NPC/RealEstateBroker.cs @@ -41,9 +41,7 @@ namespace Server.Mobiles { m_NextCheckPack = DateTime.UtcNow + TimeSpan.FromSeconds(2.0); - Item deed = pack.FindItemByType(typeof(HouseDeed), false); - - if (deed != null) + if (pack.FindItemByType(false) != null) { // If you have a deed, I can appraise it or buy it from you... PrivateOverheadMessage(MessageType.Regular, 0x3B2, 500605, m.NetState); diff --git a/Scripts/Multis/BaseHouse.cs b/Scripts/Multis/BaseHouse.cs index 6d6fda708..cfcaeb003 100644 --- a/Scripts/Multis/BaseHouse.cs +++ b/Scripts/Multis/BaseHouse.cs @@ -1105,21 +1105,21 @@ namespace Server.Multis { BaseHouse house = FindHouseAt(item); - return house != null && house.IsLockedDown(item); + return house != null && house.HasLockedDownItem(item); } public static bool CheckSecured(Item item) { BaseHouse house = FindHouseAt(item); - return house != null && house.IsSecure(item); + return house != null && house.HasSecureItem(item); } public static bool CheckLockedDownOrSecured(Item item) { BaseHouse house = FindHouseAt(item); - return house != null && (house.IsSecure(item) || house.IsLockedDown(item)); + return house != null && (house.HasSecureItem(item) || house.HasLockedDownItem(item)); } public static List GetHouses(Mobile m) @@ -1151,7 +1151,7 @@ namespace Server.Multis if (house == null || !house.IsAosRules) return true; - if (house.IsSecure(cont) && !house.CheckAosStorage(1 + item.TotalItems + plusItems)) + if (house.HasSecureItem(cont) && !house.CheckAosStorage(1 + item.TotalItems + plusItems)) { if (message) m.SendLocalizedMessage(1061839); // This action would exceed the secure storage limit of the house. @@ -1181,7 +1181,7 @@ namespace Server.Multis case SecureAccessResult.Inaccessible: return false; } - if (house.IsLockedDown(item)) + if (house.HasLockedDownItem(item)) return house.IsCoOwner(m) && item is Container; return true; @@ -1248,7 +1248,7 @@ namespace Server.Multis case SecureAccessResult.Inaccessible: return false; } - if (!IsLockedDown(item)) + if (!HasLockedDownItem(item)) return true; if (from.AccessLevel >= AccessLevel.GameMaster) return true; @@ -1662,7 +1662,7 @@ namespace Server.Multis if (!IsCoOwner(m) || !IsActive) return false; - if (item is BaseAddonContainer || item.Movable && !IsSecure(item)) + if (item is BaseAddonContainer || item.Movable && !HasSecureItem(item)) { int amt = 1 + item.TotalItems; @@ -1681,11 +1681,11 @@ namespace Server.Multis { m.SendLocalizedMessage(1005377); //You cannot lock that down } - else if (IsSecure(rootItem)) + else if (HasSecureItem(rootItem)) { m.SendLocalizedMessage(501737); // You need not lock down items in a secure container. } - else if (parentItem != null && !IsLockedDown(parentItem)) + else if (parentItem != null && !HasLockedDownItem(parentItem)) { m.SendLocalizedMessage(501736); // You must lockdown the container first! } @@ -1879,7 +1879,7 @@ namespace Server.Multis if (!IsCoOwner(m) || !IsActive) return; - if (IsLockedDown(item)) + if (HasLockedDownItem(item)) { item.PublicOverheadMessage(MessageType.Label, 0x3B2, 501657); //[no longer locked down] SetLockdown(item, false); @@ -1887,7 +1887,7 @@ namespace Server.Multis (item as RewardBrazier)?.TurnOff(); } - else if (IsSecure(item)) + else if (HasSecureItem(item)) { ReleaseSecure(m, item); } @@ -1906,7 +1906,7 @@ namespace Server.Multis { m.SendLocalizedMessage(1005525); // That is not in your house } - else if (IsLockedDown(item)) + else if (HasLockedDownItem(item)) { m.SendLocalizedMessage(1010550); // This is already locked down and cannot be secured. } @@ -3117,7 +3117,7 @@ namespace Server.Multis return false; } - public bool IsLockedDown(Item check) + public bool HasLockedDownItem(Item check) { if (check == null) return false; @@ -3128,7 +3128,7 @@ namespace Server.Multis return LockDowns.Contains(check) || VendorRentalContracts.Contains(check); } - public bool IsSecure(Item item) + public bool HasSecureItem(Item item) { if (item == null) return false; @@ -3772,7 +3772,7 @@ namespace Server.Multis isOwned = house is HouseFoundation && ((HouseFoundation)house).IsFixture(item); if (!isOwned) - isOwned = house.IsLockedDown(item); + isOwned = house.HasLockedDownItem(item); if (isOwned) sec = (ISecurable)item; diff --git a/Scripts/Multis/HousePlacementTool.cs b/Scripts/Multis/HousePlacementTool.cs index 82b481170..168a9c114 100644 --- a/Scripts/Multis/HousePlacementTool.cs +++ b/Scripts/Multis/HousePlacementTool.cs @@ -87,7 +87,7 @@ namespace Server.Items public override void OnResponse(NetState sender, RelayInfo info) { - if (!m_From.CheckAlive() || m_From.Backpack?.FindItemByType(typeof(HousePlacementTool)) == null) + if (!m_From.CheckAlive() || m_From.Backpack?.FindItemByType() == null) return; switch (info.ButtonID) @@ -194,7 +194,7 @@ namespace Server.Items public override void OnResponse(NetState sender, RelayInfo info) { - if (!m_From.CheckAlive() || m_From.Backpack?.FindItemByType(typeof(HousePlacementTool)) == null) + if (!m_From.CheckAlive() || m_From.Backpack?.FindItemByType() == null) return; int index = info.ButtonID - 1; @@ -231,7 +231,7 @@ namespace Server.Items protected override void OnTarget(Mobile from, object o) { - if (!from.CheckAlive() || from.Backpack?.FindItemByType(typeof(HousePlacementTool)) == null) + if (!from.CheckAlive() || from.Backpack?.FindItemByType() == null) return; IPoint3D ip = o as IPoint3D; @@ -262,7 +262,7 @@ namespace Server.Items protected override void OnTargetFinish(Mobile from) { - if (!from.CheckAlive() || from.Backpack?.FindItemByType(typeof(HousePlacementTool)) == null) + if (!from.CheckAlive() || from.Backpack?.FindItemByType() == null) return; if (!m_Placed) @@ -580,7 +580,7 @@ namespace Server.Items public void PlacementWarning_Callback(Mobile from, bool okay, object state) { - if (!from.CheckAlive() || from.Backpack?.FindItemByType(typeof(HousePlacementTool)) == null) + if (!from.CheckAlive() || from.Backpack?.FindItemByType() == null) return; PreviewHouse prevHouse = (PreviewHouse)state; @@ -700,12 +700,11 @@ namespace Server.Items public bool OnPlacement(Mobile from, Point3D p) { - if (!from.CheckAlive() || from.Backpack?.FindItemByType(typeof(HousePlacementTool)) == null) + if (!from.CheckAlive() || from.Backpack?.FindItemByType() == null) return false; - ArrayList toMove; Point3D center = new Point3D(p.X - Offset.X, p.Y - Offset.Y, p.Z - Offset.Z); - HousePlacementResult res = HousePlacement.Check(from, MultiID, center, out toMove); + HousePlacementResult res = HousePlacement.Check(from, MultiID, center, out ArrayList toMove); switch (res) { diff --git a/Scripts/Regions/HouseRegion.cs b/Scripts/Regions/HouseRegion.cs index 9a823dd6e..0ef94749c 100644 --- a/Scripts/Regions/HouseRegion.cs +++ b/Scripts/Regions/HouseRegion.cs @@ -189,7 +189,7 @@ namespace Server.Regions public override bool OnDecay(Item item) { - if ((House.IsLockedDown(item) || House.IsSecure(item)) && House.IsInside(item)) + if ((House.HasLockedDownItem(item) || House.HasSecureItem(item)) && House.IsInside(item)) return false; return base.OnDecay(item); } @@ -387,9 +387,9 @@ namespace Server.Regions { Item item = (Item)o; - if (House.IsLockedDown(item)) + if (House.HasLockedDownItem(item)) item.LabelTo(from, 501643); // [locked down] - else if (House.IsSecure(item)) + else if (House.HasSecureItem(item)) item.LabelTo(from, 501644); // [locked down & secure] } diff --git a/Scripts/Skills/RemoveTrap.cs b/Scripts/Skills/RemoveTrap.cs index d188ee105..e62b01d05 100644 --- a/Scripts/Skills/RemoveTrap.cs +++ b/Scripts/Skills/RemoveTrap.cs @@ -71,13 +71,10 @@ namespace Server.SkillHandlers from.SendLocalizedMessage(502372); // You fail to disarm the trap... but you don't set it off } } - else if (targeted is BaseFactionTrap) + else if (targeted is BaseFactionTrap trap) { - BaseFactionTrap trap = (BaseFactionTrap)targeted; Faction faction = Faction.Find(from); - - FactionTrapRemovalKit kit = - from.Backpack?.FindItemByType(typeof(FactionTrapRemovalKit)) as FactionTrapRemovalKit; + FactionTrapRemovalKit kit = from.Backpack?.FindItemByType(); bool isOwner = trap.Placer == from || trap.Faction != null && trap.Faction.IsCommander(from); @@ -121,12 +118,12 @@ namespace Server.SkillHandlers } if (!isOwner) - kit?.ConsumeCharge(from); + kit.ConsumeCharge(from); } } else { - from.SendLocalizedMessage(502373); // That does'nt appear to be trapped + from.SendLocalizedMessage(502373); // That doesn't appear to be trapped } } } diff --git a/Scripts/Spells/Ninjitsu/AnimalForm.cs b/Scripts/Spells/Ninjitsu/AnimalForm.cs index a8bf9b784..51b72991f 100644 --- a/Scripts/Spells/Ninjitsu/AnimalForm.cs +++ b/Scripts/Spells/Ninjitsu/AnimalForm.cs @@ -569,7 +569,7 @@ namespace Server.Spells.Ninjitsu { if (m_Mobile.Hits < m_Mobile.HitsMax && m_Mobile.Backpack != null) { - Bandage b = m_Mobile.Backpack.FindItemByType(typeof(Bandage)) as Bandage; + Bandage b = m_Mobile.Backpack.FindItemByType(); if (b != null) { diff --git a/Scripts/Spells/Spellweaving/ArcanistSpell.cs b/Scripts/Spells/Spellweaving/ArcanistSpell.cs index 0ed776797..ca12db952 100644 --- a/Scripts/Spells/Spellweaving/ArcanistSpell.cs +++ b/Scripts/Spells/Spellweaving/ArcanistSpell.cs @@ -38,8 +38,8 @@ namespace Server.Spells.Spellweaving if (from?.Backpack == null) return null; - if (from.Holding is ArcaneFocus) - return (ArcaneFocus)from.Holding; + if (from.Holding is ArcaneFocus focus) + return focus; return from.Backpack.FindItemByType(); } diff --git a/Server/Items/Container.cs b/Server/Items/Container.cs index 1db59536f..de2b2c000 100644 --- a/Server/Items/Container.cs +++ b/Server/Items/Container.cs @@ -1292,7 +1292,7 @@ namespace Server.Items } else if (recurse && item is Container) { - RecurseConsumeUpTo(item, type, amount, recurse, ref consumed, toDelete); + RecurseConsumeUpTo(item, type, amount, true, ref consumed, toDelete); } } } @@ -1459,12 +1459,7 @@ namespace Server.Items return best; } - public int GetAmount(Type type) - { - return GetAmount(type, true); - } - - public int GetAmount(Type type, bool recurse) + public int GetAmount(Type type, bool recurse = true) { Item[] items = FindItemsByType(type, recurse); @@ -1476,12 +1471,7 @@ namespace Server.Items return amount; } - public int GetAmount(Type[] types) - { - return GetAmount(types, true); - } - - public int GetAmount(Type[] types, bool recurse) + public int GetAmount(Type[] types, bool recurse = true) { Item[] items = FindItemsByType(types, recurse); @@ -1509,29 +1499,24 @@ namespace Server.Items private static void RecurseFindItemsByType(Item current, Type type, bool recurse, List list) { - if (current != null && current.Items.Count > 0) + if (current == null || current.Items.Count == 0) + return; + + List items = current.Items; + + for (int i = 0; i < items.Count; ++i) { - List items = current.Items; + Item item = items[i]; - for (int i = 0; i < items.Count; ++i) - { - Item item = items[i]; + if (type.IsInstanceOfType(item)) + list.Add(item); - if (type.IsAssignableFrom(item.GetType())) // item.GetType().IsAssignableFrom( type ) ) - list.Add(item); - - if (recurse && item is Container) - RecurseFindItemsByType(item, type, recurse, list); - } + if (recurse && item is Container) + RecurseFindItemsByType(item, type, true, list); } } - public Item[] FindItemsByType(Type[] types) - { - return FindItemsByType(types, true); - } - - public Item[] FindItemsByType(Type[] types, bool recurse) + public Item[] FindItemsByType(Type[] types, bool recurse = true) { if (m_FindItemsList.Count > 0) m_FindItemsList.Clear(); @@ -1543,87 +1528,78 @@ namespace Server.Items private static void RecurseFindItemsByType(Item current, Type[] types, bool recurse, List list) { - if (current != null && current.Items.Count > 0) + if (current == null || current.Items.Count == 0) + return; + + List items = current.Items; + + for (int i = 0; i < items.Count; ++i) { - List items = current.Items; + Item item = items[i]; - for (int i = 0; i < items.Count; ++i) - { - Item item = items[i]; + if (InTypeList(item, types)) + list.Add(item); - if (InTypeList(item, types)) - list.Add(item); - - if (recurse && item is Container) - RecurseFindItemsByType(item, types, recurse, list); - } + if (recurse && item is Container) + RecurseFindItemsByType(item, types, true, list); } } - public Item FindItemByType(Type type) - { - return FindItemByType(type, true); - } - - public Item FindItemByType(Type type, bool recurse) + public Item FindItemByType(Type type, bool recurse = true) { return RecurseFindItemByType(this, type, recurse); } private static Item RecurseFindItemByType(Item current, Type type, bool recurse) { - if (current != null && current.Items.Count > 0) + if (current == null || current.Items.Count == 0) + return null; + + List list = current.Items; + + for (int i = 0; i < list.Count; ++i) { - List list = current.Items; + Item item = list[i]; - for (int i = 0; i < list.Count; ++i) + if (type.IsInstanceOfType(item)) + return item; + + if (recurse && item is Container) { - Item item = list[i]; + Item check = RecurseFindItemByType(item, type, true); - if (type.IsAssignableFrom(item.GetType())) return item; - - if (recurse && item is Container) - { - Item check = RecurseFindItemByType(item, type, recurse); - - if (check != null) - return check; - } + if (check != null) + return check; } } return null; } - public Item FindItemByType(Type[] types) - { - return FindItemByType(types, true); - } - - public Item FindItemByType(Type[] types, bool recurse) + public Item FindItemByType(Type[] types, bool recurse = true) { return RecurseFindItemByType(this, types, recurse); } private static Item RecurseFindItemByType(Item current, Type[] types, bool recurse) { - if (current != null && current.Items.Count > 0) + if (current == null || current.Items.Count <= 0) + return null; + + List list = current.Items; + + for (int i = 0; i < list.Count; ++i) { - List list = current.Items; + Item item = list[i]; - for (int i = 0; i < list.Count; ++i) + if (InTypeList(item, types)) return item; + + if (recurse && item is Container) { - Item item = list[i]; + Item check = RecurseFindItemByType(item, types, true); - if (InTypeList(item, types)) return item; - - if (recurse && item is Container) - { - Item check = RecurseFindItemByType(item, types, recurse); - - if (check != null) - return check; - } + if (check != null) + return check; } } @@ -1634,109 +1610,74 @@ namespace Server.Items #region Generic FindItem[s] by Type - public List FindItemsByType() where T : Item - { - return FindItemsByType(true, null); - } - - public List FindItemsByType(bool recurse) where T : Item - { - return FindItemsByType(recurse, null); - } - public List FindItemsByType(Predicate predicate) where T : Item { return FindItemsByType(true, predicate); } - public List FindItemsByType(bool recurse, Predicate predicate) where T : Item + public List FindItemsByType(bool recurse = true, Predicate predicate = null) where T : Item { - if (m_FindItemsList.Count > 0) - m_FindItemsList.Clear(); - List list = new List(); - RecurseFindItemsByType(this, recurse, list, predicate); - + return list; } private static void RecurseFindItemsByType(Item current, bool recurse, List list, Predicate predicate) where T : Item { - if (current != null && current.Items.Count > 0) + if (current == null || current.Items.Count == 0) + return; + + List items = current.Items; + + for (int i = 0; i < items.Count; ++i) { - List items = current.Items; + Item item = items[i]; - for (int i = 0; i < items.Count; ++i) + if (item is T typedItem) { - Item item = items[i]; - - if (typeof(T).IsAssignableFrom(item.GetType())) - { - T typedItem = (T)item; - - if (predicate == null || predicate(typedItem)) - list.Add(typedItem); - } - - if (recurse && item is Container) - RecurseFindItemsByType(item, recurse, list, predicate); + if (predicate?.Invoke(typedItem) == true) + list.Add(typedItem); } + + if (recurse && item is Container) + RecurseFindItemsByType(item, true, list, predicate); } } - public T FindItemByType() where T : Item - { - return FindItemByType(true); - } - - - public T FindItemByType(Predicate predicate) where T : Item - { - return FindItemByType(true, predicate); - } - - public T FindItemByType(bool recurse) where T : Item - { - return FindItemByType(recurse, null); - } - - public T FindItemByType(bool recurse, Predicate predicate) where T : Item + public T FindItemByType(bool recurse = true, Predicate predicate = null) where T : Item { return RecurseFindItemByType(this, recurse, predicate); } private static T RecurseFindItemByType(Item current, bool recurse, Predicate predicate) where T : Item { - if (current != null && current.Items.Count > 0) + if (current == null || current.Items.Count == 0) + return null; + + List list = current.Items; + + for (int i = 0; i < list.Count; ++i) { - List list = current.Items; + Item item = list[i]; - for (int i = 0; i < list.Count; ++i) + if (item is T typedItem) { - Item item = list[i]; + if (predicate?.Invoke(typedItem) == true) + return typedItem; + } + else if (recurse && item is Container) + { + T check = RecurseFindItemByType(item, true, predicate); - if (typeof(T).IsAssignableFrom(item.GetType())) - { - T typedItem = (T)item; - - if (predicate == null || predicate(typedItem)) - return typedItem; - } - else if (recurse && item is Container) - { - T check = RecurseFindItemByType(item, recurse, predicate); - - if (check != null) - return check; - } + if (check != null) + return check; } } return null; } - #endregion }