Skip to content

Commit

Permalink
Core Systems Refactor (#377)
Browse files Browse the repository at this point in the history
* Remove "Services"

* Add logging to service collection

* Refactor plugin related classes to use DI

* Some code cleanup

* Inject service provider to Server.cs

* Update sample plugin

* A little refactor

* Initial impl using Microsofts DI

* Some cleanup

* Fix logging stuff :)))

* Fix error

* Little refactors

* Update dependencies

* Make PluginBase inherit IAsync/Disposable

* Refactors

* Start reworking plugin manager

* Remove plugin wrapper and anything to do with reflection

* Fix errors

* More refactors

- Removes Plugin attribute and instead tries to find an embedded resource called plugin.json
- Delete more event stuff
- Refactor SamplePlugin

* IPluginConfigurationManager -> IPluginRegistry

* More plugin registry cleanup

* Start organizing/fixing all things to do with commands

Sorry for the massive commit 😭😭

* PluginContainer now manages service injection

* Update SamplePlugin.csproj

* Fix up commands

* ObjectMethodExecutor

* Cleanup

* New event system pt. 1

* Fix build error 😭

* Some organization

* Event stuff

* Code cleanup

* More cleanup

* More cleanup

* Update PluginBase.cs

* Command refactor start

* Re-do command execution

* Refactor

* PluginContainer can be null

* Fix plugins not registering data properly

* Commands now use ObjectMethodExecutor

* Fix tests

* SamplePlugin cleanup

* Command framework refactor completed

* Fix arguments not parsing correctly

* Update SamplePlugin.cs

* Code cleanup

* Update SamplePlugin.cs

* Cleanup events (they work now)

* Move chat message event stuff

* Use EventDispatcher for all events

* Some more cleanup

* Remove overridable name property

* Update EventDispatcher.cs

* Fix errors

* Update EventDispatcher.cs

* Try catch around HostedServices

* Code cleanup

* Create IExecutor.cs

* Use IExecutor for commands

* Use IExecutor for events

* Fix commands

* Whoopsie

* Fix delegate commands not reading params properly

* Fix event dispatcher 😅

* Cleanup argument parsers

* Code cleanup

* Only return when fetching relative pos

* Assign player if valid guid

* Update Client.cs

* Code cleanup

* Fix commands and commands tests

* Use DI

* throw if we didn't find a valid executor

* Some more cleanup
  • Loading branch information
Tides authored Feb 20, 2024
1 parent d20ad96 commit 7925111
Show file tree
Hide file tree
Showing 100 changed files with 2,764 additions and 2,373 deletions.
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
using Obsidian.Entities;
namespace Obsidian.API.Commands.ArgumentParsers;

namespace Obsidian.Commands.Parsers;

