What line of code will prevent any item from being placed into a container in the hook CanAcceptItem?
Despite not knowing how to code in C#, I've been trying to figure this out on my own for a couple hours with zero previous plugin coding experience. Please help me out here.
Preventing item placement in container?
Discussion in 'Rust Development' started by Abnormal Zombie, Nov 20, 2017.
-
Wulf Community Admin
From the docs: "Returning a CanAcceptResult enum value overrides default behavior"
So you'd need to return CanAcceptResult.X (where X is the enum you want to return) from Rust.
CanAccept,
CannotAccept,
CannotAcceptRightNow -
First of all, thank you Wulf.
Here's my code. I'm getting the error: error CS0103: The name `CanAcceptResult' does not exist in the current context
Code:ItemContainer.CanAcceptResult CanAcceptItem(ItemContainer container, Item item) { if (container != null && item != null){ if (container.ShortPrefabName == "hopperoutput") { return CanAcceptResult.CannotAccept; } } return CanAcceptResult.CanAccept; }
-
Wulf Community Admin
-
Is there a way I can find what using statement I need by using JustDecompile on Assembly-CSharp.dll?
-
Wulf Community Admin
-
This might be easy for you, but for me I don't know what I'm looking for when the results come up.
-
Wulf Community Admin
-
As silly as it may sound, I didn't know that the containing class is what's used. But I figured it out:
Code:using static ItemContainer; ItemContainer.CanAcceptResult CanAcceptItem(ItemContainer container, Item item) { if (container.entityOwner.ShortPrefabName == "hopperoutput"){ return CanAcceptResult.CannotAccept; } return CanAcceptResult.CanAccept; }
-
Wulf Community Admin
Code:ItemContainer.CanAcceptResult CanAcceptItem(ItemContainer container, Item item) { return container.entityOwner.ShortPrefabName == "hopperoutput" ? ItemContainer.CanAcceptResult.CannotAccept : ItemContainer.CanAcceptResult.CanAccept; }
Alternative in the way you had it formatted:
Code:ItemContainer.CanAcceptResult CanAcceptItem(ItemContainer container, Item item) { if (container.entityOwner.ShortPrefabName == "hopperoutput") return ItemContainer.CanAcceptResult.CannotAccept else return ItemContainer.CanAcceptResult.CanAccept; }
-
I just did a try/catch to get rid of all the NullReferenceException: Object reference not set to an instance of an object errors. Is there a better way?