impl server watcher for minecraft servers
All checks were successful
Deploy to Dokku / docker (ubuntu-latest) (push) Successful in 1m27s
All checks were successful
Deploy to Dokku / docker (ubuntu-latest) (push) Successful in 1m27s
This commit is contained in:
@ -0,0 +1,143 @@
|
||||
package cc.fascinated.bat.features.minecraft;
|
||||
|
||||
import cc.fascinated.bat.Emojis;
|
||||
import cc.fascinated.bat.common.*;
|
||||
import com.google.gson.Gson;
|
||||
import lombok.Getter;
|
||||
import net.dv8tion.jda.api.EmbedBuilder;
|
||||
import org.bson.Document;
|
||||
import xyz.mcutils.McUtilsAPI;
|
||||
import xyz.mcutils.models.server.MinecraftServer;
|
||||
import xyz.mcutils.models.server.ServerPlatform;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author Fascinated (fascinated7)
|
||||
*/
|
||||
@Getter
|
||||
public class MinecraftProfile extends Serializable {
|
||||
/**
|
||||
* The servers that are getting their status watched
|
||||
*/
|
||||
private final List<ServerWatcher> serverWatchers = new ArrayList<>();
|
||||
|
||||
/**
|
||||
* Adds a server watcher
|
||||
*
|
||||
* @param serverWatcher - The server watcher to add
|
||||
*/
|
||||
public void addServerWatcher(ServerWatcher serverWatcher) {
|
||||
serverWatchers.add(serverWatcher);
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes a server watcher
|
||||
*
|
||||
* @param serverWatcher - The server watcher to remove
|
||||
*/
|
||||
public void removeServerWatcher(ServerWatcher serverWatcher) {
|
||||
serverWatchers.remove(serverWatcher);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a server watcher by hostname
|
||||
*
|
||||
* @param hostname the hostname of the server
|
||||
* @param platform the platform of the server
|
||||
* @return the server watcher
|
||||
*/
|
||||
public ServerWatcher getServerWatcher(String hostname, ServerPlatform platform) {
|
||||
for (ServerWatcher serverWatcher : serverWatchers) {
|
||||
if (serverWatcher.getHostname().equalsIgnoreCase(hostname) && serverWatcher.getPlatform() == platform) {
|
||||
return serverWatcher;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void checkServers() {
|
||||
for (ServerWatcher server : serverWatchers) {
|
||||
int platformDefaultPort = server.getPlatform() == ServerPlatform.JAVA ? 25565 : 19132;
|
||||
String hostname = server.getHostname() + (server.getPort() != platformDefaultPort ? ":" + server.getPort() : "");
|
||||
boolean isOnline = true;
|
||||
|
||||
MinecraftServer minecraftServer = null;
|
||||
switch (server.getPlatform()) {
|
||||
case JAVA -> {
|
||||
try {
|
||||
minecraftServer = McUtilsAPI.getJavaServer(hostname);
|
||||
} catch (Exception e) {
|
||||
isOnline = false;
|
||||
}
|
||||
}
|
||||
case BEDROCK -> {
|
||||
try {
|
||||
minecraftServer = McUtilsAPI.getBedrockServer(hostname);
|
||||
} catch (Exception e) {
|
||||
isOnline = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (isOnline == server.isLastState()) {
|
||||
return;
|
||||
}
|
||||
server.setLastState(isOnline);
|
||||
EmbedBuilder embedBuilder = isOnline ? EmbedUtils.successEmbed() : EmbedUtils.errorEmbed();
|
||||
EmbedDescriptionBuilder description = new EmbedDescriptionBuilder("Server Watcher");
|
||||
description.appendLine("%s %s server `%s` is now **%s**".formatted(
|
||||
isOnline ? Emojis.CHECK_MARK_EMOJI : Emojis.CROSS_MARK_EMOJI,
|
||||
EnumUtils.getEnumName(server.getPlatform()),
|
||||
hostname,
|
||||
isOnline ? "online" : "offline"
|
||||
), false);
|
||||
if (minecraftServer != null) {
|
||||
description.appendLine("Players: `%s/%s`".formatted(
|
||||
NumberFormatter.simpleFormat(minecraftServer.getPlayers().getOnline()),
|
||||
NumberFormatter.simpleFormat(minecraftServer.getPlayers().getMax())
|
||||
), true);
|
||||
}
|
||||
server.getChannel().sendMessageEmbeds(embedBuilder
|
||||
.setDescription(description.build())
|
||||
.setThumbnail("https://api.mcutils.xyz/server/icon/%s".formatted(hostname))
|
||||
.build()).queue();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void load(Document document, Gson gson) {
|
||||
for (Document watcherDocument : document.getList("serverWatchers", Document.class, new ArrayList<>())) {
|
||||
serverWatchers.add(new ServerWatcher(
|
||||
watcherDocument.getString("hostname"),
|
||||
watcherDocument.getInteger("port"),
|
||||
ServerPlatform.valueOf(watcherDocument.getString("platform")),
|
||||
watcherDocument.getString("channelId"),
|
||||
watcherDocument.getBoolean("lastState", false)
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Document serialize(Gson gson) {
|
||||
Document document = new Document();
|
||||
List<Document> watcherDocuments = new ArrayList<>();
|
||||
for (ServerWatcher serverWatcher : serverWatchers) {
|
||||
Document watcherDocument = new Document();
|
||||
watcherDocument.append("hostname", serverWatcher.getHostname());
|
||||
watcherDocument.append("port", serverWatcher.getPort());
|
||||
watcherDocument.append("platform", serverWatcher.getPlatform().name());
|
||||
watcherDocument.append("channelId", serverWatcher.getChannelId());
|
||||
watcherDocument.append("lastState", serverWatcher.isLastState());
|
||||
watcherDocuments.add(watcherDocument);
|
||||
}
|
||||
document.append("serverWatchers", watcherDocuments);
|
||||
return document;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void reset() {
|
||||
serverWatchers.clear();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user