Full Version : One target after another
xmlspawner >>Scripting Support >>One target after another


<< Prev | Next >>

ArteGordon- 03-31-2006
CODE

if ( pack != null & (d is Bag) )


you want to use the logical && operator instead of the bitwise & operator. Also you could also just write this as

CODE

if ( pack != null && bag != null )


since you already check for d as Bag here

CODE

bag = d as Bag;


which will assign it a value of null if it is not.

Fafnir- 03-31-2006
Thanks for the tip Arte. I decided that a bag was too limiting anyway and have changed to Container type. It's much more useful now.

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");
   }
  }
 }
}

}
xmlspawner/done2.gif

LowCastle- 04-01-2006
Very cool script! I am surprised this hasn't been done before. Thank you for the contribution.

I tested it on my shard. It compiles no problem. Works good too. I would only change one thing: Stackable items sorted to the target container are not stacked.

I thought you could achieve this simply by changing this line:

CODE
bag.DropItem( SortObjects[i]);


to this:

CODE
bag.TryDropItem( SortObjects[i]);


but when I tried that on my server it would not compile.

QUOTE
- Error: Scripts\Custom\Commands\SortCommand.cs: CS1501: (line 70, column 6) No
overload for method 'TryDropItem' takes '1' arguments


It was a guess. I'm xmlspawner/No.bmp scripter.

ArteGordon- 04-01-2006
it needs 3 args

CODE

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

Fafnir- 04-01-2006
Yeah, I got requests for that feature too and changed the code. I'll post the finished code below:

CODE
///Fafnir - [sortv0.3  31 Mar 2006
///This command allows a player to move any number of items in their pack
///into a sub pack.

///Version .03 Stackable items now will stack in the destination container

///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.TryDropItem( m, SortObjects[i], false);
   }
   m.SendMessage( i.ToString() + " items moved");
  }
 }
}
}

}

Fafnir- 04-04-2006
Version .04 fixes a bug which deleted any of the sorted item already in the destination container.

CODE
///Fafnir - [sortv0.4  4 Apr 2006
///This command allows a player to move any number of items in their pack
///into a sub pack.

///Version .03 Stackable items now will stack in the destination container
///Version .04 Fixed a bug which was deleting any  sorted items already in the destination container. Thanks Weiland for the catch!

///My thanks to Arte Gordon for his patient coding aid to this newbie scripter!


using System;
using Server;
using Server.Items;
using Server.Scripts.Commands;
using Server.Targeting;


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;
  bool sendFullMessage;
  if ( pack != null && bag != null)
  {
   int i;
   Item[] SortObjectsinBag = bag.FindItemsByType( thing.GetType()); //see if any of the item are in bag
   for ( i=0; i < SortObjectsinBag.Length; ++i )
   {
    pack.TryDropItem( m, SortObjectsinBag[i], false); //move items out of bag and into pack
   }
   Item[] SortObjects = pack.FindItemsByType( thing.GetType());
   
   for ( i=0; i < SortObjects.Length; ++i )
   {
    bag.TryDropItem( m, SortObjects[i], false);//move them all back to bag
   }
   m.SendMessage( i.ToString() + " items moved");
  }
 }
}
}

}

LowCastle- 04-04-2006
Thanks again Fafnir. My players love this command. Good job!

Fafnir- 04-04-2006
You are welcome LowCastle! Our players really like it too and I'm glad to give back to this community. I've learned a lot here!

LowCastle- 05-02-2006
Hello again. I noticed a warning in my console concerning this script.


QUOTE
- Warning: Scripts\Custom\Commands\SortCommand.cs: CS0168: (line 62, column 8)
The variable 'sendFullMessage' is declared but never used


The line in question is this:

bool sendFullMessage;

So I got to looking at the script and wondered to myself whether it would fill a container beyond its limit of 125 items. I did a quick test by creating 70 heating stands and 70 candles. I sorted the 70 candles into one bag and the 70 heating stands into another bag. I set the bag of candles on the ground and then (with a player character) sorted the 70 heating stands into the bag of candles, resulting in a bag with 140 items. I then sorted other things from my pack into that same bag, even though it was beyond full.

I looked at the script for an hour or so before I decided to post here for help. I am a newbie and fixing this is just a bit beyond me at this point. I'm still scratching my head.

Would it be as simple as adding an if ( sendFullMessage ) condition in there or am I way off?

ArteGordon- 05-02-2006
I believe that sendFullmessage was intended to be passed as an arg to the TryDropItem method. It shouldnt make any difference whether you use it or not.

I suppose you could add a check to make sure that the bag was already in the pack before starting to sort.

if ( pack != null && bag != null && bag.IsChildOf(pack))

LowCastle- 05-02-2006
Thanks for the fix Arte. I like the idea of being able to sort stuff to a container outside your main pack, but this will work for now.