This repository has been archived by the owner on Sep 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathKickAFK.cs
142 lines (140 loc) · 4.68 KB
/
KickAFK.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Terraria;
using TerrariaApi.Server;
using TShockAPI;
namespace KickAFK
{
[ApiVersion(1, 14)]
public class KickAFK : TerrariaPlugin
{
private Config Config = new Config();
public static AFKPlayer[] Players = new AFKPlayer[256];
private DateTime LastCheck = DateTime.UtcNow;
public override string Name
{
get
{
return "KickAFK";
}
}
public override string Author
{
get
{
return "Colin";
}
}
public override string Description
{
get
{
return "Kicks AFK players.";
}
}
public override Version Version
{
get
{
return new Version("1.2");
}
}
public KickAFK(Main game)
: base(game)
{
Order = 1;
}
public override void Initialize()
{
ServerApi.Hooks.NetGreetPlayer.Register(this, OnGreet);
ServerApi.Hooks.GameUpdate.Register(this, OnUpdate);
ServerApi.Hooks.ServerLeave.Register(this, OnLeave);
ServerApi.Hooks.GameInitialize.Register(this, OnInitialize);
ServerApi.Hooks.ServerChat.Register(this, OnChat);
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
ServerApi.Hooks.NetGreetPlayer.Deregister(this, OnGreet);
ServerApi.Hooks.GameUpdate.Deregister(this, OnUpdate);
ServerApi.Hooks.ServerLeave.Deregister(this, OnLeave);
ServerApi.Hooks.GameInitialize.Deregister(this, OnInitialize);
ServerApi.Hooks.ServerChat.Deregister(this, OnChat);
}
base.Dispose(disposing);
}
public void OnInitialize(EventArgs args)
{
string path = Path.Combine(TShock.SavePath, "KickAFK.json");
if (File.Exists(path))
{
Config = Config.Read(path);
}
Config.Write(path);
}
public void OnGreet(GreetPlayerEventArgs args)
{
lock (Players)
Players[args.Who] = new AFKPlayer(args.Who);
}
public void OnLeave(LeaveEventArgs args)
{
Players[args.Who] = null;
}
public void OnUpdate(EventArgs args)
{
if ((DateTime.UtcNow - LastCheck).TotalSeconds >= 5)
{
LastCheck = DateTime.UtcNow;
lock (Players)
foreach (AFKPlayer p in Players)
if (p != null && p.TSPlayer != null)
{
if (p.TSPlayer.TileX == p.LastX && p.TSPlayer.TileY == p.LastY)
{
p.IdleTime = p.IdleTime + 5;
}
else
{
p.IdleTime = 0;
}
p.LastX = p.TSPlayer.TileX;
p.LastY = p.TSPlayer.TileY;
if (p.IdleTime > Config.KickTime && !p.TSPlayer.Group.HasPermission("tshock.admin.nokick"))
{
TShock.Utils.Kick(p.TSPlayer, Config.KickMsg, false, false, "Server", true);
}
else if (p.IdleTime > Config.WarnTime && !p.TSPlayer.Group.HasPermission("tshock.admin.nokick"))
{
p.TSPlayer.SendErrorMessage("You will be kicked for being inactive in " + (Config.KickTime - p.IdleTime) + " seconds.");
}
}
}
}
public void OnChat(ServerChatEventArgs args)
{
var player = Players[args.Who];
if (player.IdleTime > 0)
player.IdleTime = 0;
}
public class AFKPlayer
{
public int Index { get; set; }
public TSPlayer TSPlayer { get { return TShock.Players[Index]; } }
public int IdleTime { get; set; }
public int LastX { get; set; }
public int LastY { get; set; }
public AFKPlayer(int i)
{
Index = i;
IdleTime = 0;
LastX = TShock.Players[Index].TileX;
LastY = TShock.Players[Index].TileY;
}
}
}
}