2018-02-02 22:02:57 +01:00
|
|
|
package net.knarcraft.serverlauncher;
|
|
|
|
|
2018-01-31 21:24:54 +01:00
|
|
|
import net.knarcraft.serverlauncher.profile.Collection;
|
2018-01-28 19:06:50 +01:00
|
|
|
import net.knarcraft.serverlauncher.profile.Profile;
|
2018-02-02 22:02:57 +01:00
|
|
|
import net.knarcraft.serverlauncher.server.Server;
|
2018-01-29 20:14:17 +01:00
|
|
|
import net.knarcraft.serverlauncher.server.ServerType;
|
2018-01-26 20:26:16 +01:00
|
|
|
import net.knarcraft.serverlauncher.userinterface.GUI;
|
2018-01-30 19:35:42 +01:00
|
|
|
import net.knarcraft.serverlauncher.userinterface.ServerConsoles;
|
2018-01-26 20:26:16 +01:00
|
|
|
|
2018-01-25 17:34:45 +01:00
|
|
|
import javax.naming.ConfigurationException;
|
2018-01-27 23:34:02 +01:00
|
|
|
import java.awt.*;
|
2018-02-17 22:04:06 +01:00
|
|
|
import java.io.*;
|
2018-02-13 12:21:39 +01:00
|
|
|
import java.net.URISyntaxException;
|
2018-01-30 00:44:03 +01:00
|
|
|
import java.util.concurrent.Executors;
|
|
|
|
import java.util.concurrent.ScheduledExecutorService;
|
|
|
|
import java.util.concurrent.TimeUnit;
|
2018-02-17 22:04:06 +01:00
|
|
|
|
|
|
|
import static net.knarcraft.serverlauncher.Shared.stringBetween;
|
2018-01-29 20:14:17 +01:00
|
|
|
//Java 8 required.
|
2018-01-24 12:18:06 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A software for managing Minecraft servers.
|
2018-01-25 21:17:02 +01:00
|
|
|
*
|
|
|
|
* @author Kristian Knarvik <kristian.knarvik@knett.no>
|
|
|
|
* @version 0.0.0.1
|
|
|
|
* @since 0.0.0.1
|
2018-01-24 12:18:06 +01:00
|
|
|
*/
|
2018-01-26 23:15:19 +01:00
|
|
|
|
2018-02-02 22:02:57 +01:00
|
|
|
public class Main {
|
2018-02-13 12:21:39 +01:00
|
|
|
@SuppressWarnings("CanBeFinal")
|
2018-02-17 22:04:06 +01:00
|
|
|
public static String appDir;
|
2018-02-02 22:02:57 +01:00
|
|
|
|
2018-02-13 12:21:39 +01:00
|
|
|
static {
|
|
|
|
try {
|
2018-02-17 22:04:06 +01:00
|
|
|
appDir = String.valueOf(new File(Main.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath()).getParentFile());
|
2018-02-13 12:21:39 +01:00
|
|
|
} catch (URISyntaxException e) {
|
|
|
|
e.printStackTrace();
|
2018-02-17 22:04:06 +01:00
|
|
|
System.exit(1);
|
2018-02-13 12:21:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void main(String[] args) {
|
2018-01-27 23:34:02 +01:00
|
|
|
EventQueue.invokeLater(() -> {
|
|
|
|
try {
|
|
|
|
setup();
|
2018-01-31 17:40:28 +01:00
|
|
|
new ServerConsoles();
|
2018-02-03 14:39:22 +01:00
|
|
|
Profile.load();
|
2018-01-30 00:44:03 +01:00
|
|
|
|
|
|
|
ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor();
|
2018-02-13 12:21:39 +01:00
|
|
|
exec.scheduleAtFixedRate(Main::updateConsoles, 10, 500, TimeUnit.MILLISECONDS);
|
2018-01-27 23:34:02 +01:00
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
});
|
2018-01-25 21:17:02 +01:00
|
|
|
}
|
|
|
|
|
2018-01-26 20:34:15 +01:00
|
|
|
private static void setup() {
|
2018-01-25 17:34:45 +01:00
|
|
|
try {
|
2018-01-25 21:17:02 +01:00
|
|
|
ServerType.loadServerTypes();
|
2018-01-25 17:34:45 +01:00
|
|
|
} catch (ConfigurationException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
System.exit(1);
|
|
|
|
}
|
2018-01-23 23:39:37 +01:00
|
|
|
}
|
2018-02-01 13:16:25 +01:00
|
|
|
|
2018-02-02 22:02:57 +01:00
|
|
|
/**
|
|
|
|
* Reads from server processes, and writes the output to our custom consoles.
|
|
|
|
*/
|
|
|
|
private static void updateConsoles() {
|
|
|
|
for (Collection collection : Profile.getCurrent().getCollections()) {
|
2018-02-17 22:04:06 +01:00
|
|
|
Server server = collection.getServer();
|
2018-02-02 22:02:57 +01:00
|
|
|
if (server.isEnabled() && server.getProcess() != null) {
|
|
|
|
try {
|
|
|
|
String readText = server.read();
|
|
|
|
if (!readText.equals("")) {
|
|
|
|
collection.getServerConsole().output(readText);
|
2018-02-13 12:21:39 +01:00
|
|
|
updatePlayerList(readText, server);
|
2018-02-02 22:02:57 +01:00
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Looks for strings implying a player has joined or left, and updates the appropriate lists.
|
|
|
|
*
|
|
|
|
* @param text The text to search.
|
2018-02-13 12:21:39 +01:00
|
|
|
* @param server The server which sent the text.
|
2018-02-02 22:02:57 +01:00
|
|
|
*/
|
2018-02-13 12:21:39 +01:00
|
|
|
private static void updatePlayerList(String text, Server server) {
|
2018-02-05 00:00:51 +01:00
|
|
|
if (!server.getTypeName().equals("Bungee")) {
|
2018-02-01 13:46:51 +01:00
|
|
|
String joinedPlayer = stringBetween(text, "[Server thread/INFO]: ", " joined the game");
|
|
|
|
if (!joinedPlayer.equals("")) {
|
2018-02-02 22:02:57 +01:00
|
|
|
if (!server.hasPlayer(joinedPlayer)) {
|
|
|
|
server.addPlayer(joinedPlayer);
|
2018-02-01 13:46:51 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
joinedPlayer = stringBetween(text, "UUID of player ", " is ");
|
2018-02-01 13:16:25 +01:00
|
|
|
if (!joinedPlayer.equals("")) {
|
2018-02-02 22:02:57 +01:00
|
|
|
if (!server.hasPlayer(joinedPlayer)) {
|
|
|
|
server.addPlayer(joinedPlayer);
|
2018-02-01 13:16:25 +01:00
|
|
|
}
|
|
|
|
}
|
2018-02-01 13:46:51 +01:00
|
|
|
}
|
|
|
|
if (joinedPlayer.equals("")) {
|
|
|
|
String leftPlayer = stringBetween(text, "INFO]: ", " lost connection");
|
|
|
|
if (!leftPlayer.equals("")) {
|
2018-02-02 22:02:57 +01:00
|
|
|
if (server.hasPlayer(leftPlayer)) {
|
|
|
|
server.removePlayer(leftPlayer);
|
2018-02-01 13:46:51 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
leftPlayer = stringBetween(text, "[Server thread/INFO]: ", " left the game");
|
2018-02-01 13:16:25 +01:00
|
|
|
if (!leftPlayer.equals("")) {
|
2018-02-02 22:02:57 +01:00
|
|
|
if (server.hasPlayer(leftPlayer)) {
|
|
|
|
server.removePlayer(leftPlayer);
|
2018-02-01 13:16:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-01-23 23:39:37 +01:00
|
|
|
}
|