Getting Container From HitInfo

Hello, new to the API. Currently starting with something simple and trying to create an auto deposit for players inventories when they break a barrel. I however don't know how to get a ItemContainer from HitInfo. Any suggestions?

namespace Oxide.Plugins
{
    [Info("InstaPocket", "Zlaio", "0.0.1")]
    class InstaPocket : RustPlugin
    {
        public readonly string[] barrels =
        {
            "loot_barrel_1",
            "loot_barrel_2",
            "loot-barrel-1",
            "loot-barrel-2",
            "oil_barrel"
        };

        public Hash<uint,ulong> looters = new Hash<uint, ulong>();

        object OnContainerDropItems(ItemContainer container)
        {
            if (barrels.Contains(container.entityOwner.ShortPrefabName))
            {
                PrintToChat("Barrel!");
                BasePlayer player = BasePlayer.FindByID(looters[container.uid]);

                //Just printing for debugging as of now 
                for (int i = 0; i < container.itemList.Count; i++)
                {
                    PrintToChat(player,container.itemList[i].ToString());
                }
                return container;   
            }
            return null;
        }

        object OnMeleeAttack(BasePlayer player, HitInfo info)
        {
            if (barrels.Contains(info.HitEntity.ShortPrefabName))
            {
                ItemContainer container = info.HitEntity.GetComponent<ItemContainer>();
                looters[container.uid] = player.userID;
            }
            return null;
        }
    
    }
}​

Try something like this.

        object OnMeleeAttack(BasePlayer player, HitInfo hitinfo)
        {
            if (hitinfo == null || hitinfo?.HitEntity==null) return null;
            var target = hitinfo.HitEntity as StorageContainer;
            if (target == null) Puts("Not a StorageContainer ");
            else
            {
                ItemContainer container = target.inventory;
                Puts($"This is a StorageContainer, with {container.capacity} slots");
            }
            return null;
        }​
Iq21dlQIlYnlQTl.jpg Lorenzo

Try something like this.

        object OnMeleeAttack(BasePlayer player, HitInfo hitinfo)
        {
            if (hitinfo == null || hitinfo?.HitEntity==null) return null;
            var target = hitinfo.HitEntity as StorageContainer;
            if (target == null) Puts("Not a StorageContainer ");
            else
            {
                ItemContainer container = target.inventory;
                Puts($"This is a StorageContainer, with {container.capacity} slots");
            }
            return null;
        }​

Thank you this worked! Heres the rest of the code

namespace Oxide.Plugins
{
    [Info("InstaLoot", "Zlaio", "0.0.1")]
    class InstaLoot : RustPlugin
    {
        public readonly string[] barrels =
        {
            "loot_barrel_1",
            "loot_barrel_2",
            "loot-barrel-1",
            "loot-barrel-2",
            "oil_barrel"
        };

        public Hash<uint, ulong> looters = new Hash<uint, ulong>();

        object OnContainerDropItems(ItemContainer container)
        {
            if (barrels.Contains(container.entityOwner.ShortPrefabName))
            {
                BasePlayer player = BasePlayer.FindByID(looters[container.uid]);

                //Not a player that destroyed the barrel, go ahead and drop the items
                if (player == null) return null;
                
                PlayerInventory inventory = player.inventory;
                if (!canAcceptItems(inventory, container.itemList.ToArray()))
                {
                    SendReply(player, "Couldn't fit items, dropping them...");
                    return null;
                }
                else
                {
                    foreach (Item item in container.itemList) inventory.GiveItem(ItemManager.CreateByName(item.info.shortname, item.amount));
                    looters.Remove(container.uid);
                    return container;   
                }
            }
            return null;
        }

        bool canAcceptItems(PlayerInventory inventory, Item[] items)
        {
            List<bool> fits = new List<bool>();
            foreach (var item in items)
            {
                foreach (var pItem in inventory.AllItems())
                {
                   fits.Add(pItem.amount + item.amount <= pItem.MaxStackable()); 
                }
            }
            return !fits.Contains(false);
        }

        object OnMeleeAttack(BasePlayer player, HitInfo hitinfo)
        {
            if (hitinfo == null || hitinfo?.HitEntity == null) return null;
            var target = hitinfo.HitEntity as StorageContainer;
            if (target != null)
            {
                ItemContainer container = target.inventory;
                looters[container.uid] = player.userID;
            }
            return null;
        }

        void OnEntityDeath(BaseNetworkable entity, HitInfo info)
        {
            if (info == null || info?.HitEntity == null) return;
            var target = info.HitEntity as StorageContainer;
            if (target != null)
            {
                ItemContainer container = target.inventory;

                //Wasn't a player who destroyed the barrel, go ahead and remove it from the Map.
                if (info.InitiatorPlayer == null) looters.Remove(container.uid);
            }
            
        }

    }
}