Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed commands field to a dictionary, our problems are over. #48

Merged
merged 1 commit into from
Mar 31, 2024
Merged
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
23 changes: 15 additions & 8 deletions Rocket/Rocket.Core/Commands/RocketCommandManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,24 @@ namespace Rocket.Core.Commands
{
public class RocketCommandManager : MonoBehaviour
{
private readonly List<RegisteredRocketCommand> commands = new List<RegisteredRocketCommand>();
private readonly Dictionary<string, RegisteredRocketCommand> commands = new();
internal List<RocketCommandCooldown> cooldown = new List<RocketCommandCooldown>();
public ReadOnlyCollection<RegisteredRocketCommand> Commands { get; internal set; }
public IReadOnlyDictionary<string, RegisteredRocketCommand> Commands { get; internal set; }
private XMLFileAsset<RocketCommands> commandMappings;

public delegate void ExecuteCommand(IRocketPlayer player, IRocketCommand command, ref bool cancel);
public event ExecuteCommand OnExecuteCommand;

internal void Reload()
{

commandMappings.Load();
checkCommandMappings();
}

private void Awake()
{
Commands = commands.AsReadOnly();
Commands = commands;
commandMappings = new XMLFileAsset<RocketCommands>(Environment.CommandsFile);
checkCommandMappings();
R.Plugins.OnPluginsLoaded += Plugins_OnPluginsLoaded;
Expand Down Expand Up @@ -80,9 +81,9 @@ private IRocketCommand GetCommand(IRocketCommand command)

public IRocketCommand GetCommand(string command)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it not be possible to return a bool and out a IRocketCommand, then you dont have to check for null and just see if its a true or false return.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not the scope of this PR.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ik i was just pointing it out.

{
IRocketCommand foundCommand = commands.Where(c => c.Name.ToLower() == command.ToLower()).FirstOrDefault();
if(foundCommand == null) commands.Where(c => c.Aliases.Select(a => a.ToLower()).Contains(command.ToLower())).FirstOrDefault();
return foundCommand;
if (!commands.ContainsKey(command.ToLower()))
return null;
return commands[command.ToLower()];
}

private static string getCommandIdentity(IRocketCommand command,string name)
Expand Down Expand Up @@ -157,14 +158,20 @@ public void Register(IRocketCommand command, string alias, CommandPriority prior

foreach(CommandMapping mapping in commandMappings.Instance.CommandMappings.Where(m => m.Class == className && m.Enabled))
{
commands.Add(new RegisteredRocketCommand(mapping.Name.ToLower(), command));
commands.Add(mapping.Name.ToLower(), new RegisteredRocketCommand(mapping.Name.ToLower(), command));
Logging.Logger.Log("[registered] /" + mapping.Name.ToLower() + " (" + mapping.Class + ")", ConsoleColor.Green);
}
}

public void DeregisterFromAssembly(Assembly assembly)
{
commands.RemoveAll(rc => getCommandType(rc.Command).Assembly == assembly);
List<string> toRemove = new();
foreach (var pair in commands)
{
if (getCommandType(pair.Value.Command).Assembly == assembly)
toRemove.Add(pair.Key);
}
toRemove.ForEach((string str) => commands.Remove(str));
}

public double GetCooldown(IRocketPlayer player, IRocketCommand command)
Expand Down
Loading