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

@ -46,7 +46,7 @@ namespace Server.Items
{
CampfireEntry entry = Campfire.GetEntry(from);
if (entry != null && entry.Safe)
if (entry?.Safe == true)
from.SendGump(new LogoutGump(entry, this));
}
}
@ -127,4 +127,4 @@ namespace Server.Items
}
}
}
}
}

View file

@ -86,7 +86,8 @@ namespace Server.Items
public static CampfireEntry GetEntry(Mobile player)
{
return m_Table[player];
m_Table.TryGetValue(player, out CampfireEntry value);
return value;
}
public static void RemoveEntry(CampfireEntry entry)
@ -195,4 +196,4 @@ namespace Server.Items
set => m_Safe = value;
}
}
}
}

View file

@ -288,14 +288,14 @@ namespace Server.Items
public static void AddDelay(Mobile m)
{
m_Delay[m]?.Stop();
m_Delay.TryGetValue(m, out Timer timer);
timer?.Stop();
m_Delay[m] = Timer.DelayCall(TimeSpan.FromSeconds(30), EndDelay, m);
}
public static int GetDelay(Mobile m)
{
Timer timer = m_Delay[m];
if (timer?.Next > DateTime.UtcNow)
if (m_Delay.TryGetValue(m, out Timer timer) && timer.Next > DateTime.UtcNow)
return (int)(timer.Next - DateTime.UtcNow).TotalSeconds;
return 0;
@ -303,9 +303,7 @@ namespace Server.Items
public static void EndDelay(Mobile m)
{
Timer timer = m_Delay[m];
if (timer != null)
if (m_Delay.TryGetValue(m, out Timer timer))
{
timer.Stop();
m_Delay.Remove(m);

View file

@ -153,14 +153,14 @@ namespace Server.Items
public static void AddDelay(Mobile m)
{
m_Delay[m]?.Stop();
m_Delay.TryGetValue(m, out Timer timer);
timer?.Stop();
m_Delay[m] = Timer.DelayCall(TimeSpan.FromSeconds(60), EndDelay, m);
}
public static int GetDelay(Mobile m)
{
Timer timer = m_Delay[m];
if (timer?.Next > DateTime.UtcNow)
if (m_Delay.TryGetValue(m, out Timer timer) && timer.Next > DateTime.UtcNow)
return (int)(timer.Next - DateTime.UtcNow).TotalSeconds;
return 0;
@ -168,8 +168,7 @@ namespace Server.Items
public static void EndDelay(Mobile m)
{
Timer timer = m_Delay[m];
if (timer != null)
if (m_Delay.TryGetValue(m, out Timer timer))
{
timer.Stop();
m_Delay.Remove(m);

View file

@ -63,26 +63,20 @@ namespace Server.Items
public static bool HasTimer(Mobile m)
{
return m_Table[m] != null;
return m_Table.ContainsKey(m);
}
public static void RemoveTimer(Mobile m)
public static void RemoveTimer(Mobile m, bool interrupted = false)
{
Timer timer = m_Table[m];
if (timer != null)
if (m_Table.TryGetValue(m, out Timer timer))
{
if (interrupted)
m.SendLocalizedMessage(1073187); // The invisibility effect is interrupted.
timer.Stop();
m_Table.Remove(m);
}
}
public static void Iterrupt(Mobile m)
{
m.SendLocalizedMessage(1073187); // The invisibility effect is interrupted.
RemoveTimer(m);
}
public override void Serialize(GenericWriter writer)
{
base.Serialize(writer);

View file

@ -435,11 +435,9 @@ namespace Server.Items
return null;
}
m_Table.TryGetValue(from, out List<Spellbook> list);
bool searchAgain = false;
if (list == null)
if (!m_Table.TryGetValue(from, out List<Spellbook> list))
m_Table[from] = list = FindAllSpellbooks(from);
else
searchAgain = true;
@ -911,4 +909,4 @@ namespace Server.Items
}
}
}
}
}

View file

@ -42,10 +42,8 @@ namespace Server.Items
{
get
{
if (Recipe.Recipes.ContainsKey(m_RecipeID))
return Recipe.Recipes[m_RecipeID];
return null;
Recipe.Recipes.TryGetValue(m_RecipeID, out Recipe recipe);
return recipe;
}
}
@ -121,4 +119,4 @@ namespace Server.Items
}
}
}
}
}

View file

