Full Version : [Sort command modification
xmlspawner >>Scripting Support >>[Sort command modification


<< Prev | Next >>

Odee- 04-13-2006
Here is the Sort command Fafnir made. I am using/modifying this script with his permission.

What I am trying to do is make items stack with each other if stackable. For example: if you typed [m addtopack gold 5 and added these items to your pack multiple times, and type [sort, clicked the gold pile, and moved to another bag, the gold will go there but will not stack. I am trying to have the items pile together rather than having 5 like items. Here is the script. Any help would be great. Thanks smile.gif

CODE

///Fafnir - [sortv0.2  31 Mar 2006
///This command allows a player to move any number of items in their pack
///into a sub pack.
///
///My thanks to Arte Gordon for his patient coding aid to this newbie scripter!



using System;
using System.Collections;
using Server;
using Server.Items;
using Server.Mobiles;
using Server.Targeting;
using Server.Misc;
using Server.Prompts;
using Server.ContextMenus;
using Server.Multis;

namespace Server.Scripts.Commands
{
public class Command
{
 public static void Initialize()
 {
  Server.Commands.Register( "Sort", AccessLevel.Player, new CommandEventHandler( SendCommand ) );
 }
 [Usage( "Sort" )]
 public static void SendCommand( CommandEventArgs e )
 {
  Mobile from = e.Mobile;
  from.Target = new SortTarget();
  from.SendMessage("Select the item to Sort");
 }


 public class SortTarget : Target
 {
  public SortTarget() : base( -1, true, TargetFlags.None )
  {
  }
  protected override void OnTarget( Mobile from, object o )
  {
   if ( o is Item)
   {
    from.Target = new SortDestination( from, o );
    from.SendMessage("Select the bag to Sort to");
   }
  }
 }
 public class SortDestination  : Target
 {
  Container bag = null;
  Mobile m = null;
  object thing = null;
 
  public SortDestination( Mobile m, object o ) : base( -1, true, TargetFlags.None )
  {
   thing = o;
  }
  protected override void OnTarget( Mobile m, object d )
  {
   Container pack = m.Backpack;
   bag = d as Container;
   if ( pack != null && bag != null)
   {
    Item[] SortObjects = pack.FindItemsByType( thing.GetType());
    int i;
    for ( i=0; i < SortObjects.Length; ++i )
    {
     bag.DropItem( SortObjects[i]);
    }
    m.SendMessage(1150, i.ToString());
    m.SendMessage(1150, "items moved");
   }
  }
 }
}

}



ArteGordon- 04-13-2006
if you use the TryDropItem method instead of the DropItem method, then it will automatically try and stack stackables.

CODE

for ( i=0; i < SortObjects.Length; ++i )
   {
    bag.TryDropItem( m, SortObjects[i], false);
   }


that method returns a bool if it was successful, so you might want to test for that and decide what you want to do if it fails (e.g. send a message or something).

public override bool TryDropItem( Mobile from, Item dropped, bool sendFullMessage )

Odee- 04-13-2006
Okay...heres what I tried to do...but failed miserably

CODE


for ( i=0; i < SortObjects.Length; ++i )
  {
   bag.TryDropItem( m, SortObjects[i], false);
   public override bool TryDropItem( Mobile from, Item dropped, bool sendFullMessage("Failed to Move object. Please try again.")
   }


Sorry if I totally missed it. Im really new to commands/bools. Thanks for all the help smile.gif

ArteGordon- 04-13-2006
cut this line out

CODE

public override bool TryDropItem( Mobile from, Item dropped, bool sendFullMessage("Failed to Move object. Please try again.")


I just posted it to show you what the arguments were.

Fafnir- 04-14-2006
If you'll look, I have a version .03 that already does stacking.