Repository navigation
Expand file tree
/
Copy pathVoiceActivity.cs
More file actions
164 lines (129 loc) · 4.71 KB
/
Copy pathVoiceActivity.cs
File metadata and controls
164 lines (129 loc) · 4.71 KB
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
163
164
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Facepunch;
using Oxide.Core;
using Oxide.Core.Plugins;
using Oxide.Core.Libraries.Covalence;
using UnityEngine;
using Random=System.Random;
using Network;
using Newtonsoft.Json;
using Rust;
namespace Oxide.Plugins
{
[Info("VoiceActivity", "Mheetu", "1.0.0")]
[Description("Finds players who have recently spoken in game.")]
public class VoiceActivity : RustPlugin
{
#region Initialization
private readonly Dictionary<ulong, Timer> voices = new Dictionary<ulong, Timer>();
private readonly Dictionary<string, string> defaultLang = new Dictionary<string, string> {
{"Permission: Denied", "You do not have permission to execute this command."},
{"Recently Spoke", "Recently Spoke:"},
{"No Recent Speakers", "Nobody has spoken recently."},
};
new void LoadDefaultMessages ()
{
lang.RegisterMessages(defaultLang, this);
}
void OnServerInitialized ()
{
try {
permission.RegisterPermission("voiceactivity.use", this);
} catch (Exception ex) {
PrintError("OnServerInitialized failed: {0}", ex.Message);
}
}
#endregion Initialization
#region Game Hooks
void OnPlayerVoice(BasePlayer player, Byte[] data)
{
ulong userId = player.userID;
if (voices.ContainsKey(userId)) {
voices[userId].Reset();
} else {
voices.Add(userId, timer.Once(10f, () => voices.Remove(userId)));
}
}
private void OnPlayerDeath(BasePlayer player, HitInfo info)
{
removePlayer(player);
}
private void OnPlayerRespawned(BasePlayer player)
{
removePlayer(player);
}
private void OnPlayerSleep(BasePlayer player)
{
removePlayer(player);
}
private void OnPlayerDisconnected(BasePlayer player, string reason)
{
removePlayer(player);
}
private void Unload()
{
voices.Clear();
}
#endregion Game Hooks
#region Console Commands
[ConsoleCommand("vshow")]
private void VocalCommand(ConsoleSystem.Arg arg)
{
if (arg.Player() == null) {
return;
}
BasePlayer player = arg.Player();
if ( ! hasPermissionTo(player, "use")) {
PrintToConsole(__("Permission: Denied", (player as BasePlayer)));
return;
}
if (voices.Count <= 0) {
PrintToConsole(__("No Recent Speakers", (player as BasePlayer)));
return;
}
PrintToConsole(__("Recently Spoke", (player as BasePlayer)));
foreach (ulong playerId in voices.Keys) {
IPlayer vocalPlayer = covalence.Players.FindPlayerById(playerId.ToString());
PrintToConsole(playerId.ToString() + "\t" + vocalPlayer.Name);
}
}
[ConsoleCommand("vspec")]
private void VocalSpectateCommand(ConsoleSystem.Arg arg)
{
if (arg.Player() == null) {
return;
}
BasePlayer player = arg.Player();
if ( ! hasPermissionTo(player, "use")) {
PrintToConsole(__("Permission: Denied", (player as BasePlayer)));
return;
}
List<ulong> recentSpeakers = new List<ulong>(voices.Keys);
recentSpeakers.Remove(player.userID);
if (recentSpeakers.Count <= 0) {
PrintToConsole(__("No Recent Speakers", (player as BasePlayer)));
return;
}
Random rand = new Random();
ulong randomPlayerId = recentSpeakers[rand.Next(recentSpeakers.Count)];
player.SendConsoleCommand("spectate " + randomPlayerId.ToString());
}
#endregion Chat Commands
#region Helpers
private bool hasPermissionTo(BasePlayer player, string permissionName) => permission.UserHasPermission(player.UserIDString, "voiceactivity." + permissionName);
private string __(string key, BasePlayer player = null) => lang.GetMessage(key, this, player == null ? null : player.UserIDString);
#endregion Helpers
#region Utility Methods
private void removePlayer(BasePlayer player)
{
if (voices.ContainsKey(player.userID)) {
voices[player.userID]?.Destroy();
voices.Remove(player.userID);
}
}
#endregion Utility Methods
}
}