Browse Source

[Fixed] Bischel Schtuff für Status und Resource

master
Paul Glaß 3 years ago
parent
commit
cf48147c8d
4 changed files with 53 additions and 37 deletions
  1. +1
    -1
      .gitignore
  2. +3
    -3
      app/src/main/java/de/yannicpunktdee/yoshibot/main/YoshiBot.java
  3. +2
    -2
      app/src/main/java/de/yannicpunktdee/yoshibot/utils/Resources.java
  4. +47
    -31
      app/src/main/java/de/yannicpunktdee/yoshibot/utils/StatusProvider.java

+ 1
- 1
.gitignore View File

@ -191,7 +191,7 @@ gradle-app.setting
# # Work around https://youtrack.jetbrains.com/issue/IDEA-116898 # # Work around https://youtrack.jetbrains.com/issue/IDEA-116898
# gradle/wrapper/gradle-wrapper.properties # gradle/wrapper/gradle-wrapper.properties
.classpath
updateYoshiBot.sh
# Resources # Resources
rsc/audio/* rsc/audio/*


+ 3
- 3
app/src/main/java/de/yannicpunktdee/yoshibot/main/YoshiBot.java View File

@ -115,13 +115,13 @@ public final class YoshiBot {
commandLineThread = new CommandLine(); commandLineThread = new CommandLine();
commandLineThread.start(); commandLineThread.start();
allProvides.add(new StatusProvider("Vanilla", Resources.getStatus_message_vanilla(), guild,
allProvides.add(new StatusProvider("Kreativ - 1.17", Resources.getStatus_message_vanilla(), guild,
Resources.getStatus_update(), Resources.getStatus_update(),
25565)); 25565));
allProvides.add(new StatusProvider("Valhelsia 3", Resources.getStatus_message_modded(), guild,
allProvides.add(new StatusProvider("Valhelsia 3 - 3.4.7", Resources.getStatus_message_modded(), guild,
Resources.getStatus_update(), Resources.getStatus_update(),
25566)); 25566));
allProvides.add(new StatusProvider("1.18", Resources.getStatus_message_18(), guild,
allProvides.add(new StatusProvider("Vanilla - 1.18", Resources.getStatus_message_18(), guild,
Resources.getStatus_update(), Resources.getStatus_update(),
25567)); 25567));


+ 2
- 2
app/src/main/java/de/yannicpunktdee/yoshibot/utils/Resources.java View File

@ -24,7 +24,7 @@ public final class Resources {
private static int status_update; private static int status_update;
@Getter @Getter
private static Long guild_id;
private static long guild_id;
@Getter @Getter
private static String resourcePath, configPath, audioPath, tempPath, activitiesPath, greetingsPath, byebyesPath, private static String resourcePath, configPath, audioPath, tempPath, activitiesPath, greetingsPath, byebyesPath,
@ -44,7 +44,7 @@ public final class Resources {
private static Properties propertiesFile; private static Properties propertiesFile;
public synchronized static boolean init(String resourcePathArg) {
public static boolean init(String resourcePathArg) {
boolean isOk = initResources(resourcePathArg); boolean isOk = initResources(resourcePathArg);
if (isOk) isOk = initConfig(); if (isOk) isOk = initConfig();
if (isOk) isOk = initAudio(); if (isOk) isOk = initAudio();


+ 47
- 31
app/src/main/java/de/yannicpunktdee/yoshibot/utils/StatusProvider.java View File

@ -48,7 +48,7 @@ public class StatusProvider implements Provider {
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, "query");
this.mcstatus.command(Resources.getPath_to_mcstatus(), Resources.getMc_server() + ":" + serverPort, "json");
File timeStampSave = new File(String.format("%s/%d.txt", Resources.getResourcePath(), serverPort)); File timeStampSave = new File(String.format("%s/%d.txt", Resources.getResourcePath(), serverPort));
timestampLastPlayerOnline = null; timestampLastPlayerOnline = null;
@ -66,42 +66,56 @@ public class StatusProvider implements Provider {
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@SneakyThrows
public void updateStatusMessage(TextChannel statusChannel) { public void updateStatusMessage(TextChannel statusChannel) {
EmbedBuilder eb = new EmbedBuilder(); EmbedBuilder eb = new EmbedBuilder();
eb.setTitle(desc); eb.setTitle(desc);
eb.addField("IP", "85.214.148.23" + ":" + serverPort, false);
Map<String, Object> serverInfo = getPlayersOnline();
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;
eb.addField("IP", Resources.getMc_server() + ":" + serverPort, false);
try {
Map<String, Object> serverInfo = getPlayersOnline();
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;
} }
lastPlayersOnline = newPlayersOnline;
}
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);
eb.addField("Version", (String) serverInfo.get("version"), 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:", String.join(", ", (List<String>) serverInfo.get("playerNames")), false);
}
} else {
eb.addField("Offline", "", false);
} }
} else {
eb.addField("Offline", "", false);
statusChannel.editMessageById(this.message_id, eb.build()).queue();
} catch (IOException e){
Logger.logError(e.toString());
} }
statusChannel.editMessageById(this.message_id, eb.build()).queue();
} }
private Map<String, Object> getPlayersOnline() throws IOException { private Map<String, Object> getPlayersOnline() throws IOException {
Process process = mcstatus.start(); Process process = mcstatus.start();
try {
process.waitFor();
} catch (InterruptedException e) {
e.printStackTrace();
}
if (process.exitValue() != 0){
Logger.logError("MCStatus on port " + serverPort + " exited with errorcode " + process.exitValue());
}
String output = new BufferedReader(new InputStreamReader(process.getInputStream())).lines().collect( String output = new BufferedReader(new InputStreamReader(process.getInputStream())).lines().collect(
Collectors.joining()); Collectors.joining());
@ -111,7 +125,7 @@ public class StatusProvider implements Provider {
result.put("online", false); result.put("online", false);
return result; return result;
} }
/*
String part = output.split("players")[1].replace(",", "").replace("'", ""); String part = output.split("players")[1].replace(",", "").replace("'", "");
String[] players = part.substring(part.indexOf("[")+1, part.indexOf("]")).split(" "); String[] players = part.substring(part.indexOf("[")+1, part.indexOf("]")).split(" ");
@ -124,8 +138,8 @@ public class StatusProvider implements Provider {
result.put("playerMax", playersMax); result.put("playerMax", playersMax);
result.put("playerNames", Arrays.asList(players)); result.put("playerNames", Arrays.asList(players));
/*JSONObject obj = new JSONObject(output);
*/
JSONObject obj = new JSONObject(output);
result.put("online", obj.getBoolean("online")); result.put("online", obj.getBoolean("online"));
@ -133,12 +147,14 @@ public class StatusProvider implements Provider {
return result; return result;
} }
result.put("playerCount", obj.get("player_count"));
result.put("playerMax", obj.get("player_max"));
result.put("playerCount", obj.getInt("player_count"));
result.put("playerMax", obj.getInt("player_max"));
result.put("playerNames", result.put("playerNames",
StreamSupport.stream(obj.getJSONArray("players").spliterator(), false) StreamSupport.stream(obj.getJSONArray("players").spliterator(), false)
.map(jsonobj -> ((JSONObject) jsonobj).getString("name")).sorted().collect( .map(jsonobj -> ((JSONObject) jsonobj).getString("name")).sorted().collect(
Collectors.toList()));*/
Collectors.toList()));
result.put("motd", obj.getString("motd"));
result.put("version", obj.getString("version"));
return result; return result;
} }


Loading…
Cancel
Save