Fixes dictionary uses (#7)

This commit is contained in:
Kamron Batman 2018-11-02 08:16:42 -07:00 committed by GitHub
parent d4a52344f2
commit 916d404c51
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
161 changed files with 1211 additions and 1722 deletions

View file

@ -953,10 +953,7 @@ namespace Server.Engines.CannedEvil
if (from == null || !from.Player)
return;
if (m_DamageEntries.ContainsKey(from))
m_DamageEntries[from] += amount;
else
m_DamageEntries.Add(from, amount);
m_DamageEntries[from] = amount + (m_DamageEntries.TryGetValue(from, out int value) ? value : 0);
}
public void AwardArtifact(Item artifact)
@ -1073,12 +1070,10 @@ namespace Server.Engines.CannedEvil
case 5:
{
int entries = reader.ReadInt();
Mobile m;
int damage;
for (int i = 0; i < entries; ++i)
{
m = reader.ReadMobile();
damage = reader.ReadInt();
Mobile m = reader.ReadMobile();
int damage = reader.ReadInt();
if (m == null)
continue;
@ -1243,4 +1238,4 @@ namespace Server.Engines.CannedEvil
}
}
}
}
}

View file

@ -117,9 +117,7 @@ namespace Server.Engines.ConPVP
public static void BeginIgnore(Mobile source, Mobile toIgnore)
{
List<IgnoreEntry> list = m_IgnoreLists[source];
if (list == null)
if (!m_IgnoreLists.TryGetValue(source, out List<IgnoreEntry> list))
m_IgnoreLists[source] = list = new List<IgnoreEntry>();
for (int i = 0; i < list.Count; ++i)
@ -141,9 +139,7 @@ namespace Server.Engines.ConPVP
public static bool IsIgnored(Mobile source, Mobile check)
{
List<IgnoreEntry> list = m_IgnoreLists[source];
if (list == null)
if (!m_IgnoreLists.TryGetValue(source, out List<IgnoreEntry> list))
return false;
for (int i = 0; i < list.Count; ++i)
@ -280,4 +276,4 @@ namespace Server.Engines.ConPVP
}
}
}
}
}

View file

@ -1247,7 +1247,7 @@ namespace Server.Engines.ConPVP
if (mob == null)
return null;
if (!(Players[mob] is BRPlayerInfo val))
if (!Players.TryGetValue(mob, out BRPlayerInfo val))
Players[mob] = val = new BRPlayerInfo(this, mob);
return val;

View file

@ -604,7 +604,7 @@ namespace Server.Engines.ConPVP
if (mob == null)
return null;
if (!(Players[mob] is KHPlayerInfo val))
if (!Players.TryGetValue(mob, out KHPlayerInfo val))
Players[mob] = val = new KHPlayerInfo(this, mob);
return val;

View file

@ -269,9 +269,7 @@ namespace Server.Engines.ConPVP
public LadderEntry Find(Mobile mob)
{
LadderEntry entry = m_Table[mob];
if (entry == null)
if (m_Table.TryGetValue(mob, out LadderEntry entry))
{
m_Table[mob] = entry = new LadderEntry(mob, this);
entry.Index = Entries.Count;
@ -283,7 +281,8 @@ namespace Server.Engines.ConPVP
public LadderEntry FindNoCreate(Mobile mob)
{
return m_Table[mob];
m_Table.TryGetValue(mob, out LadderEntry entry);
return entry;
}
public void Serialize(GenericWriter writer)
@ -364,4 +363,4 @@ namespace Server.Engines.ConPVP
writer.WriteEncodedInt(Losses);
}
}
}
}

View file

@ -107,9 +107,7 @@ namespace Server.Engines.ConPVP
public PreferencesEntry Find(Mobile mob)
{
PreferencesEntry entry = m_Table[mob];
if (entry == null)
if (m_Table.TryGetValue(mob, out PreferencesEntry entry))
{
m_Table[mob] = entry = new PreferencesEntry(mob);
Entries.Add(entry);
@ -276,4 +274,4 @@ namespace Server.Engines.ConPVP
m_ColumnX += width;
}
}
}
}

View file

@ -110,50 +110,50 @@ namespace Server.Engines.Craft
public static int ItemIDOf(Type type)
{
if (!_itemIds.TryGetValue(type, out int itemId))
if (_itemIds.TryGetValue(type, out int itemId))
return itemId;
if (type == typeof(FactionExplosionTrap))
itemId = 14034;
else if (type == typeof(FactionGasTrap))
itemId = 4523;
else if (type == typeof(FactionSawTrap))
itemId = 4359;
else if (type == typeof(FactionSpikeTrap)) itemId = 4517;
if (itemId == 0)
{
if (type == typeof(FactionExplosionTrap))
itemId = 14034;
else if (type == typeof(FactionGasTrap))
itemId = 4523;
else if (type == typeof(FactionSawTrap))
itemId = 4359;
else if (type == typeof(FactionSpikeTrap)) itemId = 4517;
object[] attrs = type.GetCustomAttributes(typeof(CraftItemIDAttribute), false);
if (itemId == 0)
if (attrs.Length > 0)
{
object[] attrs = type.GetCustomAttributes(typeof(CraftItemIDAttribute), false);
if (attrs.Length > 0)
{
CraftItemIDAttribute craftItemID = (CraftItemIDAttribute)attrs[0];
itemId = craftItemID.ItemID;
}
CraftItemIDAttribute craftItemID = (CraftItemIDAttribute)attrs[0];
itemId = craftItemID.ItemID;
}
if (itemId == 0)
{
Item item = null;
try
{
item = Activator.CreateInstance(type) as Item;
}
catch
{
// ignored
}
if (item != null)
{
itemId = item.ItemID;
item.Delete();
}
}
_itemIds[type] = itemId;
}
if (itemId == 0)
{
Item item = null;
try
{
item = Activator.CreateInstance(type) as Item;
}
catch
{
// ignored
}
if (item != null)
{
itemId = item.ItemID;
item.Delete();
}
}
_itemIds[type] = itemId;
return itemId;
}
@ -1248,4 +1248,4 @@ namespace Server.Engines.Craft
#endregion
}
}
}

View file

@ -81,9 +81,7 @@ namespace Server.Engines.Craft
return null;
}
m_ContextTable.TryGetValue(m, out CraftContext c);
if (c == null)
if (!m_ContextTable.TryGetValue(m, out CraftContext c))
m_ContextTable[m] = c = new CraftContext();
return c;
@ -91,9 +89,7 @@ namespace Server.Engines.Craft
public void OnMade(Mobile m, CraftItem item)
{
CraftContext c = GetContext(m);
c?.OnMade(item);
GetContext(m)?.OnMade(item);
}
public virtual bool ConsumeOnFailure(Mobile from, Type resourceType, CraftItem craftItem)
@ -104,8 +100,8 @@ namespace Server.Engines.Craft
public void CreateItem(Mobile from, Type type, Type typeRes, BaseTool tool, CraftItem realCraftItem)
{
// Verify if the type is in the list of the craftable item
CraftItem craftItem = CraftItems.SearchFor(type);
if (craftItem != null) realCraftItem.Craft(from, this, typeRes, tool);
if (CraftItems.SearchFor(type) != null)
realCraftItem.Craft(from, this, typeRes, tool);
}
public int RandomRecipe()
@ -359,4 +355,4 @@ namespace Server.Engines.Craft
public abstract int CanCraft(Mobile from, BaseTool tool, Type itemType);
}
}
}

View file

@ -5,11 +5,6 @@ namespace Server.Engines.Harvest
{
public class HarvestDefinition
{
public HarvestDefinition()
{
Banks = new Dictionary<Map, Dictionary<Point2D, HarvestBank>>();
}
public int BankWidth{ get; set; }
public int BankHeight{ get; set; }
@ -70,7 +65,8 @@ namespace Server.Engines.Harvest
public bool RandomizeVeins{ get; set; }
public Dictionary<Map, Dictionary<Point2D, HarvestBank>> Banks{ get; set; }
public Dictionary<Map, Dictionary<Point2D, HarvestBank>> Banks{ get; }
= new Dictionary<Map, Dictionary<Point2D, HarvestBank>>();
public void SendMessageTo(Mobile from, object message)
{
@ -88,15 +84,12 @@ namespace Server.Engines.Harvest
x /= BankWidth;
y /= BankHeight;
Banks.TryGetValue(map, out Dictionary<Point2D, HarvestBank> banks);
if (banks == null)
if (!Banks.TryGetValue(map, out Dictionary<Point2D, HarvestBank> banks))
Banks[map] = banks = new Dictionary<Point2D, HarvestBank>();
Point2D key = new Point2D(x, y);
banks.TryGetValue(key, out HarvestBank bank);
if (bank == null)
if (!banks.TryGetValue(key, out HarvestBank bank))
banks[key] = bank = new HarvestBank(this, GetVeinAt(map, x, y));
return bank;
@ -178,4 +171,4 @@ namespace Server.Engines.Harvest
return dist == 0;
}
}
}
}

View file

@ -199,9 +199,7 @@ namespace Server.Engines.Help
[Description("Opens the page queue menu.")]
private static void Pages_OnCommand(CommandEventArgs e)
{
PageEntry entry = (PageEntry)m_KeyedByHandler[e.Mobile];
if (entry != null)
if (m_KeyedByHandler.TryGetValue(e.Mobile, out PageEntry entry))
e.Mobile.SendGump(new PageEntryGump(e.Mobile, entry));
else if (List.Count > 0)
e.Mobile.SendGump(new PageQueueGump());
@ -224,11 +222,6 @@ namespace Server.Engines.Help
return List.IndexOf(e);
}
public static void Cancel(Mobile sender)
{
Remove((PageEntry)m_KeyedBySender[sender]);
}
public static void Remove(PageEntry e)
{
if (e == null)
@ -245,7 +238,8 @@ namespace Server.Engines.Help
public static PageEntry GetEntry(Mobile sender)
{
return (PageEntry)m_KeyedBySender[sender];
m_KeyedBySender.TryGetValue(sender, out PageEntry entry);
return entry;
}
public static void Remove(Mobile sender)
@ -285,9 +279,10 @@ namespace Server.Engines.Help
Mobile sender = entry.Sender;
DateTime time = DateTime.UtcNow;
MailMessage mail = new MailMessage(Email.FromAddress, Email.SpeechLogPageAddresses);
mail.Subject = "RunUO Speech Log Page Forwarding";
MailMessage mail = new MailMessage(Email.FromAddress, Email.SpeechLogPageAddresses)
{
Subject = "RunUO Speech Log Page Forwarding"
};
using (StringWriter writer = new StringWriter())
{

View file

@ -108,7 +108,7 @@ namespace Server.Engines.MLQuests.Gumps
private static void Timeout(NetState ns)
{
if (m_Pending.ContainsKey(ns))
if (IsPending(ns))
{
m_Pending.Remove(ns);
ns.Send(CloseRaceChanger.Instance);
@ -339,4 +339,4 @@ namespace Server.Engines.MLQuests.Gumps
}
#endregion
}
}

View file

@ -26,12 +26,7 @@ namespace Server.Engines.MLQuests
object[] attributes = t.GetCustomAttributes(m_Type, false);
if (attributes.Length != 0)
result = ((QuesterNameAttribute)attributes[0]).QuesterName;
else
result = t.Name;
return m_Cache[t] = result;
return m_Cache[t] = attributes.Length != 0 ? ((QuesterNameAttribute)attributes[0]).QuesterName : t.Name;
}
}
}
}

View file

@ -17,8 +17,7 @@ namespace Server.Engines.PartySystem
public static void Start(Mobile m, Mobile leader)
{
DeclineTimer t = m_Table[m];
m_Table.TryGetValue(m, out DeclineTimer t);
t?.Stop();
m_Table[m] = t = new DeclineTimer(m, leader);

View file

@ -87,12 +87,13 @@ namespace Server.Items
private static OrangePetalsContext GetContext(Mobile m)
{
return m_Table[m] as OrangePetalsContext;
m_Table.TryGetValue(m, out OrangePetalsContext context);
return context;
}
public static bool UnderEffect(Mobile m)
{
return GetContext(m) != null;
return m_Table.ContainsKey(m);
}
public override void Serialize(GenericWriter writer)

View file

@ -43,27 +43,29 @@ namespace Server.Engines.Plants
static PlantHueInfo()
{
m_Table = new Dictionary<PlantHue, PlantHueInfo>();
m_Table = new Dictionary<PlantHue, PlantHueInfo>
{
[PlantHue.Plain] = new PlantHueInfo(0, 1060813, PlantHue.Plain, 0x835),
[PlantHue.Red] = new PlantHueInfo(0x66D, 1060814, PlantHue.Red, 0x24),
[PlantHue.Blue] = new PlantHueInfo(0x53D, 1060815, PlantHue.Blue, 0x6),
[PlantHue.Yellow] = new PlantHueInfo(0x8A5, 1060818, PlantHue.Yellow, 0x38),
[PlantHue.BrightRed] = new PlantHueInfo(0x21, 1060814, PlantHue.BrightRed, 0x21),
[PlantHue.BrightBlue] = new PlantHueInfo(0x5, 1060815, PlantHue.BrightBlue, 0x6),
[PlantHue.BrightYellow] = new PlantHueInfo(0x38, 1060818, PlantHue.BrightYellow, 0x35),
[PlantHue.Purple] = new PlantHueInfo(0xD, 1060816, PlantHue.Purple, 0x10),
[PlantHue.Green] = new PlantHueInfo(0x59B, 1060819, PlantHue.Green, 0x42),
[PlantHue.Orange] = new PlantHueInfo(0x46F, 1060817, PlantHue.Orange, 0x2E),
[PlantHue.BrightPurple] = new PlantHueInfo(0x10, 1060816, PlantHue.BrightPurple, 0xD),
[PlantHue.BrightGreen] = new PlantHueInfo(0x42, 1060819, PlantHue.BrightGreen, 0x3F),
[PlantHue.BrightOrange] = new PlantHueInfo(0x2B, 1060817, PlantHue.BrightOrange, 0x2B),
[PlantHue.Black] = new PlantHueInfo(0x455, 1060820, PlantHue.Black, 0),
[PlantHue.White] = new PlantHueInfo(0x481, 1060821, PlantHue.White, 0x481),
[PlantHue.Pink] = new PlantHueInfo(0x48E, 1061854, PlantHue.Pink),
[PlantHue.Magenta] = new PlantHueInfo(0x486, 1061852, PlantHue.Magenta),
[PlantHue.Aqua] = new PlantHueInfo(0x495, 1061853, PlantHue.Aqua),
[PlantHue.FireRed] = new PlantHueInfo(0x489, 1061855, PlantHue.FireRed)
};
m_Table[PlantHue.Plain] = new PlantHueInfo(0, 1060813, PlantHue.Plain, 0x835);
m_Table[PlantHue.Red] = new PlantHueInfo(0x66D, 1060814, PlantHue.Red, 0x24);
m_Table[PlantHue.Blue] = new PlantHueInfo(0x53D, 1060815, PlantHue.Blue, 0x6);
m_Table[PlantHue.Yellow] = new PlantHueInfo(0x8A5, 1060818, PlantHue.Yellow, 0x38);
m_Table[PlantHue.BrightRed] = new PlantHueInfo(0x21, 1060814, PlantHue.BrightRed, 0x21);
m_Table[PlantHue.BrightBlue] = new PlantHueInfo(0x5, 1060815, PlantHue.BrightBlue, 0x6);
m_Table[PlantHue.BrightYellow] = new PlantHueInfo(0x38, 1060818, PlantHue.BrightYellow, 0x35);
m_Table[PlantHue.Purple] = new PlantHueInfo(0xD, 1060816, PlantHue.Purple, 0x10);
m_Table[PlantHue.Green] = new PlantHueInfo(0x59B, 1060819, PlantHue.Green, 0x42);
m_Table[PlantHue.Orange] = new PlantHueInfo(0x46F, 1060817, PlantHue.Orange, 0x2E);
m_Table[PlantHue.BrightPurple] = new PlantHueInfo(0x10, 1060816, PlantHue.BrightPurple, 0xD);
m_Table[PlantHue.BrightGreen] = new PlantHueInfo(0x42, 1060819, PlantHue.BrightGreen, 0x3F);
m_Table[PlantHue.BrightOrange] = new PlantHueInfo(0x2B, 1060817, PlantHue.BrightOrange, 0x2B);
m_Table[PlantHue.Black] = new PlantHueInfo(0x455, 1060820, PlantHue.Black, 0);
m_Table[PlantHue.White] = new PlantHueInfo(0x481, 1060821, PlantHue.White, 0x481);
m_Table[PlantHue.Pink] = new PlantHueInfo(0x48E, 1061854, PlantHue.Pink);
m_Table[PlantHue.Magenta] = new PlantHueInfo(0x486, 1061852, PlantHue.Magenta);
m_Table[PlantHue.Aqua] = new PlantHueInfo(0x495, 1061853, PlantHue.Aqua);
m_Table[PlantHue.FireRed] = new PlantHueInfo(0x489, 1061855, PlantHue.FireRed);
}
private PlantHueInfo(int hue, int name, PlantHue plantHue) : this(hue, name, plantHue, hue)
@ -88,9 +90,7 @@ namespace Server.Engines.Plants
public static PlantHueInfo GetInfo(PlantHue plantHue)
{
if (m_Table.TryGetValue(plantHue, out PlantHueInfo info))
return info;
return m_Table[PlantHue.Plain];
return m_Table.TryGetValue(plantHue, out PlantHueInfo info) ? info : m_Table[PlantHue.Plain];
}
public static PlantHue RandomFirstGeneration()
@ -181,4 +181,4 @@ namespace Server.Engines.Plants
return IsPrimary(PlantHue);
}
}
}
}

