forked from DevotedMC/CivSpy
-
Notifications
You must be signed in to change notification settings - Fork 7
/
CivSpyBungee.java
209 lines (185 loc) · 6.33 KB
/
CivSpyBungee.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
package com.programmerdan.minecraft.civspy;
import java.io.File;
import java.io.InputStream;
import java.io.IOException;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.HashMap;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.nio.file.Files;
import net.md_5.bungee.api.plugin.Listener;
import net.md_5.bungee.api.plugin.Plugin;
import net.md_5.bungee.api.scheduler.ScheduledTask;
import net.md_5.bungee.config.Configuration;
import net.md_5.bungee.config.YamlConfiguration;
import net.md_5.bungee.config.ConfigurationProvider;
import net.md_5.bungee.event.EventHandler;
import net.md_5.bungee.api.event.PlayerDisconnectEvent;
import net.md_5.bungee.api.event.PostLoginEvent;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import com.programmerdan.minecraft.civspy.CivSpyPlayerCount;
import com.programmerdan.minecraft.civspy.database.Database;
/**
* Lightweight Bungee data sampler; at this point just tracks player sessions, so
* has no complex infrastructure.
*
* @author ProgrammerDan
*/
public class CivSpyBungee extends Plugin implements Listener {
private Logger logger;
private Configuration config;
private Database db;
private ScheduledTask tracker;
private CivSpyPlayerCount counter;
private HashMap<UUID, Long> players;
@Override
public void onEnable() {
this.logger = getLogger();
logger.info("Getting CivSpyBungee configuration");
this.config = loadConfig();
if (this.config != null) {
logger.info("Setting up Database for CivSpyBungee");
this.db = configDatabase(config.getSection("database"));
} else {
logger.severe("Config not found, CivSpyBungee going dark.");
}
try {
if (this.db != null) {
this.db.available();
logger.info("Setting up CivSpyBungee Playercount Tracker");
this.counter = new CivSpyPlayerCount(this, db);
tracker = getProxy().getScheduler().schedule(this, this.counter,
config.getInt("interval", 60000), config.getInt("interval", 60000), TimeUnit.MILLISECONDS);
logger.info("Setting up CivSpyBungee Player Tracking");
this.players = new HashMap<>();
getProxy().getPluginManager().registerListener(this, this);
} else {
logger.severe("Database not connected, CivSpyBungee going dark.");
}
} catch(SQLException se) {
logger.severe("Database failed connecting, CivSpyBungee going dark.");
}
}
@Override
public void onDisable() {
logger.info("Shutting down CivSpyBungee");
getProxy().getScheduler().cancel(tracker);
this.counter.sample();
for (UUID player : players.keySet()) {
// TODO: Check that this doesn't double-insert.
db.insertData("bungee.logout", player);
session(player);
}
players.clear();
try {
this.db.close();
} catch (SQLException se) {
logger.log(Level.SEVERE, "Couldn't close out database and connections for CivSpyBungee");
}
}
private Configuration loadConfig() {
if (!getDataFolder().exists())
getDataFolder().mkdir();
File file = new File(getDataFolder(), "config.yml");
if (!file.exists()) {
logger.info("Setting up CivSpyBungee default configuration");
try (InputStream in = getResourceAsStream("config.yml")) {
Files.copy(in, file.toPath());
} catch (IOException e) {
logger.log(Level.SEVERE, "Failed to save CivSpyBungee default config", e);
}
}
try {
return ConfigurationProvider.getProvider(YamlConfiguration.class).load(file);
} catch (IOException ioe) {
logger.log(Level.SEVERE, "Failed to load CivSpyBungee config", ioe);
return null;
}
}
private Database configDatabase(Configuration dbStuff) {
if (dbStuff == null) {
logger.severe("No database credentials specified. This plugin requires a database to run!");
return null;
}
String host = dbStuff.getString("host");
if (host == null) {
logger.severe("No host for database specified. Could not load database credentials");
return null;
}
int port = dbStuff.getInt("port", -1);
if (port == -1) {
logger.severe("No port for database specified. Could not load database credentials");
return null;
}
String db = dbStuff.getString("database");
if (db == null) {
logger.severe("No name for database specified. Could not load database credentials");
return null;
}
String user = dbStuff.getString("user");
if (user == null) {
logger.severe("No user for database specified. Could not load database credentials");
return null;
}
String password = dbStuff.getString("password");
if (password == null) {
logger.warning("No password for database specified.");
}
Integer poolSize = dbStuff.getInt("poolsize", 10);
Long connectionTimeout = dbStuff.getLong("connectionTimeout", 10000L);
Long idleTimeout = dbStuff.getLong("idleTimeout", 600_000L);
Long maxLifetime = dbStuff.getLong("maxLifetime", 7_200_000L);
return new Database(logger, user, password, host, port, db,
poolSize, connectionTimeout, idleTimeout, maxLifetime);
}
/**
* Records player joining the network.
*
* Generates: <code>bungee.login</code> stat_key data.
*
* @param event The login event
*/
@EventHandler
public void afterLogin(PostLoginEvent event) {
if (db == null) {
return;
}
ProxiedPlayer player = event.getPlayer();
db.insertData("bungee.login", player.getUniqueId());
players.put(player.getUniqueId(), System.currentTimeMillis());
}
/**
* Records player departure from the network.
*
* Generates: <code>bungee.logout</code> stat_key data.
*
* @param event The disconnect event
*/
@EventHandler
public void afterLeave(PlayerDisconnectEvent event) {
if (db == null) {
return;
}
ProxiedPlayer player = event.getPlayer();
UUID unid = player.getUniqueId();
db.insertData("bungee.logout", unid);
session(unid);
players.remove(unid);
}
/**
* Computes the session length of the player in milliseconds.
*
* Generates: <code>bungee.session</code> stat_key data.
*
* @param player The UUID of the player whose session length needs computing.
*/
public void session(UUID player) {
Long start = players.get(player);
if (start == null) {
return;
}
db.insertData("bungee.session", player, System.currentTimeMillis() - start);
}
}