Skip to content

Commit

Permalink
Merge pull request #121 from 1242509682/master
Browse files Browse the repository at this point in the history
更新:SwitchCommands v1.2.1 加入了对指令开关的破坏保护,与该开关的独有说明,汉化了配置文件
  • Loading branch information
Controllerdestiny authored May 16, 2024
2 parents 369ccfe + 50802a3 commit 7165deb
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 37 deletions.
11 changes: 9 additions & 2 deletions SwitchCommands/CommandInfo.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
using TShockAPI;
using Newtonsoft.Json;
using TShockAPI;
//using PlaceholderAPI;

namespace SwitchCommands
{
public class CommandInfo
{
[JsonProperty("指令")]
public List<string> commandList = new List<string>();
[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()
{
Expand Down
29 changes: 27 additions & 2 deletions SwitchCommands/Database.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, CommandInfo> switchCommandList = new Dictionary<string, CommandInfo>();

#region 读取配置文件方法
public void Write(string path)
{
File.WriteAllText(path, JsonConvert.SerializeObject(this, Formatting.Indented));
Expand All @@ -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<Database>(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<Database>(json);
return cf!;
}
}
}
#endregion
}
}
33 changes: 20 additions & 13 deletions SwitchCommands/PluginCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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":
Expand Down Expand Up @@ -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);
Expand Down
36 changes: 31 additions & 5 deletions SwitchCommands/README.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
66 changes: 51 additions & 15 deletions SwitchCommands/SwitchCommands.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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 => "JohuanCjx适配";
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))
Expand All @@ -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>("PlayerState");

if (playerState == PlayerState.SelectingSwitch)
Expand Down

0 comments on commit 7165deb

Please sign in to comment.