View file

@ -118,14 +118,14 @@ namespace Server.Engines.Quests.Necro
public override void CheckProgress()
{
if (System.From.Map == Map.Malas && System.From.InRange(new Point3D(1076, 450, -84), 5))
if (SummonFamiliarSpell.Table[System.From] is HordeMinionFamiliar hmf && hmf.InRange(System.From, 5) &&
hmf.TargetLocation == null)
{
System.From.SendLocalizedMessage(
1060113); // You instinctively will your familiar to fetch the scroll for you.
hmf.TargetLocation = new Point2D(1076, 450);
}
if (System.From.Map != Map.Malas || !System.From.InRange(new Point3D(1076, 450, -84), 5) ||
!SummonFamiliarSpell.Table.TryGetValue(System.From, out BaseCreature bc) || !(bc is HordeMinionFamiliar hmf) ||
!hmf.InRange(System.From, 5) || hmf.TargetLocation != null)
return;
System.From.SendLocalizedMessage(
1060113); // You instinctively will your familiar to fetch the scroll for you.
hmf.TargetLocation = new Point2D(1076, 450);
}
public override void OnComplete()
@ -376,4 +376,4 @@ namespace Server.Engines.Quests.Necro
System.AddConversation(new BankerConversation());
}
}
}
}

View file

@ -45,7 +45,7 @@ namespace Server.Engines.Reports
if (string.IsNullOrEmpty(account))
return null;
if (!(StaffInfo[account] is StaffInfo info))
if (!StaffInfo.TryGetValue(account, out StaffInfo info))
StaffInfo[account] = info = new StaffInfo(account);
return info;
@ -57,7 +57,7 @@ namespace Server.Engines.Reports
if (string.IsNullOrEmpty(account))
return null;
if (!(UserInfo[account] is UserInfo info))
if (!UserInfo.TryGetValue(account, out UserInfo info))
UserInfo[account] = info = new UserInfo(account);
return info;

View file

@ -32,7 +32,8 @@ namespace Server.Engines.Reports
public static PersistableType Find(string name)
{
return m_Table[name];
m_Table.TryGetValue(name, out PersistableType value);
return value;
}
public static void Register(PersistableType type)

View file

@ -209,12 +209,7 @@ namespace Server.Mobiles
false);
}
public SpawnerEntry AddEntry(string creaturename, int probability, int amount)
{
return AddEntry(creaturename, probability, amount, true);
}
public SpawnerEntry AddEntry(string creaturename, int probability, int amount, bool dotimer)
public SpawnerEntry AddEntry(string creaturename, int probability, int amount, bool dotimer = true)
{
SpawnerEntry entry = new SpawnerEntry(creaturename, probability, amount);
Entries.Add(entry);
@ -356,7 +351,7 @@ namespace Server.Mobiles
if (Entries.Count <= 0 || IsFull)
return;
int probsum = 0;
for (int i = 0; i < Entries.Count; i++)
@ -365,7 +360,7 @@ namespace Server.Mobiles
if (probsum <= 0)
return;
int rand = Utility.RandomMinMax(1, probsum);
for (int i = 0; i < Entries.Count; i++)
@ -639,7 +634,7 @@ namespace Server.Mobiles
{
int x = Location.X + (Utility.Random(m_HomeRange * 2 + 1) - m_HomeRange);
int y = Location.Y + (Utility.Random(m_HomeRange * 2 + 1) - m_HomeRange);
int mapZ = map.GetAverageZ(x, y);
if (waterMob)
@ -1221,4 +1216,4 @@ namespace Server.Mobiles
}
}
}
}
}

View file

@ -77,9 +77,7 @@ namespace Server
return;
}
m_Callbacks.TryGetValue(e.GumpID, out OnVirtueUsed callback);
if (callback != null)
if (m_Callbacks.TryGetValue(e.GumpID, out OnVirtueUsed callback))
callback(e.Beholder);
else
e.Beholder.SendLocalizedMessage(1052066); // That virtue is not active yet.
@ -180,4 +178,4 @@ namespace Server
}
}
}
}
}