|
| 1 | +package dev.faststats.velocity; |
| 2 | + |
| 3 | +import com.google.gson.JsonObject; |
| 4 | +import com.velocitypowered.api.plugin.PluginContainer; |
| 5 | +import com.velocitypowered.api.plugin.annotation.DataDirectory; |
| 6 | +import com.velocitypowered.api.proxy.ProxyServer; |
| 7 | +import dev.faststats.core.Metrics; |
| 8 | +import dev.faststats.core.SimpleMetrics; |
| 9 | +import org.jetbrains.annotations.Async; |
| 10 | +import org.jspecify.annotations.Nullable; |
| 11 | +import org.slf4j.Logger; |
| 12 | + |
| 13 | +import java.io.IOException; |
| 14 | +import java.nio.file.Path; |
| 15 | +import java.util.Optional; |
| 16 | +import java.util.concurrent.TimeUnit; |
| 17 | +import java.util.function.Supplier; |
| 18 | + |
| 19 | +final class VelocityMetricsImpl extends SimpleMetrics implements VelocityMetrics { |
| 20 | + private final Logger logger; |
| 21 | + private final ProxyServer server; |
| 22 | + private final PluginContainer plugin; |
| 23 | + |
| 24 | + private VelocityMetricsImpl( |
| 25 | + SimpleMetrics.Factory<?> factory, |
| 26 | + Logger logger, |
| 27 | + ProxyServer server, |
| 28 | + Path config, |
| 29 | + PluginContainer plugin |
| 30 | + ) throws IOException, IllegalStateException { |
| 31 | + super(factory, config); |
| 32 | + |
| 33 | + this.logger = logger; |
| 34 | + this.server = server; |
| 35 | + this.plugin = plugin; |
| 36 | + |
| 37 | + startSubmitting(); |
| 38 | + } |
| 39 | + |
| 40 | + @Async.Schedule |
| 41 | + private void startSubmitting() { |
| 42 | + startSubmitting(0, 30, TimeUnit.MINUTES); |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + protected void appendDefaultData(JsonObject charts) { |
| 47 | + var pluginVersion = plugin.getDescription().getVersion().orElse("unknown"); |
| 48 | + var size = server.getPlayerCount(); |
| 49 | + |
| 50 | + charts.addProperty("online_mode", server.getConfiguration().isOnlineMode()); |
| 51 | + charts.addProperty("plugin_version", pluginVersion); |
| 52 | + charts.addProperty("server_type", server.getVersion().getName()); |
| 53 | + charts.addProperty("proxy_version", server.getVersion().getVersion()); |
| 54 | + charts.addProperty("proxy_vendor", server.getVersion().getVendor()); |
| 55 | + if (size != 0) charts.addProperty("player_count", size); |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + protected void error(String message, @Nullable Throwable throwable) { |
| 60 | + if (!isDebug()) return; |
| 61 | + var msg = "[" + VelocityMetricsImpl.class.getName() + "]: " + message; |
| 62 | + logger.error(msg, throwable); |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + protected void warn(String message) { |
| 67 | + if (!isDebug()) return; |
| 68 | + var msg = "[" + VelocityMetricsImpl.class.getName() + "]: " + message; |
| 69 | + logger.warn(msg); |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + protected void info(String message) { |
| 74 | + if (!isDebug()) return; |
| 75 | + var msg = "[" + VelocityMetricsImpl.class.getName() + "]: " + message; |
| 76 | + logger.info(msg); |
| 77 | + } |
| 78 | + |
| 79 | + private <T> Optional<T> tryOrEmpty(Supplier<@Nullable T> supplier) { |
| 80 | + try { |
| 81 | + return Optional.ofNullable(supplier.get()); |
| 82 | + } catch (NoSuchMethodError | Exception e) { |
| 83 | + error("Failed to call supplier", e); |
| 84 | + return Optional.empty(); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + static class Factory extends SimpleMetrics.Factory<Object> { |
| 89 | + protected final Logger logger; |
| 90 | + protected final Path dataDirectory; |
| 91 | + protected final ProxyServer server; |
| 92 | + |
| 93 | + public Factory(ProxyServer server, Logger logger, @DataDirectory Path dataDirectory) { |
| 94 | + this.logger = logger; |
| 95 | + this.dataDirectory = dataDirectory; |
| 96 | + this.server = server; |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public Metrics create(Object plugin) throws IOException, IllegalStateException, IllegalArgumentException { |
| 101 | + var faststats = dataDirectory.resolveSibling("faststats"); |
| 102 | + var container = server.getPluginManager().ensurePluginContainer(plugin); |
| 103 | + return new VelocityMetricsImpl(this, logger, server, faststats.resolve("config.json"), container); |
| 104 | + } |
| 105 | + } |
| 106 | +} |
0 commit comments