package net.knarcraft.serverlauncher; import net.knarcraft.serverlauncher.profile.Collection; import net.knarcraft.serverlauncher.profile.Profile; import net.knarcraft.serverlauncher.server.Server; import net.knarcraft.serverlauncher.server.ServerType; import net.knarcraft.serverlauncher.userinterface.ServerConsoles; import javax.naming.ConfigurationException; import java.awt.*; import java.io.*; import java.net.URISyntaxException; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; import static net.knarcraft.serverlauncher.Shared.stringBetween; //Java 8 required. /** * A software for managing Minecraft servers. * * @author Kristian Knarvik * @version 0.0.0.1 * @since 0.0.0.1 */ public class Main { @SuppressWarnings("CanBeFinal") public static String appDir; 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) { EventQueue.invokeLater(() -> { try { setup(); new ServerConsoles(); Profile.load(); ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor(); exec.scheduleAtFixedRate(Main::updateConsoles, 10, 500, TimeUnit.MILLISECONDS); } catch (Exception e) { e.printStackTrace(); } }); } private static void setup() { try { ServerType.loadServerTypes(); } catch (ConfigurationException e) { e.printStackTrace(); System.exit(1); } } /** * Reads from server processes, and writes the output to consoles. */ private static void updateConsoles() { 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(); } } } 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(); } } /** * 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. * @param server The server which sent the text. */ private static void updatePlayerList(String text, Server server) { if (!server.getTypeName().equals("Bungee")) { String joinedPlayer = getPlayer(text, true); String leftPlayer = getPlayer(text, false); if (!joinedPlayer.equals("")) { if (!server.hasPlayer(joinedPlayer)) { server.addPlayer(joinedPlayer); } } else if (!leftPlayer.equals("")) { if (server.hasPlayer(leftPlayer)) { server.removePlayer(leftPlayer); } } } } /** * 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"); } } return playerName; } }