Skip to content

Commit

Permalink
Added optional alert system for low TPS and server start/stop messages
Browse files Browse the repository at this point in the history
  • Loading branch information
justinmtech committed Dec 9, 2022
1 parent e4c0f98 commit c5d36e7
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 1 deletion.
17 changes: 17 additions & 0 deletions src/main/java/com/justinmtech/minebuddy/Minebuddy.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.justinmtech.minebuddy;

import com.justinmtech.minebuddy.commands.*;
import com.justinmtech.minebuddy.notifications.Notifier;
import com.justinmtech.minebuddy.util.ActivityMessageUpdater;
import com.justinmtech.minebuddy.util.ServerStatus;
import org.bukkit.plugin.java.JavaPlugin;
Expand All @@ -13,6 +14,7 @@
public final class Minebuddy extends JavaPlugin {
private DiscordApi api;
private ServerStatus status;
private Notifier notifier;

@Override
public void onEnable() {
Expand All @@ -30,11 +32,13 @@ public void onEnable() {

setStatus(new ServerStatus(getConfig()));
new ActivityMessageUpdater(this);

}

@Override
public void onDisable() {
if (getApi() != null) {
getNotifier().sendDiscordAlert(getStatus().getName() + " is now offline!");
getApi().disconnect();
setApi(null);
}
Expand All @@ -55,6 +59,15 @@ private void onConnectToDiscord(DiscordApi api) {
api.addListener(new StopServerCommand(getConfig()));
api.addListener(new ServerStatusCommand(getConfig(), getStatus()));

activateAlertsIfEnabled();
}

private void activateAlertsIfEnabled() {
if (getConfig().getBoolean("alerts.enabled", false)) {
notifier = new Notifier(this);
getServer().getPluginManager().registerEvents(getNotifier(), this);
getNotifier().sendDiscordAlert(getStatus().getName() + " is now online!");
}
}

public String getToken() {
Expand All @@ -76,4 +89,8 @@ public void setApi(DiscordApi api) {
public void setStatus(ServerStatus status) {
this.status = status;
}

public Notifier getNotifier() {
return notifier;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package com.justinmtech.minebuddy.notifications;

import com.justinmtech.minebuddy.Minebuddy;
import com.justinmtech.minebuddy.util.ServerStatus;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.event.Listener;
import org.bukkit.scheduler.BukkitRunnable;
import org.javacord.api.entity.channel.TextChannel;
import org.javacord.api.entity.message.MessageBuilder;

import java.util.Optional;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Notifier implements Listener {
private final Minebuddy plugin;
private final ServerStatus status;
private final FileConfiguration config;
private final Logger logger;
private double lastTpsCheck;

public Notifier(Minebuddy plugin) {
this.plugin = plugin;
this.status = plugin.getStatus();
this.config = plugin.getConfig();
this.logger = plugin.getLogger();
setLastTpsCheck(20.0);
run();
}

private void run() {
new BukkitRunnable() {
@Override
public void run() {
double tps = getStatus().getTpsFromPastMinuteAsDouble();
if (isTpsLow()) {
setLastTpsCheck(tps);
sendDiscordAlert(getStatus().getName() + " TPS is currently " + tps + "! :cry:");
} else if (!isTpsLow() && getLastTpsCheck() < getTpsThreshold()) {
sendDiscordAlert(getStatus().getName() + " TPS has recovered! :cowboy:");
setLastTpsCheck(tps);
}
}
}.runTaskTimerAsynchronously(getPlugin(), 1200L, 1200L);
}

private boolean isTpsLow() {
double tps = getStatus().getTpsFromPastMinuteAsDouble();
double threshold = getTpsThreshold();
return tps < threshold;
}

private double getTpsThreshold() {
return getConfig().getDouble("alerts.tps-threshold", 15.0);
}

public void sendDiscordAlert(String message) {
if (getAlertChannel().isPresent()) {
new MessageBuilder()
.append(message)
.send(getAlertChannel().get());
}
}

private Optional<TextChannel> getAlertChannel() {
String channelId = getConfig().getString("alerts.channel-id", "");
if (channelId == null) return Optional.empty();
if (channelId.equals("")) getLogger().log(Level.SEVERE, "Alerts are enabled but you have not set a channel ID!");
return getPlugin().getApi().getTextChannelById(channelId);
}

private Minebuddy getPlugin() {
return plugin;
}

public ServerStatus getStatus() {
return status;
}

public FileConfiguration getConfig() {
return config;
}

public Logger getLogger() {
return logger;
}

public double getLastTpsCheck() {
return lastTpsCheck;
}

public void setLastTpsCheck(double lastTpsCheck) {
this.lastTpsCheck = lastTpsCheck;
}
}
13 changes: 13 additions & 0 deletions src/main/java/com/justinmtech/minebuddy/util/ServerStatus.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,19 @@ public String getTpsFromPastMinute() {
return getPlaceholders("%server_tps_1%");
}

public double getTpsFromPastMinuteAsDouble() {
String tpsString = getPlaceholders("%server_tps_1%");
if (tpsString.contains("*")) tpsString = tpsString.replace("*", "");
double tps;
try {
tps = Double.parseDouble(tpsString);
} catch (NumberFormatException e) {
return 5.0;
}
return tps;
}


public int getStaffOnline() {
String permission = getConfig().getString("staff-permission");
if (permission == null) return 0;
Expand Down
12 changes: 11 additions & 1 deletion src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,31 @@ server-name: "My Server"
broadcast-prefix: "&c&l[Broadcast] &e"
staff-permission: "staff.perm"
invisible-on-list-permission: "staff.invisible"

#Symbol used before command
command-symbol: "!"

#Optional prefix used after symbol and before command (leave empty for none)
command-prefix: ""

#Do not change the values on the left, only the right
commands:
HELP: "help"
BROADCAST: "broadcast"
PLAYERS: "players"
STATUS: "status"
STOP: "stop"

messages:
server-status: "Here is the server status:"
stop-server: "The server is shutting down..."
players-online: "There are %placeholder% players online."
no-players-online: "There are no players online."
player-list: "The following players are online:"
help: "The following commands can be prefixed with '%placeholder%' to be executed:"
help: "The following commands can be prefixed with '%placeholder%' to be executed:"

#Enable TPS alerts and server start/stop messages if enabled. Channel ID cannot be blank.
alerts:
enabled: false
channel-id: ""
tps-threshold: 15.0

0 comments on commit c5d36e7

Please sign in to comment.