Useful IsEquipped check

Kilra Yan

Squire
Aug 26, 2015
278
18
18
36
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;		
			}
 
  • Like
Reactions: Muddled and m309

PyrO

Moderator
Jan 27, 2015
1,319
172
63
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:

Vorspire

Vita-Nex: Core
Admin
ServUO Developer
Jul 29, 2013
1,173
278
83
33
United Kingdom
core.vita-nex.com
Shard Name
Ultima Shards: Multiverse
Donate
Donate money to this user

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)
 
  • Like
Reactions: m309

Vorspire

Vita-Nex: Core
Admin
ServUO Developer
Jul 29, 2013
1,173
278
83
33
United Kingdom
core.vita-nex.com
Shard Name
Ultima Shards: Multiverse
Donate
Donate money to this user
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 :)
 
Dec 22, 2015
1,042
94
63
36
I agree. I added some extension functions to Container for commodity deeds and bank checks to help with the Community Collections features.