diff --git a/SwitchCommands/CommandInfo.cs b/SwitchCommands/CommandInfo.cs index f6e7fe1b..186d51d8 100644 --- a/SwitchCommands/CommandInfo.cs +++ b/SwitchCommands/CommandInfo.cs @@ -1,18 +1,25 @@ -using TShockAPI; +using Newtonsoft.Json; +using TShockAPI; //using PlaceholderAPI; namespace SwitchCommands { public class CommandInfo { + [JsonProperty("指令")] public List commandList = new List(); + [JsonProperty("冷却时间")] public float cooldown = 0; + [JsonProperty("忽略权限")] public bool ignorePerms = false; + [JsonProperty("开关说明")] + public string show = ""; } public class SwitchPos { - public int X = 0, Y = 0; + public int X = 0; + public int Y = 0; public SwitchPos() { diff --git a/SwitchCommands/Database.cs b/SwitchCommands/Database.cs index 21b42530..937f00a2 100644 --- a/SwitchCommands/Database.cs +++ b/SwitchCommands/Database.cs @@ -8,8 +8,19 @@ public class Database { public static string databasePath = Path.Combine(TShock.SavePath, "开关配置表.json"); + [JsonProperty("是否开启开关保护", Order = -3)] + public bool SwitchEnable = true; + + [JsonProperty("试图破坏开关的警告", Order = -3)] + public string SwitchText = "你没有权限破坏指令开关!"; + + [JsonProperty("添加指令时的前缀", Order = -3)] + public string SwitchPrefix = "/'"; + + [JsonProperty("开关指令表")] public Dictionary switchCommandList = new Dictionary(); + #region 读取配置文件方法 public void Write(string path) { File.WriteAllText(path, JsonConvert.SerializeObject(this, Formatting.Indented)); @@ -18,8 +29,22 @@ public void Write(string path) public static Database Read(string path) { if (!File.Exists(path)) - return new Database(); - return JsonConvert.DeserializeObject(File.ReadAllText(path)); + { + var c = new Database(); + c.Write(path); + return c; + } + else + { + using (var fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read)) + using (var sr = new StreamReader(fs)) + { + var json = sr.ReadToEnd(); + var cf = JsonConvert.DeserializeObject(json); + return cf!; + } + } } + #endregion } } diff --git a/SwitchCommands/PluginCommands.cs b/SwitchCommands/PluginCommands.cs index 8746e3df..6f5b6e87 100644 --- a/SwitchCommands/PluginCommands.cs +++ b/SwitchCommands/PluginCommands.cs @@ -6,22 +6,12 @@ namespace SwitchCommands { public class PluginCommands { - public static string switchParameters = "/开关 <添加/列表/删除/冷却/权限忽略/取消/重绑/完成>"; + public static Database database = null!; + public static string switchParameters = "/开关 <添加/列表/删除/冷却/说明/权限忽略/取消/重绑/完成> (支持拼音)"; public static void RegisterCommands() { Commands.ChatCommands.Add(new Command("switch.admin", SwitchCmd, "开关", "kg", "switch")); - Commands.ChatCommands.Add(new Command("switch.admin", SwitchReload, "重载开关", "reload")); - } - - private static void SwitchReload(CommandArgs args) - { - SwitchCommands.database = Database.Read(Database.databasePath); - args.Player.SendErrorMessage("开关插件重载成功!!!"); - if (!File.Exists(Database.databasePath)) - { - SwitchCommands.database.Write(Database.databasePath); - } } private static void SwitchCmd(CommandArgs args) @@ -53,7 +43,7 @@ private static void SwitchCmd(CommandArgs args) case "add": case "添加": case "tj": - var command = "/" + string.Join(" ", args.Parameters.Skip(1)); + var command = $"{database.SwitchPrefix}" + string.Join(" ", args.Parameters.Skip(1)); cmdInfo.commandList.Add(command); player.SendSuccessMessage("成功添加: {0}".SFormat(command)); SwitchCommands.database.Write(Database.databasePath); @@ -107,6 +97,22 @@ private static void SwitchCmd(CommandArgs args) SwitchCommands.database.Write(Database.databasePath); break; + case "说明": + case "sm": + if (args.Parameters.Count < 2) + { + player.SendErrorMessage("语法错误:/开关 说明 <内容>"); + SwitchCommands.database.Write(Database.databasePath); + return; + } + string 说明 = args.Parameters[1]; + + cmdInfo.show = 说明; + + player.SendSuccessMessage($"开关说明已设置为:{说明}"); + SwitchCommands.database.Write(Database.databasePath); + break; + case "权限忽略": case "ignoreperms": case "qxhl": @@ -137,6 +143,7 @@ private static void SwitchCmd(CommandArgs args) case "重绑": case "rebind": case "zb": + case "cb": player.SendSuccessMessage("重新激活开关后可以重新绑定"); player.SetData("PlayerState", PlayerState.SelectingSwitch); SwitchCommands.database.Write(Database.databasePath); diff --git a/SwitchCommands/README.md b/SwitchCommands/README.md index a86f2cd9..a17f8d1d 100644 --- a/SwitchCommands/README.md +++ b/SwitchCommands/README.md @@ -1,25 +1,51 @@ -# SwitchCommand 区域显示 +# SwitchCommand 开关指令 -- 作者: Johuan ,Cjx +- 作者: Johuan ,Cjx,羽学 - 出处: github - 区域指令 ## 更新日志 ``` -暂无 +1.2.1 +汉化了配置文件,加入了新指令:/开关 说明 +加入了只对指令开关的特殊保护,可通过配置项开启关闭 +给每个独特的开关能返回自己的使用说明 +对试图破坏指令开关的人进行反馈警告 ``` ## 指令 | 语法 | 权限 | 说明 | | -------------- | :-----------------: | :------: | -| /switch(开关) <添加/列表/删除/冷却/权限忽略/取消/重绑/完成> | switch.admin | 无 | +| /switch(开关) <添加/列表/删除/冷却/说明/权限忽略/取消/重绑/完成> (支持拼音) | switch.admin | 需要放置并点击1次开关才能使用该指令 | ## 配置 ```json -暂无 +{ + "是否开启开关保护": true, + "试图破坏开关的警告": "你没有权限破坏此'开关'", + "添加指令时的前缀": "/'", + "开关指令表": { + "X: 1984, Y: 226": { + "指令": [ + "/god" + ], + "冷却时间": 0.0, + "忽略权限": true, + "开关说明": "我无敌了!!!" + }, + "X: 1985, Y: 226": { + "指令": [ + "/butcher" + ], + "冷却时间": 0.0, + "忽略权限": false, + "开关说明": "让我们清清怪~" + } + } +} ``` ## 反馈 - 共同维护的插件库:https://github.com/Controllerdestiny/TShockPlugin diff --git a/SwitchCommands/SwitchCommands.cs b/SwitchCommands/SwitchCommands.cs index e54e411b..1bd7e265 100644 --- a/SwitchCommands/SwitchCommands.cs +++ b/SwitchCommands/SwitchCommands.cs @@ -1,8 +1,11 @@ -using System.IO.Streams; +using Org.BouncyCastle.Asn1.X509; +using System.IO.Streams; using Terraria; using Terraria.ID; using TerrariaApi.Server; using TShockAPI; +using TShockAPI.Hooks; + //using PlaceholderAPI; using static SwitchCommands.PluginCommands; @@ -12,38 +15,66 @@ namespace SwitchCommands public class SwitchCommands : TerrariaPlugin { - public static Database database; + public static Database database = null!; + public static SwitchPos switchPos = null!; public override string Name => "SwitchCommands"; - public override string Author => "Johuan(Cjx适配)"; + public override string Author => "Johuan Cjx适配 羽学优化"; public override string Description => "触发开关可以执行指令"; - public override Version Version => new Version(1, 2, 0, 0); + public override Version Version => new Version(1, 2, 1, 0); public SwitchCommands(Main game) : base(game) { } public override void Initialize() { - database = Database.Read(Database.databasePath); - if (!File.Exists(Database.databasePath)) - { - database.Write(Database.databasePath); - } - + LoadConfig(); + GeneralHooks.ReloadEvent += LoadConfig; + GetDataHandlers.TileEdit.Register(OnEdit); PluginCommands.RegisterCommands(); ServerApi.Hooks.NetGetData.Register(this, GetData); - } protected override void Dispose(bool disposing) { - if (disposing) + GeneralHooks.ReloadEvent -= LoadConfig; + GetDataHandlers.TileEdit.UnRegister(OnEdit); + base.Dispose(disposing); + } + + #region 配置文件创建与重读加载方法 + private static void LoadConfig(ReloadEventArgs args = null!) + { + database = Database.Read(Database.databasePath); + database.Write(Database.databasePath); + if (args != null && args.Player != null) { - ServerApi.Hooks.NetGetData.Deregister(this, GetData); + args.Player.SendSuccessMessage("[开关指令插件]重新加载配置完毕。"); + } + } + #endregion - database.Write(Database.databasePath); + #region 阻止没有权限的人破坏开关 + private static void OnEdit(object? sender, GetDataHandlers.TileEditEventArgs e) + { + var pos = new SwitchPos(e.X, e.Y); + if (IsProtectedSwitch(pos)) + { + if (e == null || !database.SwitchEnable || e.Player.HasPermission("switch.admin")) { return; } + + if (Main.tile[e.X, e.Y].type == 136 && e.Player.Active) + { + e.Player.SendMessage($"{database.SwitchText}", color: Microsoft.Xna.Framework.Color.Yellow); + e.Player.SendTileSquareCentered(e.X, e.Y, 1); + e.Handled = true; + } } - base.Dispose(disposing); } + private static bool IsProtectedSwitch(SwitchPos pos) + { + return database.switchCommandList.ContainsKey(pos.ToString()); + } + #endregion + private void GetData(GetDataEventArgs args) { using (MemoryStream data = new MemoryStream(args.Msg.readBuffer, args.Index, args.Length)) @@ -64,6 +95,11 @@ private void GetData(GetDataEventArgs args) pos.Y++; } + if (database.switchCommandList.ContainsKey(pos.ToString()) && !string.IsNullOrEmpty(database.switchCommandList[pos.ToString()].show)) + { + player.SendMessage($"开关说明:{database.switchCommandList[pos.ToString()].show}",color:Microsoft.Xna.Framework.Color.Yellow); + } + var playerState = player.GetData("PlayerState"); if (playerState == PlayerState.SelectingSwitch)