-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRegCommand.cs
111 lines (110 loc) · 5.49 KB
/
RegCommand.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
using MC;
using LiteLoader.DynamicCommand;
using static LiteLoader.DynamicCommand.DynamicCommand;
using LiteLoader.Logger;
using Newtonsoft.Json;
namespace FantasyTownAllowlist
{
internal class RegCommand
{
Logger logger = new("FantasyTownAllowlist");
DynamicCommandInstance cmd = CreateCommand("ftallowlist", "Allowlist Commands", CommandPermissionLevel.GameMasters);
Allowlist allowlistMgr = new();
public void CommandRegisiter()
{
logger.Info.WriteLine("Registering Commands...");
logger.Info.WriteLine("正在注册命令……");
var AllowlistOperate = cmd.SetEnum("Operate Allowlist", new() { "add", "remove" }); //增删操作指令枚举
var Allowlister = cmd.SetEnum("List the player in the Allowlist", new() { "list" }); //列出白名单指令枚举
//设置指令参数
cmd.Mandatory("OperateEnum", ParameterType.Enum, AllowlistOperate, CommandParameterOption.EnumAutocompleteExpansion);
cmd.Mandatory("Lister", ParameterType.Enum, Allowlister, CommandParameterOption.EnumAutocompleteExpansion);
cmd.Mandatory("PlayerName", ParameterType.String);
//设置指令重载
cmd.AddOverload(new List<string>() { AllowlistOperate, "PlayerName" });
cmd.AddOverload(new List<string>() { Allowlister });
//设置回调
cmd.SetCallback((command, origin, output, results) => {
if (results != null && results.Count != 0)
{
try
{
switch (results["OperateEnum"].Get())
{
case "add":
{
if ($"{results["PlayerName"].Get()}" != "" && $"{results["PlayerName"].Get()}" != null)
{
if(allowlistMgr.PlayerInAllowlist($"{results["PlayerName"].Get()}"))
{
output.Error($"{results["PlayerName"].Get()} already in allowlist");
}
else
{
allowlistMgr.Write($"{results["PlayerName"].Get()}");
output.Success($"{results["PlayerName"].Get()} was added in allowlist!");
}
}
else
{
output.Error("Error - PlayerName is null!");
}
}
break;
case "remove":
{
if (allowlistMgr.PlayerInAllowlist($"{results["PlayerName"].Get()}"))
{
allowlistMgr.Delete($"{results["PlayerName"].Get()}");
output.Success($"{results["PlayerName"].Get()} was removed from allowlist!");
}
else
{
output.Error($"{results["PlayerName"].Get()} not in allowlist!");
}
}
break;
default:
break;
}
switch (results["Lister"].Get())
{
case "list":
{
List<AllowlistFile>? list = JsonConvert.DeserializeObject<List<AllowlistFile>>(allowlistMgr.Read());
string pln = "";
if (list == null || list.Count == 0)
break;
else
{
foreach (var item in list)
{
if (pln == "")
pln = item.Name;
else pln += $", {item.Name}";
}
}
logger.Info.WriteLine("There are players in allowlist:");
logger.Info.WriteLine(pln);
}
break;
default:
break;
}
}
catch (Exception e)
{
logger.Error.WriteLine(e.Message);
logger.Error.WriteLine(e.Source);
logger.Error.WriteLine(e.StackTrace);
throw;
}
}
else logger.Error.WriteLine("results is null or empty!");
});
Setup(cmd);
logger.Info.WriteLine("Commands Registered!");
logger.Info.WriteLine("命令注册成功!");
}
}
}