Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .idea/.idea.DaemonMC/.idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/.idea.DaemonMC/.idea/.name

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/.idea.DaemonMC/.idea/AndroidProjectSystem.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .idea/.idea.DaemonMC/.idea/encodings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/.idea.DaemonMC/.idea/indexLayout.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/.idea.DaemonMC/.idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

104 changes: 79 additions & 25 deletions DaemonMC/CommandManager.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using System.Numerics;
using DaemonMC;
using DaemonMC.Network;
using DaemonMC.Network.Bedrock;
using DaemonMC.Network.Enumerations;
using DaemonMC.Plugin;
using DaemonMC.Utils;
using DaemonMC.Utils.Game;
using DaemonMC.Utils.Text;
Expand All @@ -9,7 +12,7 @@ namespace DaemonMC
{
public class CommandManager
{
public static List<Command> AvailableCommands { get; set; } = new List<Command>();
public static Dictionary<string, (Plugin.Plugin Plugin, Command Command)> AvailableCommands { get; set; } = new();
public static List<string> EnumValues { get; set; } = new List<string>();
public static List<CommandEnum> RealEnums { get; set; } = new List<CommandEnum>();

Expand All @@ -30,30 +33,57 @@ public class CommandManager
{ typeof(Player), "target" },
{ typeof(Vector3), "x y z" },
};

public static List<Command> GetAvailableCommands() {
return AvailableCommands.Values.Select(x => x.Command).ToList();
}

public static List<Command> GetCommandsByPlugin(Plugin.Plugin plugin) {
return AvailableCommands.Where(kvp => kvp.Value.Plugin == plugin).Select(kvp => kvp.Value.Command).ToList();
}

public static void Register(Command command, Action<CommandAction> commandFunction)
{
var regCommand = AvailableCommands.FirstOrDefault(p => p.Name == command.Name);
public static void Register(Plugin.Plugin plugin, Command command, Action<CommandAction> commandFunction) {

if (plugin == null!) {
Log.warn($"Cannot register command '{command.Name}' without a plugin reference.");
return;
}

var existingEntry = AvailableCommands.FirstOrDefault(p => p.Value.Plugin == plugin && p.Value.Command.Name == command.Name);

if (regCommand != null)
if (existingEntry.Key != null)
{
bool overloadExists = regCommand.Overloads.Any(existingOverload => existingOverload.Count == command.Parameters.Count && !existingOverload.Where((param, index) => param.Name != command.Parameters[index].Name || param.Type != command.Parameters[index].Type).Any());
var existingCommand = existingEntry.Value.Command;
bool overloadExists = existingCommand.Overloads.Any(existingOverload =>
existingOverload.Count == command.Parameters.Count &&
!existingOverload.Where((param, index) =>
param.Name != command.Parameters[index].Name ||
param.Type != command.Parameters[index].Type).Any());

if (overloadExists)
{
Log.warn($"Couldn't register {command.Name}. Command already registered.");
Log.warn($"Couldn't register {command.Name}. Command already registered by plugin {plugin.GetName()}.");
return;
}
else
{
regCommand.Overloads.Add(command.Parameters);
regCommand.CommandFunction.Add(commandFunction);
existingCommand.Overloads.Add(command.Parameters);
existingCommand.CommandFunction.Add(commandFunction);
}
}
else
{
// Cerca se il comando esiste per altri plugin
var conflict = AvailableCommands.FirstOrDefault(p => p.Value.Command.Name == command.Name);
if (conflict.Key != null)
{
Log.warn($"Command name '{command.Name}' is already registered by plugin {conflict.Value.Plugin.GetName()}. Use a different name.");
return;
}

command.Overloads.Add(command.Parameters);
command.CommandFunction.Add(commandFunction);
AvailableCommands.Add(command);
AvailableCommands.Add(command.Name, (plugin, command));
}

foreach (var param in command.Parameters)
Expand All @@ -71,13 +101,31 @@ public static void Register(Command command, Action<CommandAction> commandFuncti

public static void Unregister(string commandName)
{
var cmd = AvailableCommands.FirstOrDefault(p => p.Name == commandName);
if (cmd == null)
if (AvailableCommands.Remove(commandName))
{
Log.debug($"Command '{commandName}' unregistered successfully.");
}
else
{
Log.warn($"Couldn't unregister {commandName}. Command not found.");
return;
}
AvailableCommands.Remove(cmd);
}

public static void UnregisterAll(Plugin.Plugin plugin)
{
if (plugin == null) return;

var commandsToRemove = AvailableCommands
.Where(kvp => kvp.Value.Plugin == plugin)
.Select(kvp => kvp.Key)
.ToList();

foreach (var commandName in commandsToRemove)
{
AvailableCommands.Remove(commandName);
}

Log.debug($"Unregistered {commandsToRemove.Count} commands for plugin {plugin.GetName()}");
}

public static int GetSymbol(Type type, int enumIndex = -1)
Expand All @@ -97,6 +145,7 @@ public static int GetSymbol(Type type, int enumIndex = -1)

public static void Execute(string command, Player player)
{

if (string.IsNullOrWhiteSpace(command)) return;

string[] commandParts = command.Split(' ');
Expand All @@ -105,13 +154,14 @@ public static void Execute(string command, Player player)
string commandName = commandParts[0];
string[] argParts = commandParts.Skip(1).ToArray();

var regCommand = AvailableCommands.FirstOrDefault(c => c.Name == commandName);
if (regCommand == null)
if (!AvailableCommands.TryGetValue(commandName, out var commandEntry))
{
player.SendMessage($"§cUnknown command: {commandName}");
return;
}

var regCommand = commandEntry.Command;

for (int i = 0; i < regCommand.Overloads.Count; i++)
{
var overload = regCommand.Overloads[i];
Expand Down Expand Up @@ -169,22 +219,26 @@ public static void Execute(string command, Player player)
}

Log.debug($"Failed command request: {command}");
player.SendMessage("§cIncorrect usage. Available parameters:");
player.SendMessage($"{TextFormat.Red}Incorrect usage. Available parameters:");
foreach (var overload in regCommand.Overloads)
{
var parameters = string.Join(" ", overload.Select(p => $"<{p.Name}: {(p.Type == typeof(EnumP) ? string.Join(" | ", p.Values) : typeStringMap[p.Type])}>"));
player.SendMessage($"§f/{regCommand.Name} §a{parameters}");
player.SendMessage($"{TextFormat.White}/{regCommand.Name} {TextFormat.Green}{parameters}");
}
}

public static void RegisterBuiltinCommands()
{
Register(new Command("about", "Information about the server"), about);
Register(new Command("pos", "Your current position"), position);
public static void RegisterBuiltinCommands() {
RegisterInternal(new Command("about", "Information about the server"), about);
RegisterInternal(new Command("pos", "Your current position"), position);
}

private static void RegisterInternal(Command command, Action<CommandAction> commandFunction) {
command.Overloads.Add(command.Parameters);
command.CommandFunction.Add(commandFunction);
AvailableCommands.Add(command.Name, (null!, command));
}

public static void about(CommandAction action)
{
public static void about(CommandAction action) {
action.Player.SendMessage($"§k§r§7§lDaemon§8MC§r§k§r {DaemonMC.Version} \n§r§fProject URL: §agithub.com/laz1444/DaemonMC \n§r§fGit hash: §a{DaemonMC.GitHash} \n§r§fEnvironment: §a.NET{Environment.Version}, {Environment.OSVersion} \n§r§fSupported MCBE versions: §a{string.Join(", ", Info.ProtocolVersion)}");
}

Expand Down Expand Up @@ -217,4 +271,4 @@ public CommandEnum(string name, List<string> values)
Values = values;
}
}
}
}
2 changes: 2 additions & 0 deletions DaemonMC/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class Config
public bool ForcePacks { get; set; } = false;
public bool XboxAuth { get; set; } = true;
public bool Debug { get; set; } = false;
public bool HotReloading { get; set; } = true;

public static void Set()
{
Expand Down Expand Up @@ -57,6 +58,7 @@ public static void Set()
DaemonMC.GameMode = ToGameMode(config.DefaultGamemode);
DaemonMC.DrawDistance = config.DrawDistance;
DaemonMC.UnloadChunks = config.UnloadChunks;
DaemonMC.HotReloading = config.HotReloading;
Server.Port = config.Port;
JWT.XboxAuth = config.XboxAuth;
ResourcePackManager.ForcePacks = config.ForcePacks;
Expand Down
16 changes: 11 additions & 5 deletions DaemonMC/DaemonMC.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using DaemonMC.Utils.Game;
using System.Numerics;
using System.Reflection;
using DaemonMC.Plugin;

namespace DaemonMC
{
Expand All @@ -17,6 +18,7 @@ public static class DaemonMC
public static int GameMode = 0;
public static int DrawDistance = 10;
public static bool UnloadChunks = true;
public static bool HotReloading = true;
internal static string Version = "unknown";
internal static string GitHash = "unknown";
public static void Main()
Expand Down Expand Up @@ -69,17 +71,17 @@ static void OnExit(object? sender, EventArgs e)
Server.ServerClose();
}

static void Command()
{
string cmd = Console.ReadLine();
switch (cmd)
{
private static void Command() {

var cmd = Console.ReadLine();
switch (cmd) {
case "/help":
Log.line();
Log.info("/shutdown - Close server");
Log.info("/dev - Debugging mode");
Log.info("/list - Player list");
Log.info("/liste - Entity list");
Log.info("/plugins - Plugins list");
Log.line();
break;
case "/list":
Expand All @@ -91,6 +93,10 @@ static void Command()
case "/shutdown":
Server.ServerClose();
break;
case "/plugins":
var plugins = PluginManager.GetLoadedPlugins();
Log.info($"Plugins list ({plugins.Count}): " + string.Join(", ", plugins.Select(x => $"{x.PluginInstance.GetName()} v{x.PluginInstance.GetVersion()}")));
break;
case "/dev":
Log.line();
Log.warn("================== DaemonMC Debugging mode ==================");
Expand Down
2 changes: 2 additions & 0 deletions DaemonMC/DaemonMC.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.13.0" />
<PackageReference Include="MiNET.fnbt" Version="1.0.22" />
<PackageReference Include="MiNET.LevelDB-CobwebSMP" Version="0.0.0" />
<PackageReference Include="NETStandard.Library" Version="2.0.3" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="SixLabors.ImageSharp.Drawing" Version="2.1.5" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="8.6.0" />
Expand Down
2 changes: 1 addition & 1 deletion DaemonMC/Network/Bedrock/BedrockPacketProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ internal static void HandlePacket(Packet packet, IPEndPoint clientEp)
{
EnumValues = CommandManager.EnumValues,
Enums = CommandManager.RealEnums,
Commands = CommandManager.AvailableCommands
Commands = CommandManager.GetAvailableCommands()
};
player.Send(commands);

Expand Down
2 changes: 1 addition & 1 deletion DaemonMC/Network/Packet.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using DaemonMC.Network.Bedrock;
using DaemonMC.Plugin.Plugin;
using DaemonMC.Plugin;
using DaemonMC.Utils.Text;

namespace DaemonMC.Network
Expand Down
Loading