C#:
using System;
using System.Collections.Generic;
using Oxide.Core;
using Oxide.Core.Plugins;
using UnityEngine;
using System.Linq;
namespace Oxide.Plugins
{
[Info("Restrict Teams", "&anhe", "1.0.3")]
[Description("Restricts who certain players may team with.")]
public class RestrictTeams : RustPlugin
{
#region Data
private StoredData data;
class StoredData
{
public Dictionary<ulong, HashSet<ulong>> Allowed = new Dictionary<ulong, HashSet<ulong>>();
}
private void LoadData()
{
try
{
data = Interface.Oxide.DataFileSystem.ReadObject<StoredData>(Name);
}
catch
{
data = new StoredData();
}
}
private void SaveData() =>
Interface.Oxide.DataFileSystem.WriteObject(Name, data);
#endregion
private void Init() =>
LoadData();
private bool AllowedId(ulong restrictedPlayer, ulong otherPlayer)
{
if (!data.Allowed.TryGetValue(restrictedPlayer, out var list))
return true;
return list.Contains(otherPlayer);
}
#region Hooks
private object OnTeamInvite(RelationshipManager.PlayerTeam team, BasePlayer inviter, BasePlayer target)
{
foreach (ulong memberId in team.members)
{
if (
AllowedId(memberId, target.userID) &&
AllowedId(target.userID, memberId)
)
continue;
inviter.ChatMessage($"{target.displayName} is not allowed to join this team.");
return false;
}
return null;
}
#endregion
#region Commands
private void RestrictTeamCommand(string[] args, Action<string> reply)
{
if (args == null || args.Length < 1)
{
reply("Usage: restrict_team <player_id> [other_player_id]");
return;
}
if (!ulong.TryParse(args[0], out ulong restricted))
{
reply("Invalid SteamID64.");
return;
}
switch (args.Length)
{
case 1:
{
if (data.Allowed.ContainsKey(restricted))
{
reply($"Player {restricted} is already restricted from teaming.");
return;
}
data.Allowed[restricted] = new HashSet<ulong>();
bool removedFromTeam = false;
if (RelationshipManager.ServerInstance.FindPlayersTeam(restricted) is {} team)
{
foreach (ulong memberId in team.members)
{
if (
!AllowedId(restricted, memberId) ||
!AllowedId(memberId, restricted)
) {
team.RemovePlayer(restricted);
removedFromTeam = true;
if (BasePlayer.FindByID(restricted) is {} target)
target.ChatMessage("You have been removed from your team because of a teaming restriction.");
break;
}
}
}
SaveData();
reply(
removedFromTeam
? $"Player {restricted} is now restricted from teaming and was removed from their current team."
: $"Player {restricted} is now restricted from teaming."
);
break;
}
case 2:
{
if (!ulong.TryParse(args[1], out ulong other))
{
reply("Invalid SteamID64.");
return;
}
if (data.Allowed.TryGetValue(restricted, out var list))
{
if (list.Remove(other))
{
SaveData();
reply($"Removed {other} from {restricted}'s allowed list.");
}
else
reply($"{other} was not on {restricted}'s allowed list.");
}
else
reply($"{restricted} is not restricted.");
break;
}
}
}
private void AllowTeamCommand(string[] args, Action<string> reply)
{
if (args == null || args.Length < 1)
{
reply("Usage: allow_team <player_id> [other_player_id]");
return;
}
if (!ulong.TryParse(args[0], out ulong restricted))
{
reply("Invalid SteamID64.");
return;
}
switch (args.Length)
{
case 1:
{
if (data.Allowed.Remove(restricted))
{
SaveData();
reply($"Player {restricted} is no longer restricted.");
}
else
reply($"{restricted} was not restricted.");
break;
}
case 2:
{
if (!ulong.TryParse(args[1], out ulong other))
{
reply("Invalid SteamID64.");
return;
}
if (!data.Allowed.TryGetValue(restricted, out var list))
{
reply($"Player {restricted} is not restricted from teaming.");
return;
}
if (list.Add(other))
{
SaveData();
reply($"Allowed {other} to team with {restricted}.");
}
else
reply($"{other} is already allowed.");
break;
}
}
}
[ChatCommand("restrict_team")]
private void RestrictTeamChat(BasePlayer player, string command, string[] args)
{
if (!player.IsAdmin)
return;
RestrictTeamCommand(args, msg => SendReply(player, msg));
}
[ChatCommand("allow_team")]
private void AllowTeamChat(BasePlayer player, string command, string[] args)
{
if (!player.IsAdmin)
return;
AllowTeamCommand(args, msg => SendReply(player, msg));
}
[ConsoleCommand("restrict_team")]
private void RestrictTeamConsole(ConsoleSystem.Arg arg)
{
RestrictTeamCommand(
arg.Args.Select(x => x.ToString()).ToArray(),
msg => arg.ReplyWith(msg)
);
}
[ConsoleCommand("allow_team")]
private void AllowTeamConsole(ConsoleSystem.Arg arg)
{
AllowTeamCommand(
arg.Args.Select(x => x.ToString()).ToArray(),
msg => arg.ReplyWith(msg)
);
}
#endregion
}
}