Skip to content

Commit 5aa60eb

Browse files
committed
Update Sponge to API 2.0. Supports @Inject and detecting online/offline mode. MC version will be supported in 2.1.
Note the addition of @Inject. The Metrics class can now be injected into your class w/ @plugin automatically if you'd prefer that. Partially addresses #58.
1 parent 42a4221 commit 5aa60eb

File tree

4 files changed

+99
-74
lines changed

4 files changed

+99
-74
lines changed

mods/sponge/metrics-lite/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@
1717
<repository>
1818
<id>sponge-maven-repo</id>
1919
<name>Sponge maven repo</name>
20-
<url>http://repo.spongepowered.org/Sponge/maven</url>
20+
<url>http://repo.spongepowered.org/maven</url>
2121
</repository>
2222
</repositories>
2323

2424
<dependencies>
2525
<dependency>
2626
<groupId>org.spongepowered</groupId>
2727
<artifactId>spongeapi</artifactId>
28-
<version>1.0</version>
28+
<version>2.0</version>
2929
<scope>provided</scope>
3030
</dependency>
3131
</dependencies>

mods/sponge/metrics-lite/src/main/java/org/mcstats/MetricsLite.java

Lines changed: 48 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@
2727
*/
2828
package org.mcstats;
2929

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;
3333
import org.spongepowered.api.Game;
3434
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;
3736

37+
import javax.inject.Inject;
3838
import java.io.BufferedReader;
3939
import java.io.ByteArrayOutputStream;
4040
import java.io.File;
@@ -46,12 +46,8 @@
4646
import java.net.URL;
4747
import java.net.URLConnection;
4848
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;
5449
import java.util.UUID;
50+
import java.util.concurrent.TimeUnit;
5551
import java.util.zip.GZIPOutputStream;
5652

