C#:
using System.Collections.Generic;
namespace Oxide.Plugins
{
[Info("Trap Whitelist", "Rogder Dodger", "1.0.1")]
[Description("Stops turrets and traps targeting players with permission")]
public class TrapWhitelist : RustPlugin
{
#region globals
private const string PermissionTurrets = "trapwhitelist.turrets";
private const string PermissionShotgunTraps = "trapwhitelist.shotguntraps";
private const string PermissionFlameTurrets = "trapwhitelist.flameturrets";
private const string PermissionAll = "trapwhitelist.all";
private readonly HashSet<ulong> _cacheAll = new HashSet<ulong>();
private readonly HashSet<ulong> _cacheTurrets = new HashSet<ulong>();
private readonly HashSet<ulong> _cacheShotgunTraps = new HashSet<ulong>();
private readonly HashSet<ulong> _cacheFlameTurrets = new HashSet<ulong>();
#endregion
#region Hooks
private void Init()
{
permission.RegisterPermission(PermissionTurrets, this);
permission.RegisterPermission(PermissionShotgunTraps, this);
permission.RegisterPermission(PermissionFlameTurrets, this);
permission.RegisterPermission(PermissionAll, this);
}
void OnServerInitialized()
{
foreach (var player in BasePlayer.activePlayerList)
BuildCache(player.userID);
}
void OnPlayerConnected(BasePlayer player) => BuildCache(player.userID);
void OnPlayerDisconnected(BasePlayer player, string reason) => PurgeCache(player.userID);
void OnUserPermissionGranted(string id, string perm) => RefreshUser(id);
void OnUserPermissionRevoked(string id, string perm) => RefreshUser(id);
void OnUserGroupAdded(string id, string group) => RefreshUser(id);
void OnUserGroupRemoved(string id, string group) => RefreshUser(id);
void OnGroupPermissionGranted(string group, string perm) => RebuildAllCaches();
void OnGroupPermissionRevoked(string group, string perm) => RebuildAllCaches();
object CanBeTargeted(BasePlayer player, AutoTurret turret)
{
ulong uid = player?.userID ?? 0UL;
if (uid == 0UL) return null;
return (_cacheAll.Contains(uid) || _cacheTurrets.Contains(uid)) ? (object)false : null;
}
object CanBeTargeted(BasePlayer player, GunTrap trap)
{
ulong uid = player?.userID ?? 0UL;
if (uid == 0UL) return null;
return (_cacheAll.Contains(uid) || _cacheShotgunTraps.Contains(uid)) ? (object)false : null;
}
object CanBeTargeted(BasePlayer player, FlameTurret turret)
{
ulong uid = player?.userID ?? 0UL;
if (uid == 0UL) return null;
return (_cacheAll.Contains(uid) || _cacheFlameTurrets.Contains(uid)) ? (object)false : null;
}
#endregion
#region Methods
private void BuildCache(ulong uid)
{
string id = uid.ToString();
if (permission.UserHasPermission(id, PermissionAll)) _cacheAll.Add(uid);
if (permission.UserHasPermission(id, PermissionTurrets)) _cacheTurrets.Add(uid);
if (permission.UserHasPermission(id, PermissionShotgunTraps)) _cacheShotgunTraps.Add(uid);
if (permission.UserHasPermission(id, PermissionFlameTurrets)) _cacheFlameTurrets.Add(uid);
}
private void PurgeCache(ulong uid)
{
_cacheAll.Remove(uid);
_cacheTurrets.Remove(uid);
_cacheShotgunTraps.Remove(uid);
_cacheFlameTurrets.Remove(uid);
}
private void RefreshUser(string id)
{
if (!ulong.TryParse(id, out ulong uid)) return;
PurgeCache(uid);
BuildCache(uid);
}
private void RebuildAllCaches()
{
_cacheAll.Clear();
_cacheTurrets.Clear();
_cacheShotgunTraps.Clear();
_cacheFlameTurrets.Clear();
foreach (var player in BasePlayer.activePlayerList)
{
BuildCache(player.userID);
}
}
#endregion
}
}