-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from gnmyt/features/statistics
📊 Added the stats feature & fixed a bug
- Loading branch information
Showing
6 changed files
with
171 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
.idea | ||
MinecraftDashboard.iml | ||
MCDash.iml | ||
/target/ |
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
97 changes: 97 additions & 0 deletions
97
src/main/java/de/gnmyt/mcdash/api/controller/StatsController.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,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(); | ||
} | ||
|
||
} |
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,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
24
src/main/java/de/gnmyt/mcdash/panel/routes/stats/StatsRoute.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,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()); | ||
} | ||
} |
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 |
---|---|---|
@@ -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 |