1. Hello. I have been trying to make a plugin work, but I cant get part of it to work. When an explosive blows up and hits an entity, it will only register the owner of said entity (building blocks or deployables) in the entity is self but not in the HitInfo bit.

    My current testing code:
    Code:
            void OnEntityTakeDamage(BaseCombatEntity entity, HitInfo info)
            {
                if(info.InitiatorPlayer != null){
                    PrintToChat("EntityOwnerId " + entity.OwnerID.ToString());
                    PrintToChat("HitEntityOwnerId " + info.HitEntity.OwnerID.ToString());
                }
    }
    Error:
    Code:
    Failed to call hook 'OnEntityTakeDamage' on plugin 'RustNotifications v0.1' (NullReferenceException: Object reference not set to an instance of an object)
    at Oxide.Plugins.RustNotifications.OnEntityTakeDamage (.BaseCombatEntity entity, .HitInfo info) [0x00000] in <filename unknown>:0
    at Oxide.Plugins.RustNotifications.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
    The InitiatorPlayer bit is so I dont get chat spammed with 0s.

    Screenshot with the code on my test server: Steam Community :: Screenshot
     
  2. Wulf

    Wulf Community Admin

    info.HitEntity may not always exist, same with OwnerID.
     
  3. I had it working fine like an hour or so ago, but then explosive bullets gave errors and now i lost the working file. :/
     
  4. Wulf

    Wulf Community Admin

    Bullets wouldn't have an OwnerID.
     
  5. No, I mean when you hit the entity with the bullet, it does say the ownerid of the entity it hit. But with explosive bullets it doesnt work right.
     
  6. Wulf

    Wulf Community Admin

    The entity arg would be the target, the HitInfo would be the bullet. The way your example code is, there's lots of room for NREs there as you're assuming both are players.
     
  7. Code:
            void OnEntityTakeDamage(BaseCombatEntity entity, HitInfo info)
            {
                if (info == null || info.Initiator == null || info.Weapon == null || info.HitEntity == null)
                {
                    return;
                }            BasePlayer player = (BasePlayer)info.Initiator;
                ulong hitEntityOwnerID = entity.OwnerID != 0 ? entity.OwnerID : info.HitEntity.OwnerID;
                if (hitEntityOwnerID == 0)
                {
                    return;
                }            // if (hitEntityOwnerID == player.userID)
                //     return;            if (entity.ShortPrefabName.Contains("target"))
                    return;            if (IO())
                {
                    string tag = Clans.Call<string>("GetClanOf", player.userID);                if (tag != null)
                    {                    var clan = GetOnlineClanMembers(tag);
                        if (clan.Contains(hitEntityOwnerID.ToString()))
                            return;                }
                }
                string name = entity.ShortPrefabName.Replace(".entity", "");
                
                if(entity is BuildingBlock)
                {
                    var block = (BuildingBlock)entity;
                    name = block?.grade.ToString() + " " + entity.ShortPrefabName.Replace(".entity", "").Replace(".", " ");
                } else
                {
                    foreach (var def in ItemManager.GetItemDefinitions())
                    {
                        if (def.shortname == entity.ShortPrefabName)
                        {
                            name = def.displayName.english;
                            break;
                        }
                    }
                }
                
                string MessageText = lang.GetMessage("BaseAttackedMessageTemplate", this, player.UserIDString)
                                                    .Replace("{Attacker}", player.displayName)
                                                    .Replace("{Owner}", GetDisplayNameByID(hitEntityOwnerID))
                                                    .Replace("{Weapon}", info.Weapon.GetOwnerItemDefinition().displayName.english)
                                                    .Replace("{Position}", String.Format("X: {0}, Y: {1}, Z:{2}", Math.Round(entity.transform.position.x, 2), Math.Round(entity.transform.position.y, 2), Math.Round(entity.transform.position.z, 2)))
                                                    .Replace("{Structure}", name)
                                                    .Replace("{Damage}", Math.Round(info.damageTypes.Total(), 2).ToString());            //get structure's percentage health remaining for check against threshold
                int PercentHealthRemaining = (int)((entity.Health() / entity.MaxHealth()) * 100);
                string rd = discordData.getID(hitEntityOwnerID);            if (IsPlayerActive(hitEntityOwnerID) && IsPlayerNotificationCooledDown(hitEntityOwnerID, NotificationType.ServerNotification, Settings.ServerConfig.NotificationCooldownInSeconds))
                {
                    if (PercentHealthRemaining <= Settings.ServerConfig.ThresholdPercentageHealthRemaining)
                    {
                        BasePlayer p = BasePlayer.activePlayerList.Find(x => x.userID == hitEntityOwnerID);
                        if (rd != null)
                        {
                            Client.DiscordServer.GetGuildMember(Client, rd, (GuildMember) =>
                            {
                                GuildMember.user.CreateDM(Client, (Channel) => {
                                    Channel.CreateMessage(Client, MessageText);
                                });
                            });
                        }
                        BasePlayer sp = BasePlayer.sleepingPlayerList.Find(x => x.userID == hitEntityOwnerID);
                        if (sp == null)
                            PrintToChat(p, MessageText);
                        if (IO())
                        {
                            string tag = Clans.Call<string>("GetClanOf", hitEntityOwnerID);                        if (tag != null)
                            {                            var clan = GetOnlineClanMembers(tag);                            foreach (String c in clan)
                                {
                                    if (p.userID.ToString() != c)
                                    {
                                        BasePlayer bp = BasePlayer.Find(c);
                                        PrintToChat(bp, MessageText);
                                    }
                                }
                            }
                        }
                    }
                }
                //Slack
                if (Settings.SlackConfig.DoNotifyWhenBaseAttacked && IsPlayerNotificationCooledDown(hitEntityOwnerID, NotificationType.SlackNotification, Settings.SlackConfig.NotificationCooldownInSeconds))
                {
                    if (PercentHealthRemaining <= Settings.SlackConfig.ThresholdPercentageHealthRemaining)
                    {
                        SendSlackNotification(player, MessageText);
                    }
                }
                //Discord
                if (Settings.DiscordConfig.DoNotifyWhenBaseAttacked && IsPlayerNotificationCooledDown(hitEntityOwnerID, NotificationType.DiscordNotification, Settings.DiscordConfig.NotificationCooldownInSeconds))
                {
                    if (PercentHealthRemaining <= Settings.DiscordConfig.ThresholdPercentageHealthRemaining)
                    {
                        if (rd != null) return;
                        SendDiscordNotification(player, MessageText);
                    }
                }
            }
    This is what it is in total.