Hey,
I was wondering if anyone knows how I would get the inventory of a wooden box when hit with a hammer using the HitInfo.
Here is what I have so far:
Not sure how I would go about this, any help would be appreciated.Code:using Rust; using System.Collections.Generic; using System; using System.Reflection; using System.Data; using System.Linq; using UnityEngine; using Oxide.Core; using Oxide.Core.Configuration; using Oxide.Core.Logging; using Oxide.Core.Plugins; namespace Oxide.Plugins { [Info("EasyTransfer", "Panda", 1.0)] class EasyTransfer : RustPlugin { void OnHammerHit(BasePlayer player, HitInfo info) { if(info.HitEntity.gameObject.name.ToString() == "assets/prefabs/deployable/woodenbox/woodbox_deployed.prefab") { Puts("It is a wooden box!"); } } }
Thanks![]()
Getting inventory from hitinfo?
Discussion in 'Rust Development' started by Panday, Jul 7, 2016.
-
From there, you can use container.inventory.Code:
void OnHammerHit(BasePlayer player, HitInfo info) { var name = info?.HitEntity?.LookupShortPrefabNameWithoutExtension() ?? string.Empty; if (!name.Contains("woodbox")) return; var container = info?.HitEntity?.GetComponent<StorageContainer>() ?? null; if (container == null) return; } -
Thanks for the reply, also it doesn't seem to work, unless I did something wrong heres what I got:
It justs says 'ItemContainer' in the console.Code:void OnHammerHit(BasePlayer player, HitInfo info) { if (info.HitEntity.GetType().ToString() == "StorageContainer") { var container = info?.HitEntity?.GetComponent<StorageContainer>() ?? null; if (container == null) return; SendReply(player, "{0}", container.inventory); } else { return; } }
Also may I ask what does the question marks do when after info? and HitEntity? . -
They are null operators, and they default to the value you supply at the end with ?? if it is null. Read more here:
?? Operator (C# Reference)
As for your code, I assumed you knew what to do once you got the inventory. You're just sending a reply of the type of inventory to a string. If you're wanting to get a list of items, do this:
If you wish to modify any of the items in that foreach loop, you'll have to convert it to a new list/array, like this:Code:foreach(var item in container.inventory.itemList) { SendReply(player, item.info.displayName.english); }
Code:foreach(var item in container.inventory.itemlist.ToList()) { //do things }
