|
@ -0,0 +1,84 @@ |
|
|
|
|
|
package de.yannicpunktdee.yoshibot.utils; |
|
|
|
|
|
|
|
|
|
|
|
import net.dv8tion.jda.api.EmbedBuilder; |
|
|
|
|
|
import net.dv8tion.jda.api.entities.Guild; |
|
|
|
|
|
import net.dv8tion.jda.api.entities.TextChannel; |
|
|
|
|
|
|
|
|
|
|
|
import java.io.DataInputStream; |
|
|
|
|
|
import java.io.DataOutputStream; |
|
|
|
|
|
import java.io.IOException; |
|
|
|
|
|
import java.net.Socket; |
|
|
|
|
|
import java.net.UnknownHostException; |
|
|
|
|
|
import java.util.concurrent.Executors; |
|
|
|
|
|
import java.util.concurrent.ScheduledExecutorService; |
|
|
|
|
|
import java.util.concurrent.TimeUnit; |
|
|
|
|
|
|
|
|
|
|
|
public class StatusProvider { |
|
|
|
|
|
|
|
|
|
|
|
private static final String SERVER_URL = "85.214.148.23"; |
|
|
|
|
|
private static final String statusChannelId = "889880296168755231"; |
|
|
|
|
|
private static final String messageId = "889887149850251304"; |
|
|
|
|
|
|
|
|
|
|
|
private static final ScheduledExecutorService statusScheduler = Executors.newScheduledThreadPool(1); |
|
|
|
|
|
|
|
|
|
|
|
private static int lastPlayersOnline = -1; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static void provide(int secondsPerTime, Guild guild){ |
|
|
|
|
|
TextChannel statusChannel = guild.getTextChannelById(statusChannelId); |
|
|
|
|
|
statusScheduler.scheduleAtFixedRate(() -> { |
|
|
|
|
|
try { |
|
|
|
|
|
updateStatusMessage(statusChannel); |
|
|
|
|
|
} catch (IOException e) { |
|
|
|
|
|
Logger.logError("Konnte Status nicht richtig abfragen."); |
|
|
|
|
|
} |
|
|
|
|
|
}, 0, secondsPerTime, TimeUnit.SECONDS); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static void updateStatusMessage(TextChannel statusChannel) throws IOException { |
|
|
|
|
|
int newPlayersOnline = getPlayersOnline(); |
|
|
|
|
|
if(newPlayersOnline == lastPlayersOnline) return; |
|
|
|
|
|
else lastPlayersOnline = newPlayersOnline; |
|
|
|
|
|
|
|
|
|
|
|
EmbedBuilder eb = new EmbedBuilder(); |
|
|
|
|
|
eb.setTitle("Status"); |
|
|
|
|
|
eb.addField("Minecraft-Server, Spieler online: ", Integer.toString(lastPlayersOnline), false); |
|
|
|
|
|
statusChannel.editMessageById(messageId, eb.build()).queue(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private static int getPlayersOnline() throws IOException { |
|
|
|
|
|
Socket socket = null; |
|
|
|
|
|
DataOutputStream out = null; |
|
|
|
|
|
DataInputStream in = null; |
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
|
socket = new Socket(SERVER_URL, 25565); |
|
|
|
|
|
out = new DataOutputStream(socket.getOutputStream()); |
|
|
|
|
|
in = new DataInputStream(socket.getInputStream()); |
|
|
|
|
|
} catch (UnknownHostException e) { |
|
|
|
|
|
System.err.println("Don't know about host: " + SERVER_URL); |
|
|
|
|
|
} catch (IOException e) { |
|
|
|
|
|
System.err.println("Couldn't get I/O for " + "the connection to:" + SERVER_URL); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
out.write(0xFE); |
|
|
|
|
|
|
|
|
|
|
|
byte[] b = new byte[241]; |
|
|
|
|
|
in.read(b, 0, 241); |
|
|
|
|
|
StringBuffer buffer = new StringBuffer(); |
|
|
|
|
|
for (int i = 4; i < b.length; i++) { |
|
|
|
|
|
if (b[i] != 0) { |
|
|
|
|
|
buffer.append((char) b[i]); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
String[] split = buffer.toString().split(String.valueOf((char) -89)); |
|
|
|
|
|
|
|
|
|
|
|
out.close(); |
|
|
|
|
|
in.close(); |
|
|
|
|
|
socket.close(); |
|
|
|
|
|
|
|
|
|
|
|
return Integer.parseInt(split[1]); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |