ServUO Version
Publish 57
Ultima Expansion
Endless Journey
I'm working on the "old" Resource Storage Box system, trying to set them up so that you can add resources not only from your backpack, or by drag and drop, but from a secured container or a pets backpack. The only thing I could think of that might work is from the BOD gumps, using something similar to the Combine from them with the IsChildOf & targeted is Container. But so far I've managed to get non compiles more than anything. On the rare occasions I've gotten it to compile, the add either didn't work at all or crashed my Test server. I was originally going to use a 2nd add resource button for the other containers, but decided a single button would work for it all. I've gotten grumpy trying to do this with my admittedly limited skills & have removed all the changes several times now, so unfortunately, I have no current attempts to show. The section of BOD gump coding I thought might work is as follows. Oh yeah, I'm also not sure which script to put it in at the moment. The BaseResourceStorageBox or the BaseResourceStorageBoxGump.

C#:
        public override void OnResponse(NetState sender, RelayInfo info)
        {
            if (m_Deed.Deleted || !m_Deed.IsChildOf(m_From.Backpack))
                return;

            switch (info.ButtonID)
            {
                case 2: // Combine
                    {
                        m_From.SendGump(new SmallBODGump(m_From, m_Deed));
                        m_Deed.BeginCombine(m_From);
                        break;
                    }
                case 3: // points mode
                    {
                        BODContext c = BulkOrderSystem.GetContext(m_From);

                        if (c != null)
                        {
                            switch (c.PointsMode)
                            {
                                case PointsMode.Enabled: c.PointsMode = PointsMode.Disabled; break;
                                case PointsMode.Disabled: c.PointsMode = PointsMode.Automatic; break;
                                case PointsMode.Automatic: c.PointsMode = PointsMode.Enabled; break;
                            }
                        }

                        m_From.SendGump(new SmallBODGump(m_From, m_Deed));
                        break;
                    }
                case 4: // combine from container
                    {
                        m_From.BeginTarget(-1, false, Targeting.TargetFlags.None, (m, targeted) =>
                            {
                                if (!m_Deed.Deleted && targeted is Container)
                                {
                                    List<Item> list = new List<Item>(((Container)targeted).Items);

                                    foreach (Item item in list)
                                    {
                                        m_Deed.EndCombine(m_From, item);
                                    }
                                }
                            });
                        break;
                    }
            }
        }
 
These are the ones I've been "targeting".
 

Attachments

  • BaseResourceStorageBox.cs
    4.4 KB · Views: 5
  • BaseResourceStorageBoxGump.cs
    4.7 KB · Views: 4
I played around with pack animals a bit but so far can't seem to get it working, mostly non-compile errors...

This checks the container you target and goes through it for other containers within it and adds all items it's able to the storage box, I also left the code in that just goes through the first level of the container you select and does not go through all containers.

The only issue thus far is because it goes through all items and tries to add them all to the box you get some spam from the box telling you items can't be added. But should give you some working code at least.

I'll keep looking into the animal pack and see if I can get it working.
 

Attachments

  • BaseResourceStorageBoxGump.cs
    8 KB · Views: 3
Utilize Linq to sift through Items

Code:
using System.Linq;

var someItems = BackPack.Items.FindAll(i => i is BaseOre)

if (someItems.Count > 0)
{
//run a for loop on someItems
}
 
The only issue I see with that would be that the item is being pulled from a type list on the storage box item.. so at the moment I'm not sure how I would modify your statement to pull from that type list to check the items against.

This is from the base storage box..

C#:
        public bool TryAdd(Item item)
        {
            foreach (Type type in m_ResourceTypes)
            {
                if (item.GetType() == type)
                {
                    if (m_Resources.ContainsKey(type) && (int)m_Resources[type] + item.Amount >= 100000)
                    {
                        this.PublicOverheadMessage(MessageType.Whisper, 0, false, "I cannot hold more of that resource!");
                        return false;
                    }

                    AddResource(type, item.Amount);
                    this.PublicOverheadMessage(MessageType.Whisper, 0, false, "Resource Added.");
                    item.Delete();
                    return true;
                }
            }
            this.PublicOverheadMessage(MessageType.Whisper, 0, false, "I don't hold that resource!");
            return false;
        }
 
Code:
        public bool TryAdd(Item item)
        {
            if (item.Stackable)
            {
                var someItems = Items.FindAll(i => i.GetType() == item.GetType());

                if (someItems.Count > 0)
                {
                    foreach (var resource in someItems)
                    {
                        if (resource.Amount + item.Amount >= 100000)
                        {
                            PublicOverheadMessage(MessageType.Whisper, 0, false, "I cannot hold more of that resource!");

                            return false;
                        }

                        resource.Amount += item.Amount;

                        item.Delete();

                        PublicOverheadMessage(MessageType.Whisper, 0, false, "Resource Added.");

                        return true;
                    }
                }
                else
                {
                    PublicOverheadMessage(MessageType.Whisper, 0, false, "I don't hold that resource!");

                    return false;
                }
            }
            else
            {
                PublicOverheadMessage(MessageType.Whisper, 0, false, "That is not a resource!");

                return false;
            }
}
 
I was able to get that working with some modifications, it didn't work as is, at least not for me.
Anyways this also seems to search just the first level of the pack not other containers within the pack, is there a way for it to do that too?
Not that it's required just thought about it for convivence..
 
I should have mentioned that my example might need tweaking to work for you, I am just coding it up to show the path I would take, depending on server, my code might not be adaptable! I'll try to keep my examples a few versions back from current c# =p

Code:
if (resource is Container)
{
var container = resource as Container;

foreach(var subItem in container.Items)
{
// now you can filter through these items from the Container types in bag!
}
}
 
I took a little advice from some of your code, but still using lists.. this is what I have working so far which works for both containers and pets now..

C#:
                if (targeted is BaseCreature animal)
                {
                    if (!animal.Body.IsHuman)
                    {
                        Container container = animal.Backpack;

                        if (container != null)
                        {
                            List<Item> itemsToAdd = new List<Item>();
                            List<Container> containersToCheck = new List<Container>() { container };

                            while (containersToCheck.Count > 0)
                            {
                                Container currentContainer = containersToCheck[0];
                                containersToCheck.RemoveAt(0);
                                foreach (Item item in currentContainer.Items)
                                {
                                    if (item is Container subContainer)
                                    {
                                        containersToCheck.Add(subContainer);
                                    }
                                    else
                                    {
                                        foreach (Type type in m_StorageBox.m_ResourceTypes)
                                        {
                                            if (item.GetType() == type)
                                            {
                                                itemsToAdd.Add(item);
                                            }
                                        }
                                    }
                                }
                            }

                            bool added = false;
                            foreach (Item item in itemsToAdd)
                            {
                                m_StorageBox.TryAdd(item);
                                added = true;
                            }
                            if (added)
                            {
                                from.SendMessage("Resources added, please choose another.");
                            }
                            else
                            {
                                from.SendMessage("No resources could be added, please try another.");
                            }

                            from.Target = new ResourceStorageBoxTarget(m_StorageBox, m_Offset);
                        }
                    }
                }

                else if (targeted is Container container)
                {
                    List<Item> itemsToAdd = new List<Item>();
                    List<Container> containersToCheck = new List<Container>() { container };

                    while (containersToCheck.Count > 0)
                    {
                        Container currentContainer = containersToCheck[0];
                        containersToCheck.RemoveAt(0);
                        foreach (Item item in currentContainer.Items)
                        {
                            if (item is Container subContainer)
                            {
                                containersToCheck.Add(subContainer);
                            }
                            else
                            {
                                foreach (Type type in m_StorageBox.m_ResourceTypes)
                                {
                                    if (item.GetType() == type)
                                    {
                                        itemsToAdd.Add(item);
                                    }
                                }
                            }
                        }
                    }

                    bool added = false;
                    foreach (Item item in itemsToAdd)
                    {
                        m_StorageBox.TryAdd(item);
                        added = true;
                    }
                    if (added)
                    {
                        from.SendMessage("Resources added, please choose another.");
                    }
                    else
                    {
                        from.SendMessage("No resources could be added, please try another.");
                    }

                    from.Target = new ResourceStorageBoxTarget(m_StorageBox, m_Offset);
                }

                else if (targeted is Item)
                {
                    if (m_StorageBox.TryAdd(targeted as Item))
                        from.SendMessage("Resource added, please choose another.");
                    else
                        from.SendMessage("Resource could not be added, please try another.");

                    from.Target = new ResourceStorageBoxTarget(m_StorageBox, m_Offset);
                }

I will see if I can work on converting that over to Linq..
 

Attachments

  • BaseResourceStorageBox.cs
    7.3 KB · Views: 6
  • BaseResourceStorageBoxGump.cs
    10 KB · Views: 5
This works great, it really does. Just one thing though. It will actually allow a player to add resources from another players pet's backpack, which wouldn't be a good thing in most cases.
 
Hehe whoops.. this is why we test things lol, I added in a check to make sure the player controls the mobile before it continues, while I was at it I also added a check for the players pack to make sure you're selecting your pack and not another players. Theres probably more checks that might need to be put in place but those are probably the two biggest ones.
 

Attachments

  • BaseResourceStorageBox.cs
    5.7 KB · Views: 14
  • BaseResourceStorageBoxGump.cs
    10.8 KB · Views: 13
Back