/
AFKPGC.java
236 lines (193 loc) · 7.22 KB
/
AFKPGC.java
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package com.github.Kraken3.AFKPGC;
import java.util.logging.Logger;
import java.util.*;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
public class AFKPGC extends JavaPlugin {
public static Logger logger;
public static JavaPlugin plugin;
public static boolean enabled = true;
@Override
public void onEnable(){
//setting a couple of static fields so that they are available elsewhere
logger = getLogger();
plugin = this;
//Reads Config.yml - false as an answer indicated unrecoverable error
AFKPGC.enabled = ConfigurationReader.readConfig();
if(!AFKPGC.enabled) Message.error(0);
getServer().getPluginManager().registerEvents(new EventHandlers(), this);
//Checks whether to 'garbage collect' AFKers every 20 ticks (1 seconds);
getServer().getScheduler().scheduleSyncRepeatingTask(this, new Kicker(), 0, 20L);
getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
public void run() {
LastActivity.currentTime = System.currentTimeMillis();
}
}, 0, 1L);
//Because bukkit..
getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {
public void run() {
LastActivity.FixInconsitencies();
}
}, 0, 6000L);
Message.send(1);
}
@Override
public void onDisable(){
Message.send(2);
}
@Override
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
Player player = null;
if (sender instanceof Player) player = (Player) sender;
if(args.length == 0) return false;
if (cmd.getName().equalsIgnoreCase("afkpgc")){
if(args[0].equalsIgnoreCase("times")){
return onCommandTimes(player);
} else if(args[0].equalsIgnoreCase("info")){
return onCommandInfo(player);
} else if(args[0].equalsIgnoreCase("reload") || args[0].equalsIgnoreCase("list") || args[0].equalsIgnoreCase("stop") ){
if(player != null && !player.isOp()) {
player.sendMessage("Only OP can do that. ");
return true;
}
if(args[0].equalsIgnoreCase("reload")) return onCommandReload(player);
else if(args[0].equalsIgnoreCase("list")) return onCommandList(player, args);
else if(args[0].equalsIgnoreCase("stop")) return onCommandStop(player);
}
}
return false;
}
private boolean onCommandTimes(Player player){
int[] kt = Kicker.kickThresholds;
int ktlen = kt.length;
int starti = -1;
int stopi = -1;
int val = -1;
for(int i = 0; i < ktlen; i++){
if(val == kt[i]) {
stopi = i;
} else {
if(starti != -1) sendRangeMessage(player, starti, stopi,val);
starti = i;
stopi = i;
}
val = kt[i];
}
sendRangeMessage(player, starti, stopi,val);
return true;
}
private void sendRangeMessage(Player player, int start, int stop, int val){
if(val != 0){
if(start == stop) Message.send(player, 3, (start+1), readableTimeSpan(val));
else Message.send(player, 4, (start+1), (stop+1), readableTimeSpan(val));
}
}
private boolean onCommandReload(Player player){
AFKPGC.enabled = ConfigurationReader.readConfig();
if(AFKPGC.enabled) Message.send(player, 5);
else Message.send(player, 6);
return true;
}
private boolean onCommandStop(Player player){
Message.send(player, 7);
AFKPGC.enabled = false;
return true;
}
private boolean onCommandInfo(Player player){
Message.send(player, 15, plugin.getDescription().getVersion(), AFKPGC.enabled ? "enabled" : "disabled");
if(Kicker.amIStillAlivePlayer == null) Kicker.amIStillAlivePlayer = new ArrayList<String>();
if(player != null) Kicker.amIStillAlivePlayer.add(player.getName());
else Kicker.amIStillAlivePlayer.add(null);
return true;
}
private boolean onCommandList(Player player, String[] args){
int p = 10;
if(args.length == 2){
try{
p = Integer.parseInt(args[1]);
} catch (Exception e){}
}
if(p < 0) p = 10;
Message.send(player, 8, p);
ArrayList<LastActivity> las = new ArrayList<LastActivity>();
Set<String> set = LastActivity.lastActivities.keySet();
for(String i:set) las.add(LastActivity.lastActivities.get(i));
int laslen = las.size();
Collections.sort(las, new Comparator<LastActivity>(){
public int compare(LastActivity arg0, LastActivity arg1) {
return (int)(arg0.timeOfLastActivity - arg1.timeOfLastActivity);
}
});
for(int i = 0; i < laslen; i++){
if(i == p) break;
int t = (int)((LastActivity.currentTime - las.get(i).timeOfLastActivity)/1000);
Message.send(player, 9, las.get(i).playerName, readableTimeSpan(t));
}
return true;
}
public static String readableTimeSpan(int t){
String b = "";
int s = t % 60;
t /= 60;
int m = t % 60;
t /= 60;
int h = t % 24;
t /= 24;
int d = t;
if(d != 0) b += d + "d ";
if(d != 0 || h != 0) b += h + "h ";
if(d != 0 || h != 0 || m != 0) b += m + "m ";
b += s + "s ";
return b;
}
public static void removerPlayer(String name){
if(LastActivity.lastActivities.containsKey(name))LastActivity.lastActivities.remove(name);
}
public static void addPlayer(String name){
if(name == null) return;
LastActivity la;
if(LastActivity.lastActivities.containsKey(name)){
la = LastActivity.lastActivities.get(name);
} else {
la = new LastActivity();
LastActivity.lastActivities.put(name,la);
}
la.timeOfLastActivity = System.currentTimeMillis();
la.timeOflastKickerPass = System.currentTimeMillis();
la.playerName = name;
}
}
class Warning{
public int time;
public String message;
public Warning (int time, String message){
this.time = time;
this.message = message;
}
}
class LastActivity{
public static Map<String, LastActivity> lastActivities = new TreeMap<String, LastActivity>();
public static long currentTime; //OCD compels me to save a few System.currentTimeMillis() calls..
public long timeOfLastActivity;
public long timeOflastKickerPass; //time of the last Kicker.run call, relevant for warnings
public String playerName; //useful only in onCommandList
//let's be polite.. I strongly dislike bukkit.. onPlayerQuitEvent doesn't trigger on all
//player log off events for some reason. This causes LastActivity.lastActivities to contain more players
//than there are playing on the server. FixInconsitencies() fixes this problem.
static public void FixInconsitencies(){
Map<String, LastActivity> lastActivities = LastActivity.lastActivities;
Player[] players = AFKPGC.plugin.getServer().getOnlinePlayers();
TreeSet<String> playersTree = new TreeSet<String>();
for(Player p:players) {
String name = p.getName();
if(!lastActivities.containsKey(name)) AFKPGC.addPlayer(p.getName());
playersTree.add(name);
}
String[] keySet = lastActivities.keySet().toArray(new String[0]);
for(String i:keySet){
if(!playersTree.contains(i)) AFKPGC.removerPlayer(i);
}
}
}