|
| 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 | +} |
0 commit comments