Skip to content

Commit

Permalink
Add metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
magicmq committed Jul 5, 2023
1 parent 8d7f2dd commit 38916d5
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 0 deletions.
10 changes: 10 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@
<version>2.11.3</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.bstats</groupId>
<artifactId>bstats-bukkit</artifactId>
<version>3.0.2</version>
<scope>compile</scope>
</dependency>
</dependencies>

<build>
Expand Down Expand Up @@ -109,6 +115,10 @@
<pattern>me.lucko.jarrelocator</pattern>
<shadedPattern>dev.magicmq.pyspigot.libs.me.lucko.jarrelocator</shadedPattern>
</relocation>
<relocation>
<pattern>org.bstats</pattern>
<shadedPattern>dev.magicmq</shadedPattern>
</relocation>
</relocations>
<filters>
<filter>
Expand Down
35 changes: 35 additions & 0 deletions src/main/java/dev/magicmq/pyspigot/PySpigot.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,17 @@
import dev.magicmq.pyspigot.manager.protocol.ProtocolManager;
import dev.magicmq.pyspigot.manager.script.ScriptManager;
import dev.magicmq.pyspigot.manager.task.TaskManager;
import org.bstats.bukkit.Metrics;
import org.bstats.charts.SimpleBarChart;
import org.bstats.charts.SimplePie;
import org.bukkit.Bukkit;
import org.bukkit.command.PluginCommand;
import org.bukkit.command.SimpleCommandMap;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;

import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;

/**
Expand Down Expand Up @@ -94,6 +99,9 @@ public void onEnable() {
getLogger().log(Level.SEVERE, "Error when accessing CraftBukkit (Are you on a supported MC version?), PySpigot will not work correctly.");
Bukkit.getPluginManager().disablePlugin(this);
}

if (PluginConfig.getMetricsEnabled())
setupMetrics();
}

@Override
Expand Down Expand Up @@ -134,6 +142,33 @@ private void checkReflection() throws NoSuchMethodException, NoSuchFieldExceptio
SimpleCommandMap.class.getDeclaredField("knownCommands");
}

private void setupMetrics() {
final Metrics metrics = new Metrics(this, 18991);

boolean isSpigot = true;
try {
Class.forName("org.spigotmc.SpigotConfig");
} catch (ClassNotFoundException ignored) {
isSpigot = false;
}
boolean finalIsSpigot = isSpigot;
metrics.addCustomChart(new SimplePie("using_spigot", () -> finalIsSpigot ? "yes" : "no"));

int allScripts = ScriptManager.get().getAllScripts().size();
metrics.addCustomChart(new SimpleBarChart("all_scripts", () -> {
Map<String, Integer> map = new HashMap<>();
map.put(allScripts + " Scripts", 1);
return map;
}));

int loadedScripts = ScriptManager.get().getLoadedScripts().size();
metrics.addCustomChart(new SimpleBarChart("loaded_scripts", () -> {
Map<String, Integer> map = new HashMap<>();
map.put(loadedScripts + " Scripts", 1);
return map;
}));
}

/**
* Get the instance of this plugin.
* @return The instance
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/dev/magicmq/pyspigot/config/PluginConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ public static void reload() {
logTimestamp = DateTimeFormatter.ofPattern(config.getString("log-timestamp-format", "MMM dd yyyy HH:mm:ss"));
}

public static boolean getMetricsEnabled() {
return config.getBoolean("metrics-enabled", true);
}

public static long getLoadScriptDelay() {
return config.getLong("script-load-delay", 20L);
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#If false, will disable collection of metrics information by bStats for PySpigot. You may also disable bStats server-wide in the bStats config.yml under /plugins/bStats.
metrics-enabled: true
# The delay for loading scripts (in ticks) after the server finishes loading.
script-load-delay: 20
#List of relocation rules for libraries in the libs folder. Format as <pattern>|<relocated pattern>
Expand Down

0 comments on commit 38916d5

Please sign in to comment.