#W# Added: Auto Dismount/Remount in certian regions.

This commit is contained in:
WarrentyExpired 2026-09-06 20:14:01 -04:00
parent df8da1c9cb
commit ae6013a1bf
4 changed files with 142 additions and 1 deletions

View file

@ -187,7 +187,7 @@
"Name": "the Bastion Healers Shop",
"Priority": 55,
"Area": [
{ "x1": 2725, "y1": 1882, "x2": 2734, "y2": 1879 }
{ "x1": 2725, "y1": 1882, "x2": 2734, "y2": 1897 }
]
},
{

View file

@ -0,0 +1,115 @@
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;
}
}
}
}
}
}

View file

@ -1,5 +1,7 @@
using Server;
using Server.Mobiles;
using Server.Custom;
namespace Server.Regions
{
public class GreetingBusinessRegion : TownRegion
@ -16,11 +18,23 @@ namespace Server.Regions
{
m.SendMessage($"Welcome to <BASEFONT COLOR='#FFD700'>{this.Name}</BASEFONT>.");
}
if (m is PlayerMobile player )
{
if ( player.Mounted )
{
player.SendMessage("Your mount is waiting safely for you.");
Server.Custom.AutoStable.StoreMount(player);
}
}
}
public override void OnExit(Mobile m)
{
base.OnExit(m);
if ( m is PlayerMobile player )
{
Server.Custom.AutoStable.RestoreMount(player);
}
}
}
}

View file

@ -17,11 +17,23 @@ namespace Server.Regions
{
m.SendMessage($"You entered <BASEFONT COLOR='#FF0000'>{this.Name}</BASEFONT>.");
}
if (m is PlayerMobile player )
{
if ( player.Mounted )
{
player.SendMessage("Your mount is waiting safely for you.");
Server.Custom.AutoStable.StoreMount(player);
}
}
}
public override void OnExit(Mobile m)
{
base.OnExit(m);
if ( m is PlayerMobile player )
{
Server.Custom.AutoStable.RestoreMount(player);
}
}
}
}