Kilra Yan

Member
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:
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 :)
 
I agree. I added some extension functions to Container for commodity deeds and bank checks to help with the Community Collections features.
 

Active Shards

Donations

Total amount
$0.00
Goal
$1,000.00
Back