How can I forbid take an item in hand with this 'OnActiveItemChange / d' hook? Whatever I do, I get an error.
Example of OnActiveItemChange hook usage?
You'd need to provide more details as to what issue you are having with it. There is no "OnActiveItemChange/d" hook, there are OnActiveItemChange and OnActiveItemChanged.
I want to forbid take an item in hand.
for example: the item is a stone, it lies in a quick slot, I want to forbid take an item in hand
Okay, but what is the "error" and how are you using it?
private object OnActiveItemChange(BasePlayer player, Item oldItem, uint newItemId)
{
if (oldItem.info.shortname == "rock")
return false;
return null;
}Failed to call hook 'OnActiveItemChange' on plugin 'Test v0.0.0' (NullReferenceException: Object reference not set to an instance of an object)
at Oxide.Plugins.Test.OnActiveItemChange (BasePlayer player, Item oldItem, System.UInt32 newItemId) [0x00000] in <ae190f513fc642a78a2702e6e158559d>:0
at Oxide.Plugins.Test.DirectCallHook (System.String name, System.Object& ret, System.Object[] args) [0x000b8] in <ae190f513fc642a78a2702e6e158559d>:0
at Oxide.Plugins.CSharpPlugin.InvokeMethod (Oxide.Core.Plugins.HookMethod method, System.Object[] args) [0x00079] in <9affce1cd15c4ec183941adef8db1722>:0
at Oxide.Core.Plugins.CSPlugin.OnCallHook (System.String name, System.Object[] args) [0x000d8] in <4452f821def6406d834e4149849fe7ea>:0
at Oxide.Core.Plugins.Plugin.CallHook (System.String hook, System.Object[] args) [0x00060] in <4452f821def6406d834e4149849fe7ea>:0 I'd suggest adding some output that checks to see what is null. oldItem.info would be my guess.
help me please how can I forbid take an item in hand
I have already don’t understand what to do)
I have already don’t understand what to do)
This will work for what you need
private void OnActiveItemChanged(BasePlayer player, Item oldItem, Item newItem)
{
var item = player.GetActiveItem();
if (item == null) return;
if (item.info.shortname == "rock")
{
HeldEntity heldEntity = player.GetHeldEntity();
if (heldEntity != null)
{
heldEntity.SetHeld(false);
heldEntity.UpdateVisiblity_Invis();
heldEntity.SendNetworkUpdate();
}
}
} Could you please clarify, what exactly is null? To do this you should add some null checks for olditem and it's info field value. You should use Changed hook tho, since you are trying to prevent changing item TO rock. And to get new active item you can use player.GetActiveItem(), this may work too.
OnActiveItemChange -
oldItem can be null if they weren't holding something prior (empty slot to a slot with an item) so you will needed to check oldItem for null.
OnActiveItemChanged -
oldItem and newItem can be null if either the old / new slot was empty so you will need to check for null on these items.