|
|
@ -1,8 +1,11 @@ |
|
|
|
package de.yannicpunktdee.yoshibot.utils; |
|
|
|
|
|
|
|
import de.yannicpunktdee.yoshibot.main.YoshiBot; |
|
|
|
import lombok.SneakyThrows; |
|
|
|
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; |
|
|
@ -30,7 +33,7 @@ public class StatusProvider implements Provider { |
|
|
|
|
|
|
|
private int lastPlayersOnline = -1; |
|
|
|
|
|
|
|
private final String message_id; |
|
|
|
private volatile String messageId = null; |
|
|
|
|
|
|
|
private final int serverPort; |
|
|
|
|
|
|
@ -45,9 +48,13 @@ public class StatusProvider implements Provider { |
|
|
|
|
|
|
|
|
|
|
|
@SneakyThrows |
|
|
|
public StatusProvider(String desc, String messageId, Guild guild, int secondsPerTime, int serverPort) { |
|
|
|
@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.message_id = messageId; |
|
|
|
this.messageId = StatusProvider.ensureMessageId(statusChannel, messageIdKey, guild); |
|
|
|
this.serverPort = serverPort; |
|
|
|
this.mcstatus.command(Resources.getPath_to_mcstatus(), Resources.getMc_server() + ":" + serverPort, "json"); |
|
|
|
|
|
|
@ -61,11 +68,24 @@ public class StatusProvider implements Provider { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
TextChannel statusChannel = guild.getTextChannelById(Resources.getStatus_channel()); |
|
|
|
statusScheduler.scheduleAtFixedRate(() -> updateStatusMessage(statusChannel), 0, secondsPerTime, |
|
|
|
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") |
|
|
|
public void updateStatusMessage(TextChannel statusChannel) { |
|
|
|
EmbedBuilder eb = new EmbedBuilder(); |
|
|
@ -102,7 +122,7 @@ public class StatusProvider implements Provider { |
|
|
|
eb.addField("Offline", "", false); |
|
|
|
} |
|
|
|
|
|
|
|
statusChannel.editMessageById(this.message_id, eb.build()).queue(); |
|
|
|
statusChannel.editMessageById(this.messageId, eb.build()).queue(); |
|
|
|
} catch (IOException e) { |
|
|
|
Logger.logError(e.toString()); |
|
|
|
} |
|
|
@ -121,8 +141,8 @@ public class StatusProvider implements Provider { |
|
|
|
Logger.logError("MCStatus on port " + serverPort + " exited with errorcode " + process.exitValue()); |
|
|
|
} |
|
|
|
|
|
|
|
String output = new BufferedReader(new InputStreamReader(process.getInputStream())).lines().collect( |
|
|
|
Collectors.joining()); |
|
|
|
String output = new BufferedReader(new InputStreamReader(process.getInputStream())).lines() |
|
|
|
.collect(Collectors.joining()); |
|
|
|
|
|
|
|
Map<String, Object> result = new HashMap<>(); |
|
|
|
|
|
|
@ -147,10 +167,8 @@ public class StatusProvider implements Provider { |
|
|
|
|
|
|
|
result.put("playerCount", obj.getInt("player_count")); |
|
|
|
result.put("playerMax", obj.getInt("player_max")); |
|
|
|
result.put("playerNames", |
|
|
|
StreamSupport.stream(obj.getJSONArray("players").spliterator(), false) |
|
|
|
.map(jsonobj -> ((JSONObject) jsonobj).getString("name")).sorted().collect( |
|
|
|
Collectors.toList())); |
|
|
|
result.put("playerNames", StreamSupport.stream(obj.getJSONArray("players").spliterator(), false) |
|
|
|
.map(jsonobj -> ((JSONObject) jsonobj).getString("name")).sorted().collect(Collectors.toList())); |
|
|
|
result.put("motd", obj.getString("motd")); |
|
|
|
result.put("version", obj.getString("version")); |
|
|
|
|
|
|
@ -163,8 +181,7 @@ public class StatusProvider implements Provider { |
|
|
|
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"); |
|
|
|
TIME_FORMATTER.format(timestampLastPlayerOnline), "UTF" + "-8"); |
|
|
|
} |
|
|
|
} |
|
|
|
} |