Skip to content
This repository was archived by the owner on Jan 16, 2020. It is now read-only.

Commit c8ee8c4

Browse files
committed
Add Commands example
1 parent 6251781 commit c8ee8c4

19 files changed

Lines changed: 310 additions & 61 deletions

Rocket.API/Commands/ICommand.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ public interface ICommand
99
string Permission { get; }
1010
string Syntax { get; }
1111

12-
List<ISubCommand> ChildCommands { get; }
13-
List<string> Aliases { get; }
12+
ISubCommand[] ChildCommands { get; }
13+
string[] Aliases { get; }
1414

1515
bool SupportsCaller(ICommandCaller caller);
1616

Rocket.API/Entities/ILivingEntity.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ namespace Rocket.API.Player
55
{
66
public interface ILivingEntity
77
{
8-
double MaxHealth { get; }
8+
double MaxHealth { get; set; }
99

10-
double Health { get; }
10+
double Health { get; set; }
1111

1212
void Kill();
1313

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
3+
namespace Rocket.Core.Commands
4+
{
5+
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
6+
public class CommandAliasAttribute : Attribute
7+
{
8+
public CommandAliasAttribute(string aliasName)
9+
{
10+
AliasName = aliasName;
11+
}
12+
13+
public string AliasName { get; }
14+
}
15+
}

Rocket.Core/Commands/CommandAttribute.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
namespace Rocket.Core.Commands
55
{
6-
[AttributeUsage(AttributeTargets.Method)]
6+
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
77
public class CommandAttribute : Attribute
88
{
99
public string Name { get; set; }
@@ -13,9 +13,5 @@ public class CommandAttribute : Attribute
1313
public string Permission { get; set; }
1414

1515
public string Syntax { get; set; }
16-
17-
public string[] Aliases { get; set; }
18-
19-
public Type[] SupportedCommandCallers = { typeof(ICommandCaller)};
2016
}
2117
}

Rocket.Core/Commands/CommandAttributeWrapper.cs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using System.Linq;
34
using System.Reflection;
45
using Rocket.API.Commands;
@@ -8,27 +9,36 @@ namespace Rocket.Core.Commands
89
{
910
public class CommandAttributeWrapper : ICommand
1011
{
12+
private readonly Type[] supportedCallers;
1113
public CommandAttribute Attribute { get; }
1214

13-
public CommandAttributeWrapper(object instance, MethodBase method, CommandAttribute attribute)
15+
public CommandAttributeWrapper(object instance, MethodBase method,
16+
CommandAttribute attribute,
17+
string[] aliases,
18+
Type[] supportedCallers)
1419
{
20+
this.supportedCallers = supportedCallers;
1521
Instance = instance;
1622
Method = method;
1723
Attribute = attribute;
24+
Aliases = aliases;
1825
}
1926

2027
public string Name => Attribute.Name;
2128
public string Description => Attribute.Description;
2229
public string Permission => Attribute.Permission;
2330
public string Syntax => Attribute.Syntax;
24-
public List<ISubCommand> ChildCommands { get; } //todo
25-
public List<string> Aliases => Attribute.Aliases.ToList();
31+
public ISubCommand[] ChildCommands { get; } //todo
32+
public string[] Aliases { get; }
2633
public object Instance { get; }
2734
public MethodBase Method { get; set; }
2835

2936
public bool SupportsCaller(ICommandCaller caller)
3037
{
31-
return Attribute.SupportedCommandCallers.Any(c => c.IsInstanceOfType(caller));
38+
if (supportedCallers.Length == 0)
39+
return true;
40+
41+
return supportedCallers.Any(c => c.IsInstanceOfType(caller));
3242
}
3343

3444
public void Execute(ICommandContext context)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
3+
namespace Rocket.Core.Commands
4+
{
5+
[AttributeUsage(AttributeTargets.Method)]
6+
public class CommandCallerAttribute :Attribute
7+
{
8+
public Type SupportedCaller { get; }
9+
10+
public CommandCallerAttribute(Type supportedCaller)
11+
{
12+
SupportedCaller = supportedCaller;
13+
}
14+
}
15+
}

Rocket.Core/Commands/RocketCommands/CommandPermission.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ public class CommandPermission : ICommand
1414
public string Syntax => "<add/remove/reload>";
1515
public string Description => "Manage rocket permissions";
1616

17-
public List<ISubCommand> ChildCommands => new List<ISubCommand> { new PermissionSubCommandAdd(), new PermissionSubCommandRemove(), new PermissionSubCommandReload() };
18-
public List<string> Aliases => new List<string> { "P" };
17+
public ISubCommand[] ChildCommands => new ISubCommand[] { new PermissionSubCommandAdd(), new PermissionSubCommandRemove(), new PermissionSubCommandReload() };
18+
public string[] Aliases => new[] {"P"};
1919

2020
public void Execute(ICommandContext context)
2121
{
@@ -34,8 +34,8 @@ public abstract class PermissionSubCommandUpdate : ISubCommand<CommandPermission
3434
public abstract string Permission { get; }
3535
public string Syntax => "<[p]layer/[g]roup> [target] [permission]";
3636

37-
public List<ISubCommand> ChildCommands => null;
38-
public abstract List<string> Aliases { get; }
37+
public ISubCommand[] ChildCommands => null;
38+
public abstract string[] Aliases { get; }
3939

4040
public bool SupportsCaller(ICommandCaller caller)
4141
{
@@ -97,7 +97,7 @@ public class PermissionSubCommandAdd : PermissionSubCommandUpdate
9797
public override string Name => "Add";
9898
public override string Description => "Add a permission to a group or player";
9999
public override string Permission => "Rocket.Permissions.ManagePermissions.Add";
100-
public override List<string> Aliases => new List<string> { "a", "+" };
100+
public override string[] Aliases => new[] {"a", "+"};
101101

102102
protected override void UpdatePermission(ICommandCaller caller, IPermissionProvider permissions, IPermissible target, string permissionToUpdate)
103103
{
@@ -117,7 +117,7 @@ public class PermissionSubCommandRemove : PermissionSubCommandUpdate
117117
public override string Name => "Remove";
118118
public override string Description => "Remove permission to a group or player";
119119
public override string Permission => "Rocket.Permissions.ManagePermissions.Remove";
120-
public override List<string> Aliases => new List<string> { "r", "-" };
120+
public override string[] Aliases => new[] {"r", "-"};
121121

122122
protected override void UpdatePermission(ICommandCaller caller, IPermissionProvider permissions, IPermissible target, string permissionToUpdate)
123123
{
@@ -138,8 +138,8 @@ public class PermissionSubCommandReload : ISubCommand<CommandPermission>
138138
public string Description => "Reload permissions";
139139
public string Permission => "Rocket.Permissions.ManagePermissions.Reload";
140140
public string Syntax => "";
141-
public List<ISubCommand> ChildCommands => null;
142-
public List<string> Aliases => new List<string> { "R" };
141+
public ISubCommand[] ChildCommands => null;
142+
public string[] Aliases => new[] {"R"};
143143
public bool SupportsCaller(ICommandCaller caller)
144144
{
145145
return true;

Rocket.Core/Commands/RocketCommands/CommandPermissionGroup.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ namespace Rocket.Core.Commands.RocketCommands
1010
public class CommandPermissionGroup : ICommand
1111
{
1212
public string Syntax { get; }
13-
public List<ISubCommand> ChildCommands => new List<ISubCommand> { new PermissionGroupSubCommandAdd(), new PermissionGroupSubCommandRemove() };
13+
public ISubCommand[] ChildCommands => new ISubCommand[] {new PermissionGroupSubCommandAdd(), new PermissionGroupSubCommandRemove()};
1414

1515
public string Description { get; }
1616

17-
public List<string> Aliases => new List<string> { "PG" };
17+
public string[] Aliases => new[] {"PG"};
1818

1919
public string Name => "PermissionGroup";
2020
public string Permission => "Rocket.Permissions.ManageGroups";
@@ -37,8 +37,8 @@ public abstract class PermissionGroupSubCommandUpdate : ISubCommand<CommandPermi
3737
public abstract string Permission { get; }
3838
public string Syntax => "<player> <group>";
3939

40-
public List<ISubCommand> ChildCommands => null;
41-
public abstract List<string> Aliases { get; }
40+
public ISubCommand[] ChildCommands => null;
41+
public abstract string[] Aliases { get; }
4242

4343
public bool SupportsCaller(ICommandCaller caller)
4444
{
@@ -74,7 +74,7 @@ public class PermissionGroupSubCommandAdd : PermissionGroupSubCommandUpdate
7474
public override string Name => "Add";
7575
public override string Description => "Add a player to a permission group";
7676
public override string Permission => "Rocket.Permissions.ManageGroups.Add";
77-
public override List<string> Aliases => new List<string> { "a", "+" };
77+
public override string[] Aliases => new[] {"a", "+"};
7878
protected override void UpdateGroup(ICommandCaller caller, IPermissionProvider permissions, IPlayer targetPlayer, IPermissionGroup groupToUpdate)
7979
{
8080
if (permissions.AddGroup(targetPlayer, groupToUpdate))
@@ -89,7 +89,7 @@ public class PermissionGroupSubCommandRemove : PermissionGroupSubCommandUpdate
8989
public override string Name => "Remove";
9090
public override string Description => "Remove a player from a permission group";
9191
public override string Permission => "Rocket.Permissions.ManageGroups.Remove";
92-
public override List<string> Aliases => new List<string> { "r", "-" };
92+
public override string[] Aliases => new[] {"r", "-"};
9393
protected override void UpdateGroup(ICommandCaller caller, IPermissionProvider permissions, IPlayer targetPlayer, IPermissionGroup groupToUpdate)
9494
{
9595
if (permissions.RemoveGroup(targetPlayer, groupToUpdate))

Rocket.Core/Commands/RocketCommands/CommandRocket.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ public class CommandRocket : ICommand
1313
public string Syntax => "<reload>";
1414

1515
public string Description => "Manage Rocket";
16-
public List<string> Aliases => null;
17-
public List<ISubCommand> ChildCommands => new List<ISubCommand> { new RocketSubCommandReload() };
16+
public string[] Aliases => null;
17+
public ISubCommand[] ChildCommands => new ISubCommand[] {new RocketSubCommandReload()};
1818

1919
public void Execute(ICommandContext context)
2020
{
@@ -33,8 +33,8 @@ public class RocketSubCommandReload : ISubCommand<CommandPermission>
3333
public string Description => "Reload Rocket";
3434
public string Permission => "Rocket.ManageRocket.Reload";
3535
public string Syntax => "";
36-
public List<ISubCommand> ChildCommands => null;
37-
public List<string> Aliases => null;
36+
public ISubCommand[] ChildCommands => null;
37+
public string[] Aliases => null;
3838
public bool SupportsCaller(ICommandCaller caller)
3939
{
4040
return true;

Rocket.Core/Plugins/PluginManager.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,18 @@ public virtual void RegisterCommands(IPlugin plugin, object o)
6565
{
6666
foreach (var method in o.GetType().GetMethods(BindingFlags.Public | BindingFlags.Instance))
6767
{
68-
var attr = (CommandAttribute)method.GetCustomAttributes(typeof(CommandAttribute), true).FirstOrDefault();
69-
CommandAttributeWrapper wrapper = new CommandAttributeWrapper(o, method, attr);
68+
var cmdAttr = (CommandAttribute)method.GetCustomAttributes(typeof(CommandAttribute), true).FirstOrDefault();
69+
var aliasAttrs = method.GetCustomAttributes(typeof(CommandAliasAttribute), true)
70+
.Cast<CommandAliasAttribute>();
71+
72+
var supportedTypeAttrs = method.GetCustomAttributes(typeof(CommandCallerAttribute), true)
73+
.Cast<CommandCallerAttribute>();
74+
75+
76+
CommandAttributeWrapper wrapper = new CommandAttributeWrapper(o, method, cmdAttr,
77+
aliasAttrs.Select(c => c.AliasName).ToArray(),
78+
supportedTypeAttrs.Select(c => c.SupportedCaller).ToArray());
79+
7080
if (!commands.ContainsKey(plugin))
7181
commands.Add(plugin, new List<ICommand>());
7282

0 commit comments

Comments
 (0)