Skip to content
RustPluginGenerator

CanEntityBeHostile

CanEntityBeHostile is called when the game asks whether a combat entity (or player) is currently hostile, and a returned bool overrides the engine's answer. It fires from BaseCombatEntity.IsHostile and BasePlayer.IsHostile.

Signature
object CanEntityBeHostile(BaseCombatEntity entity)

Parameters

Hook parameters
ParameterTypeDescription
entityBaseCombatEntityThe combat entity whose hostility is being queried. In the BasePlayer overload it is the player being checked; otherwise it is any BaseCombatEntity such as an animal, NPC or deployed turret. It is never null at this call site.

Return value

Return a bool to replace the engine result entirely: true makes the entity hostile, false makes it non-hostile, regardless of recent damage. Return null to let the original check (unHostileTime for entities, State.unHostileTimestamp for players) decide as normal.

How it works

The hook fires inside BaseCombatEntity.IsHostile (and the overridden BasePlayer.IsHostile) every time something queries hostility. That happens when NPCs and turrets evaluate targets, when hostile AI decides whether to attack, and whenever game code checks the unHostileTime / unHostileTimestamp flag that normally marks a recent attacker as hostile. entity is the object being checked, so you can branch on entity is BasePlayer, entity is BaseNpc or specific types like BaseAnimalNPC or AutoTurret.

Oxide dispatches by parameter type: declaring BasePlayer instead of BaseCombatEntity only receives the player call, while the base type receives both. This is a bool-returning query, so return a bool to force an answer or return null to fall through to the vanilla check. Because the method is called frequently (targeting runs often), keep the hook cheap and side-effect free; avoid heavy LINQ over serverEntities or chat output.

Other signatures

Oxide also calls CanEntityBeHostile from other places in the game with these parameter types. Declare the overload you need, or several:

  • object CanEntityBeHostile(BasePlayer player)

Example plugin: Keep animals neutral unless permission allows aggression

PeacefulAnimals.cs
using System;
using Newtonsoft.Json;
using Oxide.Core;

namespace Oxide.Plugins
{
    [Info("Peaceful Animals", "RustPluginGenerator", "1.0.0")]
    [Description("Forces animals to report as non-hostile so turrets and NPCs ignore them.")]
    public class PeacefulAnimals : RustPlugin
    {
        private const string PermExempt = "peacefulanimals.exempt";

        private PluginConfig config;

        private class PluginConfig
        {
            [JsonProperty("Make animals non-hostile")]
            public bool NeutralAnimals { get; set; } = true;

            [JsonProperty("Exempt permission holders from hostility changes")]
            public bool UseExemptPermission { get; set; } = true;
        }

        protected override void LoadDefaultConfig() => config = new PluginConfig();

        protected override void LoadConfig()
        {
            base.LoadConfig();
            try
            {
                config = Config.ReadObject<PluginConfig>();
                if (config == null) throw new Exception();
            }
            catch
            {
                PrintWarning("Configuration file is invalid, using defaults");
                LoadDefaultConfig();
            }
            SaveConfig();
        }

        protected override void SaveConfig() => Config.WriteObject(config);

        private void Init()
        {
            permission.RegisterPermission(PermExempt, this);
        }

        // BaseCombatEntity.IsHostile path: covers animals, NPCs and other combat entities.
        private object CanEntityBeHostile(BaseCombatEntity entity)
        {
            if (!config.NeutralAnimals || entity == null) return null;

            var animal = entity as BaseAnimalNPC;
            if (animal == null) return null;

            return false;
        }

        // BasePlayer.IsHostile path: only players reach this overload.
        private object CanEntityBeHostile(BasePlayer player)
        {
            if (player == null || !config.UseExemptPermission) return null;
            if (player.IsNpc) return null;

            if (permission.UserHasPermission(player.UserIDString, PermExempt)) return null;

            return null;
        }
    }
}

The plugin reads a config flag and only intervenes when it is enabled and the queried entity is a BaseAnimalNPC. For animals it returns false, overriding the vanilla timer so animals never register as hostile and NPCs or turrets that rely on that check ignore them. Players return null, so their normal hostility from recent combat is untouched. A permission peacefulanimals.exempt lets selected players keep normal behaviour if an admin wants specific players to still be treated as hostile. Everything runs with a single type check and a permission lookup only for players in the exempt path, keeping the hot hook lightweight.

Common use cases

  • Neutral animals on PvE servers

    Return false for BaseAnimalNPC so turrets and hostile NPCs stop treating wildlife as a threat, while players keep vanilla hostility rules.

  • Flag players as hostile during events

    Return true for specific players during a PvP event so guards and turrets engage them even if they have not attacked anyone recently.

  • Suppress hostility during a truce

    Return false for players in a safe zone or during scheduled downtime so automated defences do not fire on recent attackers.

Pitfalls

  • This hook fires very often

    IsHostile is queried repeatedly during targeting. Do not call PrintWarning, send chat, or iterate large collections here; cache results or use cheap type and permission checks only.

  • BasePlayer and BaseCombatEntity are separate call sites

    Declaring BasePlayer means the hook is only invoked for the player override; animals and NPCs never reach it. Pick the parameter type that matches the entities you actually want to influence.

  • Returning false does not stop damage

    Hostility is a targeting signal, not a damage filter. If you need to block damage, use OnEntityTakeDamage; this hook only changes what the game reports as hostile.

Build a plugin with CanEntityBeHostile

Describe what should happen and get a compiled plugin that uses this hook.

Prompt: Neutral animals on PvE servers using the CanEntityBeHostile hook, with a config file and a permission
Generate this plugin

Edit the prompt on the next screen. 2 free plugins a month, no card.

Last updated Oct 26, 2026. Found a mistake? Email support@rustplugingenerator.com.

Build a plugin with CanEntityBeHostile

Generate