1
0
mirror of https://github.com/GeyserMC/Geyser.git synced 2026-01-04 15:31:36 +00:00

Use global bStats config where possible

This commit is contained in:
Camotoy
2024-09-12 15:36:42 -04:00
parent b71927840b
commit 1271505c26
13 changed files with 423 additions and 27 deletions

View File

@@ -0,0 +1,99 @@
/*
* Copyright (c) 2024 GeyserMC. http://geysermc.org
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @author GeyserMC
* @link https://github.com/GeyserMC/Geyser
*/
package org.geysermc.geyser.platform.bungeecord;
import net.md_5.bungee.api.plugin.Plugin;
import net.md_5.bungee.config.Configuration;
import net.md_5.bungee.config.ConfigurationProvider;
import net.md_5.bungee.config.YamlConfiguration;
import org.geysermc.geyser.util.metrics.MetricsPlatform;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.UUID;
public final class BungeeMetrics implements MetricsPlatform {
private final Configuration configuration;
public BungeeMetrics(Plugin plugin) throws IOException {
// https://github.com/Bastian/bstats-metrics/blob/master/bungeecord/src/main/java/org/bstats/bungeecord/Metrics.java
File bStatsFolder = new File(plugin.getDataFolder().getParentFile(), "bStats");
//noinspection ResultOfMethodCallIgnored
bStatsFolder.mkdirs();
File configFile = new File(bStatsFolder, "config.yml");
if (!configFile.exists()) {
writeFile(configFile,
"# bStats (https://bStats.org) collects some basic information for plugin authors, like how",
"# many people use their plugin and their total player count. It's recommended to keep bStats",
"# enabled, but if you're not comfortable with this, you can turn this setting off. There is no",
"# performance penalty associated with having metrics enabled, and data sent to bStats is fully",
"# anonymous.",
"enabled: true",
"serverUuid: \"" + UUID.randomUUID() + "\"",
"logFailedRequests: false",
"logSentData: false",
"logResponseStatusText: false");
}
this.configuration = ConfigurationProvider.getProvider(YamlConfiguration.class).load(configFile);
}
@Override
public boolean enabled() {
return configuration.getBoolean("enabled", true);
}
@Override
public String serverUuid() {
return configuration.getString("serverUuid");
}
@Override
public boolean logFailedRequests() {
return configuration.getBoolean("logFailedRequests", false);
}
@Override
public boolean logSentData() {
return configuration.getBoolean("logSentData", false);
}
@Override
public boolean logResponseStatusText() {
return configuration.getBoolean("logResponseStatusText", false);
}
private void writeFile(File file, String... lines) throws IOException {
try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file))) {
for (String line : lines) {
bufferedWriter.write(line);
bufferedWriter.newLine();
}
}
}
}

View File

@@ -47,10 +47,12 @@ import org.geysermc.geyser.ping.GeyserLegacyPingPassthrough;
import org.geysermc.geyser.ping.IGeyserPingPassthrough;
import org.geysermc.geyser.platform.bungeecord.command.BungeeCommandSource;
import org.geysermc.geyser.text.GeyserLocale;
import org.geysermc.geyser.util.metrics.MetricsPlatform;
import org.incendo.cloud.CommandManager;
import org.incendo.cloud.bungee.BungeeCommandManager;
import org.incendo.cloud.execution.ExecutionCoordinator;
import java.io.IOException;
import java.lang.reflect.Field;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
@@ -288,6 +290,19 @@ public class GeyserBungeePlugin extends Plugin implements GeyserPluginBootstrap
return FloodgateKeyLoader.getKeyPath(geyserConfig, floodgateDataFolder, geyserDataFolder, geyserLogger);
}
@Override
public MetricsPlatform createMetricsPlatform() {
try {
return new BungeeMetrics(this);
} catch (IOException e) {
this.geyserLogger.debug("Integrated bStats support failed to load.");
if (this.config().debugMode()) {
e.printStackTrace();
}
return null;
}
}
private Optional<InetSocketAddress> findCompatibleListener() {
return getProxy().getConfig().getListeners().stream()
.filter(info -> info.getSocketAddress() instanceof InetSocketAddress)

View File

@@ -67,6 +67,7 @@ import org.geysermc.geyser.platform.spigot.world.manager.GeyserSpigotLegacyNativ
import org.geysermc.geyser.platform.spigot.world.manager.GeyserSpigotNativeWorldManager;
import org.geysermc.geyser.platform.spigot.world.manager.GeyserSpigotWorldManager;
import org.geysermc.geyser.text.GeyserLocale;
import org.geysermc.geyser.util.metrics.MetricsPlatform;
import org.incendo.cloud.bukkit.BukkitCommandManager;
import org.incendo.cloud.execution.ExecutionCoordinator;
import org.incendo.cloud.paper.LegacyPaperCommandManager;
@@ -462,6 +463,11 @@ public class GeyserSpigotPlugin extends JavaPlugin implements GeyserPluginBootst
return FloodgateKeyLoader.getKeyPath(geyserConfig, floodgateDataFolder, geyserDataFolder, geyserLogger);
}
@Override
public MetricsPlatform createMetricsPlatform() {
return new SpigotMetrics(this);
}
@SuppressWarnings("BooleanMethodIsAlwaysInverted")
private boolean loadConfig() {
this.geyserConfig = new ConfigLoader(this).createFolder().load(GeyserPluginConfig.class);

View File

@@ -0,0 +1,91 @@
/*
* Copyright (c) 2024 GeyserMC. http://geysermc.org
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @author GeyserMC
* @link https://github.com/GeyserMC/Geyser
*/
package org.geysermc.geyser.platform.spigot;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.Plugin;
import org.geysermc.geyser.util.metrics.MetricsPlatform;
import java.io.File;
import java.io.IOException;
import java.util.UUID;
public final class SpigotMetrics implements MetricsPlatform {
private final YamlConfiguration config;
public SpigotMetrics(Plugin plugin) {
// https://github.com/Bastian/bstats-metrics/blob/master/bukkit/src/main/java/org/bstats/bukkit/Metrics.java
// Get the config file
File bStatsFolder = new File(plugin.getDataFolder().getParentFile(), "bStats");
File configFile = new File(bStatsFolder, "config.yml");
config = YamlConfiguration.loadConfiguration(configFile);
if (!config.isSet("serverUuid")) {
config.addDefault("enabled", true);
config.addDefault("serverUuid", UUID.randomUUID().toString());
config.addDefault("logFailedRequests", false);
config.addDefault("logSentData", false);
config.addDefault("logResponseStatusText", false);
// Inform the server owners about bStats
config.options().header(
"bStats (https://bStats.org) collects some basic information for plugin authors, like how\n" +
"many people use their plugin and their total player count. It's recommended to keep bStats\n" +
"enabled, but if you're not comfortable with this, you can turn this setting off. There is no\n" +
"performance penalty associated with having metrics enabled, and data sent to bStats is fully\n" +
"anonymous."
).copyDefaults(true);
try {
config.save(configFile);
} catch (IOException ignored) { }
}
}
@Override
public boolean enabled() {
return config.getBoolean("enabled", true);
}
@Override
public String serverUuid() {
return config.getString("serverUuid");
}
@Override
public boolean logFailedRequests() {
return config.getBoolean("logFailedRequests", false);
}
@Override
public boolean logSentData() {
return config.getBoolean("logSentData", false);
}
@Override
public boolean logResponseStatusText() {
return config.getBoolean("logResponseStatusText", false);
}
}

View File

@@ -54,11 +54,13 @@ import org.geysermc.geyser.ping.GeyserLegacyPingPassthrough;
import org.geysermc.geyser.ping.IGeyserPingPassthrough;
import org.geysermc.geyser.platform.velocity.command.VelocityCommandSource;
import org.geysermc.geyser.text.GeyserLocale;
import org.geysermc.geyser.util.metrics.MetricsPlatform;
import org.incendo.cloud.CommandManager;
import org.incendo.cloud.execution.ExecutionCoordinator;
import org.incendo.cloud.velocity.VelocityCommandManager;
import org.slf4j.Logger;
import java.io.IOException;
import java.net.SocketAddress;
import java.nio.file.Path;
import java.nio.file.Paths;
@@ -250,6 +252,19 @@ public class GeyserVelocityPlugin implements GeyserPluginBootstrap {
return FloodgateKeyLoader.getKeyPath(geyserConfig, floodgateDataPath, configFolder, geyserLogger);
}
@Override
public MetricsPlatform createMetricsPlatform() {
try {
return new VelocityMetrics(this.configFolder);
} catch (IOException e) {
this.geyserLogger.debug("Integrated bStats support failed to load.");
if (this.config().debugMode()) {
e.printStackTrace();
}
return null;
}
}
@SuppressWarnings("BooleanMethodIsAlwaysInverted")
private boolean loadConfig() {
this.geyserConfig = new ConfigLoader(this).createFolder().load(GeyserPluginConfig.class);

View File

@@ -0,0 +1,69 @@
/*
* Copyright (c) 2024 GeyserMC. http://geysermc.org
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @author GeyserMC
* @link https://github.com/GeyserMC/Geyser
*/
package org.geysermc.geyser.platform.velocity;
import org.bstats.config.MetricsConfig;
import org.geysermc.geyser.util.metrics.MetricsPlatform;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
public final class VelocityMetrics implements MetricsPlatform {
private final MetricsConfig config;
public VelocityMetrics(Path dataDirectory) throws IOException {
// https://github.com/Bastian/bstats-metrics/blob/master/velocity/src/main/java/org/bstats/velocity/Metrics.java
File configFile = dataDirectory.getParent().resolve("bStats").resolve("config.txt").toFile();
this.config = new MetricsConfig(configFile, true);
// No logger message is implemented as Velocity should print its own before we do.
}
@Override
public boolean enabled() {
return config.isEnabled();
}
@Override
public String serverUuid() {
return config.getServerUUID();
}
@Override
public boolean logFailedRequests() {
return config.isLogErrorsEnabled();
}
@Override
public boolean logSentData() {
return config.isLogSentDataEnabled();
}
@Override
public boolean logResponseStatusText() {
return config.isLogResponseStatusTextEnabled();
}
}