160 lines
5.2 KiB
Java
Raw Normal View History

package net.knarcraft.serverlauncher;
import net.knarcraft.serverlauncher.profile.Collection;
import net.knarcraft.serverlauncher.profile.Profile;
import net.knarcraft.serverlauncher.server.Server;
2018-01-29 20:14:17 +01:00
import net.knarcraft.serverlauncher.server.ServerType;
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.*;
import java.io.*;
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;
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
*
2018-02-22 14:12:42 +01:00
* @author Kristian Knarvik <kristian.knarvik@knett.no>
* @version 1.0.0
* @since 1.0.0
2018-01-24 12:18:06 +01:00
*/
2018-01-26 23:15:19 +01:00
public class Main {
@SuppressWarnings("CanBeFinal")
public static String appDir;
2018-02-22 11:49:12 +01:00
private static boolean running = false;
static {
try {
appDir = String.valueOf(new File(Main.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath()).getParentFile());
} catch (URISyntaxException e) {
e.printStackTrace();
System.exit(1);
}
}
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();
Profile.load();
2018-01-30 00:44:03 +01:00
ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor();
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-02-22 11:49:12 +01:00
* Reads from server processes, and writes the output to consoles.
*/
private static void updateConsoles() {
2018-02-22 11:49:12 +01:00
try {
for (Collection collection : Profile.getCurrent().getCollections()) {
Server server = collection.getServer();
if (server.isEnabled() && server.getProcess() != null) {
try {
String readText = server.read();
if (!readText.equals("")) {
collection.getServerConsole().output(readText);
updatePlayerList(readText, server);
}
} catch (IOException e) {
e.printStackTrace();
}
if (!server.getProcess().isAlive()) {
server.stopped();
}
2018-02-20 13:39:21 +01:00
}
}
2018-02-22 11:49:12 +01:00
boolean runningNew = serversRunning();
if (!runningNew && running) {
Profile.getGUI().updateRunning(false);
Profile.getGUI().setStatus("Servers are stopped");
} else if (runningNew && !running) {
Profile.getGUI().updateRunning(true);
}
running = runningNew;
} catch (Exception e) {
e.printStackTrace();
}
}
2018-02-22 11:49:12 +01:00
/**
* Goes through all servers and looks for any running servers.
*
* @return Is at least one server running?
*/
private static boolean serversRunning() {
int num = 0;
for (Collection collection : Profile.getCurrent().getCollections()) {
if (collection.getServer().isStarted() || (collection.getServer().getProcess() != null && collection.getServer().getProcess().isAlive())) {
num++;
}
}
return num > 0;
}
/**
* Looks for strings implying a player has joined or left, and updates the appropriate lists.
*
* @param text The text to search.
2018-02-22 11:49:12 +01:00
* @param server The server which sent the text.
*/
private static void updatePlayerList(String text, Server server) {
if (!server.getTypeName().equals("Bungee")) {
2018-02-22 11:49:12 +01:00
String joinedPlayer = getPlayer(text, true);
String leftPlayer = getPlayer(text, false);
if (!joinedPlayer.equals("")) {
if (!server.hasPlayer(joinedPlayer)) {
server.addPlayer(joinedPlayer);
2018-02-01 13:46:51 +01:00
}
2018-02-22 11:49:12 +01:00
} else if (!leftPlayer.equals("")) {
if (server.hasPlayer(leftPlayer)) {
server.removePlayer(leftPlayer);
}
2018-02-01 13:46:51 +01:00
}
2018-02-22 11:49:12 +01:00
}
}
/**
* Searches a string for players joining or leaving.
*
* @param text The string to search
* @param joined Are we searching for a joining player or not
* @return The name of a player or an empty string
*/
private static String getPlayer(String text, boolean joined) {
String playerName;
if (joined) {
playerName = stringBetween(text, "[Server thread/INFO]: ", " joined the game");
if (playerName.equals("")) {
playerName = stringBetween(text, "UUID of player ", " is ");
}
} else {
playerName = stringBetween(text, "INFO]: ", " lost connection");
if (playerName.equals("")) {
playerName = stringBetween(text, "[Server thread/INFO]: ", " left the game");
}
}
2018-02-22 11:49:12 +01:00
return playerName;
}
}