-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5bdca4a
commit 8c637a7
Showing
3 changed files
with
130 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
129 changes: 129 additions & 0 deletions
129
src/main/java/com/froobworld/farmcontrol/metrics/Metrics.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
package com.froobworld.farmcontrol.metrics; | ||
|
||
import com.froobworld.farmcontrol.FarmControl; | ||
import org.bstats.MetricsBase; | ||
import org.bstats.charts.CustomChart; | ||
import org.bstats.json.JsonObjectBuilder; | ||
import org.bukkit.Bukkit; | ||
import org.bukkit.configuration.file.YamlConfiguration; | ||
import org.bukkit.entity.Player; | ||
import org.bukkit.plugin.Plugin; | ||
import org.bukkit.plugin.java.JavaPlugin; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.lang.reflect.Method; | ||
import java.util.Collection; | ||
import java.util.UUID; | ||
import java.util.logging.Level; | ||
|
||
public class Metrics { | ||
private final FarmControl plugin; | ||
private final MetricsBase metricsBase; | ||
|
||
/** | ||
* Creates a new Metrics instance. | ||
* | ||
* @param plugin Your plugin instance. | ||
* @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> | ||
*/ | ||
public Metrics(FarmControl plugin, int serviceId) { | ||
this.plugin = plugin; | ||
|
||
// Get the config file | ||
File bStatsFolder = new File(plugin.getDataFolder().getParentFile(), "bStats"); | ||
File configFile = new File(bStatsFolder, "config.yml"); | ||
YamlConfiguration 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) { } | ||
} | ||
|
||
// Load the data | ||
boolean enabled = config.getBoolean("enabled", true); | ||
String serverUUID = config.getString("serverUuid"); | ||
boolean logErrors = config.getBoolean("logFailedRequests", false); | ||
boolean logSentData = config.getBoolean("logSentData", false); | ||
boolean logResponseStatusText = config.getBoolean("logResponseStatusText", false); | ||
|
||
metricsBase = new MetricsBase( | ||
"bukkit", | ||
serverUUID, | ||
serviceId, | ||
enabled, | ||
this::appendPlatformData, | ||
this::appendServiceData, | ||
plugin.getHookManager().getSchedulerHook()::runTask, | ||
plugin::isEnabled, | ||
(message, error) -> this.plugin.getLogger().log(Level.WARNING, message, error), | ||
(message) -> this.plugin.getLogger().log(Level.INFO, message), | ||
logErrors, | ||
logSentData, | ||
logResponseStatusText | ||
); | ||
} | ||
|
||
/** | ||
* Shuts down the underlying scheduler service. | ||
*/ | ||
public void shutdown() { | ||
metricsBase.shutdown(); | ||
} | ||
|
||
/** | ||
* Adds a custom chart. | ||
* | ||
* @param chart The chart to add. | ||
*/ | ||
public void addCustomChart(CustomChart chart) { | ||
metricsBase.addCustomChart(chart); | ||
} | ||
|
||
private void appendPlatformData(JsonObjectBuilder builder) { | ||
builder.appendField("playerAmount", getPlayerAmount()); | ||
builder.appendField("onlineMode", Bukkit.getOnlineMode() ? 1 : 0); | ||
builder.appendField("bukkitVersion", Bukkit.getVersion()); | ||
builder.appendField("bukkitName", Bukkit.getName()); | ||
|
||
builder.appendField("javaVersion", System.getProperty("java.version")); | ||
builder.appendField("osName", System.getProperty("os.name")); | ||
builder.appendField("osArch", System.getProperty("os.arch")); | ||
builder.appendField("osVersion", System.getProperty("os.version")); | ||
builder.appendField("coreCount", Runtime.getRuntime().availableProcessors()); | ||
} | ||
|
||
private void appendServiceData(JsonObjectBuilder builder) { | ||
builder.appendField("pluginVersion", plugin.getDescription().getVersion()); | ||
} | ||
|
||
private int getPlayerAmount() { | ||
try { | ||
// Around MC 1.8 the return type was changed from an array to a collection, | ||
// This fixes java.lang.NoSuchMethodError: org.bukkit.Bukkit.getOnlinePlayers()Ljava/util/Collection; | ||
Method onlinePlayersMethod = Class.forName("org.bukkit.Server").getMethod("getOnlinePlayers"); | ||
return onlinePlayersMethod.getReturnType().equals(Collection.class) | ||
? ((Collection<?>) onlinePlayersMethod.invoke(Bukkit.getServer())).size() | ||
: ((Player[]) onlinePlayersMethod.invoke(Bukkit.getServer())).length; | ||
} catch (Exception e) { | ||
return Bukkit.getOnlinePlayers().size(); // Just use the new method if the reflection failed | ||
} | ||
} | ||
|
||
} |