diff --git a/.gitignore b/.gitignore index c07eaf28..b9388916 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ .idea -MinecraftDashboard.iml +MCDash.iml /target/ \ No newline at end of file diff --git a/pom.xml b/pom.xml index df81a4a7..5e620280 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ de.gnmyt MCDash - 1.0.3 + 1.0.4 8 diff --git a/src/main/java/de/gnmyt/mcdash/api/controller/StatsController.java b/src/main/java/de/gnmyt/mcdash/api/controller/StatsController.java new file mode 100644 index 00000000..55c91ccc --- /dev/null +++ b/src/main/java/de/gnmyt/mcdash/api/controller/StatsController.java @@ -0,0 +1,97 @@ +package de.gnmyt.mcdash.api.controller; + +import de.gnmyt.mcdash.MinecraftDashboard; +import de.gnmyt.mcdash.api.tasks.TPSRunnable; +import org.bukkit.Bukkit; + +import java.io.File; + +public class StatsController { + + private final File SERVER_FOLDER = new File("."); + private final TPSRunnable TPS_RUNNABLE = new TPSRunnable(); + + private MinecraftDashboard instance; + + /** + * Basic constructor of the {@link StatsController} + * @param instance The main instance of the plugin + */ + public StatsController(MinecraftDashboard instance) { + this.instance = instance; + startTPSRunnable(); + } + + /** + * Starts the {@link TPSRunnable} + * The runnable gets the current tps from the server + */ + private void startTPSRunnable() { + Bukkit.getScheduler().scheduleSyncRepeatingTask(instance, TPS_RUNNABLE, 0, 1); + } + + /** + * Gets the current tps + * @return the current tps + */ + public long getTPS() { + return TPS_RUNNABLE.getCurrentRoundedTPS(); + } + + /** + * Gets the amount of free memory in the jvm + * @return the amount of free memory in the jvm + */ + public long getFreeMemory() { + return Runtime.getRuntime().freeMemory(); + } + + /** + * Gets the maximum amount of memory that the jvm will use + * @return the maximum amount of memory that the jvm will use + */ + public long getTotalMemory() { + return Runtime.getRuntime().totalMemory(); + } + + /** + * Gets the used amount of memory from the jvm + * @return the used amount of memory from the jvm + */ + public long getUsedMemory() { + return getTotalMemory() - getFreeMemory(); + } + + /** + * Gets the total amount of space from the server + * @return the total amount space from the server + */ + public long getTotalSpace() { + return SERVER_FOLDER.getTotalSpace(); + } + + /** + * Gets the free amount of space from the server + * @return the free amount of space from the server + */ + public long getFreeSpace() { + return SERVER_FOLDER.getFreeSpace(); + } + + /** + * Gets the used amount of space from the server + * @return the used amount of space from the server + */ + public long getUsedSpace() { + return getTotalSpace() - getFreeSpace(); + } + + /** + * Gets the total amount of processors available to the server + * @return the total amount of processors available to the server + */ + public long getAvailableProcessors() { + return Runtime.getRuntime().availableProcessors(); + } + +} diff --git a/src/main/java/de/gnmyt/mcdash/api/tasks/TPSRunnable.java b/src/main/java/de/gnmyt/mcdash/api/tasks/TPSRunnable.java new file mode 100644 index 00000000..ecb241d2 --- /dev/null +++ b/src/main/java/de/gnmyt/mcdash/api/tasks/TPSRunnable.java @@ -0,0 +1,47 @@ +package de.gnmyt.mcdash.api.tasks; + +public class TPSRunnable implements Runnable { + + public int tick_count = 0; + public long[] ticks = new long[600]; + + /** + * Updates the tick variables + */ + @Override + public void run() { + ticks[(tick_count % ticks.length)] = System.currentTimeMillis(); + tick_count++; + } + + /** + * Gets the current tps of the server + * @return the current tps of the server + */ + public double getCurrentTPS() { + return getCurrentTPS(100); + } + + /** + * Gets the current tps of the server + * @param ticks The amount of ticks + * @return the current tps of the server + */ + public double getCurrentTPS(int ticks) { + if (tick_count< ticks) return 20.0D; + + int target = (tick_count-ticks-1) % this.ticks.length; + long elapsed = System.currentTimeMillis() - this.ticks[target]; + + return ticks / (elapsed / 1000.0D); + } + + /** + * Gets the tps of the server (rounded) + * @return the tps of the server (rounded) + */ + public long getCurrentRoundedTPS() { + return Math.round(getCurrentTPS()); + } + +} diff --git a/src/main/java/de/gnmyt/mcdash/panel/routes/stats/StatsRoute.java b/src/main/java/de/gnmyt/mcdash/panel/routes/stats/StatsRoute.java new file mode 100644 index 00000000..fc1d9979 --- /dev/null +++ b/src/main/java/de/gnmyt/mcdash/panel/routes/stats/StatsRoute.java @@ -0,0 +1,24 @@ +package de.gnmyt.mcdash.panel.routes.stats; + +import de.gnmyt.mcdash.MinecraftDashboard; +import de.gnmyt.mcdash.api.controller.StatsController; +import de.gnmyt.mcdash.api.handler.DefaultHandler; +import de.gnmyt.mcdash.api.http.Request; +import de.gnmyt.mcdash.api.http.ResponseController; + +public class StatsRoute extends DefaultHandler { + + private final StatsController STATS = new StatsController(MinecraftDashboard.getInstance()); + + /** + * Gets the current server statistics such as the tps, processors, memory and the space + * @param request The request object from the HttpExchange + * @param response The response controller from the HttpExchange + */ + @Override + public void get(Request request, ResponseController response) { + response.json("tps="+STATS.getTPS(), "processors="+STATS.getAvailableProcessors(), + "free_memory="+STATS.getFreeMemory(), "total_memory="+STATS.getTotalMemory(), "used_memory="+STATS.getUsedMemory(), + "free_space="+STATS.getFreeSpace(), "total_space="+STATS.getTotalSpace(), "used_space="+STATS.getUsedSpace()); + } +} diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 9cc083cc..d778a60d 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -1,5 +1,5 @@ name: MinecraftDashboard main: de.gnmyt.mcdash.MinecraftDashboard -version: 1.0-SNAPSHOT +version: 1.4 description: This is the official plugin (wrapper) for MCDash, a free and open-source minecraft dashboard. author: GNMYT \ No newline at end of file