1
0
mirror of https://github.com/RaidMax/IW4M-Admin.git synced 2025-06-26 15:13:00 -05:00

Adding Mute for IW4x (#257)

* Adding Mute for IW4x
This commit is contained in:
Amos
2022-08-26 18:09:33 +01:00
committed by GitHub
parent 423cc57986
commit c8d94ec57b
6 changed files with 208 additions and 0 deletions

View File

@ -0,0 +1,42 @@
using Data.Models.Client;
using SharedLibraryCore;
using SharedLibraryCore.Commands;
using SharedLibraryCore.Configuration;
using SharedLibraryCore.Interfaces;
namespace Mute.Commands;
public class MuteCommand : Command
{
public MuteCommand(CommandConfiguration config, ITranslationLookup translationLookup) : base(config,
translationLookup)
{
Name = "mute";
Description = translationLookup["PLUGINS_MUTE_COMMANDS_MUTE_DESC"];
Alias = "mu";
Permission = EFClient.Permission.Moderator;
RequiresTarget = true;
SupportedGames = Plugin.SupportedGames;
Arguments = new[]
{
new CommandArgument
{
Name = translationLookup["COMMANDS_ARGS_PLAYER"],
Required = true
}
};
}
private readonly MuteManager _muteManager = new();
public override async Task ExecuteAsync(GameEvent gameEvent)
{
if (await _muteManager.Mute(gameEvent))
{
gameEvent.Origin.Tell($"{_translationLookup["PLUGINS_MUTE_MUTED"]} {gameEvent.Target.Name}");
return;
}
gameEvent.Origin.Tell($"{_translationLookup["PLUGINS_MUTE_UNMUTED"]} {gameEvent.Target.Name}");
}
}

View File

@ -0,0 +1,38 @@
using Data.Models.Client;
using SharedLibraryCore.Interfaces;
using static System.Enum;
namespace Mute;
public class DataManager
{
public DataManager(IMetaServiceV2 metaService)
{
_metaService = metaService;
}
private readonly IMetaServiceV2 _metaService;
public async Task<MuteState> ReadPersistentData(EFClient client)
{
var clientMuteState = client.GetAdditionalProperty<MuteState?>(Plugin.MuteKey) ??
Parse<MuteState>((await _metaService.GetPersistentMeta(Plugin.MuteKey, client.ClientId))?
.Value ?? nameof(MuteState.Unmuted));
client.SetAdditionalProperty(Plugin.MuteKey, clientMuteState);
return clientMuteState;
}
public async Task WritePersistentData(EFClient client, MuteState state)
{
await _metaService.SetPersistentMeta(Plugin.MuteKey, state.ToString(), client.ClientId);
client.SetAdditionalProperty(Plugin.MuteKey, state);
}
}
public enum MuteState
{
Muted,
Unmuting,
Unmuted
}

20
Plugins/Mute/Mute.csproj Normal file
View File

@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Authors>MrAmos123</Authors>
<OutputType>Library</OutputType>
<Configurations>Debug;Release;Prerelease</Configurations>
<Platforms>AnyCPU</Platforms>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="RaidMax.IW4MAdmin.SharedLibraryCore" Version="2022.6.16.1"/>
</ItemGroup>
<Target Name="PostBuild" AfterTargets="PostBuildEvent">
<Exec Command="dotnet publish $(ProjectPath) -c $(ConfigurationName) -o $(ProjectDir)..\..\Build\Plugins --no-build --no-restore --no-dependencies"/>
</Target>
</Project>

View File

@ -0,0 +1,21 @@
using SharedLibraryCore;
namespace Mute;
public class MuteManager
{
public async Task<bool> Mute(GameEvent gameEvent)
{
if (await Plugin.DataManager.ReadPersistentData(gameEvent.Target) == MuteState.Muted)
{
await gameEvent.Owner.ExecuteCommandAsync($"unmute {gameEvent.Target.ClientNumber}");
await Plugin.DataManager.WritePersistentData(gameEvent.Target,
gameEvent.Target.IsIngame ? MuteState.Unmuted : MuteState.Unmuting);
return false;
}
await gameEvent.Owner.ExecuteCommandAsync($"muteClient {gameEvent.Target.ClientNumber}");
await Plugin.DataManager.WritePersistentData(gameEvent.Target, MuteState.Muted);
return true;
}
}

60
Plugins/Mute/Plugin.cs Normal file
View File

@ -0,0 +1,60 @@
using SharedLibraryCore;
using SharedLibraryCore.Interfaces;
namespace Mute;
public class Plugin : IPlugin
{
public Plugin(IMetaServiceV2 metaService)
{
DataManager = new DataManager(metaService);
}
public string Name => "Mute";
public float Version => (float) Utilities.GetVersionAsDouble();
public string Author => "Amos";
public static string MuteKey = "IW4MMute";
public static DataManager DataManager;
public static readonly Server.Game[] SupportedGames = {Server.Game.IW4};
public async Task OnEventAsync(GameEvent gameEvent, Server server)
{
if (!SupportedGames.Contains(server.GameName)) return;
switch (gameEvent.Type)
{
case GameEvent.EventType.Join:
switch (await DataManager.ReadPersistentData(gameEvent.Origin))
{
case MuteState.Muted:
await server.ExecuteCommandAsync($"muteClient {gameEvent.Origin.ClientNumber}");
break;
case MuteState.Unmuting:
await server.ExecuteCommandAsync($"unmute {gameEvent.Origin.ClientNumber}");
await DataManager.WritePersistentData(gameEvent.Origin, MuteState.Unmuted);
break;
case MuteState.Unmuted:
break;
}
break;
}
}
public Task OnLoadAsync(IManager manager)
{
return Task.CompletedTask;
}
public Task OnUnloadAsync()
{
return Task.CompletedTask;
}
public Task OnTickAsync(Server server)
{
return Task.CompletedTask;
}
}