| Failed to call hook 'OnEntityKill' on plugin 'Backpacks v3.5.2' (NullReferenceException: Object reference not set to an instance of an object)
at Oxide.Plugins.Backpacks.OnEntityKill (BasePlayer player, HitInfo info) [0x0005f] in <4bcbb2ed55c044af9d6e7caeb1a2f74f>:0
at Oxide.Plugins.Backpacks.DirectCallHook (System.String name, System.Object& ret, System.Object[] args) [0x00377] in <4bcbb2ed55c044af9d6e7caeb1a2f74f>:0
at Oxide.Plugins.CSharpPlugin.InvokeMethod (Oxide.Core.Plugins.HookMethod method, System.Object[] args) [0x00079] in <3606d2af539c45e4b5c61658e6a8b307>:0
at Oxide.Core.Plugins.CSPlugin.OnCallHook (System.String name, System.Object[] args) [0x000d8] in <c2afd8354b8b4f3ca451cf5a1aa111c3>:0
at Oxide.Core.Plugins.Plugin.CallHook (System.String hook, System.Object[] args) [0x00060] in <c2afd8354b8b4f3ca451cf5a1aa111c3>:0
(06:03:40) | Failed to run a 3.00 timer in 'Backpacks v3.5.2' (NullReferenceException: Object reference not set to an instance of an object)
at BasePlayer.SendConsoleCommand (System.String command, System.Object[] obj) [0x00000] in <93f94eb944e64b7bbcd380f87491f424>:0
at BasePlayer.ChatMessage (System.String msg) [0x00018] in <93f94eb944e64b7bbcd380f87491f424>:0
at Oxide.Plugins.Backpacks+<OnEntityKill>c__AnonStorey0.<>m__0 () [0x00000] in <4bcbb2ed55c044af9d6e7caeb1a2f74f>:0
at Oxide.Core.Libraries.Timer+TimerInstance.FireCallback () [0x00018] in <c2afd8354b8b4f3ca451cf5a1aa111c3>:0 How to drop backpacks only on suicideNot An Issue
if (!Backpack.HasBackpackFile(player.userID) || permission.UserHasPermission(player.UserIDString, KeepOnDeathPermission)) //Restore the backpack after the player dies
{
if (info.damageTypes.GetMajorityDamageType () != Rust.DamageType.Suicide) //If there is no suicide, then restore the backpack
{
timer.Once (3f, () => {player.ChatMessage ($ "Your backpack has been restored!");});
return;
}
else if (info.damageTypes.GetMajorityDamageType () == Rust.DamageType.Suicide) //If there is suicide then do not restore the backpack
{
timer.Once (3f, () => {player.ChatMessage ($ "Your backpack was not restored! \ nReason <color = # FF0000> Suicide! </color>");});
}
}Merged post
This is what I added and sometimes I get the error
To be clear, you experience this error only with your customized version of Backpacks? Customizing the plugin this way is a software term called forking. I do not recommend forking any plugin, especially one like Backpacks which is maintained, mainly because you will have to add your changes in each time you update the plugin. That extra work can ideally be avoided by using a different approach to add the behavior you want.
The preferred alternative is create a new plugin that uses hooks and API methods to integrate with Backpacks. For example, you can disable backpacks from dropping on death in the config, and then use the OnEntityDeath and OnEntityKill hooks to only call the API_DropBackpack method when the death was not a suicide. This approach is much more maintainable since you will be able to safely update the plugin in the future without any manual effort involved.
As for the cause of the NullReferenceException (NRE), I assume you must have added a HitInfo parameter to the OnEntityKill method signature. The problem is that OnEntityKill is by itself an Oxide hook, which is called when the player is killed while sleeping in a safe zone (and possibly other cases). In such a case, Oxide will call the OnEntityKill method directly with only one parameter, so any code trying to use the second parameter will result in NRE errors you are seeing.