public class LocationTypeParser : BaseArgumentParser<VectorF>
public sealed class LocationArgumentParser : BaseArgumentParser<VectorF>
{
public LocationTypeParser() : base(10, "minecraft:vec3")
public LocationArgumentParser() : base(10, "minecraft:vec3")
{
}

public override bool TryParseArgument(string input, CommandContext context, out VectorF result)
{
var splitted = input.Split(' ');
var location = new VectorF();
var player = context.Player;

for (int i = 0; i < splitted.Length; i++)
for (var i = 0; i < splitted.Length; i++)
{
var text = splitted[i];
if (float.TryParse(text, out var floatResult))
Expand All @@ -31,25 +30,24 @@ public override bool TryParseArgument(string input, CommandContext context, out
default:
throw new IndexOutOfRangeException("Count went out of range");
}
else if (text.StartsWith("~"))
else if (text.StartsWith('~'))
{
if (context.Player is not Player player)
if (player is null)
{
result = default;
return false;
}

float.TryParse(text.Replace("~", ""), out float relative);
switch (i)
{
case 0:
location.X = player.Position.X + relative;
location.X = player.Position.X;
break;
case 1:
location.Y = player.Position.Y + relative;
location.Y = player.Position.Y;
break;
case 2:
location.Z = player.Position.Z + relative;
location.Z = player.Position.Z;
break;
default:
throw new IndexOutOfRangeException("Count went out of range");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,81 +1,81 @@
namespace Obsidian.API;
namespace Obsidian.API.Commands.ArgumentParsers;

public class SignedIntArgumentParser : BaseArgumentParser<int>
public sealed class SignedIntArgumentParser : BaseArgumentParser<int>
{
public SignedIntArgumentParser() : base(3, "brigadier:integer") { }
public override bool TryParseArgument(string input, CommandContext ctx, out int result)
=> int.TryParse(input, out result);
}

public class BoolArgumentParser : BaseArgumentParser<bool>
public sealed class BoolArgumentParser : BaseArgumentParser<bool>
{
public BoolArgumentParser() : base(0, "brigadier:bool") { }
public override bool TryParseArgument(string input, CommandContext ctx, out bool result)
=> bool.TryParse(input, out result);
}

public class UnsignedByteArgumentParser : BaseArgumentParser<byte>
public sealed class UnsignedByteArgumentParser : BaseArgumentParser<byte>
{
public UnsignedByteArgumentParser() : base(3, "brigadier:integer") { }
public override bool TryParseArgument(string input, CommandContext ctx, out byte result)
=> byte.TryParse(input, out result);
}

public class SignedByteArgumentParser : BaseArgumentParser<sbyte>
public sealed class SignedByteArgumentParser : BaseArgumentParser<sbyte>
{
public SignedByteArgumentParser() : base(3, "brigadier:integer") { }
public override bool TryParseArgument(string input, CommandContext ctx, out sbyte result)
=> sbyte.TryParse(input, out result);
}

public class SignedShortArgumentParser : BaseArgumentParser<short>
public sealed class SignedShortArgumentParser : BaseArgumentParser<short>
{
public SignedShortArgumentParser() : base(3, "brigadier:integer") { }
public override bool TryParseArgument(string input, CommandContext ctx, out short result)
=> short.TryParse(input, out result);
}

public class UnsignedShortArgumentParser : BaseArgumentParser<ushort>
public sealed class UnsignedShortArgumentParser : BaseArgumentParser<ushort>
{
public UnsignedShortArgumentParser() : base(3, "brigadier:integer") { }
public override bool TryParseArgument(string input, CommandContext ctx, out ushort result)
=> ushort.TryParse(input, out result);
}

public class UnsignedIntArgumentParser : BaseArgumentParser<uint>
public sealed class UnsignedIntArgumentParser : BaseArgumentParser<uint>
{
public UnsignedIntArgumentParser() : base(3, "brigadier:integer") { }
public override bool TryParseArgument(string input, CommandContext ctx, out uint result)
=> uint.TryParse(input, out result);
}

public class SignedLongArgumentParser : BaseArgumentParser<long>
public sealed class SignedLongArgumentParser : BaseArgumentParser<long>
{
public SignedLongArgumentParser() : base(3, "brigadier:long") { }
public override bool TryParseArgument(string input, CommandContext ctx, out long result)
=> long.TryParse(input, out result);
}

public class UnsignedLongArgumentParser : BaseArgumentParser<ulong>
public sealed class UnsignedLongArgumentParser : BaseArgumentParser<ulong>
{
public UnsignedLongArgumentParser() : base(4, "brigadier:long") { }
public override bool TryParseArgument(string input, CommandContext ctx, out ulong result)
=> ulong.TryParse(input, out result);
}

public class FloatArgumentParser : BaseArgumentParser<float>
public sealed class FloatArgumentParser : BaseArgumentParser<float>
{
public FloatArgumentParser() : base(1, "brigadier:float") { }
public override bool TryParseArgument(string input, CommandContext ctx, out float result)
=> float.TryParse(input, out result);
}
public class DoubleArgumentParser : BaseArgumentParser<double>
public sealed class DoubleArgumentParser : BaseArgumentParser<double>
{
public DoubleArgumentParser() : base(2, "brigadier:double") { }
public override bool TryParseArgument(string input, CommandContext ctx, out double result)
=> double.TryParse(input, out result);
}
public class DecimalArgumentParser : BaseArgumentParser<decimal>
public sealed class DecimalArgumentParser : BaseArgumentParser<decimal>
{
public DecimalArgumentParser() : base(3, "brigadier:integer") { }
public override bool TryParseArgument(string input, CommandContext ctx, out decimal result)
Expand Down
25 changes: 25 additions & 0 deletions Obsidian.API/Commands/ArgumentParsers/PlayerArgumentParser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
namespace Obsidian.API.Commands.ArgumentParsers;

public class PlayerArgumentParser : BaseArgumentParser<IPlayer>
{
public PlayerArgumentParser() : base(7, "minecraft:game_profile")
{
}

//TODO support selectors
public override bool TryParseArgument(string input, CommandContext context, out IPlayer result)
{
var server = context.Server;
IPlayer? player = null;

if (Guid.TryParse(input, out var guid))
// is valid GUID, try find with guid
player = server.GetPlayer(guid);
else
// is not valid GUID, try find with name
player = server.GetPlayer(input);

result = player;
return true;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Obsidian.API;
namespace Obsidian.API.Commands.ArgumentParsers;

public class StringArgumentParser : BaseArgumentParser<string>
public sealed class StringArgumentParser : BaseArgumentParser<string>
{
public StringArgumentParser() : base(5, "brigadier:string") { }
public override bool TryParseArgument(string input, CommandContext ctx, out string result)
Expand All @@ -10,9 +10,9 @@ public override bool TryParseArgument(string input, CommandContext ctx, out stri
}
}

public class GuidArgumentParser : BaseArgumentParser<Guid>
public sealed class GuidArgumentParser : BaseArgumentParser<Guid>
{
public GuidArgumentParser() : base(47, "minecraft:uuid") { }
public GuidArgumentParser() : base(49, "minecraft:uuid") { }
public override bool TryParseArgument(string input, CommandContext ctx, out Guid result)
{
return Guid.TryParse(input, out result);
Expand Down
34 changes: 34 additions & 0 deletions Obsidian.API/Commands/CommandModuleBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using Obsidian.API.Plugins;
using System.Diagnostics;

namespace Obsidian.API.Commands;
public abstract class CommandModuleBase
{
private CommandContext? commandContext;

[CommandContext]
public CommandContext CommandContext
{
get
{
if (commandContext == null)
throw new UnreachableException();//TODO empty command context maybe??

return this.commandContext;
}
set
{
ArgumentNullException.ThrowIfNull(value);

this.commandContext = value;
}
}

public IPlayer? Player => this.CommandContext.Player;
public IServer Server => this.CommandContext.Server;
public ICommandSender Sender => this.CommandContext.Sender;

public bool IsPlayer => this.CommandContext.IsPlayer;

public PluginBase? Plugin => this.CommandContext.Plugin;
}
2 changes: 1 addition & 1 deletion Obsidian.API/Events/BaseMinecraftEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/// <summary>
/// Represents the base class for classes that contain minecraft event data.
/// </summary>
public class BaseMinecraftEventArgs : AsyncEventArgs
public abstract class BaseMinecraftEventArgs : AsyncEventArgs
{
/// <summary>
/// Server this event took place in.
Expand Down
4 changes: 2 additions & 2 deletions Obsidian.API/Logging/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ internal Logger(string prefix, LogLevel minLevel = LogLevel.Debug)
Prefix = prefix;
}

public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func<TState, Exception, string> formatter)
public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter)
{
if (!IsEnabled(logLevel))
return;
Expand Down Expand Up @@ -64,7 +64,7 @@ void PrintLinePrefix()
var lines = message.Split('\n');

if (message.IsNullOrEmpty())
{
{
this.LogTrace($"Empty log message sent. Dumping stacktrace:\n{new StackTrace().ToString().Replace("\n", " ")}");
return;
}
Expand Down
4 changes: 2 additions & 2 deletions Obsidian.API/Obsidian.API.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.0" />
<PackageReference Include="SharpNoise" Version="0.12.1.1" />
</ItemGroup>

Expand Down
34 changes: 0 additions & 34 deletions Obsidian.API/Plugins/DependencyAttribute.cs

This file was deleted.

2 changes: 1 addition & 1 deletion Obsidian.API/Plugins/IPluginInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ public interface IPluginInfo
public string Name { get; }
public Version Version { get; }
public string Description { get; }
public string Authors { get; }
public string[] Authors { get; }
public Uri ProjectUrl { get; }
}
19 changes: 19 additions & 0 deletions Obsidian.API/Plugins/IPluginRegistry.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Obsidian.API.Events;

namespace Obsidian.API.Plugins;
public interface IPluginRegistry
{
public IPluginRegistry RegisterCommandArgumentHandler<T>(T parser) where T : BaseArgumentParser;

public IPluginRegistry MapCommands();
public IPluginRegistry MapCommand(string name, Delegate contextDelegate);
public IPluginRegistry MapCommand(string name, ValueTaskContextDelegate<CommandContext> contextDelegate);

public IPluginRegistry MapEvent<TEventArgs>(ValueTaskContextDelegate<TEventArgs> contextDelegate, Priority priority = Priority.Low) where TEventArgs : BaseMinecraftEventArgs;
public IPluginRegistry MapEvent(Delegate contextDelegate, Priority priority = Priority.Low);
public IPluginRegistry MapEvents();
}


//TODO better name maybe??
public delegate ValueTask ValueTaskContextDelegate<TContext>(TContext context);
42 changes: 0 additions & 42 deletions Obsidian.API/Plugins/PluginAttribute.cs

This file was deleted.

Loading

0 comments on commit 7925111

Please sign in to comment.