@ -203,12 +203,8 @@ namespace Server.Items
public static BaseInstrument GetInstrument(Mobile from)
{
BaseInstrument item = m_Instruments[from];
if (item == null)
return null;
if (item.IsChildOf(from.Backpack))
return item;
if (m_Instruments.TryGetValue(from, out BaseInstrument instrument) && instrument.IsChildOf(from.Backpack))
return instrument;
m_Instruments.Remove(from);
return null;
@ -222,7 +218,6 @@ namespace Server.Items
public static void PickInstrument(Mobile from, InstrumentPickedCallback callback)
{
BaseInstrument instrument = GetInstrument(from);
if (instrument != null)
{
callback?.Invoke(from, instrument);
@ -531,7 +526,7 @@ namespace Server.Items
{
SetInstrument(from, this);
// Delay of 7 second before beign able to play another instrument again
// Delay of 7 second before being able to play another instrument again
new InternalTimer(from).Start();
if (CheckMusicianship(from))

View file

@ -65,7 +65,7 @@ namespace Server.Items
ConsumeUse(weapon);
if (CombatCheck(from, target))
Timer.DelayCall(TimeSpan.FromSeconds(1.0), OnHit, new object[] { from, target, weapon });
Timer.DelayCall(TimeSpan.FromSeconds(1.0), () => OnHit(from, target, weapon));
Timer.DelayCall(TimeSpan.FromSeconds(2.5), ResetUsing, from);
}
@ -187,15 +187,15 @@ namespace Server.Items
BaseWeapon defWeapon = defender.Weapon as BaseWeapon;
Skill atkSkill = defender.Skills.Ninjitsu;
Skill defSkill = defender.Skills[defWeapon.Skill];
// Skill defSkill = defender.Skills[defWeapon.Skill];
double atSkillValue = attacker.Skills.Ninjitsu.Value;
double defSkillValue = defWeapon.GetDefendSkillValue(attacker, defender);
double attackValue = AosAttributes.GetValue(attacker, AosAttribute.AttackChance);
double defSkillValue = defWeapon?.GetDefendSkillValue(attacker, defender) ?? 0.0;
if (defSkillValue <= -20.0) defSkillValue = -19.9;
double attackValue = AosAttributes.GetValue(attacker, AosAttribute.AttackChance);
if (DivineFurySpell.UnderEffect(attacker)) attackValue += 10;
if (AnimalForm.UnderTransformation(attacker, typeof(GreyWolf)) ||
@ -230,29 +230,24 @@ namespace Server.Items
return attacker.CheckSkill(atkSkill.SkillName, chance);
}
private static void OnHit(object[] states)
private static void OnHit(Mobile from, Mobile target, INinjaWeapon weapon)
{
Mobile from = states[0] as Mobile;
Mobile target = states[1] as Mobile;
INinjaWeapon weapon = states[2] as INinjaWeapon;
if (!from.CanBeHarmful(target))
return;
from.DoHarmful(target);
if (from.CanBeHarmful(target))
AOS.Damage(target, from, weapon.WeaponDamage, 100, 0, 0, 0, 0);
if (weapon.Poison != null && weapon.PoisonCharges > 0)
{
from.DoHarmful(target);
if (EvilOmenSpell.TryEndEffect(target))
target.ApplyPoison(from, Poison.GetPoison(weapon.Poison.Level + 1));
else
target.ApplyPoison(from, weapon.Poison);
AOS.Damage(target, from, weapon.WeaponDamage, 100, 0, 0, 0, 0);
weapon.PoisonCharges--;
if (weapon.Poison != null && weapon.PoisonCharges > 0)
{
if (EvilOmenSpell.TryEndEffect(target))
target.ApplyPoison(from, Poison.GetPoison(weapon.Poison.Level + 1));
else
target.ApplyPoison(from, weapon.Poison);
weapon.PoisonCharges--;
if (weapon.PoisonCharges < 1) weapon.Poison = null;
}
if (weapon.PoisonCharges < 1) weapon.Poison = null;
}
}
@ -314,4 +309,4 @@ namespace Server.Items
}
}
}
}
}

View file

@ -259,15 +259,13 @@ namespace Server.Items
public static void CreateTimer(Mobile m, TimeSpan delay)
{
if (m != null)
if (!Timers.ContainsKey(m))
Timers[m] = new InternalTimer(m, delay);
if (m != null && !IsDisguised(m))
Timers[m] = new InternalTimer(m, delay);
}
public static void StartTimer(Mobile m)
{
Timers.TryGetValue(m, out Timer t);
t?.Start();
}
@ -276,43 +274,31 @@ namespace Server.Items
return Timers.ContainsKey(m);
}
public static bool StopTimer(Mobile m)
public static void StopTimer(Mobile m)
{
Timers.TryGetValue(m, out Timer t);
if (!Timers.TryGetValue(m, out Timer t))
return;
if (t != null)
{
TimeSpan ts = t.Next - DateTime.UtcNow;
if (ts < TimeSpan.Zero)
ts = TimeSpan.Zero;
TimeSpan ts = t.Next - DateTime.UtcNow;
if (ts < TimeSpan.Zero)
ts = TimeSpan.Zero;
t.Delay = ts;
t.Stop();
}
return t != null;
t.Delay = ts;
t.Stop();
}
public static bool RemoveTimer(Mobile m)
public static void RemoveTimer(Mobile m)
{
Timers.TryGetValue(m, out Timer t);
if (t != null)
if (Timers.TryGetValue(m, out Timer t))
{
t.Stop();
Timers.Remove(m);
}
return t != null;
}
public static TimeSpan TimeRemaining(Mobile m)
{
Timers.TryGetValue(m, out Timer t);
if (t != null) return t.Next - DateTime.UtcNow;
return TimeSpan.Zero;
return Timers.TryGetValue(m, out Timer t) ? t.Next - DateTime.UtcNow : TimeSpan.Zero;
}
private class InternalTimer : Timer
@ -336,4 +322,4 @@ namespace Server.Items
}
}
}
}
}