Skip to content

Commit

Permalink
option to restart at specific tps
Browse files Browse the repository at this point in the history
  • Loading branch information
moom0o committed Jul 1, 2021
1 parent 07ba7ae commit 5b80320
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 2 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,10 @@ finalstring: "&e[SERVER] Server restarting..."
minutestring: "minutes..."
secondsstring: "seconds..."
secondstring: "second..."
# REBOOT AT LOW TPS SETTINGS
RebootFromLowTPS: false
TPSToStartCounting: 10
HowLongShouldTheServerGoWithLowTPS: 300 # seconds, the counter is reset if the server is above the specified tps for one second.
InstantRestart: false # Should the server send the 15 minute countdown messages?
```
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>me.moomoo</groupId>
<artifactId>RestartPlugin</artifactId>
<version>2.0</version>
<version>3.0</version>
<packaging>jar</packaging>

<name>RestartPlugin</name>
Expand Down
35 changes: 35 additions & 0 deletions src/main/java/me/moomoo/restartplugin/RestartPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

public class RestartPlugin extends JavaPlugin implements Listener {
FileConfiguration config = getConfig();
int secondsServerWasAtLowTPS = 0;
Boolean serverIsRestarting = false;

public void onEnable() {
saveDefaultConfig();
Expand Down Expand Up @@ -48,10 +50,43 @@ public void onEnable() {
});
t.start();
}, initalDelay, TimeUnit.DAYS.toSeconds(1), TimeUnit.SECONDS);

if (getConfig().getBoolean("RebootFromLowTPS")) {
ScheduledExecutorService schedulerTPS = Executors.newScheduledThreadPool(1);
schedulerTPS.scheduleAtFixedRate(() -> {
Thread t = new Thread(() -> {
double tps = Bukkit.getServer().getTPS()[0];
if (secondsServerWasAtLowTPS > getConfig().getInt("HowLongShouldTheServerGoWithLowTPS")) {
if (!serverIsRestarting) {
getLogger().warning("The server is rebooting because the tps was lower than " + getConfig().getDouble("TPSToStartCounting") + " for " + getConfig().getInt("HowLongShouldTheServerGoWithLowTPS") + " seconds.");
}
if (getConfig().getBoolean("InstantRestart")) {
Bukkit.shutdown();
} else {
try {
restart();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
if (tps < getConfig().getDouble("TPSToStartCounting")) {
secondsServerWasAtLowTPS++;
} else {
secondsServerWasAtLowTPS = 0;
}
});
t.start();
}, 1, 1, TimeUnit.SECONDS);
}
});
}

public void restart() throws InterruptedException {
if (serverIsRestarting) {
return;
}
serverIsRestarting = true;
String s = get("string");
// Pull req. If you find a better way to do this!
b(translate(s.replace("%time%", "15").replace("%timeword%", get("minutestring"))));
Expand Down
8 changes: 7 additions & 1 deletion src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,10 @@ string: "&e[SERVER] Server restarting in %time% %timeword%"
finalstring: "&e[SERVER] Server restarting..."
minutestring: "minutes..."
secondsstring: "seconds..."
secondstring: "second..."
secondstring: "second..."

# REBOOT AT LOW TPS SETTINGS
RebootFromLowTPS: false
TPSToStartCounting: 10
HowLongShouldTheServerGoWithLowTPS: 300 # seconds, the counter is reset if the server is above the specified tps for one second.
InstantRestart: false # Should the server send the 15 minute countdown messages?

0 comments on commit 5b80320

Please sign in to comment.