27
27
*/
28
28
package org .mcstats ;
29
29
30
- import com . typesafe . config . Config ;
31
- import com . typesafe . config . ConfigFactory ;
32
- import com . typesafe . config . ConfigValueFactory ;
30
+ import ninja . leaping . configurate . commented . CommentedConfigurationNode ;
31
+ import ninja . leaping . configurate . hocon . HoconConfigurationLoader ;
32
+ import ninja . leaping . configurate . loader . ConfigurationLoader ;
33
33
import org .spongepowered .api .Game ;
34
34
import org .spongepowered .api .plugin .PluginContainer ;
35
- import org .spongepowered .api .service .scheduler .RepeatingTask ;
36
- import org .spongepowered .api .util .config .ConfigFile ;
35
+ import org .spongepowered .api .service .scheduler .Task ;
37
36
37
+ import javax .inject .Inject ;
38
38
import java .io .BufferedReader ;
39
39
import java .io .ByteArrayOutputStream ;
40
40
import java .io .File ;
46
46
import java .net .URL ;
47
47
import java .net .URLConnection ;
48
48
import java .net .URLEncoder ;
49
- import java .util .Collections ;
50
- import java .util .HashSet ;
51
- import java .util .Iterator ;
52
- import java .util .LinkedHashSet ;
53
- import java .util .Set ;
54
49
import java .util .UUID ;
50
+ import java .util .concurrent .TimeUnit ;
55
51
import java .util .zip .GZIPOutputStream ;
56
52
57
53
public class MetricsLite {
@@ -89,7 +85,12 @@ public class MetricsLite {
89
85
/**
90
86
* The plugin configuration file
91
87
*/
92
- private ConfigFile configuration ;
88
+ private CommentedConfigurationNode config ;
89
+
90
+ /**
91
+ * The configuration loader
92
+ */
93
+ private ConfigurationLoader <CommentedConfigurationNode > configurationLoader ;
93
94
94
95
/**
95
96
* The plugin configuration file
@@ -114,8 +115,9 @@ public class MetricsLite {
114
115
/**
115
116
* The scheduled task
116
117
*/
117
- private volatile RepeatingTask task = null ;
118
+ private volatile Task task = null ;
118
119
120
+ @ Inject
119
121
public MetricsLite (final Game game , final PluginContainer plugin ) throws IOException {
120
122
if (plugin == null ) {
121
123
throw new IllegalArgumentException ("Plugin cannot be null" );
@@ -131,20 +133,29 @@ public MetricsLite(final Game game, final PluginContainer plugin) throws IOExcep
131
133
* Loads the configuration
132
134
*/
133
135
private void loadConfiguration () {
134
- Config fallback = ConfigFactory .parseString ("mcstats = { opt-out = false, guid = null, debug = false }" );
135
-
136
- // load the config
137
136
configurationFile = getConfigFile ();
138
- configuration = ConfigFile . parseFile ( configurationFile ).resolveWith ( fallback );
137
+ configurationLoader = HoconConfigurationLoader . builder (). setFile ( configurationFile ).build ( );
139
138
140
- // Do we need to create the file?
141
- if (configuration .getString ("mcstats.guid" ) == null ) {
142
- configuration = (ConfigFile ) configuration .withValue ("mcstats.guid" , ConfigValueFactory .fromAnyRef (UUID .randomUUID ()));
143
- configuration .save (false );
144
- }
139
+ try {
140
+ if (!configurationFile .exists ()) {
141
+ configurationFile .createNewFile ();
142
+ config = configurationLoader .load ();
143
+
144
+ config .setComment ("This contains settings for MCStats: http://mcstats.org" );
145
+ config .getNode ("mcstats.guid" ).setValue (UUID .randomUUID ().toString ());
146
+ config .getNode ("mcstats.opt-out" ).setValue (false );
147
+ config .getNode ("mcstats.debug" ).setValue (false );
148
+
149
+ configurationLoader .save (config );
150
+ } else {
151
+ config = configurationLoader .load ();
152
+ }
145
153
146
- guid = configuration .getString ("guid" );
147
- debug = configuration .getBoolean ("debug" );
154
+ guid = config .getNode ("mcstats.guid" ).getString ();
155
+ debug = config .getNode ("mcstats.debug" ).getBoolean ();
156
+ } catch (IOException e ) {
157
+ e .printStackTrace ();
158
+ }
148
159
}
149
160
150
161
/**
@@ -167,7 +178,7 @@ public boolean start() {
167
178
}
168
179
169
180
// Begin hitting the server with glorious data
170
- task = game .getScheduler ().runRepeatingTask (plugin , new Runnable () {
181
+ task = game .getAsyncScheduler ().runRepeatingTask (plugin , new Runnable () {
171
182
private boolean firstPost = true ;
172
183
173
184
public void run () {
@@ -195,7 +206,7 @@ public void run() {
195
206
}
196
207
}
197
208
}
198
- }, PING_INTERVAL * 1200 ).orNull ();
209
+ }, TimeUnit . MINUTES , PING_INTERVAL ).orNull ();
199
210
200
211
return true ;
201
212
}
@@ -210,7 +221,7 @@ public boolean isOptOut() {
210
221
synchronized (optOutLock ) {
211
222
loadConfiguration ();
212
223
213
- return configuration . getBoolean ( " opt-out" );
224
+ return config . getNode ( "mcstats. opt-out"). getBoolean ( );
214
225
}
215
226
}
216
227
@@ -224,8 +235,8 @@ public void enable() throws IOException {
224
235
synchronized (optOutLock ) {
225
236
// Check if the server owner has already set opt-out, if not, set it.
226
237
if (isOptOut ()) {
227
- configuration = ( ConfigFile ) configuration . withValue ( "metrics .opt-out", ConfigValueFactory . fromAnyRef (false ) );
228
- configuration .save (false );
238
+ config . getNode ( "mcstats .opt-out"). setValue (false );
239
+ configurationLoader .save (config );
229
240
}
230
241
231
242
// Enable Task, if it is not running
@@ -245,8 +256,8 @@ public void disable() throws IOException {
245
256
synchronized (optOutLock ) {
246
257
// Check if the server owner has already set opt-out, if not, set it.
247
258
if (!isOptOut ()) {
248
- configuration = ( ConfigFile ) configuration . withValue ( "metrics .opt-out", ConfigValueFactory . fromAnyRef (true ) );
249
- configuration .save (false );
259
+ config . getNode ( "mcstats .opt-out"). setValue (true );
260
+ configurationLoader .save (config );
250
261
}
251
262
252
263
// Disable Task, if it is running
@@ -263,25 +274,25 @@ public void disable() throws IOException {
263
274
* @return the File object for the config file
264
275
*/
265
276
public File getConfigFile () {
266
- // TODO way to get data folder
267
- File pluginsFolder = new File ("plugins " );
277
+ // TODO configDir
278
+ File configFolder = new File ("config " );
268
279
269
- // return => base/plugins/PluginMetrics/config.yml
270
- return new File (new File (pluginsFolder , "PluginMetrics" ), "config.hocon" );
280
+ return new File (configFolder , "PluginMetrics.conf" );
271
281
}
272
282
273
283
/**
274
284
* Generic method that posts a plugin to the metrics website
285
+ *
275
286
*/
276
287
private void postPlugin (final boolean isPing ) throws IOException {
277
288
// Server software specific section
278
289
String pluginName = plugin .getName ();
279
- // TODO no visible way to get onlineMode at the moment
280
- boolean onlineMode = true ; // TRUE if online mode is enabled
290
+ boolean onlineMode = game .getServer ().getOnlineMode (); // TRUE if online mode is enabled
281
291
String pluginVersion = plugin .getVersion ();
282
292
// TODO no visible way to get MC version at the moment
293
+ // TODO added by game.getPlatform().getMinecraftVersion() -- impl in 2.1
283
294
String serverVersion = String .format ("%s %s" , "Sponge" , game .getImplementationVersion ());
284
- int playersOnline = game .getOnlinePlayers ().size ();
295
+ int playersOnline = game .getServer (). getOnlinePlayers ().size ();
285
296
286
297
// END server software specific section -- all code below does not use any code outside of this class / Java
287
298
0 commit comments