-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommandToInvite.cs
162 lines (149 loc) · 7.12 KB
/
CommandToInvite.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
//check out TeamHandler in this repo for a plugin with more team control using commands. prevents accidental leaving of team and invite, since there is no confirm for leave team, I made it a command instead.
//████████╗ █████╗ ██████╗███╗ ███╗ █████╗ ███╗ ██╗
//╚══██╔══╝██╔══██╗██╔════╝████╗ ████║ ██╔══██╗████╗ ██║
// ██║ ███████║██║ ██╔████╔██║ ███████║██╔██╗ ██║
// ██║ ██╔══██║██║ ██║╚██╔╝██║ ██╔══██║██║╚██╗██║
// ██║ ██║ ██║╚██████╗██║ ╚═╝ ██║ ██║ ██║██║ ╚████║
// ╚═╝ ╚═╝ ╚═╝ ╚═════╝╚═╝ ╚═╝ ╚═╝ ╚═╝╚═╝ ╚═══╝
using Oxide.Core;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
namespace Oxide.Plugins
{
[Info("Command To Invite", "Tacman", "1.6.0")]
[Description("Allows players to send team invites to other players")]
class CommandToInvite : RustPlugin
{
private void SendInvite(BasePlayer sender, BasePlayer target)
{
RelationshipManager.PlayerTeam playerTeam = sender.Team;
// Create a team if the sender is not already part of one
if (playerTeam == null)
{
if (!TryCreateTeam(sender))
{
sender.ChatMessage("Failed to create team.");
return;
}
playerTeam = sender.Team;
}
// Check if team is full
if (playerTeam.members.Count >= 8)
{
sender.ChatMessage("Your team is already full.");
return;
}
// Check if sender is team leader
if (!playerTeam.GetLeader().Equals(sender))
{
sender.ChatMessage("You are not the team leader.");
return;
}
// Check for valid players
if (target == null)
{
sender.ChatMessage("Player not found");
return;
}
// Check if player is inviting self
if (target == sender)
{
sender.ChatMessage("You cannot invite yourself to the team");
}
// Check if player is already in the inviting team
RelationshipManager.PlayerTeam targetTeam = target.Team;
if (targetTeam != null && targetTeam == playerTeam)
{
sender.ChatMessage($"{target.displayName} is already in your team.");
return;
}
// Check if player is already in another team
if (targetTeam != null)
{
sender.ChatMessage($"{target.displayName} is already in a team.");
return;
}
playerTeam.SendInvite(target);
sender.ChatMessage($"You have invited {target.displayName} to your team.");
target.ChatMessage($"{sender.displayName} has invited you to join their team from afar. Please press tab to accept or decline");
}
private bool TryCreateTeam(BasePlayer player)
{
if (global::RelationshipManager.maxTeamSize == 0)
{
player.ChatMessage("Teams are disabled on this server");
return false;
}
if (player.currentTeam != 0UL) { return false; }
RelationshipManager.PlayerTeam playerTeam = RelationshipManager.ServerInstance.CreateTeam();
playerTeam.teamLeader = player.userID;
playerTeam.AddPlayer(player);
return true;
}
// /invite command for sending team invites
[ChatCommand("invite")]
private void InviteCommand(BasePlayer player, string command, string[] args)
{
if (args.Length != 1)
{
player.ChatMessage("Usage: /invite <player name or ID>");
return;
}
string playerName = args[0];
List<BasePlayer> players = BasePlayer.activePlayerList
.Where(x => x.displayName.ToLower().Contains(playerName.ToLower()))
.ToList();
if (players.Count == 0) { player.ChatMessage("Player not found."); }
else if (players.Count > 1)
{
var distinctPlayers = players.Select(x => x.displayName).Distinct().ToList();
if (distinctPlayers.Count > 1) { player.ChatMessage($"Multiple players found. Please refine your search criteria."); return; }
SendInvite(player, players.First());
}
else { SendInvite(player, players[0]); }
}
// /leaveteam command for leaving a team
[ChatCommand("leaveteam")]
private void LeaveTeamCommand(BasePlayer player, string command, string[] args)
{
if (player.currentTeam == 0UL)
{
player.ChatMessage("You are not in a team.");
return;
}
RelationshipManager.PlayerTeam playerTeam = player.Team;
if (playerTeam != null && playerTeam.GetLeader() == player)
{
// Prevent team leader from leaving directly, handle disbanding first
player.ChatMessage("You have left the team");
playerTeam.RemovePlayer(player.userID);
player.ClearTeam();
}
// Remove the player from the team and clear their team
if (playerTeam != null && playerTeam.GetLeader() != player)
{
playerTeam.RemovePlayer(player.userID);
player.ClearTeam();
player.ChatMessage("You have left the team.");
}
}
// Intercepting the team leave action (BLOCK IN-GAME BUTTON ACTION)
private object OnTeamLeave(RelationshipManager.PlayerTeam team, BasePlayer player)
{
// Block leave action if player is not the leader
if (team.GetLeader() != player)
{
player.ChatMessage("To leave your team, use the /leaveteam command.");
return true; // Returning true blocks the leave action via the in-game button
}
// If the player is the team leader, prevent leaving until they disband the team
if (team.GetLeader() == player)
{
player.ChatMessage("Please use /leaveteam instead");
return true; // Block the leave action as the team leader cannot leave with members
}
return null; // Allow the team leave action to proceed if all conditions are met
}
}
}