1. Hi,

    I am currently trying to retrieve the container type during the OnItemAddedToContainer event to prevent players from searching for the blueprint of certain objects (AK, Bolt).

    I tried a lot of things without success.

    Code:
    namespace Oxide.Plugins
    {
        [Info("BlockItemResearch", "Sorrow", 0.1)]
        [Description("Block Item Research")]    class BlockItemResearch : RustPlugin
        {
            private void OnItemResearch(ResearchTable table, Item targetItem, BasePlayer player)
            {
                if (player == null) return;            if(targetItem.ToString().Contains("rifle.ak"))
                {
                    targetItem.MoveToContainer(player.inventory.containerMain);
                }
                Puts(targetItem.ToString());
            }        private void OnItemAddedToContainer(ItemContainer container, Item item, BasePlayer player)
            {
                Puts("1: " + container.uid.ToString());
                Puts("2: " + container.ToString());
                Puts("3: " + container.GetType());
                Puts("4: " + container.FindContainer(container.uid));
            }
        }
    }
    
    And the result is :
    Code:
    [BlockItemResearch] 1: 3563
    [BlockItemResearch] 2: ItemContainer
    [BlockItemResearch] 3: ItemContainer
    [BlockItemResearch] 4: ItemContainer
    Can you help me to find the container type?

    Thanks a lot.
     
  2. Hello Sorrow,

    Correct me if I'm wrong but for what I can see you are attempting to block the items from being researched by not allowing them to be put in the research table container.

    You can achieve this by making use of the CanMoveItem hook (documented here: Oxide API for Rust)
    Here is also a small example to block the Assault Rifle and Bolt Action Rifle to be put into the research table:

    Code:
    namespace Oxide.Plugins
    {
        [Info("BlockItemResearch", "Sorrow", 0.1)]
        [Description("Block Item Research")]    class BlockItemResearch : RustPlugin
        {
            private List<string> itemBlacklist = new List<String>
            {
                "rifle.ak",
                "rifle.bolt"
            };        private object CanMoveItem(Item item, PlayerInventory playerLoot, uint targetContainerId, int targetSlot)
            {
                // Find the container object the player is attempting to move an item to.
                ItemContainer targetContainer = playerLoot.FindContainer(targetContainerId);
                // Try to cast the entity owner of the container to ResearchTable. This will return a ResearchTable object if the owner is indeed a research table, otherwise this will be null.
                ResearchTable researchTable = (ResearchTable)targetContainer.entityOwner;            if (researchTable == null)
                {
                    // Target isn't a research table so we don't want to do anything here
                    return null;
                }            // The item is being put in a research table. Check if we allow this item to be researched!
                string itemShortname = item.info.shortname;
                if (itemBlacklist.Contains(itemShortname))
                {
                    // The item is blacklisted, don't allow it to be put in the container!
                    return false;
                }            // This is another item so we can let the game decide what needs to happen.
                return null;
            }
        }
    }
     
  3. You can easily edit the above example to include more items by adding more shortnames of the item you want to block to the itemBlacklist list. For a full list of items and their shortnames you can view the item list we have available in the documentation: Oxide API for Rust
     
  4. Yes I know and thank you lot for this work. I was just asking if we could reproduce the default rust behavior when we put a m249 in the ResearchTable :).
     
  5. Ah like that, well, the above example will just block the ak and the bolt and will reproduce the default rust behavior for all other items. (That is where the return null is for)
     
  6. Thanks your code works fine but i have few error in console :
    Code:
    (02:13:23) | Failed to call hook 'CanMoveItem' on plugin 'BlockItemResearch v0.1.0' (NullReferenceException: Object reference not set to an instance of an object)
      at Oxide.Plugins.BlockItemResearch.CanMoveItem (.Item item, .PlayerInventory playerLoot, UInt32 targetContainerId, Int32 targetSlot) [0x00000] in <filename unknown>:0
      at Oxide.Plugins.BlockItemResearch.DirectCallHook (System.String name, System.Object& ret, System.Object[] args) [0x00000] in <filename unknown>:0
      at Oxide.Plugins.CSharpPlugin.InvokeMethod (Oxide.Core.Plugins.HookMethod method, System.Object[] args) [0x00000] in <filename unknown>:0
      at Oxide.Core.Plugins.CSPlugin.OnCallHook (System.String name, System.Object[] args) [0x00000] in <filename unknown>:0
      at Oxide.Core.Plugins.Plugin.CallHook (System.String hook, System.Object[] args) [0x00000] in <filename unknown>:0

    Code:
    using System;
    using System.Collections.Generic;namespace Oxide.Plugins
    {
        [Info("BlockItemResearch", "Sorrow", 0.1)]
        [Description("Block Item Research")]    class BlockItemResearch : RustPlugin
        {
            private List<string> itemBlacklist = new List<String>
            {
                "rifle.ak",
                "rifle.bolt",
                "metal.facemask",
                "metal.plate.torso"        };        private object CanMoveItem(Item item, PlayerInventory playerLoot, uint targetContainerId, int targetSlot)
            {
                // Find the container object the player is attempting to move an item to.
                ItemContainer targetContainer = playerLoot.FindContainer(targetContainerId);
                // Try to cast the entity owner of the container to ResearchTable. This will return a ResearchTable object if the owner is indeed a research table, otherwise this will be null.
                ResearchTable researchTable = (ResearchTable)targetContainer.entityOwner;            if (researchTable == null)
                {
                    // Target isn't a research table so we don't want to do anything here
                    return null;
                }            // The item is being put in a research table. Check if we allow this item to be researched!
                string itemShortname = item.info.shortname;
                if (itemBlacklist.Contains(itemShortname))
                {
                    BasePlayer player = item.GetOwnerPlayer();
                    // The item is blacklisted, don't allow it to be put in the container!
                    PrintToChat(player, "Tu croyais vraiment qu'avec des bouts de feraille t'allais pouvoir crafter รงa ? Va l'acheter chez <color=orange>l'Armurier</color> !");
                    return false;
                }            // This is another item so we can let the game decide what needs to happen.
                return null;
            }
        }
    }
    
     
  7. In some cases an item does not have an owner player so you want to get the BasePlayer from PlayerLoot instead. If I'm not mistaken you should be able to use the following:
    Code:
    BasePlayer player = playerLoot.GetComponent<BasePlayer>();
     
  8. How is this right? Obviously I don't have any mistakes in the console. But I couldn't make the mistake when I wanted to.


    Code:
    using System;
    using System.Collections.Generic;namespace Oxide.Plugins
    {
        [Info("BlockItemResearch", "Sorrow", 0.1)]
        [Description("Block Item Research")]    class BlockItemResearch : RustPlugin
        {
            private List<string> itemBlacklist = new List<String>
            {
                "rifle.ak",
                "rifle.bolt",
                "metal.facemask",
                "metal.plate.torso"        };        private object CanMoveItem(Item item, PlayerInventory playerLoot, uint targetContainerId, int targetSlot)
            {
                BasePlayer player = null;
                if (playerLoot == null)
                {
                    return null;
                }
                else
                {
                    player = playerLoot.GetComponent<BasePlayer>();
                }           
                // Find the container object the player is attempting to move an item to.
                ItemContainer targetContainer = playerLoot.FindContainer(targetContainerId);
                // Try to cast the entity owner of the container to ResearchTable. This will return a ResearchTable object if the owner is indeed a research table, otherwise this will be null.
                ResearchTable researchTable = (ResearchTable)targetContainer.entityOwner;            if (researchTable == null)
                {
                    // Target isn't a research table so we don't want to do anything here
                    return null;
                }            // The item is being put in a research table. Check if we allow this item to be researched!
                string itemShortname = item.info.shortname;
                if (itemBlacklist.Contains(itemShortname))
                {
                    if (player != null)
                    {
                        PrintToChat(player, "Tu croyais vraiment qu'avec des bouts de feraille t'allais pouvoir crafter รงa ? Va l'acheter chez <color=orange>l'Armurier</color> !");
                    }
                    // The item is blacklisted, don't allow it to be put in the container!
                    return false;
                }            // This is another item so we can let the game decide what needs to happen.
                return null;
            }
        }
    }
    
    Thank you =)
     
    Last edited by a moderator: Jan 20, 2018
  9. Same error :/
     
  10. I solved the problem like this:
    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;namespace Oxide.Plugins
    {
        [Info("BlockItemResearch", "Sorrow", "0.1.2")]
        [Description("Block Item Research")]    class BlockItemResearch : RustPlugin
        {
            private List<string> itemBlacklist = new List<String>
            {
                "rifle.ak",
                "rifle.bolt",
                "metal.facemask",
                "metal.plate.torso"        };        private object CanMoveItem(Item item, PlayerInventory playerLoot, uint targetContainerId, int targetSlot)
            {
                // Find the container object the player is attempting to move an item to.
                ItemContainer targetContainer = playerLoot.FindContainer(targetContainerId);
            
                if (!targetContainer.entityOwner.ToString().Contains("researchtable"))
                {
                    // Target isn't a research table so we don't want to do anything here
                    return null;
                }        
                // The item is being put in a research table. Check if we allow this item to be researched!
                string itemShortname = item.info.shortname;
                if (itemBlacklist.Contains(itemShortname))
                {
                    BasePlayer player = playerLoot.GetComponent<BasePlayer>();
                    PrintToChat(player, "Tu croyais vraiment qu'avec des bouts de feraille t'allais pouvoir crafter รงa ? Va l'acheter chez <color=orange>l'Armurier</color> !");
                    // The item is blacklisted, don't allow it to be put in the container!
                    return false;
                }            // This is another item so we can let the game decide what needs to happen.
                return null;
            }
        }
    }
    
    But why didn't it work?

    Edit : Error :(

    Code:
    (12:18:35) | twila[2981458/2604679] was killed by Sadness[2549514/76561198228115720]
    (12:18:41) | Failed to call hook 'CanMoveItem' on plugin 'BlockItemResearch v0.1.2' (NullReferenceException: Object reference not set to an instance of an object)
      at Oxide.Plugins.BlockItemResearch.CanMoveItem (.Item item, .PlayerInventory playerLoot, UInt32 targetContainerId, Int32 targetSlot) [0x00000] in <filename unknown>:0
      at Oxide.Plugins.BlockItemResearch.DirectCallHook (System.String name, System.Object& ret, System.Object[] args) [0x00000] in <filename unknown>:0
      at Oxide.Plugins.CSharpPlugin.InvokeMethod (Oxide.Core.Plugins.HookMethod method, System.Object[] args) [0x00000] in <filename unknown>:0
      at Oxide.Core.Plugins.CSPlugin.OnCallHook (System.String name, System.Object[] args) [0x00000] in <filename unknown>:0
      at Oxide.Core.Plugins.Plugin.CallHook (System.String hook, System.Object[] args) [0x00000] in <filename unknown>:0
    (12:18:51) | [Death Notes] helene tué par Sadness
     Pistolet semi-automatique à 5.37m
     Impact : Avant-bras droit
    (12:18:51) | helene[2981484/5971875] was killed by Sadness[2549514/76561198228115720]
    Edit 2:
    The error appears after MoveItem Box -> Inventory

    Edit 3:
    After a lot of debugging I got to this

    Code:
    using System;
    using System.Collections.Generic;namespace Oxide.Plugins
    {
        [Info("BlockItemResearch", "Sorrow", "0.1.2")]
        [Description("Block Item Research")]    class BlockItemResearch : RustPlugin
        {
            private List<string> itemBlacklist = new List<String>
            {
                "rifle.ak",
                "rifle.bolt",
                "metal.facemask",
                "metal.plate.torso"        };        private object CanMoveItem(Item item, PlayerInventory playerLoot, uint targetContainerId, int targetSlot)
            {
                // Find the container object the player is attempting to move an item to.
                ItemContainer targetContainer = (ItemContainer)playerLoot.FindContainer(targetContainerId);            if (targetContainer.entityOwner is ResearchTable)
                {
                    // The item is being put in a research table. Check if we allow this item to be researched!
                    string itemShortname = item.info.shortname;
                    if (itemBlacklist.Contains(itemShortname))
                    {
                        BasePlayer player = playerLoot.GetComponent<BasePlayer>();
                        PrintToChat(player, "Tu croyais vraiment qu'avec des bouts de ferraille t'allais pouvoir crafter รงa ? Va l'acheter chez <color=orange>l'Armurier</color> !");
                        // The item is blacklisted, don't allow it to be put in the container!
                        return false;
                    } else
                    {
                        return null;
                    }
                } else
                {
                    // Target isn't a research table so we don't want to do anything here
                    return null;
                }
            }
        }
    }
    Edit 4: No same error again and again :(
    After loot TreasureBox i have this error in my console

    Code:
    (01:45:09) | Failed to call hook 'CanMoveItem' on plugin 'BlockItemResearch v0.1.2' (NullReferenceException: Object reference not set to an instance of an object)at Oxide.Plugins.BlockItemResearch.CanMoveItem (.Item item, .PlayerInventory playerLoot, UInt32 targetContainerId, Int32 targetSlot) [0x00000] in <filename unknown>:0at Oxide.Plugins.BlockItemResearch.DirectCallHook (System.String name, System.Object& ret, System.Object[] args) [0x00000] in <filename unknown>:0at Oxide.Plugins.CSharpPlugin.InvokeMethod (Oxide.Core.Plugins.HookMethod method, System.Object[] args) [0x00000] in <filename unknown>:0at Oxide.Core.Plugins.CSPlugin.OnCallHook (System.String name, System.Object[] args) [0x00000] in <filename unknown>:0at Oxide.Core.Plugins.Plugin.CallHook (System.String hook, System.Object[] args) [0x00000] in <filename unknown>:0
     
    Last edited by a moderator: Jan 24, 2018