Skip to content

Commit 02889d6

Browse files
committed
Add command interfaces
1 parent d44a75a commit 02889d6

File tree

7 files changed

+174
-0
lines changed

7 files changed

+174
-0
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
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+
}

tests/UnitTest/NbtTest.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ public void Test1()
9898
testList.Add(new NbtInt(2));
9999
testList.Add(new NbtInt(4));
100100
testCompound.Add(new NbtLong(0x000000FFFFFFFFFF, "testLong"));
101+
testCompound.Add(new NbtDouble(3.1415926, "testDouble"));
101102

102103
using (var sw = new StringWriter())
103104
{

0 commit comments

Comments
 (0)