|
@ -1,25 +1,15 @@ |
|
|
package de.yannicpunktdee.yoshibot.utils; |
|
|
package de.yannicpunktdee.yoshibot.utils; |
|
|
|
|
|
|
|
|
import de.yannicpunktdee.yoshibot.main.YoshiBot; |
|
|
|
|
|
import lombok.SneakyThrows; |
|
|
|
|
|
|
|
|
import lombok.Getter; |
|
|
import net.dv8tion.jda.api.EmbedBuilder; |
|
|
import net.dv8tion.jda.api.EmbedBuilder; |
|
|
import net.dv8tion.jda.api.MessageBuilder; |
|
|
|
|
|
import net.dv8tion.jda.api.entities.Guild; |
|
|
|
|
|
import net.dv8tion.jda.api.entities.Message; |
|
|
|
|
|
import net.dv8tion.jda.api.entities.TextChannel; |
|
|
|
|
|
import org.apache.commons.io.FileUtils; |
|
|
|
|
|
import org.json.JSONObject; |
|
|
import org.json.JSONObject; |
|
|
|
|
|
|
|
|
import java.io.BufferedReader; |
|
|
import java.io.BufferedReader; |
|
|
import java.io.File; |
|
|
|
|
|
import java.io.IOException; |
|
|
import java.io.IOException; |
|
|
import java.io.InputStreamReader; |
|
|
import java.io.InputStreamReader; |
|
|
import java.nio.file.Files; |
|
|
|
|
|
import java.time.LocalDateTime; |
|
|
import java.time.LocalDateTime; |
|
|
import java.time.format.DateTimeFormatter; |
|
|
|
|
|
import java.util.HashMap; |
|
|
import java.util.HashMap; |
|
|
import java.util.List; |
|
|
import java.util.List; |
|
|
import java.util.Locale; |
|
|
|
|
|
import java.util.Map; |
|
|
import java.util.Map; |
|
|
import java.util.concurrent.Executors; |
|
|
import java.util.concurrent.Executors; |
|
|
import java.util.concurrent.ScheduledExecutorService; |
|
|
import java.util.concurrent.ScheduledExecutorService; |
|
@ -33,64 +23,31 @@ public class StatusProvider implements Provider { |
|
|
|
|
|
|
|
|
private int lastPlayersOnline = -1; |
|
|
private int lastPlayersOnline = -1; |
|
|
|
|
|
|
|
|
private volatile String messageId = null; |
|
|
|
|
|
|
|
|
|
|
|
private final int serverPort; |
|
|
|
|
|
|
|
|
@Getter |
|
|
|
|
|
private final String ip; |
|
|
|
|
|
|
|
|
private final String desc; |
|
|
private final String desc; |
|
|
|
|
|
|
|
|
private LocalDateTime timestampLastPlayerOnline; |
|
|
private LocalDateTime timestampLastPlayerOnline; |
|
|
|
|
|
|
|
|
private static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern("HH:mm:ss E, dd.MM.yyyy", |
|
|
|
|
|
Locale.GERMANY); |
|
|
|
|
|
|
|
|
|
|
|
private final ProcessBuilder mcstatus = new ProcessBuilder(); |
|
|
private final ProcessBuilder mcstatus = new ProcessBuilder(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@SneakyThrows |
|
|
|
|
|
@SuppressWarnings("empty") |
|
|
|
|
|
public StatusProvider(String desc, String messageIdKey, Guild guild, int secondsPerTime, |
|
|
|
|
|
int serverPort) { |
|
|
|
|
|
TextChannel statusChannel = guild.getTextChannelById(Resources.getStatus_channel()); |
|
|
|
|
|
|
|
|
|
|
|
this.desc = desc; |
|
|
|
|
|
this.messageId = StatusProvider.ensureMessageId(statusChannel, messageIdKey, guild); |
|
|
|
|
|
this.serverPort = serverPort; |
|
|
|
|
|
this.mcstatus.command(Resources.getPath_to_mcstatus(), Resources.getMc_server() + ":" + serverPort, "json"); |
|
|
|
|
|
|
|
|
public StatusProvider(String desc, int secondsPerTime, String ip, LocalDateTime timestamp) { |
|
|
|
|
|
this.timestampLastPlayerOnline = timestamp; |
|
|
|
|
|
this.desc = desc; |
|
|
|
|
|
this.ip = ip; |
|
|
|
|
|
this.mcstatus.command(Resources.getPath_to_mcstatus(), ip, "json"); |
|
|
|
|
|
|
|
|
File timeStampSave = new File(String.format("%s/%d.txt", Resources.getResourcePath(), serverPort)); |
|
|
|
|
|
timestampLastPlayerOnline = null; |
|
|
|
|
|
|
|
|
|
|
|
if (!timeStampSave.createNewFile()) { |
|
|
|
|
|
List<String> contents = Files.readAllLines(timeStampSave.toPath()); |
|
|
|
|
|
if (contents.size() > 0) { |
|
|
|
|
|
timestampLastPlayerOnline = LocalDateTime.parse(contents.get(0), TIME_FORMATTER); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
statusScheduler.scheduleAtFixedRate(() -> updateStatusMessage(statusChannel), 0, secondsPerTime, |
|
|
|
|
|
|
|
|
statusScheduler.scheduleAtFixedRate(this::updateStatusMessage, 0, secondsPerTime, |
|
|
TimeUnit.SECONDS); |
|
|
TimeUnit.SECONDS); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private synchronized static String ensureMessageId(TextChannel statusChannel, String messageIdKey, Guild guild) { |
|
|
|
|
|
if (Resources.getProperty(messageIdKey) != null) { |
|
|
|
|
|
return Resources.getProperty(messageIdKey); |
|
|
|
|
|
} else { |
|
|
|
|
|
assert statusChannel != null; |
|
|
|
|
|
MessageBuilder mb = new MessageBuilder(); |
|
|
|
|
|
mb.append("ServerInformation"); |
|
|
|
|
|
Message msg = mb.build(); |
|
|
|
|
|
statusChannel.sendMessage(msg).complete(); |
|
|
|
|
|
Resources.setProperty(messageIdKey, statusChannel.getLatestMessageId()); |
|
|
|
|
|
return statusChannel.getLatestMessageId(); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@SuppressWarnings("unchecked") |
|
|
@SuppressWarnings("unchecked") |
|
|
public void updateStatusMessage(TextChannel statusChannel) { |
|
|
|
|
|
|
|
|
public void updateStatusMessage() { |
|
|
EmbedBuilder eb = new EmbedBuilder(); |
|
|
EmbedBuilder eb = new EmbedBuilder(); |
|
|
eb.setTitle(desc); |
|
|
eb.setTitle(desc); |
|
|
eb.addField("IP", Resources.getMc_server() + ":" + serverPort, false); |
|
|
|
|
|
|
|
|
eb.addField("IP", ip, false); |
|
|
|
|
|
|
|
|
try { |
|
|
try { |
|
|
Map<String, Object> serverInfo = getPlayersOnline(); |
|
|
Map<String, Object> serverInfo = getPlayersOnline(); |
|
@ -99,30 +56,27 @@ public class StatusProvider implements Provider { |
|
|
eb.addField("Server still Starting", "True", false); |
|
|
eb.addField("Server still Starting", "True", false); |
|
|
} else { |
|
|
} else { |
|
|
int newPlayersOnline = (int) serverInfo.get("playerCount"); |
|
|
int newPlayersOnline = (int) serverInfo.get("playerCount"); |
|
|
if (newPlayersOnline == lastPlayersOnline) return; |
|
|
|
|
|
else { |
|
|
|
|
|
if (timestampLastPlayerOnline == null && newPlayersOnline == 0 && lastPlayersOnline != -1) { |
|
|
|
|
|
timestampLastPlayerOnline = LocalDateTime.now(); |
|
|
|
|
|
} else if (timestampLastPlayerOnline != null && newPlayersOnline > 0) { |
|
|
|
|
|
timestampLastPlayerOnline = null; |
|
|
|
|
|
} |
|
|
|
|
|
lastPlayersOnline = newPlayersOnline; |
|
|
|
|
|
} |
|
|
|
|
|
eb.addField("Version", (String) serverInfo.get("version"), true); |
|
|
eb.addField("Version", (String) serverInfo.get("version"), true); |
|
|
eb.addField("MOTD", (String) serverInfo.get("motd"), true); |
|
|
eb.addField("MOTD", (String) serverInfo.get("motd"), true); |
|
|
eb.addField("Spieler online", lastPlayersOnline + " / " + serverInfo.get("playerMax"), false); |
|
|
|
|
|
if (timestampLastPlayerOnline != null) { |
|
|
|
|
|
eb.addField("Zuletzt gesehen", TIME_FORMATTER.format(timestampLastPlayerOnline), false); |
|
|
|
|
|
} |
|
|
|
|
|
if (lastPlayersOnline > 0) { |
|
|
|
|
|
|
|
|
eb.addField("Spieler online", newPlayersOnline + " / " + serverInfo.get("playerMax"), false); |
|
|
|
|
|
if (newPlayersOnline == 0) { |
|
|
|
|
|
if (lastPlayersOnline > newPlayersOnline) { |
|
|
|
|
|
timestampLastPlayerOnline = LocalDateTime.now(); |
|
|
|
|
|
StatusProviderFactory.updateTimestamp(this, timestampLastPlayerOnline); |
|
|
|
|
|
} |
|
|
|
|
|
eb.addField("Zuletzt gesehen", |
|
|
|
|
|
StatusProviderFactory.TIME_FORMATTER.format(timestampLastPlayerOnline), false); |
|
|
|
|
|
} else { |
|
|
eb.addField("Spieler:", String.join(", ", (List<String>) serverInfo.get("playerNames")), false); |
|
|
eb.addField("Spieler:", String.join(", ", (List<String>) serverInfo.get("playerNames")), false); |
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
lastPlayersOnline = newPlayersOnline; |
|
|
} |
|
|
} |
|
|
} else { |
|
|
} else { |
|
|
eb.addField("Offline", "", false); |
|
|
eb.addField("Offline", "", false); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
statusChannel.editMessageById(this.messageId, eb.build()).queue(); |
|
|
|
|
|
|
|
|
StatusProviderFactory.updateStatus(this, eb.build()); |
|
|
} catch (IOException e) { |
|
|
} catch (IOException e) { |
|
|
Logger.logError(e.toString()); |
|
|
Logger.logError(e.toString()); |
|
|
} |
|
|
} |
|
@ -138,7 +92,7 @@ public class StatusProvider implements Provider { |
|
|
e.printStackTrace(); |
|
|
e.printStackTrace(); |
|
|
} |
|
|
} |
|
|
if (process.exitValue() != 0) { |
|
|
if (process.exitValue() != 0) { |
|
|
Logger.logError("MCStatus on port " + serverPort + " exited with errorcode " + process.exitValue()); |
|
|
|
|
|
|
|
|
Logger.logError("MCStatus on IP " + ip + " exited with errorcode " + process.exitValue()); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
String output = new BufferedReader(new InputStreamReader(process.getInputStream())).lines() |
|
|
String output = new BufferedReader(new InputStreamReader(process.getInputStream())).lines() |
|
@ -175,13 +129,8 @@ public class StatusProvider implements Provider { |
|
|
return result; |
|
|
return result; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@SneakyThrows |
|
|
|
|
|
@Override |
|
|
@Override |
|
|
public void onStop() { |
|
|
public void onStop() { |
|
|
Logger.logInfo(String.format("Stopping StatusProvider for \"%s\" on Port %d", desc, serverPort)); |
|
|
|
|
|
if (timestampLastPlayerOnline != null) { |
|
|
|
|
|
FileUtils.writeStringToFile(new File(Resources.getResourcePath() + "/" + serverPort + ".txt"), |
|
|
|
|
|
TIME_FORMATTER.format(timestampLastPlayerOnline), "UTF" + "-8"); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
StatusProviderFactory.updateTimestamp(this, timestampLastPlayerOnline); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |