Skip to content

Commit

Permalink
Command framework refactor completed
Browse files Browse the repository at this point in the history
  • Loading branch information
Tides committed Jan 19, 2024
1 parent 9fd8809 commit 43b0b3e
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 20 deletions.
6 changes: 0 additions & 6 deletions Obsidian.API/_Attributes/CommandRootAttribute.cs

This file was deleted.

5 changes: 3 additions & 2 deletions Obsidian/Commands/Framework/CommandHandler.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Internal;
using Microsoft.Extensions.Logging;
using Obsidian.API.Commands;
using Obsidian.API.Utilities;
using Obsidian.Commands.Builders;
using Obsidian.Commands.Framework.Entities;
Expand Down Expand Up @@ -47,7 +48,7 @@ public CommandHandler(IServiceProvider serviceProvider, ILogger<CommandHandler>?
var parserType = _argumentParsers.FirstOrDefault(x => x.GetType().BaseType?.GetGenericArguments()[0] == type)?.GetType();

if (parserType is null || Activator.CreateInstance(parserType) is not BaseArgumentParser parserInstance)
throw new Exception("No such parser registered!");
throw new Exception($"No such parser registered! {type}");

return (parserInstance.Id, parserInstance.ParserIdentifier);
}
Expand Down Expand Up @@ -105,7 +106,7 @@ public void RegisterCommands(PluginContainer pluginContainer)
this.RegisterCommandClass(pluginContainer, pluginContainer.Plugin.GetType());

// Registering commands found in the plugin assembly
var commandRoots = pluginContainer.Plugin.GetType().Assembly.GetTypes().Where(x => x.GetCustomAttributes(false).Any(y => y.GetType() == typeof(CommandRootAttribute)));
var commandRoots = pluginContainer.Plugin.GetType().Assembly.GetTypes().Where(x => x.IsSubclassOf(typeof(CommandModuleBase)));
foreach (var root in commandRoots)
{
this.RegisterCommandClass(pluginContainer, root);
Expand Down
3 changes: 2 additions & 1 deletion Obsidian/Commands/Framework/Entities/Command.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Obsidian.API.Utilities;
using Obsidian.Commands.Framework.Exceptions;
using Obsidian.Plugins;
Expand Down Expand Up @@ -64,7 +65,7 @@ public async Task ExecuteAsync(CommandContext context, string[] args)
}

var method = Overloads.FirstOrDefault(x => x.MatchParams(args)
|| x.GetParameters().Last().GetCustomAttribute<RemainingAttribute>() != null);
|| x.GetParameters().LastOrDefault()?.GetCustomAttribute<RemainingAttribute>() != null);

// Find matching overload
if (method == null)
Expand Down
8 changes: 4 additions & 4 deletions Obsidian/Commands/MainCommandModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -312,9 +312,9 @@ public async Task SendTitleAsync()
[Command("spawnentity")]
[CommandInfo("Spawns an entity", "/spawnentity [entityType]")]
[IssuerScope(CommandIssuers.Client)]
public async Task SpawnEntityAsync(CommandContext context, string entityType)
public async Task SpawnEntityAsync(string entityType)
{
if (context.Player is not IPlayer player)
if (this.Player is not IPlayer player)
return;

if (!Enum.TryParse<EntityType>(entityType, true, out var type))
Expand Down Expand Up @@ -433,9 +433,9 @@ public async Task ListAsync()
[Command("breakpoint")]
[CommandInfo("Creats a breakpoint to help debug", "/breakpoint")]
[RequirePermission(op: true)]
public async Task BreakpointAsync(CommandContext Context)
public async Task BreakpointAsync()
{
Context.Server.BroadcastMessage("You might get kicked due to timeout, a breakpoint will hit in 3 seconds!");
this.Server.BroadcastMessage("You might get kicked due to timeout, a breakpoint will hit in 3 seconds!");
await Task.Delay(3000);
Debugger.Break();
}
Expand Down
6 changes: 2 additions & 4 deletions Obsidian/Net/Packets/Play/Serverbound/ChatCommandPacket.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,14 @@ public partial class ChatCommandPacket : IServerboundPacket

public async ValueTask HandleAsync(Server server, Player player)
{
var loggerProvider = new LoggerProvider(LogLevel.Error);
var logger = loggerProvider.CreateLogger("ChatCommandPacket");
var context = new CommandContext($"/{this.Command}", new CommandSender(CommandIssuers.Client, player, logger), player, server);
var context = new CommandContext($"/{this.Command}", new CommandSender(CommandIssuers.Client, player, server._logger), player, server);
try
{
await server.CommandsHandler.ProcessCommand(context);
}
catch (Exception e)
{
logger.LogError(e, e.Message);
server._logger.LogError(e, e.Message);
}
}
}
4 changes: 2 additions & 2 deletions Obsidian/Registries/CommandsRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ public static void Register(Server server)
Type = CommandNodeType.Literal
};

foreach (var overload in cmd.Overloads.Take(1))
foreach (var overload in cmd.Overloads)
{
var args = overload.GetParameters().Skip(1); // skipping obsidian context
var args = overload.HasModule ? overload.GetParameters() : overload.GetParameters().Skip(1); // skipping obsidian context
if (!args.Any())
cmdNode.Type |= CommandNodeType.IsExecutable;

Expand Down
2 changes: 1 addition & 1 deletion Obsidian/Server.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public static string VERSION
private readonly ILoggerFactory loggerFactory;
private readonly RconServer _rconServer;
private readonly IUserCache userCache;
private readonly ILogger _logger;
internal readonly ILogger _logger;
private readonly IServiceProvider serviceProvider;

private IConnectionListener? _tcpListener;
Expand Down

0 comments on commit 43b0b3e

Please sign in to comment.