Skip to content

Commit 93b365c

Browse files
committed
Add command interfaces
1 parent e24858c commit 93b365c

File tree

11 files changed

+178
-2
lines changed

11 files changed

+178
-2
lines changed

build.bat

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@echo off
2+
cd src
3+
dotnet restore
4+
dotnet build
5+
6+
pause

run_gateway.bat

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
@echo off
12
cd src\MineCase.Gateway
23
start dotnet run
34
pause

run_server.bat

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
@echo off
12
cd src
23
dotnet restore
34
dotnet build
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace MineCase.Server.Game
5+
{
6+
/// <summary>
7+
/// 命令接口
8+
/// </summary>
9+
public interface ICommand
10+
{
11+
/// <summary>
12+
/// Gets 该命令的名称
13+
/// </summary>
14+
string Name { get; }
15+
16+
/// <summary>
17+
/// Gets 该命令的描述,可为 null
18+
/// </summary>
19+
string Description { get; }
20+
21+
/// <summary>
22+
/// Gets 要执行此命令需要的权限,可为 null
23+
/// </summary>
24+
Permission NeededPermission { get; }
25+
26+
/// <summary>
27+
/// Gets 该命令的别名,可为 null
28+
/// </summary>
29+
IEnumerable<string> Aliases { get; }
30+
31+
/// <summary>
32+
/// 执行该命令
33+
/// </summary>
34+
/// <param name="commandSender">发送命令者</param>
35+
/// <param name="args">命令的参数</param>
36+
/// <returns>执行是否成功,如果成功则返回 true</returns>
37+
bool Execute(ICommandSender commandSender, IEnumerable<string> args);
38+
}
39+
40+
/// <summary>
41+
/// 可发送命令者接口
42+
/// </summary>
43+
public interface ICommandSender : IPermissible
44+
{
45+
}
46+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace MineCase.Server.Game
6+
{
7+
/// <summary>
8+
/// 具有权限者接口
9+
/// </summary>
10+
public interface IPermissible
11+
{
12+
/// <summary>
13+
/// 判断是否具有特定的权限
14+
/// </summary>
15+
/// <param name="permission">要判断的权限</param>
16+
bool HasPermission(Permission permission);
17+
}
18+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace MineCase.Server.Game
7+
{
8+
/// <summary>
9+
/// 权限
10+
/// </summary>
11+
public class Permission
12+
{
13+
/// <summary>
14+
/// Gets 该权限的名称
15+
/// </summary>
16+
public string Name { get; }
17+
18+
/// <summary>
19+
/// Gets 该权限的描述,可为 null
20+
/// </summary>
21+
public string Description { get; }
22+
23+
/// <summary>
24+
/// Gets or sets 该权限的默认值
25+
/// </summary>
26+
public PermissionDefaultValue DefaultValue { get; set; }
27+
28+
private readonly Dictionary<string, Permission> _children;
29+
30+
public Permission(
31+
string name,
32+
string description = null,
33+
PermissionDefaultValue permissionDefaultValue = PermissionDefaultValue.True,
34+
IEnumerable<Permission> children = null)
35+
{
36+
Name = name ?? throw new ArgumentNullException(nameof(name));
37+
Description = description;
38+
DefaultValue = permissionDefaultValue;
39+
_children = children.ToDictionary(
40+
p => p?.Name ?? throw new ArgumentException("子权限不得为 null", nameof(children))) ??
41+
new Dictionary<string, Permission>();
42+
}
43+
}
44+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace MineCase.Server.Game
6+
{
7+
/// <summary>
8+
/// 权限 (<see cref="Permission"/>) 的默认值
9+
/// </summary>
10+
public enum PermissionDefaultValue
11+
{
12+
/// <summary>
13+
/// 对所有人都启用
14+
/// </summary>
15+
True,
16+
17+
/// <summary>
18+
/// 对 Operator 启用
19+
/// </summary>
20+
TrueIfOp,
21+
22+
/// <summary>
23+
/// 对所有人都禁用
24+
/// </summary>
25+
False
26+
}
27+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace MineCase.Server.Game
6+
{
7+
public abstract class SimpleCommand : ICommand
8+
{
9+
public string Name { get; }
10+
11+
public string Description { get; }
12+
13+
public Permission NeededPermission { get; }
14+
15+
private readonly HashSet<string> _aliases;
16+
17+
public IEnumerable<string> Aliases => _aliases;
18+
19+
protected SimpleCommand(string name, string description = null, Permission neededPermission = null, IEnumerable<string> aliases = null)
20+
{
21+
Name = name ?? throw new ArgumentNullException(nameof(name));
22+
Description = description;
23+
NeededPermission = neededPermission;
24+
if (aliases != null)
25+
{
26+
_aliases = new HashSet<string>(aliases);
27+
}
28+
}
29+
30+
public abstract bool Execute(ICommandSender commandSender, IEnumerable<string> args);
31+
}
32+
}

src/MineCase.sln

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MineCase.Server.Grains", "M
1717
EndProject
1818
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MineCase.UnitTest", "..\tests\UnitTest\MineCase.UnitTest.csproj", "{35618D69-85F0-4C38-8224-04A2D75825B6}"
1919
EndProject
20-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MineCase.Nbt", "MineCase.NBT\MineCase.Nbt.csproj", "{199335D6-9726-42E1-94F2-7D95932D4DDC}"
20+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MineCase.Nbt", "MineCase.Nbt\MineCase.Nbt.csproj", "{199335D6-9726-42E1-94F2-7D95932D4DDC}"
2121
EndProject
2222
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{A0B013BF-4BEB-4044-AA59-A9ED3AFBF99D}"
2323
ProjectSection(SolutionItems) = preProject

tests/UnitTest/MineCase.UnitTest.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
<ItemGroup>
2323
<ProjectReference Include="..\..\src\MineCase.Algorithm\MineCase.Algorithm.csproj" />
24-
<ProjectReference Include="..\..\src\MineCase.NBT\MineCase.Nbt.csproj" />
24+
<ProjectReference Include="..\..\src\MineCase.Nbt\MineCase.Nbt.csproj" />
2525
<ProjectReference Include="..\..\src\MineCase.Protocol\MineCase.Protocol.csproj" />
2626
</ItemGroup>
2727

0 commit comments

Comments
 (0)