Skip to content

Commit e307b23

Browse files
authored
Merge branch 'Controllerdestiny:master' into master
2 parents 9662667 + c4bc156 commit e307b23

7 files changed

Lines changed: 222 additions & 0 deletions

File tree

Plugin.sln

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Goodnight", "GoodNight\Good
180180
EndProject
181181
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MusicPlayer", "musicplayer\MusicPlayer.csproj", "{57C04DD8-4DAE-42AE-BC48-E1D41152321E}"
182182
EndProject
183+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TimerKeeper", "TimerKeeper\TimerKeeper.csproj", "{5F5D175C-F0EF-44E0-B5C4-7C9CA89E01D1}"
184+
EndProject
183185
Global
184186
GlobalSection(SolutionConfigurationPlatforms) = preSolution
185187
Debug|Any CPU = Debug|Any CPU
@@ -876,6 +878,14 @@ Global
876878
{57C04DD8-4DAE-42AE-BC48-E1D41152321E}.Release|Any CPU.Build.0 = Release|Any CPU
877879
{57C04DD8-4DAE-42AE-BC48-E1D41152321E}.Release|x64.ActiveCfg = Release|Any CPU
878880
{57C04DD8-4DAE-42AE-BC48-E1D41152321E}.Release|x64.Build.0 = Release|Any CPU
881+
{5F5D175C-F0EF-44E0-B5C4-7C9CA89E01D1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
882+
{5F5D175C-F0EF-44E0-B5C4-7C9CA89E01D1}.Debug|Any CPU.Build.0 = Debug|Any CPU
883+
{5F5D175C-F0EF-44E0-B5C4-7C9CA89E01D1}.Debug|x64.ActiveCfg = Debug|Any CPU
884+
{5F5D175C-F0EF-44E0-B5C4-7C9CA89E01D1}.Debug|x64.Build.0 = Debug|Any CPU
885+
{5F5D175C-F0EF-44E0-B5C4-7C9CA89E01D1}.Release|Any CPU.ActiveCfg = Release|Any CPU
886+
{5F5D175C-F0EF-44E0-B5C4-7C9CA89E01D1}.Release|Any CPU.Build.0 = Release|Any CPU
887+
{5F5D175C-F0EF-44E0-B5C4-7C9CA89E01D1}.Release|x64.ActiveCfg = Release|Any CPU
888+
{5F5D175C-F0EF-44E0-B5C4-7C9CA89E01D1}.Release|x64.Build.0 = Release|Any CPU
879889
EndGlobalSection
880890
GlobalSection(SolutionProperties) = preSolution
881891
HideSolutionNode = FALSE

Plugins.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,12 @@
329329
"Author": "Johuan Cjx适配 羽学,肝帝熙恩优化",
330330
"Description": "触发开关可以执行指令"
331331
},
332+
{
333+
"Name": "TimerKeeper",
334+
"Version": "1.0.0.0",
335+
"Author": "Cai",
336+
"Description": "保存计时器"
337+
},
332338
{
333339
"Name": "TownNPCHomes",
334340
"Version": "1.1.0",

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@
129129
| [RealTime](RealTime/README.md) | 使服务器内时间同步现实时间 ||
130130
| [GoodNight](GoodNight/README.md) | 宵禁 ||
131131
| [Musicplayer](Musicplayer/README.md) | 简易音乐播放器 ||
132+
| [TimerKeeper](TimerKeeper/README.md) | 保存计时器状态 ||
132133
</Details>
133134

134135
## 代码贡献

TimerKeeper/DB.cs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
using Microsoft.Data.Sqlite;
2+
using MySql.Data.MySqlClient;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Data;
6+
using System.Diagnostics;
7+
using System.IO;
8+
using System.Linq;
9+
using TerrariaApi.Server;
10+
using TShockAPI;
11+
using TShockAPI.DB;
12+
namespace TimerKeeper
13+
{
14+
public static class DB
15+
{
16+
private static IDbConnection db => TShock.DB;
17+
public static void Connect()
18+
{
19+
SqlTableCreator sqlcreator = new SqlTableCreator(db, new MysqlQueryCreator());
20+
21+
sqlcreator.EnsureTableStructure(new SqlTable("TimerKeeper",
22+
new SqlColumn("X", MySqlDbType.Int32) { Length = 4 },
23+
new SqlColumn("Y", MySqlDbType.Int32) { Length = 4 }));
24+
25+
}
26+
27+
28+
public static List<TimerPos> LoadAll()
29+
{
30+
List<TimerPos> dataInfos = new();
31+
using (QueryResult result = db.QueryReader("SELECT * FROM TimerKeeper;"))
32+
{
33+
34+
while (result.Read())
35+
{
36+
dataInfos.Add(new TimerPos
37+
{
38+
X = result.Get<int>("X"),
39+
Y = result.Get<int>("Y")
40+
41+
});
42+
}
43+
}
44+
return dataInfos;
45+
}
46+
47+
public static void AddTimer(int x,int y)
48+
{
49+
db.Query("INSERT INTO TimerKeeper (X, Y) VALUES (@0, @1);", x, y);
50+
}
51+
52+
public static void RemoveTimer(int x,int y)
53+
{
54+
db.Query("DELETE FROM TimerKeeper WHERE X=@0 AND Y=@1;", x,y);
55+
}
56+
57+
public static void ClearDB()
58+
{
59+
db.Query("DELETE FROM TimerKeeper;");
60+
}
61+
62+
public class TimerPos
63+
{
64+
public int X { get; set; }
65+
public int Y { get; set; }
66+
}
67+
68+
}
69+
}

TimerKeeper/MainPlugin.cs

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
using System.Drawing;
2+
using System.IO.Streams;
3+
using System.Text;
4+
using Terraria;
5+
using TerrariaApi.Server;
6+
using TShockAPI;
7+
using TShockAPI.Hooks;
8+
9+
namespace TimerKeeper
10+
{
11+
[ApiVersion(2, 1)]
12+
public class TimerKeeper : TerrariaPlugin
13+
{
14+
15+
public override string Author => "Cai";
16+
17+
public override string Description => "保存计时器";
18+
19+
public override string Name => "TimerKeeper";
20+
21+
public override Version Version => new Version(1, 0, 0, 0);
22+
public static PlayerData data { get; set; }
23+
public TimerKeeper(Main game)
24+
: base(game)
25+
{
26+
}
27+
public override void Initialize()
28+
{
29+
DB.Connect();
30+
ServerApi.Hooks.NetGetData.Register(this, OnGetData);
31+
//当世界加载完成
32+
GetDataHandlers.TileEdit.Register(OnTileEdit);
33+
ServerApi.Hooks.GamePostInitialize.Register(this, OnPostInitialize);
34+
}
35+
36+
private void OnTileEdit(object sender, GetDataHandlers.TileEditEventArgs e)
37+
{
38+
if (Main.tile[e.X, e.Y].type == 144)
39+
{
40+
DB.RemoveTimer(e.X, e.Y);
41+
}
42+
}
43+
44+
private void OnPostInitialize(EventArgs args)
45+
{
46+
var timers = DB.LoadAll();
47+
foreach (var i in timers)
48+
{
49+
if (!WorldGen.InWorld(i.X, i.Y) || Main.tile[i.X, i.Y] == null || Main.tile[i.X, i.Y].type != 144)
50+
{
51+
DB.RemoveTimer(i.X, i.Y);
52+
continue;
53+
}
54+
else
55+
{
56+
Main.tile[i.X, i.Y].frameY = 18;
57+
Wiring.CheckMech(i.X, i.Y, 18000);
58+
}
59+
}
60+
TShock.Log.ConsoleWarn("[TimerKeeper]计时器已经加载!");
61+
}
62+
63+
private void OnGetData(GetDataEventArgs args)
64+
{
65+
if (args.MsgID == PacketTypes.HitSwitch)
66+
{
67+
using (MemoryStream data = new MemoryStream(args.Msg.readBuffer, args.Index, args.Length))
68+
{
69+
int i = data.ReadInt16();
70+
int j = data.ReadInt16();
71+
if (!WorldGen.InWorld(i, j) || Main.tile[i, j] == null || Main.tile[i, j].type != 144)
72+
{
73+
return;
74+
}
75+
if (Main.tile[i, j].frameY == 0)
76+
{
77+
DB.AddTimer(i, j);
78+
}
79+
else
80+
{
81+
DB.RemoveTimer(i, j);
82+
}
83+
}
84+
85+
86+
}
87+
}
88+
89+
90+
protected override void Dispose(bool disposing)
91+
{
92+
if (disposing)
93+
{
94+
ServerApi.Hooks.NetGetData.Deregister(this, OnGetData);
95+
ServerApi.Hooks.GamePostInitialize.Deregister(this, OnPostInitialize);
96+
GetDataHandlers.TileEdit.UnRegister(OnTileEdit);
97+
98+
}
99+
base.Dispose(disposing);
100+
}
101+
102+
103+
}
104+
}

TimerKeeper/README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# TimerKeeper 保存计时器
2+
3+
- 作者: Cai
4+
- 出处: [github](https://github.com/THEXN/CaiPlugins)
5+
- 保存计时器状态,重启后继续计时
6+
7+
## 更新日志
8+
9+
```
10+
暂无
11+
```
12+
13+
## 指令
14+
15+
```
16+
暂无
17+
```
18+
19+
## 配置
20+
21+
```
22+
暂无
23+
```
24+
```
25+
## 反馈
26+
- 共同维护的插件库:https://github.com/Controllerdestiny/TShockPlugin
27+
- 国内社区trhub.cn 或 TShock官方群等

TimerKeeper/TimerKeeper.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<Import Project="..\template.targets" />
4+
5+
</Project>

0 commit comments

Comments
 (0)