Skip to content

Commit

Permalink
fix: Fixes ethics causing crashes when not used (#1972)
Browse files Browse the repository at this point in the history
  • Loading branch information
kamronbatman authored Oct 16, 2024
1 parent b7316a5 commit f16bce6
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 76 deletions.
60 changes: 24 additions & 36 deletions Projects/UOContent/Engines/Ethics/Core/Ethic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,37 +10,23 @@ namespace Server.Ethics;
[SerializationGenerator(1)]
public abstract partial class Ethic : EthicsEntity
{
public static Ethic Hero { get; private set; }
public static Ethic Evil { get; private set; }
public static Ethic Hero => Ethics[0];
public static Ethic Evil => Ethics[1];

public static readonly Ethic[] Ethics =
{
Hero,
Evil
};
private static Ethic[] Ethics => [null, null];

public static bool RegisterEthic(Ethic ethic)
{
if (ethic is HeroEthic)
if (ethic is HeroEthic && Hero == null)
{
if (Hero == null)
{
Hero = ethic;
return true;
}

return false;
Ethics[0] = ethic;
return true;
}

if (ethic is EvilEthic)
if (ethic is EvilEthic && Evil == null)
{
if (Evil == null)
{
Evil = ethic;
return true;
}

return false;
Ethics[1] = ethic;
return true;
}

return false;
Expand All @@ -58,6 +44,11 @@ public static Ethic Find(Item item)
{
if ((item.SavedFlags & 0x100) != 0)
{
if (Hero == null)
{
return null;
}

if (item.Hue == Hero.Definition.PrimaryHue)
{
return Hero;
Expand All @@ -68,6 +59,11 @@ public static Ethic Find(Item item)

if ((item.SavedFlags & 0x200) != 0)
{
if (Evil == null)
{
return null;
}

if (item.Hue == Evil.Definition.PrimaryHue)
{
return Evil;
Expand Down Expand Up @@ -172,7 +168,7 @@ public static void EventSink_Speech(SpeechEventArgs e)
{
var ethic = Ethics[i];

if (!ethic.IsEligible(e.Mobile))
if (ethic?.IsEligible(e.Mobile) != true)
{
continue;
}
Expand Down Expand Up @@ -240,11 +236,7 @@ public static void EventSink_Speech(SpeechEventArgs e)
}
}

public static Ethic Find(Mobile mob) => Find(mob, false, false);

public static Ethic Find(Mobile mob, bool inherit) => Find(mob, inherit, false);

public static Ethic Find(Mobile mob, bool inherit, bool allegiance)
public static Ethic Find(Mobile mob, bool inherit = false, bool allegiance = false)
{
var pl = Player.Find(mob);

Expand All @@ -255,14 +247,10 @@ public static Ethic Find(Mobile mob, bool inherit, bool allegiance)

if (inherit && mob is BaseCreature bc)
{
if (bc.Controlled)
{
return Find(bc.ControlMaster, false);
}

if (bc.Summoned)
var master = bc.GetMaster();
if (master != null)
{
return Find(bc.SummonMaster, false);
return Find(master);
}

if (allegiance)
Expand Down
14 changes: 9 additions & 5 deletions Projects/UOContent/Engines/Ethics/Evil/Mobiles/UnholyFamiliar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,19 @@ public UnholyFamiliar() : base(AIType.AI_Melee)

public override string ApplyNameSuffix(string suffix)
{
if (suffix.Length == 0)
var ethic = Ethic.Evil;
if (ethic == null)
{
suffix = Ethic.Evil.Definition.Adjunct.String;
return base.ApplyNameSuffix("");
}
else

var adjunct = ethic.Definition.Adjunct;

if (suffix.Length == 0)
{
suffix = $"{suffix} {Ethic.Evil.Definition.Adjunct.String}";
return base.ApplyNameSuffix(adjunct);
}

return base.ApplyNameSuffix(suffix);
return base.ApplyNameSuffix($"{suffix} {adjunct}");
}
}
14 changes: 11 additions & 3 deletions Projects/UOContent/Engines/Ethics/Evil/Mobiles/UnholySteed.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,25 @@ public UnholySteed() : base(0x74, 0x3EA7, AIType.AI_Melee, FightMode.Aggressor)

public override string ApplyNameSuffix(string suffix)
{
var ethic = Ethic.Evil;
if (ethic == null)
{
return base.ApplyNameSuffix("");
}

var adjunct = ethic.Definition.Adjunct;

if (suffix.Length == 0)
{
return base.ApplyNameSuffix(Ethic.Evil.Definition.Adjunct.String);
return base.ApplyNameSuffix(adjunct);
}

return base.ApplyNameSuffix($"{suffix} {Ethic.Evil.Definition.Adjunct.String}");
return base.ApplyNameSuffix($"{suffix} {adjunct}");
}

public override void OnDoubleClick(Mobile from)
{
if (Ethic.Find(from) != Ethic.Evil)
if (Ethic.Evil == null || Ethic.Find(from) != Ethic.Evil)
{
from.SendMessage("You may not ride this steed.");
}
Expand Down
14 changes: 9 additions & 5 deletions Projects/UOContent/Engines/Ethics/Hero/Mobiles/HolyFamiliar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,19 @@ public HolyFamiliar() : base(AIType.AI_Melee)

public override string ApplyNameSuffix(string suffix)
{
if (suffix.Length == 0)
var ethic = Ethic.Hero;
if (ethic == null)
{
suffix = Ethic.Hero.Definition.Adjunct.String;
return base.ApplyNameSuffix("");
}
else

var adjunct = ethic.Definition.Adjunct;

if (suffix.Length == 0)
{
suffix = $"{suffix} {Ethic.Hero.Definition.Adjunct.String}";
return base.ApplyNameSuffix(adjunct);
}

return base.ApplyNameSuffix(suffix);
return base.ApplyNameSuffix($"{suffix} {adjunct}");
}
}
14 changes: 11 additions & 3 deletions Projects/UOContent/Engines/Ethics/Hero/Mobiles/HolySteed.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,25 @@ public HolySteed() : base(0x75, 0x3EA8, AIType.AI_Melee, FightMode.Aggressor)

public override string ApplyNameSuffix(string suffix)
{
var ethic = Ethic.Hero;
if (ethic == null)
{
return base.ApplyNameSuffix("");
}

var adjunct = ethic.Definition.Adjunct;

if (suffix.Length == 0)
{
return base.ApplyNameSuffix(Ethic.Hero.Definition.Adjunct.String);
return base.ApplyNameSuffix(adjunct);
}

return base.ApplyNameSuffix($"{suffix} {Ethic.Hero.Definition.Adjunct.String}");
return base.ApplyNameSuffix($"{suffix} {adjunct}");
}

public override void OnDoubleClick(Mobile from)
{
if (Ethic.Find(from) != Ethic.Hero)
if (Ethic.Hero == null || Ethic.Find(from) != Ethic.Hero)
{
from.SendMessage("You may not ride this steed.");
}
Expand Down
29 changes: 5 additions & 24 deletions Projects/UOContent/Mobiles/PlayerMobile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1327,32 +1327,13 @@ private void ValidateEquipment_Sandbox()
}

var item = items[i];
var itemEthic = Ethic.Find(item);

if ((item.SavedFlags & 0x100) != 0)
if (itemEthic != null && itemEthic != ethic)
{
if (item.Hue != Ethic.Hero.Definition.PrimaryHue)
{
item.SavedFlags &= ~0x100;
}
else if (ethic != Ethic.Hero)
{
from.AddToBackpack(item);
moved = true;
continue;
}
}
else if ((item.SavedFlags & 0x200) != 0)
{
if (item.Hue != Ethic.Evil.Definition.PrimaryHue)
{
item.SavedFlags &= ~0x200;
}
else if (ethic != Ethic.Evil)
{
from.AddToBackpack(item);
moved = true;
continue;
}
from.AddToBackpack(item);
moved = true;
continue;
}

if (item is BaseWeapon weapon)
Expand Down

0 comments on commit f16bce6

Please sign in to comment.