|
@ -4,12 +4,14 @@ import lombok.SneakyThrows; |
|
|
import net.dv8tion.jda.api.EmbedBuilder; |
|
|
import net.dv8tion.jda.api.EmbedBuilder; |
|
|
import net.dv8tion.jda.api.entities.Guild; |
|
|
import net.dv8tion.jda.api.entities.Guild; |
|
|
import net.dv8tion.jda.api.entities.TextChannel; |
|
|
import net.dv8tion.jda.api.entities.TextChannel; |
|
|
|
|
|
import org.apache.commons.io.FileUtils; |
|
|
import org.json.JSONArray; |
|
|
import org.json.JSONArray; |
|
|
import org.json.JSONObject; |
|
|
import org.json.JSONObject; |
|
|
|
|
|
|
|
|
import java.io.*; |
|
|
import java.io.*; |
|
|
import java.net.Socket; |
|
|
import java.net.Socket; |
|
|
import java.net.UnknownHostException; |
|
|
import java.net.UnknownHostException; |
|
|
|
|
|
import java.nio.file.Files; |
|
|
import java.time.LocalDateTime; |
|
|
import java.time.LocalDateTime; |
|
|
import java.time.format.DateTimeFormatter; |
|
|
import java.time.format.DateTimeFormatter; |
|
|
import java.util.*; |
|
|
import java.util.*; |
|
@ -19,7 +21,7 @@ import java.util.concurrent.TimeUnit; |
|
|
import java.util.stream.Collectors; |
|
|
import java.util.stream.Collectors; |
|
|
import java.util.stream.StreamSupport; |
|
|
import java.util.stream.StreamSupport; |
|
|
|
|
|
|
|
|
public class StatusProvider { |
|
|
|
|
|
|
|
|
public class StatusProvider implements Provider { |
|
|
|
|
|
|
|
|
private static final ScheduledExecutorService statusScheduler = Executors.newScheduledThreadPool(1); |
|
|
private static final ScheduledExecutorService statusScheduler = Executors.newScheduledThreadPool(1); |
|
|
|
|
|
|
|
@ -31,7 +33,7 @@ public class StatusProvider { |
|
|
|
|
|
|
|
|
private final String desc; |
|
|
private final String desc; |
|
|
|
|
|
|
|
|
private LocalDateTime timestampLastPlayerOnline = null; |
|
|
|
|
|
|
|
|
private LocalDateTime timestampLastPlayerOnline; |
|
|
|
|
|
|
|
|
private static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern("HH:mm:ss E, dd.MM.yyyy", |
|
|
private static final DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern("HH:mm:ss E, dd.MM.yyyy", |
|
|
Locale.GERMANY); |
|
|
Locale.GERMANY); |
|
@ -39,12 +41,23 @@ public class StatusProvider { |
|
|
private final ProcessBuilder mcstatus = new ProcessBuilder(); |
|
|
private final ProcessBuilder mcstatus = new ProcessBuilder(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@SneakyThrows |
|
|
public StatusProvider(String desc, String messageId, Guild guild, int secondsPerTime, int serverPort) { |
|
|
public StatusProvider(String desc, String messageId, Guild guild, int secondsPerTime, int serverPort) { |
|
|
this.desc = desc; |
|
|
this.desc = desc; |
|
|
this.message_id = messageId; |
|
|
this.message_id = messageId; |
|
|
this.serverPort = serverPort; |
|
|
this.serverPort = serverPort; |
|
|
this.mcstatus.command(Resources.getPath_to_mcstatus(), Resources.getMc_server() + ":" + serverPort, "json"); |
|
|
this.mcstatus.command(Resources.getPath_to_mcstatus(), Resources.getMc_server() + ":" + serverPort, "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); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
TextChannel statusChannel = guild.getTextChannelById(Resources.getStatus_channel()); |
|
|
TextChannel statusChannel = guild.getTextChannelById(Resources.getStatus_channel()); |
|
|
statusScheduler.scheduleAtFixedRate(() -> updateStatusMessage(statusChannel), 0, secondsPerTime, |
|
|
statusScheduler.scheduleAtFixedRate(() -> updateStatusMessage(statusChannel), 0, secondsPerTime, |
|
|
TimeUnit.SECONDS); |
|
|
TimeUnit.SECONDS); |
|
@ -53,29 +66,33 @@ public class StatusProvider { |
|
|
@SuppressWarnings("unchecked") |
|
|
@SuppressWarnings("unchecked") |
|
|
@SneakyThrows |
|
|
@SneakyThrows |
|
|
public void updateStatusMessage(TextChannel statusChannel) { |
|
|
public void updateStatusMessage(TextChannel statusChannel) { |
|
|
|
|
|
EmbedBuilder eb = new EmbedBuilder(); |
|
|
|
|
|
eb.setTitle(desc); |
|
|
|
|
|
eb.addField("IP", "85.214.148.23" + ":" + serverPort, false); |
|
|
|
|
|
|
|
|
Map<String, Object> serverInfo = getPlayersOnline(); |
|
|
Map<String, Object> serverInfo = getPlayersOnline(); |
|
|
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; |
|
|
|
|
|
|
|
|
if ((boolean) serverInfo.get("online")) { |
|
|
|
|
|
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("Spieler online", lastPlayersOnline + " / " + serverInfo.get("playerMax"), false); |
|
|
|
|
|
if (timestampLastPlayerOnline != null) { |
|
|
|
|
|
eb.addField("Zuletzt gesehen", TIME_FORMATTER.format(timestampLastPlayerOnline), false); |
|
|
} |
|
|
} |
|
|
lastPlayersOnline = newPlayersOnline; |
|
|
|
|
|
|
|
|
if (lastPlayersOnline > 0) { |
|
|
|
|
|
eb.addField("Spieler:", String.join(", ", (List<String>) serverInfo.get("playerNames")), false); |
|
|
|
|
|
} |
|
|
|
|
|
} else { |
|
|
|
|
|
eb.addField("Offline", "", false); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
EmbedBuilder eb = new EmbedBuilder(); |
|
|
|
|
|
eb.setTitle(desc); |
|
|
|
|
|
eb.addField("IP", Resources.getMc_server() + ":" + serverPort, false); |
|
|
|
|
|
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:", String.join(", ", (List<String>) serverInfo.get("playerNames")), false); |
|
|
|
|
|
} |
|
|
|
|
|
statusChannel.editMessageById(this.message_id, "Serverinformation").queue(); |
|
|
|
|
|
statusChannel.editMessageById(this.message_id, eb.build()).queue(); |
|
|
statusChannel.editMessageById(this.message_id, eb.build()).queue(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -90,6 +107,12 @@ public class StatusProvider { |
|
|
|
|
|
|
|
|
JSONObject obj = new JSONObject(output); |
|
|
JSONObject obj = new JSONObject(output); |
|
|
|
|
|
|
|
|
|
|
|
result.put("online", obj.getBoolean("online")); |
|
|
|
|
|
|
|
|
|
|
|
if (!obj.getBoolean("online")) { |
|
|
|
|
|
return result; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
result.put("playerCount", obj.get("player_count")); |
|
|
result.put("playerCount", obj.get("player_count")); |
|
|
result.put("playerMax", obj.get("player_max")); |
|
|
result.put("playerMax", obj.get("player_max")); |
|
|
result.put("playerNames", |
|
|
result.put("playerNames", |
|
@ -100,4 +123,14 @@ public class StatusProvider { |
|
|
return result; |
|
|
return result; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@SneakyThrows |
|
|
|
|
|
@Override |
|
|
|
|
|
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"); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
} |