I wrote a function which check if player has any input item equipped

Basically it works like that:

supposed you have to discover if player has the Axe Of Abadon equipped you call

if (IsEquipped (mobile as PlayerMobile, Axeofabadon))
{ do this }

could become useful in order not to recall everytime the exact layer reference.

Code:
public bool IsEquipped( Mobile m, Item item )
			{
				if (m == null)
				return false;
				foreach (Layer l in Enum.GetValues(typeof(Layer)))
				{
					Item tocheck = m.FindItemOnLayer( l );
					if (tocheck == item)
						return true;		
				}
				return false;		
			}
 
Proposal for an extension method

usages
- this.IsEquipped(mobile, item)
- item.IsEquipped(mobile)
- mobile.HasEquipped(item)

Version - 1 :p
Code:
using System;
using Server;

namespace Server.Items
{
    public static class ItemExtensions
    {
        public static bool IsEquipped(this Item item, Mobile m)
        {
            if (m != null)
            {
                Item tocheck = m.FindItemOnLayer(item.Layer);
                if (tocheck == item)
                    return true;
            }
            return false;
        }
        public static bool HasEquipped(this Mobile m, Item item)
        {
            return item.IsEquipped(m);
        }
        public static bool IsEquipped(this Object obj, Mobile m, Item item)
        {
            return item.IsEquipped(m);
        }
    }
}

Version - 2 With methods for checking if type is equipped
Additional methods calls:
- this.IsEquipped(from, typeof(<Item you want to look for>))
- typeof(<Item you want to look for>).IsEquipped(from)
- mobile.HasEquipped(typeof(<Item you want to look for>))

Code:
using System;
using Server;

namespace Server.Items
{
    public static class ItemExtensions
    {
        public static bool IsEquipped(this Item item, Mobile m)
        {
            if (m != null)
            {
                Item tocheck = m.FindItemOnLayer(item.Layer);
                if (tocheck == item)
                    return true;
            }
            return false;
        }
        public static bool HasEquipped(this Mobile m, Item item)
        {
            return item.IsEquipped(m);
        }
        public static bool IsEquipped(this Object obj, Mobile m, Item item)
        {
            return item.IsEquipped(m);
        }
        public static bool IsEquipped(this Type itemtype, Mobile m)
        {
            if (m != null && itemtype != null)
            {
                Item temp = null;
                try
                {
                    temp = (Item)Activator.CreateInstance(itemtype);
                    Item tocheck = m.FindItemOnLayer(temp.Layer);
                    if (tocheck.GetType() == itemtype)
                        return true;
                }catch(Exception)
                { /* If for example you looked for a 1 handed weapon but use a 2 handed weapon instead */ }
                finally
                {
                    if(temp != null)
                        temp.Delete();
                }
            }
            return false;
        }
        public static bool HasEquipped(this Mobile m, Type itemtype)
        {
            return itemtype.IsEquipped(m);
        }
        public static bool IsEquipped(this Object obj, Mobile m, Type itemtype)
        {
            return itemtype.IsEquipped(m);
        }
    }
}
 
Last edited:

Alternatively, if you have an Item object reference, you can do mobile.Items.Contains( item ) to determine whether the item is equipped.

There is also specific properties, like Mobile.Weapon, Mobile.NeckArmor, Mobile.LegArmor, etc, that can be used like this: if(mobile.Weapon is Longsword) or if(mobile.Weapon == item)
 
Extension methods are amazing, I encourage anyone to give them a go.
I've only written extension methods in VNc for things I've found a use for, but there are still many concepts and use-cases I haven't covered :)
 
Back