Skip to content

Commit 02cad38

Browse files
Micky5991dr1zzle
authored and
dr1zzle
committed
Avoid repetition by creating method that checks client first
1 parent 0d5eb26 commit 02cad38

File tree

1 file changed

+10
-16
lines changed

1 file changed

+10
-16
lines changed

JustAnotherVoiceChat.Server.GTMP/src/Elements/Server/GtmpVoiceServer.Events.cs

+10-16
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
* SOFTWARE.
2626
*/
2727

28+
using System;
2829
using GrandTheftMultiplayer.Server.Constant;
2930
using GrandTheftMultiplayer.Server.Elements;
3031
using JustAnotherVoiceChat.Server.GTMP.Interfaces;
@@ -98,35 +99,28 @@ private void OnPlayerDisconnect(Client player, string reason)
9899

99100
private void OnVoiceClientConnected(IVoiceClient client)
100101
{
101-
var voiceClient = client as IGtmpVoiceClient;
102-
if (voiceClient == null)
103-
{
104-
return;
105-
}
106-
107-
OnClientConnected?.Invoke(voiceClient);
102+
TriggerOnClient(client, c => OnClientConnected?.Invoke(c));
108103
}
109104

110105
private void OnVoiceClientDisconnected(IVoiceClient client)
111106
{
112-
var voiceClient = client as IGtmpVoiceClient;
113-
if (voiceClient == null)
114-
{
115-
return;
116-
}
117-
118-
OnClientDisconnected?.Invoke(voiceClient);
107+
TriggerOnClient(client, c => OnClientDisconnected?.Invoke(c));
119108
}
120109

121110
private void OnVoiceClientTalkingChanged(IVoiceClient client, bool newStatus)
111+
{
112+
TriggerOnClient(client, c => OnClientTalkingChanged?.Invoke(c, newStatus));
113+
}
114+
115+
private void TriggerOnClient(IVoiceClient client, Action<IGtmpVoiceClient> callback)
122116
{
123117
var voiceClient = client as IGtmpVoiceClient;
124118
if (voiceClient == null)
125119
{
126120
return;
127121
}
128-
129-
OnClientTalkingChanged?.Invoke(voiceClient, newStatus);
122+
123+
callback(voiceClient);
130124
}
131125

132126
public void TriggerOnClientConnectedEvent(ushort handle)

0 commit comments

Comments
 (0)