Skip to content

Commit

Permalink
Merge pull request #4 from gnmyt/features/statistics
Browse files Browse the repository at this point in the history
📊 Added the stats feature & fixed a bug
  • Loading branch information
gnmyt authored Aug 13, 2021
2 parents af3d518 + 308347b commit f4e0e23
Show file tree
Hide file tree
Showing 6 changed files with 171 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
.idea
MinecraftDashboard.iml
MCDash.iml
/target/
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>de.gnmyt</groupId>
<artifactId>MCDash</artifactId>
<version>1.0.3</version>
<version>1.0.4</version>

<properties>
<maven.compiler.source>8</maven.compiler.source>
Expand Down
97 changes: 97 additions & 0 deletions src/main/java/de/gnmyt/mcdash/api/controller/StatsController.java
Original file line number Diff line number Diff line change
@@ -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();
}

}
47 changes: 47 additions & 0 deletions src/main/java/de/gnmyt/mcdash/api/tasks/TPSRunnable.java
Original file line number Diff line number Diff line change
@@ -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());
}

}
24 changes: 24 additions & 0 deletions src/main/java/de/gnmyt/mcdash/panel/routes/stats/StatsRoute.java
Original file line number Diff line number Diff line change
@@ -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());
}
}
2 changes: 1 addition & 1 deletion src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit f4e0e23

Please sign in to comment.