namespace Oxide.Plugins;

[Info("No Unauthorised Damage", "&anhe", "1.1.7")]
[Description("Prevents players from damaging other players’ belongings.")]
public class NoUnauthorisedDamage : RustPlugin
{
    private object OnEntityTakeDamage(BaseCombatEntity target, HitInfo info) =>
        // Allow if
        (
            // World
            target.OwnerID == 0 ||
            // Player’s
            (
                (
                    info.InitiatorPlayer ??
                    BasePlayer.FindByID(info.Initiator?.OwnerID ?? 0)
                ) is BasePlayer attacker &&
                (
                    // Yours
                    target.OwnerID == attacker.userID ||
                    // Team’s
                    attacker.Team
                        ?.members.Contains(target.OwnerID) == true ||
                    // Authorised
                    target.GetBuildingPrivilege()
                        ?.IsAuthed(attacker) == true ||
                    // Admin
                    attacker.IsAdmin
                )
            ) ||
            // Decay
            info?.damageTypes?.Has(Rust.DamageType.Decay) == true
        )
            ? null : false;
}