Skip to content

Commit f4e0e23

Browse files
authored
Merge pull request #4 from gnmyt/features/statistics
📊 Added the stats feature & fixed a bug
2 parents af3d518 + 308347b commit f4e0e23

File tree

6 files changed

+171
-3
lines changed

6 files changed

+171
-3
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
.idea
2-
MinecraftDashboard.iml
2+
MCDash.iml
33
/target/

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>de.gnmyt</groupId>
88
<artifactId>MCDash</artifactId>
9-
<version>1.0.3</version>
9+
<version>1.0.4</version>
1010

1111
<properties>
1212
<maven.compiler.source>8</maven.compiler.source>
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package de.gnmyt.mcdash.api.controller;
2+
3+
import de.gnmyt.mcdash.MinecraftDashboard;
4+
import de.gnmyt.mcdash.api.tasks.TPSRunnable;
5+
import org.bukkit.Bukkit;
6+
7+
import java.io.File;
8+
9+
public class StatsController {
10+
11+
private final File SERVER_FOLDER = new File(".");
12+
private final TPSRunnable TPS_RUNNABLE = new TPSRunnable();
13+
14+
private MinecraftDashboard instance;
15+
16+
/**
17+
* Basic constructor of the {@link StatsController}
18+
* @param instance The main instance of the plugin
19+
*/
20+
public StatsController(MinecraftDashboard instance) {
21+
this.instance = instance;
22+
startTPSRunnable();
23+
}
24+
25+
/**
26+
* Starts the {@link TPSRunnable}
27+
* The runnable gets the current tps from the server
28+
*/
29+
private void startTPSRunnable() {
30+
Bukkit.getScheduler().scheduleSyncRepeatingTask(instance, TPS_RUNNABLE, 0, 1);
31+
}
32+
33+
/**
34+
* Gets the current tps
35+
* @return the current tps
36+
*/
37+
public long getTPS() {
38+
return TPS_RUNNABLE.getCurrentRoundedTPS();
39+
}
40+
41+
/**
42+
* Gets the amount of free memory in the jvm
43+
* @return the amount of free memory in the jvm
44+
*/
45+
public long getFreeMemory() {
46+
return Runtime.getRuntime().freeMemory();
47+
}
48+
49+
/**
50+
* Gets the maximum amount of memory that the jvm will use
51+
* @return the maximum amount of memory that the jvm will use
52+
*/
53+
public long getTotalMemory() {
54+
return Runtime.getRuntime().totalMemory();
55+
}
56+
57+
/**
58+
* Gets the used amount of memory from the jvm
59+
* @return the used amount of memory from the jvm
60+
*/
61+
public long getUsedMemory() {
62+
return getTotalMemory() - getFreeMemory();
63+
}
64+
65+
/**
66+
* Gets the total amount of space from the server
67+
* @return the total amount space from the server
68+
*/
69+
public long getTotalSpace() {
70+
return SERVER_FOLDER.getTotalSpace();
71+
}
72+
73+
/**
74+
* Gets the free amount of space from the server
75+
* @return the free amount of space from the server
76+
*/
77+
public long getFreeSpace() {
78+
return SERVER_FOLDER.getFreeSpace();
79+
}
80+
81+
/**
82+
* Gets the used amount of space from the server
83+
* @return the used amount of space from the server
84+
*/
85+
public long getUsedSpace() {
86+
return getTotalSpace() - getFreeSpace();
87+
}
88+
89+
/**
90+
* Gets the total amount of processors available to the server
91+
* @return the total amount of processors available to the server
92+
*/
93+
public long getAvailableProcessors() {
94+
return Runtime.getRuntime().availableProcessors();
95+
}
96+
97+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package de.gnmyt.mcdash.api.tasks;
2+
3+
public class TPSRunnable implements Runnable {
4+
5+
public int tick_count = 0;
6+
public long[] ticks = new long[600];
7+
8+
/**
9+
* Updates the tick variables
10+
*/
11+
@Override
12+
public void run() {
13+
ticks[(tick_count % ticks.length)] = System.currentTimeMillis();
14+
tick_count++;
15+
}
16+
17+
/**
18+
* Gets the current tps of the server
19+
* @return the current tps of the server
20+
*/
21+
public double getCurrentTPS() {
22+
return getCurrentTPS(100);
23+
}
24+
25+
/**
26+
* Gets the current tps of the server
27+
* @param ticks The amount of ticks
28+
* @return the current tps of the server
29+
*/
30+
public double getCurrentTPS(int ticks) {
31+
if (tick_count< ticks) return 20.0D;
32+
33+
int target = (tick_count-ticks-1) % this.ticks.length;
34+
long elapsed = System.currentTimeMillis() - this.ticks[target];
35+
36+
return ticks / (elapsed / 1000.0D);
37+
}
38+
39+
/**
40+
* Gets the tps of the server (rounded)
41+
* @return the tps of the server (rounded)
42+
*/
43+
public long getCurrentRoundedTPS() {
44+
return Math.round(getCurrentTPS());
45+
}
46+
47+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package de.gnmyt.mcdash.panel.routes.stats;
2+
3+
import de.gnmyt.mcdash.MinecraftDashboard;
4+
import de.gnmyt.mcdash.api.controller.StatsController;
5+
import de.gnmyt.mcdash.api.handler.DefaultHandler;
6+
import de.gnmyt.mcdash.api.http.Request;
7+
import de.gnmyt.mcdash.api.http.ResponseController;
8+
9+
public class StatsRoute extends DefaultHandler {
10+
11+
private final StatsController STATS = new StatsController(MinecraftDashboard.getInstance());
12+
13+
/**
14+
* Gets the current server statistics such as the tps, processors, memory and the space
15+
* @param request The request object from the HttpExchange
16+
* @param response The response controller from the HttpExchange
17+
*/
18+
@Override
19+
public void get(Request request, ResponseController response) {
20+
response.json("tps="+STATS.getTPS(), "processors="+STATS.getAvailableProcessors(),
21+
"free_memory="+STATS.getFreeMemory(), "total_memory="+STATS.getTotalMemory(), "used_memory="+STATS.getUsedMemory(),
22+
"free_space="+STATS.getFreeSpace(), "total_space="+STATS.getTotalSpace(), "used_space="+STATS.getUsedSpace());
23+
}
24+
}

src/main/resources/plugin.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: MinecraftDashboard
22
main: de.gnmyt.mcdash.MinecraftDashboard
3-
version: 1.0-SNAPSHOT
3+
version: 1.4
44
description: This is the official plugin (wrapper) for MCDash, a free and open-source minecraft dashboard.
55
author: GNMYT

0 commit comments

Comments
 (0)