Skip to content

Commit

Permalink
Use DI
Browse files Browse the repository at this point in the history
  • Loading branch information
Tides committed Jan 26, 2024
1 parent 619eff1 commit b4efc52
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 5 deletions.
4 changes: 2 additions & 2 deletions Obsidian.Tests/Commands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ public void TestTokenizing()
Assert.Equal(split, expected);
}

// TODO overload support is not here yet, there has to be a loop through qualified commands in CommandHandler.cs:executeCommand
[Fact]
public async Task TestCommandExec()
{
var services = new ServiceCollection()
.AddLogging((builder) => builder.AddXUnit(this.output))
.AddSingleton<CommandHandler>()
.BuildServiceProvider();

var cmd = new CommandHandler(services, services.GetRequiredService<ILogger<CommandHandler>>());
var cmd = services.GetRequiredService<CommandHandler>();

ICommandSender sender = new CommandSender(CommandIssuers.Console, player: null, logger: null);

Expand Down
4 changes: 2 additions & 2 deletions Obsidian/Commands/Framework/CommandHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ namespace Obsidian.Commands.Framework;

public sealed class CommandHandler
{
internal readonly ILogger? logger;
internal readonly ILogger logger;

private readonly List<Command> _commands;
private readonly CommandParser _commandParser;
private readonly List<BaseArgumentParser> _argumentParsers;

public IServiceProvider ServiceProvider { get; }

public CommandHandler(IServiceProvider serviceProvider, ILogger<CommandHandler>? logger = null)
public CommandHandler(IServiceProvider serviceProvider, ILogger<CommandHandler> logger)
{
_commandParser = new CommandParser(CommandHelpers.DefaultPrefix);
_commands = [];
Expand Down
4 changes: 3 additions & 1 deletion Obsidian/Server.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Connections;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Obsidian.API.Boss;
Expand Down Expand Up @@ -103,6 +104,7 @@ public static string VERSION
RconServer rconServer,
IUserCache playerCache,
EventDispatcher eventDispatcher,
CommandHandler commandHandler,
IServiceProvider serviceProvider)
{
Configuration = environment.Configuration;
Expand All @@ -121,7 +123,7 @@ public static string VERSION

_logger.LogDebug(message: "Initializing command handler...");

CommandsHandler = new CommandHandler(serviceProvider, loggerFactory.CreateLogger<CommandHandler>());
CommandsHandler = commandHandler;

PluginManager = new PluginManager(this.serviceProvider, this, eventDispatcher, CommandsHandler, _logger);

Expand Down

0 comments on commit b4efc52

Please sign in to comment.