5753
public class MetricsLite {
@@ -89,7 +85,12 @@ public class MetricsLite {
8985
/**
9086
* The plugin configuration file
9187
*/
92-
private ConfigFile configuration;
88+
private CommentedConfigurationNode config;
89+
90+
/**
91+
* The configuration loader
92+
*/
93+
private ConfigurationLoader<CommentedConfigurationNode> configurationLoader;
9394

9495
/**
9596
* The plugin configuration file
@@ -114,8 +115,9 @@ public class MetricsLite {
114115
/**
115116
* The scheduled task
116117
*/
117-
private volatile RepeatingTask task = null;
118+
private volatile Task task = null;
118119

120+
@Inject
119121
public MetricsLite(final Game game, final PluginContainer plugin) throws IOException {
120122
if (plugin == null) {
121123
throw new IllegalArgumentException("Plugin cannot be null");
@@ -131,20 +133,29 @@ public MetricsLite(final Game game, final PluginContainer plugin) throws IOExcep
131133
* Loads the configuration
132134
*/
133135
private void loadConfiguration() {
134-
Config fallback = ConfigFactory.parseString("mcstats = { opt-out = false, guid = null, debug = false }");
135-
136-
// load the config
137136
configurationFile = getConfigFile();
138-
configuration = ConfigFile.parseFile(configurationFile).resolveWith(fallback);
137+
configurationLoader = HoconConfigurationLoader.builder().setFile(configurationFile).build();
139138

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+
}
145153

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+
}
148159
}
149160

150161
/**
@@ -167,7 +178,7 @@ public boolean start() {
167178
}
168179

169180
// Begin hitting the server with glorious data
170-
task = game.getScheduler().runRepeatingTask(plugin, new Runnable() {
181+
task = game.getAsyncScheduler().runRepeatingTask(plugin, new Runnable() {
171182
private boolean firstPost = true;
172183

173184
public void run() {
@@ -195,7 +206,7 @@ public void run() {
195206
}
196207
}
197208
}
198-
}, PING_INTERVAL * 1200).orNull();
209+
}, TimeUnit.MINUTES, PING_INTERVAL).orNull();
199210

200211
return true;
201212
}
@@ -210,7 +221,7 @@ public boolean isOptOut() {
210221
synchronized (optOutLock) {
211222
loadConfiguration();
212223

213-
return configuration.getBoolean("opt-out");
224+
return config.getNode("mcstats.opt-out").getBoolean();
214225
}
215226
}
216227

@@ -224,8 +235,8 @@ public void enable() throws IOException {
224235
synchronized (optOutLock) {
225236
// Check if the server owner has already set opt-out, if not, set it.
226237
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);
229240
}
230241

231242
// Enable Task, if it is not running
@@ -245,8 +256,8 @@ public void disable() throws IOException {
245256
synchronized (optOutLock) {
246257
// Check if the server owner has already set opt-out, if not, set it.
247258
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);
250261
}
251262

252263
// Disable Task, if it is running
@@ -263,25 +274,25 @@ public void disable() throws IOException {
263274
* @return the File object for the config file
264275
*/
265276
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");
268279

269-
// return => base/plugins/PluginMetrics/config.yml
270-
return new File(new File(pluginsFolder, "PluginMetrics"), "config.hocon");
280+
return new File(configFolder, "PluginMetrics.conf");
271281
}
272282

273283
/**
274284
* Generic method that posts a plugin to the metrics website
285+
*
275286
*/
276287
private void postPlugin(final boolean isPing) throws IOException {
277288
// Server software specific section
278289
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
281291
String pluginVersion = plugin.getVersion();
282292
// TODO no visible way to get MC version at the moment
293+
// TODO added by game.getPlatform().getMinecraftVersion() -- impl in 2.1
283294
String serverVersion = String.format("%s %s", "Sponge", game.getImplementationVersion());
284-
int playersOnline = game.getOnlinePlayers().size();
295+
int playersOnline = game.getServer().getOnlinePlayers().size();
285296

286297
// END server software specific section -- all code below does not use any code outside of this class / Java
287298

mods/sponge/metrics/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@
1717
<repository>
1818
<id>sponge-maven-repo</id>
1919
<name>Sponge maven repo</name>
20-
<url>http://repo.spongepowered.org/Sponge/maven</url>
20+
<url>http://repo.spongepowered.org/maven</url>
2121
</repository>
2222
</repositories>
2323

2424
<dependencies>
2525
<dependency>
2626
<groupId>org.spongepowered</groupId>
2727
<artifactId>spongeapi</artifactId>
28-
<version>1.0</version>
28+
<version>2.0</version>
2929
<scope>provided</scope>
3030
</dependency>
3131
</dependencies>

mods/sponge/metrics/src/main/java/org/mcstats/Metrics.java

Lines changed: 47 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@
2727
*/
2828
package org.mcstats;
2929

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;
3333
import org.spongepowered.api.Game;
3434
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;
3736

37+
import javax.inject.Inject;
3838
import java.io.BufferedReader;
3939
import java.io.ByteArrayOutputStream;
4040
import java.io.File;
@@ -52,7 +52,7 @@
5252
import java.util.LinkedHashSet;
5353
import java.util.Set;
5454
import java.util.UUID;
55-
import java.util.logging.Level;
55+
import java.util.concurrent.TimeUnit;
5656
import java.util.zip.GZIPOutputStream;
5757

5858
public class Metrics {
@@ -95,13 +95,18 @@ public class Metrics {
9595
/**
9696
* The plugin configuration file
9797
*/
98-
private ConfigFile configuration;
98+
private CommentedConfigurationNode config;
9999

100100
/**
101101
* The plugin configuration file
102102
*/
103103
private File configurationFile;
104104

105+
/**
106+
* The configuration loader
107+
*/
108+
private ConfigurationLoader<CommentedConfigurationNode> configurationLoader;
109+
105110
/**
106111
* Unique server id
107112
*/
@@ -120,8 +125,9 @@ public class Metrics {
120125
/**
121126
* The scheduled task
122127
*/
123-
private volatile RepeatingTask task = null;
128+
private volatile Task task = null;
124129

130+
@Inject
125131
public Metrics(final Game game, final PluginContainer plugin) throws IOException {
126132
if (plugin == null) {
127133
throw new IllegalArgumentException("Plugin cannot be null");
@@ -137,20 +143,29 @@ public Metrics(final Game game, final PluginContainer plugin) throws IOException
137143
* Loads the configuration
138144
*/
139145
private void loadConfiguration() {
140-
Config fallback = ConfigFactory.parseString("mcstats = { opt-out = false, guid = null, debug = false }");
141-
142-
// load the config
143146
configurationFile = getConfigFile();
144-
configuration = ConfigFile.parseFile(configurationFile).resolveWith(fallback);
147+
configurationLoader = HoconConfigurationLoader.builder().setFile(configurationFile).build();
145148

146-
// Do we need to create the file?
147-
if (configuration.getString("mcstats.guid") == null) {
148-
configuration = (ConfigFile) configuration.withValue("mcstats.guid", ConfigValueFactory.fromAnyRef(UUID.randomUUID()));
149-
configuration.save(false);
150-
}
149+
try {
150+
if (!configurationFile.exists()) {
151+
configurationFile.createNewFile();
152+
config = configurationLoader.load();
153+
154+
config.setComment("This contains settings for MCStats: http://mcstats.org");
155+
config.getNode("mcstats.guid").setValue(UUID.randomUUID().toString());
156+
config.getNode("mcstats.opt-out").setValue(false);
157+
config.getNode("mcstats.debug").setValue(false);
158+
159+
configurationLoader.save(config);
160+
} else {
161+
config = configurationLoader.load();
162+
}
151163

152-
guid = configuration.getString("guid");
153-
debug = configuration.getBoolean("debug");
164+
guid = config.getNode("mcstats.guid").getString();
165+
debug = config.getNode("mcstats.debug").getBoolean();
166+
} catch (IOException e) {
167+
e.printStackTrace();
168+
}
154169
}
155170

156171
/**
@@ -208,7 +223,7 @@ public boolean start() {
208223
}
209224

210225
// Begin hitting the server with glorious data
211-
task = game.getScheduler().runRepeatingTask(plugin, new Runnable() {
226+
task = game.getAsyncScheduler().runRepeatingTask(plugin, new Runnable() {
212227
private boolean firstPost = true;
213228

214229
public void run() {
@@ -240,7 +255,7 @@ public void run() {
240255
}
241256
}
242257
}
243-
}, PING_INTERVAL * 1200).orNull();
258+
}, TimeUnit.MINUTES, PING_INTERVAL).orNull();
244259

245260
return true;
246261
}
@@ -255,7 +270,7 @@ public boolean isOptOut() {
255270
synchronized (optOutLock) {
256271
loadConfiguration();
257272

258-
return configuration.getBoolean("opt-out");
273+
return config.getNode("mcstats.opt-out").getBoolean();
259274
}
260275
}
261276

@@ -269,8 +284,8 @@ public void enable() throws IOException {
269284
synchronized (optOutLock) {
270285
// Check if the server owner has already set opt-out, if not, set it.
271286
if (isOptOut()) {
272-
configuration = (ConfigFile) configuration.withValue("metrics.opt-out", ConfigValueFactory.fromAnyRef(false));
273-
configuration.save(false);
287+
config.getNode("mcstats.opt-out").setValue(false);
288+
configurationLoader.save(config);
274289
}
275290

276291
// Enable Task, if it is not running
@@ -290,8 +305,8 @@ public void disable() throws IOException {
290305
synchronized (optOutLock) {
291306
// Check if the server owner has already set opt-out, if not, set it.
292307
if (!isOptOut()) {
293-
configuration = (ConfigFile) configuration.withValue("metrics.opt-out", ConfigValueFactory.fromAnyRef(true));
294-
configuration.save(false);
308+
config.getNode("mcstats.opt-out").setValue(true);
309+
configurationLoader.save(config);
295310
}
296311

297312
// Disable Task, if it is running
@@ -308,11 +323,10 @@ public void disable() throws IOException {
308323
* @return the File object for the config file
309324
*/
310325
public File getConfigFile() {
311-
// TODO way to get data folder
312-
File pluginsFolder = new File("plugins");
326+
// TODO configDir
327+
File configFolder = new File("config");
313328

314-
// return => base/plugins/PluginMetrics/config.yml
315-
return new File(new File(pluginsFolder, "PluginMetrics"), "config.hocon");
329+
return new File(configFolder, "PluginMetrics.conf");
316330
}
317331

318332
/**
@@ -321,12 +335,12 @@ public File getConfigFile() {
321335
private void postPlugin(final boolean isPing) throws IOException {
322336
// Server software specific section
323337
String pluginName = plugin.getName();
324-
// TODO no visible way to get onlineMode at the moment
325-
boolean onlineMode = true; // TRUE if online mode is enabled
338+
boolean onlineMode = game.getServer().getOnlineMode(); // TRUE if online mode is enabled
326339
String pluginVersion = plugin.getVersion();
327340
// TODO no visible way to get MC version at the moment
341+
// TODO added by game.getPlatform().getMinecraftVersion() -- impl in 2.1
328342
String serverVersion = String.format("%s %s", "Sponge", game.getImplementationVersion());
329-
int playersOnline = game.getOnlinePlayers().size();
343+
int playersOnline = game.getServer().getOnlinePlayers().size();
330344

331345
// END server software specific section -- all code below does not use any code outside of this class / Java
332346

0 commit comments

Comments
 (0)