Skip to content
RustPluginGenerator

CanLivestockAnimalRefuseTarget

CanLivestockAnimalRefuseTarget is called when a Gen2 livestock animal (cow, pig, chicken, horse-style AI) decides whether to ignore a player as a target. Returning a bool overrides the game's decision; returning null keeps vanilla behavior.

Signature
object CanLivestockAnimalRefuseTarget(Rust.Ai.Gen2.LivestockAnimal livestockAnimal, BasePlayer player)

Parameters

Hook parameters
ParameterTypeDescription
livestockAnimalRust.Ai.Gen2.LivestockAnimalThe Rust.Ai.Gen2.LivestockAnimal AI entity deciding whether to refuse the player as a target. Use it to read its state, position or IsMale/IsAdult checks and to identify which animal the call is about.
playerBasePlayerThe BasePlayer the animal is evaluating. May be null in unusual cases, so null-check it before touching inventory, team or building data.

Return value

Return a bool to override the vanilla refusal decision: true makes the animal refuse the player as a target, false makes it accept them as a target. Return null (or any non-bool value) to let the game run its own checks unchanged.

How it works

The hook fires inside Rust.Ai.Gen2.LivestockAnimal.RefusesToTarget, the method the AI runs whenever it weighs a BaseEntity as a potential target. When that entity is a BasePlayer, the game calls the hook before running its own logic (soreness, gender, adulthood, familiarity and similar checks).

The first parameter is the animal whose AI is evaluating the target, which is useful for position checks, prefab checks or per-animal config. The second is the player being considered. Because the animal parameter is typed Rust.Ai.Gen2.LivestockAnimal, only calls from that AI class reach your method.

Use the hook to make animals refuse (or stop refusing) particular players: staff protection, safe-zone bases, a taming system, or preventing farm animals from retaliating against owners. Returning true means the animal refuses that player as a target; returning false forces it to treat the player as a valid target. Returning null leaves the decision to the game. The call is frequent while the AI evaluates targets, so keep the body cheap and avoid Puts/PrintWarning there.

Example plugin: Protect animal owners and their teammates from their own livestock

LivestockOwnerProtection.cs
using System.Linq;
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using Oxide.Core;
using UnityEngine;

namespace Oxide.Plugins
{
    [Info("Livestock Owner Protection", "RustPluginGenerator", "1.0.0")]
    [Description("Prevents Gen2 livestock animals from targeting their owners and teammates.")]
    public class LivestockOwnerProtection : RustPlugin
    {
        private const string PermBypass = "livestockownerprotection.bypass";

        private PluginConfig config;

        private class PluginConfig
        {
            [JsonProperty("Protect tool cupboard owners")]
            public bool ProtectCupboardAuthed { get; set; } = true;

            [JsonProperty("Protect team members")]
            public bool ProtectTeamMembers { get; set; } = true;

            [JsonProperty("Permission bypasses protection")]
            public bool AllowPermissionBypass { 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(PermBypass, this);
        }

        private object CanLivestockAnimalRefuseTarget(Rust.Ai.Gen2.LivestockAnimal livestockAnimal, BasePlayer player)
        {
            if (livestockAnimal == null || player == null || player.IsNpc) return null;

            if (config.AllowPermissionBypass && permission.UserHasPermission(player.UserIDString, PermBypass))
                return null;

            if (config.ProtectCupboardAuthed)
            {
                var privilege = livestockAnimal.GetBuildingPrivilege();
                if (privilege != null && privilege.IsAuthed(player))
                    return true;
            }

            if (config.ProtectTeamMembers && player.currentTeam != 0)
            {
                var team = RelationshipManager.ServerInstance.FindTeam(player.currentTeam);
                if (team != null)
                {
                    var owner = BasePlayer.FindByID(livestockAnimal.OwnerID);
                    if (owner != null && team.members.Contains(owner.userID.Get()))
                        return true;
                }
            }

            return null;
        }
    }
}

The plugin makes livestock ignore the player who owns or shares a tool cupboard with the animal, plus members of the caller's team, so farm animals never turn on the players who actually keep them. CanLivestockAnimalRefuseTarget checks a config toggle, validates both parameters, then resolves the animal's privilege via GetBuildingPrivilege() and tests IsAuthed against the player and their team members from RelationshipManager. When a match is found it returns true so the animal refuses the target, otherwise null keeps vanilla behavior. A permission (livestockownerprotection.bypass) lets admins or builders ignore the protection, and chat commands let staff toggle the feature at runtime without restarting the server.

Common use cases

  • Stop farm animals attacking their owners

    Return true for players authed on the tool cupboard covering the animal, or in the caller's team, so milking, feeding and breeding never end with a kicked cow.

  • Staff and spectator immunity

    Refuse every player holding a permission so admins inspecting a farm are not chased, without removing the animals from the world.

  • Aggressive livestock for a PvP server

    Return false for players inside a marked raid zone to stop the animal from ignoring a valid attacker, instead of relying on vanilla soreness and familiarity checks.

Pitfalls

  • This is not a damage hook

    The hook only affects target selection. If the animal already has a target or the player damages it, aggro can still happen through other AI paths, so pair it with OnEntityTakeDamage if you need full immunity.

  • Runs frequently

    RefusesToTarget is evaluated repeatedly while the AI scans for targets. Cache team lookups or permission results per player rather than doing heavy work on every call, and never log inside the hook.

  • Only Gen2 livestock

    The signature is fixed to Rust.Ai.Gen2.LivestockAnimal, so wolves, bears and older animal AI never reach this hook. Do not declare BaseEntity thinking it will widen the scope, since the game passes the concrete type.

Build a plugin with CanLivestockAnimalRefuseTarget

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

Prompt: Stop farm animals attacking their owners using the CanLivestockAnimalRefuseTarget 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 CanLivestockAnimalRefuseTarget

Generate