Skip to content

Commit

Permalink
Port to Sponge API version 8
Browse files Browse the repository at this point in the history
  • Loading branch information
me4502 committed Jan 26, 2022
1 parent cb92d26 commit 390d4c3
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 35 deletions.
2 changes: 1 addition & 1 deletion sponge/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ repositories {
}

dependencies {
compileOnly("org.spongepowered:spongeapi:7.1.0")
compileOnly("org.spongepowered:spongeapi:8.0.0")
api(project(":base")) {
isTransitive = true
}
Expand Down
68 changes: 34 additions & 34 deletions sponge/src/main/java/org/bstats/sponge/Metrics.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
package org.bstats.sponge;

import com.google.inject.Inject;
import ninja.leaping.configurate.commented.CommentedConfigurationNode;
import ninja.leaping.configurate.hocon.HoconConfigurationLoader;
import org.apache.logging.log4j.Logger;
import org.bstats.MetricsBase;
import org.bstats.charts.CustomChart;
import org.bstats.json.JsonObjectBuilder;
import org.slf4j.Logger;
import org.spongepowered.api.Platform;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.config.ConfigDir;
import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.game.state.GamePreInitializationEvent;
import org.spongepowered.api.plugin.PluginContainer;
import org.spongepowered.api.event.lifecycle.ConstructPluginEvent;
import org.spongepowered.api.scheduler.Scheduler;
import org.spongepowered.api.scheduler.Task;
import org.spongepowered.configurate.CommentedConfigurationNode;
import org.spongepowered.configurate.hocon.HoconConfigurationLoader;
import org.spongepowered.plugin.PluginContainer;

import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -46,7 +46,7 @@ private Factory(PluginContainer plugin, Logger logger, @ConfigDir(sharedRoot = t
*
* @param serviceId The id of the service.
* It can be found at <a href="https://bstats.org/what-is-my-plugin-id">What is my plugin id?</a>
* <p>Not to be confused with Sponge's {@link PluginContainer#getId()} method!
* <p>Not to be confused with Sponge's {@link org.spongepowered.plugin.metadata.PluginMetadata#id()} method!
* @return A Metrics instance that can be used to register custom charts.
* <p>The return value can be ignored, when you do not want to register custom charts.
*/
Expand All @@ -73,11 +73,11 @@ private Metrics(PluginContainer plugin, Logger logger, Path configDir, int servi
this.configDir = configDir;
this.serviceId = serviceId;

Sponge.getEventManager().registerListeners(plugin, this);
Sponge.eventManager().registerListeners(plugin, this);
}

@Listener
public void startup(GamePreInitializationEvent event) {
public void startup(ConstructPluginEvent event) {
try {
loadConfig();
} catch (IOException e) {
Expand All @@ -90,13 +90,13 @@ public void startup(GamePreInitializationEvent event) {
"sponge",
serverUUID,
serviceId,
Sponge.getMetricsConfigManager().areMetricsEnabled(plugin),
Sponge.metricsConfigManager().effectiveCollectionState(plugin).asBoolean(),
this::appendPlatformData,
this::appendServiceData,
task -> {
Scheduler scheduler = Sponge.getScheduler();
Task.Builder taskBuilder = scheduler.createTaskBuilder();
taskBuilder.execute(task).submit(plugin);
Scheduler scheduler = Sponge.asyncScheduler();
Task.Builder taskBuilder = Task.builder();
scheduler.submit(taskBuilder.execute(task).plugin(plugin).build());
},
() -> true,
logger::warn,
Expand All @@ -107,8 +107,8 @@ public void startup(GamePreInitializationEvent event) {
);

StringBuilder builder = new StringBuilder().append(System.lineSeparator());
builder.append("Plugin ").append(plugin.getName()).append(" is using bStats Metrics ");
if (Sponge.getMetricsConfigManager().areMetricsEnabled(plugin)) {
builder.append("Plugin ").append(plugin.metadata().name().orElse(plugin.metadata().id())).append(" is using bStats Metrics ");
if (Sponge.metricsConfigManager().effectiveCollectionState(plugin).asBoolean()) {
builder.append(" and is allowed to send data.");
} else {
builder.append(" but currently has data sending disabled.").append(System.lineSeparator());
Expand All @@ -124,7 +124,7 @@ private void loadConfig() throws IOException {
File configPath = configDir.resolve("bStats").toFile();
configPath.mkdirs();
File configFile = new File(configPath, "config.conf");
HoconConfigurationLoader configurationLoader = HoconConfigurationLoader.builder().setFile(configFile).build();
HoconConfigurationLoader configurationLoader = HoconConfigurationLoader.builder().file(configFile).build();
CommentedConfigurationNode node;

String serverUuidComment =
Expand All @@ -138,36 +138,36 @@ private void loadConfig() throws IOException {
configFile.createNewFile();
node = configurationLoader.load();

node.getNode("serverUuid").setValue(UUID.randomUUID().toString());
node.getNode("logFailedRequests").setValue(false);
node.getNode("logSentData").setValue(false);
node.getNode("logResponseStatusText").setValue(false);
node.getNode("serverUuid").setComment(serverUuidComment);
node.getNode("configVersion").setValue(2);
node.node("serverUuid").set(UUID.randomUUID().toString());
node.node("logFailedRequests").set(false);
node.node("logSentData").set(false);
node.node("logResponseStatusText").set(false);
node.node("serverUuid").set(serverUuidComment);
node.node("configVersion").set(2);

configurationLoader.save(node);
} else {
node = configurationLoader.load();

if (!node.getNode("configVersion").isVirtual()) {
if (!node.node("configVersion").virtual()) {

node.getNode("configVersion").setValue(2);
node.node("configVersion").set(2);

node.getNode("enabled").setComment(
node.node("enabled").comment(
"Enabling bStats in this file is deprecated. At least one of your plugins now uses the\n" +
"Sponge config to control bStats. Leave this value as you want it to be for outdated plugins,\n" +
"but look there for further control");

node.getNode("serverUuid").setComment(serverUuidComment);
node.node("serverUuid").comment(serverUuidComment);
configurationLoader.save(node);
}
}

// Load configuration
serverUUID = node.getNode("serverUuid").getString();
logErrors = node.getNode("logFailedRequests").getBoolean(false);
logSentData = node.getNode("logSentData").getBoolean(false);
logResponseStatusText = node.getNode("logResponseStatusText").getBoolean(false);
serverUUID = node.node("serverUuid").getString();
logErrors = node.node("logFailedRequests").getBoolean(false);
logSentData = node.node("logSentData").getBoolean(false);
logResponseStatusText = node.node("logResponseStatusText").getBoolean(false);
}

/**
Expand All @@ -180,10 +180,10 @@ public void addCustomChart(CustomChart chart) {
}

private void appendPlatformData(JsonObjectBuilder builder) {
builder.appendField("playerAmount", Sponge.getServer().getOnlinePlayers().size());
builder.appendField("onlineMode", Sponge.getServer().getOnlineMode() ? 1 : 0);
builder.appendField("minecraftVersion", Sponge.getGame().getPlatform().getMinecraftVersion().getName());
builder.appendField("spongeImplementation", Sponge.getPlatform().getContainer(Platform.Component.IMPLEMENTATION).getName());
builder.appendField("playerAmount", Sponge.server().onlinePlayers().size());
builder.appendField("onlineMode", Sponge.server().isOnlineModeEnabled() ? 1 : 0);
builder.appendField("minecraftVersion", Sponge.game().platform().minecraftVersion().name());
builder.appendField("spongeImplementation", Sponge.platform().container(Platform.Component.IMPLEMENTATION).metadata().id());

builder.appendField("javaVersion", System.getProperty("java.version"));
builder.appendField("osName", System.getProperty("os.name"));
Expand All @@ -193,7 +193,7 @@ private void appendPlatformData(JsonObjectBuilder builder) {
}

private void appendServiceData(JsonObjectBuilder builder) {
builder.appendField("pluginVersion", plugin.getVersion().orElse("unknown"));
builder.appendField("pluginVersion", plugin.metadata().version().toString());
}

}

0 comments on commit 390d4c3

Please sign in to comment.