context: using System;
  using System.Collections;
  using System.Collections.Generic;
  using Server.ContextMenus;
  using Server.Engines.Craft;
  using Server.Network;
  using Server.Targeting;
  
  namespace Server.Items
  {
      public class SalvageBag : Bag
      {
  		private bool m_Failure;
  		
          public override int LabelNumber { get { return 1079931; } } // Salvage Bag
  
          [Constructable]
          public SalvageBag()
              : this( Utility.RandomBlueHue() )
          {
          }
  
          [Constructable]
          public SalvageBag( int hue )
          {
              Weight = 2.0;
              Hue = hue;
  			m_Failure = false;
          }
  
          public override void GetContextMenuEntries( Mobile from, List<ContextMenuEntry> list )
          {
              base.GetContextMenuEntries( from, list );
  
              if( from.Alive )
              {
                  list.Add( new SalvageIngotsEntry( this, IsChildOf( from.Backpack ) && Resmeltables() ) );
                  list.Add( new SalvageClothEntry( this, IsChildOf( from.Backpack ) && Scissorables() ) );
                  list.Add( new SalvageAllEntry( this, IsChildOf( from.Backpack ) && Resmeltables() && Scissorables() ) );
              }
          }
  		
  		#region Checks
  		private bool Resmeltables() //Where context menu checks for metal items and dragon barding deeds
  		{
  			foreach( Item i in Items )
  			{
  				if( i != null && !i.Deleted )
  				{
  					if( i is BaseWeapon )
  					{
  						if( CraftResources.GetType( ( (BaseWeapon)i ).Resource ) == CraftResourceType.Metal )
  						return true;
  					}
  					if( i is BaseArmor )
  					{
  						if( CraftResources.GetType( ( (BaseArmor)i ).Resource ) == CraftResourceType.Metal )
  						return true;
  					}
  					if( i is DragonBardingDeed )
  						return true;
  				}
  			}
  			return false;
  		}
  		
  		private bool Scissorables() //Where context menu checks for Leather items and cloth items
  		{
  			foreach( Item i in Items )
  			{
  				if( i != null && !i.Deleted )
  				{
  					if( i is IScissorable )
  					{
  						if( i is BaseClothing )
  							return true;
  						if( i is BaseArmor )
  						{
  							if( CraftResources.GetType( ( (BaseArmor)i ).Resource ) == CraftResourceType.Leather )
  							return true;
  						}
  						if( ( i is Cloth ) || ( i is BoltOfCloth ) || ( i is Hides ) || ( i is BonePile ) )
  							return true;
  					}
  				}
  			}
  			return false;
  		}
  		#endregion	
  
  		#region Resmelt.cs
          private bool Resmelt( Mobile from, Item item, CraftResource resource )
          {
              try
              {
                  if( CraftResources.GetType( resource ) != CraftResourceType.Metal )
                      return false;
  
                  CraftResourceInfo info = CraftResources.GetInfo( resource );
  
                  if( info == null || info.ResourceTypes.Length == 0 )
                      return false;
  
                  CraftItem craftItem = DefBlacksmithy.CraftSystem.CraftItems.SearchFor( item.GetType() );
  
                  if( craftItem == null || craftItem.Resources.Count == 0 )
                      return false;
  
                  CraftRes craftResource = craftItem.Resources.GetAt( 0 );
  
                  if( craftResource.Amount < 2 )
                      return false; // Not enough metal to resmelt
  					
  				double difficulty = 0.0;
  
  				switch ( resource )
  				{
  					case CraftResource.DullCopper: difficulty = 65.0; break;
  					case CraftResource.ShadowIron: difficulty = 70.0; break;
  					case CraftResource.Copper: difficulty = 75.0; break;
  					case CraftResource.Bronze: difficulty = 80.0; break;
  					case CraftResource.Gold: difficulty = 85.0; break;
  					case CraftResource.Agapite: difficulty = 90.0; break;
  					case CraftResource.Verite: difficulty = 95.0; break;
  					case CraftResource.Valorite: difficulty = 99.0; break;
  				}				 
  
                  Type resourceType = info.ResourceTypes[ 0 ];
                  Item ingot = (Item)Activator.CreateInstance( resourceType );
  
                  if( item is DragonBardingDeed || ( item is BaseArmor && ( (BaseArmor)item ).PlayerConstructed ) || ( item is BaseWeapon && ( (BaseWeapon)item ).PlayerConstructed ) || ( item is BaseClothing && ( (BaseClothing)item ).PlayerConstructed ) )
  					{
  						double mining = from.Skills[ SkillName.Mining ].Value;
  						if( mining > 100.0 )
  							mining = 100.0;
  						double amount = ( ( ( 4 + mining ) * craftResource.Amount - 4 ) * 0.0068 );
  						if( amount < 2 )
  							ingot.Amount = 2;
  						else
  						ingot.Amount = (int)amount;
  					}
                  else
  				{
                      ingot.Amount = 2;
  				}
  				
  				if ( difficulty > from.Skills[ SkillName.Mining ].Value )
  				{
  					m_Failure = true; 
  					ingot.Delete();
  				}
  				else
  					item.Delete();
  
                  from.AddToBackpack( ingot );
  
                  from.PlaySound( 0x2A );
                  from.PlaySound( 0x240 );
  
                  return true;
              }
              catch( Exception ex )
              {
                  Console.WriteLine( ex.ToString() );
              }
  
              return false;
          }
  		#endregion
  		
  		#region Salvaging
          private void SalvageIngots( Mobile from )
          {
              Item[] tools = from.Backpack.FindItemsByType( typeof( BaseTool ) );
  
              bool ToolFound = false;
              foreach( Item tool in tools )
              {
                  if( tool is BaseTool && ( (BaseTool)tool ).CraftSystem == DefBlacksmithy.CraftSystem )
                      ToolFound = true;
              }
  
              if( !ToolFound )
              {
                  from.SendLocalizedMessage( 1079822 ); // You need a blacksmithing tool in order to salvage ingots.
                  return;
              }
  
              bool anvil, forge;
              DefBlacksmithy.CheckAnvilAndForge( from, 2, out anvil, out forge );
  
              if( !forge )
              {
                  from.SendLocalizedMessage( 1044265 ); // You must be near a forge.
                  return;
              }
  
              int salvaged = 0;
              int notSalvaged = 0;
  			
  			Container sBag = this;
  			
  			List<Item> Smeltables = sBag.FindItemsByType<Item>();
  
  
gt:             for(int i = Smeltables.Count - 1; i >= 0; i--)