|
| 1 | +package dev.faststats.sponge; |
| 2 | + |
| 3 | +import com.google.gson.JsonObject; |
| 4 | +import dev.faststats.core.Metrics; |
| 5 | +import dev.faststats.core.SimpleMetrics; |
| 6 | +import org.apache.logging.log4j.Logger; |
| 7 | +import org.jetbrains.annotations.Async; |
| 8 | +import org.jetbrains.annotations.Contract; |
| 9 | +import org.jspecify.annotations.Nullable; |
| 10 | +import org.spongepowered.api.Platform; |
| 11 | +import org.spongepowered.api.Sponge; |
| 12 | +import org.spongepowered.api.config.ConfigDir; |
| 13 | +import org.spongepowered.plugin.PluginContainer; |
| 14 | + |
| 15 | +import java.nio.file.Path; |
| 16 | + |
| 17 | +final class SpongeMetricsImpl extends SimpleMetrics implements SpongeMetrics { |
| 18 | + public static final String COMMENT = """ |
| 19 | + FastStats (https://faststats.dev) collects anonymous usage statistics for plugin developers. |
| 20 | + # This helps developers understand how their projects are used in the real world. |
| 21 | + # |
| 22 | + # No IP addresses, player data, or personal information is collected. |
| 23 | + # The server ID below is randomly generated and can be regenerated at any time. |
| 24 | + # |
| 25 | + # Enabling metrics has no noticeable performance impact. |
| 26 | + # Enabling metrics is recommended, you can do so in the Sponge config. |
| 27 | + # |
| 28 | + # If you suspect a plugin is collecting personal data or bypassing the Sponge config, |
| 29 | + # please report it at: https://faststats.dev/abuse |
| 30 | + # |
| 31 | + # For more information, visit: https://faststats.dev/info |
| 32 | + """; |
| 33 | + |
| 34 | + private final Logger logger; |
| 35 | + private final PluginContainer plugin; |
| 36 | + |
| 37 | + @Async.Schedule |
| 38 | + @Contract(mutates = "io") |
| 39 | + private SpongeMetricsImpl( |
| 40 | + SimpleMetrics.Factory<?> factory, |
| 41 | + Logger logger, |
| 42 | + PluginContainer plugin, |
| 43 | + Path config |
| 44 | + ) throws IllegalStateException { |
| 45 | + super(factory, SimpleMetrics.Config.read(config, COMMENT, true, Sponge.metricsConfigManager() |
| 46 | + .effectiveCollectionState(plugin).asBoolean())); |
| 47 | + |
| 48 | + this.logger = logger; |
| 49 | + this.plugin = plugin; |
| 50 | + |
| 51 | + startSubmitting(); |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + protected void appendDefaultData(JsonObject charts) { |
| 56 | + charts.addProperty("online_mode", Sponge.server().isOnlineModeEnabled()); |
| 57 | + charts.addProperty("player_count", Sponge.server().onlinePlayers().size()); |
| 58 | + charts.addProperty("plugin_version", plugin.metadata().version().toString()); |
| 59 | + charts.addProperty("minecraft_version", Sponge.platform().minecraftVersion().name()); |
| 60 | + charts.addProperty("server_type", Sponge.platform().container(Platform.Component.IMPLEMENTATION).metadata().id()); |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + protected void printError(String message, @Nullable Throwable throwable) { |
| 65 | + logger.error(message, throwable); |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + protected void printInfo(String message) { |
| 70 | + logger.info(message); |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + protected void printWarning(String message) { |
| 75 | + logger.warn(message); |
| 76 | + } |
| 77 | + |
| 78 | + static class Factory extends SimpleMetrics.Factory<PluginContainer> { |
| 79 | + protected final Logger logger; |
| 80 | + protected final Path dataDirectory; |
| 81 | + |
| 82 | + public Factory(Logger logger, @ConfigDir(sharedRoot = true) Path dataDirectory) { |
| 83 | + this.logger = logger; |
| 84 | + this.dataDirectory = dataDirectory; |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public Metrics create(PluginContainer plugin) throws IllegalStateException, IllegalArgumentException { |
| 89 | + var faststats = dataDirectory.resolveSibling("faststats"); |
| 90 | + return new SpongeMetricsImpl(this, logger, plugin, faststats.resolve("config.properties")); |
| 91 | + } |
| 92 | + } |
| 93 | +} |
0 commit comments