115 lines
3.5 KiB
C#
115 lines
3.5 KiB
C#
using System;
|
|
using System.Linq; // Required to use .ToArray() on the HashSet
|
|
using Server;
|
|
using Server.Mobiles;
|
|
|
|
namespace Server.Custom
|
|
{
|
|
public static class AutoStable
|
|
{
|
|
public static void StoreMount(PlayerMobile m)
|
|
{
|
|
CleanClaimList(m);
|
|
|
|
if (m.Mount is BaseCreature horse)
|
|
{
|
|
Server.Mobiles.BaseMount.Dismount(m);
|
|
|
|
horse.ControlTarget = null;
|
|
horse.Internalize();
|
|
horse.SetControlMaster(null);
|
|
horse.SummonMaster = null;
|
|
horse.IsStabled = true;
|
|
|
|
horse.Language = "mount";
|
|
|
|
// Bypass the "inaccessible setter" restriction using C# Reflection
|
|
if (m.Stabled == null)
|
|
{
|
|
var prop = typeof(PlayerMobile).GetProperty("Stabled");
|
|
prop?.SetValue(m, new System.Collections.Generic.HashSet<Mobile>());
|
|
}
|
|
|
|
// Safely add the horse now that we are certain the list exists
|
|
m.Stabled?.Add(horse);
|
|
}
|
|
}
|
|
|
|
public static void RestoreMount(PlayerMobile from)
|
|
{
|
|
// Prevent crashes if the stable list is null (e.g., during logout)
|
|
if (from.Stabled == null || from.Stabled.Count == 0)
|
|
return;
|
|
|
|
BaseCreature bc = null;
|
|
|
|
// Loop through a snapshot of the HashSet to find the mount
|
|
foreach (Mobile mob in from.Stabled.ToArray())
|
|
{
|
|
if (mob is BaseCreature horse)
|
|
{
|
|
if (horse.Deleted)
|
|
{
|
|
horse.IsStabled = false;
|
|
from.Stabled.Remove(horse);
|
|
}
|
|
else if (horse.Language == "mount")
|
|
{
|
|
bc = horse;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (bc != null)
|
|
{
|
|
if ((from.Followers + bc.ControlSlots) <= from.FollowersMax)
|
|
{
|
|
bc.SetControlMaster(from);
|
|
bc.ControlTarget = from;
|
|
bc.MoveToWorld(from.Location, from.Map);
|
|
bc.IsStabled = false;
|
|
|
|
from.Stabled.Remove(bc);
|
|
bc.Language = null;
|
|
|
|
if (bc is BaseMount mount)
|
|
{
|
|
mount.Rider = from;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
from.SendMessage("You have too many followers active to retrieve your mount.");
|
|
bc.Language = null;
|
|
}
|
|
}
|
|
|
|
CleanClaimList(from);
|
|
}
|
|
|
|
public static void CleanClaimList(PlayerMobile from)
|
|
{
|
|
// Prevent crashes if the stable list is null
|
|
if (from.Stabled == null || from.Stabled.Count == 0)
|
|
return;
|
|
|
|
// Loop through a snapshot of the HashSet to clean up tags
|
|
foreach (Mobile mob in from.Stabled.ToArray())
|
|
{
|
|
if (mob is BaseCreature horse)
|
|
{
|
|
if (horse.Deleted)
|
|
{
|
|
horse.IsStabled = false;
|
|
from.Stabled.Remove(horse);
|
|
}
|
|
else
|
|
{
|
|
horse.Language = null;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|