fix: Fixes ethics causing crashes when not used (#1972)

This commit is contained in:
Kamron Batman 2024-10-15 17:02:11 -07:00 • committed by GitHub
parent b7316a5bd6
commit f16bce60e6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 75 additions and 82 deletions

View file

@ -10,37 +10,23 @@ namespace Server.Ethics;
[SerializationGenerator(1)] [SerializationGenerator(1)]
public abstract partial class Ethic : EthicsEntity public abstract partial class Ethic : EthicsEntity
{ {
public static Ethic Hero { get; private set; } public static Ethic Hero => Ethics[0];
public static Ethic Evil { get; private set; } public static Ethic Evil => Ethics[1];
public static readonly Ethic[] Ethics = private static Ethic[] Ethics => [null, null];
{
Hero,
Evil
};
public static bool RegisterEthic(Ethic ethic) public static bool RegisterEthic(Ethic ethic)
{ {
if (ethic is HeroEthic) if (ethic is HeroEthic && Hero == null)
{ {
if (Hero == null) Ethics[0] = ethic;
{ return true;
Hero = ethic;
return true;
}
return false;
} }
if (ethic is EvilEthic) if (ethic is EvilEthic && Evil == null)
{ {
if (Evil == null) Ethics[1] = ethic;
{ return true;
Evil = ethic;
return true;
}
return false;
} }
return false; return false;
@ -58,6 +44,11 @@ public abstract partial class Ethic : EthicsEntity
{ {
if ((item.SavedFlags & 0x100) != 0) if ((item.SavedFlags & 0x100) != 0)
{ {
if (Hero == null)
{
return null;
}
if (item.Hue == Hero.Definition.PrimaryHue) if (item.Hue == Hero.Definition.PrimaryHue)
{ {
return Hero; return Hero;
@ -68,6 +59,11 @@ public abstract partial class Ethic : EthicsEntity
if ((item.SavedFlags & 0x200) != 0) if ((item.SavedFlags & 0x200) != 0)
{ {
if (Evil == null)
{
return null;
}
if (item.Hue == Evil.Definition.PrimaryHue) if (item.Hue == Evil.Definition.PrimaryHue)
{ {
return Evil; return Evil;
@ -172,7 +168,7 @@ public abstract partial class Ethic : EthicsEntity
{ {
var ethic = Ethics[i]; var ethic = Ethics[i];
if (!ethic.IsEligible(e.Mobile)) if (ethic?.IsEligible(e.Mobile) != true)
{ {
continue; continue;
} }
@ -240,11 +236,7 @@ public abstract partial class Ethic : EthicsEntity
} }
} }
public static Ethic Find(Mobile mob) => Find(mob, false, false); public static Ethic Find(Mobile mob, bool inherit = false, bool allegiance = false)
public static Ethic Find(Mobile mob, bool inherit) => Find(mob, inherit, false);
public static Ethic Find(Mobile mob, bool inherit, bool allegiance)
{ {
var pl = Player.Find(mob); var pl = Player.Find(mob);
@ -255,14 +247,10 @@ public abstract partial class Ethic : EthicsEntity
if (inherit && mob is BaseCreature bc) if (inherit && mob is BaseCreature bc)
{ {
if (bc.Controlled) var master = bc.GetMaster();
if (master != null)
{ {
return Find(bc.ControlMaster, false); return Find(master);
}
if (bc.Summoned)
{
return Find(bc.SummonMaster, false);
} }
if (allegiance) if (allegiance)

View file

@ -54,15 +54,19 @@ public partial class UnholyFamiliar : BaseCreature
public override string ApplyNameSuffix(string suffix) 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
{
suffix = $"{suffix} {Ethic.Evil.Definition.Adjunct.String}";
} }
return base.ApplyNameSuffix(suffix); var adjunct = ethic.Definition.Adjunct;
if (suffix.Length == 0)
{
return base.ApplyNameSuffix(adjunct);
}
return base.ApplyNameSuffix($"{suffix} {adjunct}");
} }
} }

View file

@ -53,17 +53,25 @@ public partial class UnholySteed : BaseMount
public override string ApplyNameSuffix(string suffix) public override string ApplyNameSuffix(string suffix)
{ {
if (suffix.Length == 0) var ethic = Ethic.Evil;
if (ethic == null)
{ {
return base.ApplyNameSuffix(Ethic.Evil.Definition.Adjunct.String); return base.ApplyNameSuffix("");
} }
return base.ApplyNameSuffix($"{suffix} {Ethic.Evil.Definition.Adjunct.String}"); var adjunct = ethic.Definition.Adjunct;
if (suffix.Length == 0)
{
return base.ApplyNameSuffix(adjunct);
}
return base.ApplyNameSuffix($"{suffix} {adjunct}");
} }
public override void OnDoubleClick(Mobile from) 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."); from.SendMessage("You may not ride this steed.");
} }

View file

@ -55,15 +55,19 @@ public partial class HolyFamiliar : BaseCreature
public override string ApplyNameSuffix(string suffix) 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
{
suffix = $"{suffix} {Ethic.Hero.Definition.Adjunct.String}";
} }
return base.ApplyNameSuffix(suffix); var adjunct = ethic.Definition.Adjunct;
if (suffix.Length == 0)
{
return base.ApplyNameSuffix(adjunct);
}
return base.ApplyNameSuffix($"{suffix} {adjunct}");
} }
} }

View file

@ -53,17 +53,25 @@ public partial class HolySteed : BaseMount
public override string ApplyNameSuffix(string suffix) public override string ApplyNameSuffix(string suffix)
{ {
if (suffix.Length == 0) var ethic = Ethic.Hero;
if (ethic == null)
{ {
return base.ApplyNameSuffix(Ethic.Hero.Definition.Adjunct.String); return base.ApplyNameSuffix("");
} }
return base.ApplyNameSuffix($"{suffix} {Ethic.Hero.Definition.Adjunct.String}"); var adjunct = ethic.Definition.Adjunct;
if (suffix.Length == 0)
{
return base.ApplyNameSuffix(adjunct);
}
return base.ApplyNameSuffix($"{suffix} {adjunct}");
} }
public override void OnDoubleClick(Mobile from) 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."); from.SendMessage("You may not ride this steed.");
} }

View file

@ -1327,32 +1327,13 @@ namespace Server.Mobiles
} }
var item = items[i]; 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) from.AddToBackpack(item);
{ moved = true;
item.SavedFlags &= ~0x100; continue;
}
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;
}
} }
if (item is BaseWeapon weapon) if (item is BaseWeapon weapon)