package net.knarcraft.minecraftserverlauncher; import net.knarcraft.minecraftserverlauncher.profile.Collection; import net.knarcraft.minecraftserverlauncher.profile.ServerLauncherController; import net.knarcraft.minecraftserverlauncher.server.ServerHandler; import net.knarcraft.minecraftserverlauncher.userinterface.ServerConsoles; import net.knarcraft.minecraftserverlauncher.userinterface.ServerLauncherGUI; import net.knarcraft.minecraftserverlauncher.utility.CommonFunctions; import net.knarcraft.minecraftserverlauncher.utility.Updater; import java.awt.*; import java.io.File; import java.io.IOException; import java.net.URISyntaxException; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; //Java 8 required. /** * A software for managing Minecraft servers. * * @author Kristian Knarvik * @version b1.3.4 * @since 1.0.0 */ public class Main { private static final String updateChannel = "beta"; private static final String updateURL = "https://api.knarcraft.net/minecraftserverlauncher"; private static final ServerLauncherController controller = ServerLauncherController.getInstance(); private static String applicationWorkDirectory; private static boolean serversAreRunning = false; private static ServerLauncherGUI gui; public static void main(String[] args) throws IOException { String logFile = Main.getApplicationWorkDirectory() + File.separator + "latestrun.log"; CommonFunctions.writeFile(logFile, "[Info]: Starting Minecraft Server Launcher v." + Updater.getCurrentVersion()[1]); try { Updater.checkForUpdate(updateURL, updateChannel); } catch (IOException e) { CommonFunctions.appendFile(logFile, "[Warning]: Unable to complete update procedure: " + e.getMessage()); } EventQueue.invokeLater(() -> { try { ServerConsoles.instantiate(); controller.loadState(); gui = controller.getGUI(); ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor(); exec.scheduleAtFixedRate(Main::updateServersRunningState, 10, 500, TimeUnit.MILLISECONDS); } catch (Exception e) { e.printStackTrace(); } }); } /** * Gets the controller used by the software * * @return

The controller used by the software

*/ public static ServerLauncherController getController() { return controller; } /** * Retrieves the directory the .jar file is running from * * @return A string path */ public static String getApplicationWorkDirectory() { if (applicationWorkDirectory == null) { try { applicationWorkDirectory = String.valueOf(new File(Main.class.getProtectionDomain().getCodeSource().getLocation().toURI().getPath()).getParentFile()); } catch (URISyntaxException e) { e.printStackTrace(); System.exit(1); } } return applicationWorkDirectory; } /** * Updates the software state if the servers' running state has changed */ private static void updateServersRunningState() { boolean runningNew = serversRunning(); if (serversAreRunning && !runningNew) { //Servers stopped running ServerHandler.serversStopped(); gui.updateGUIElementsWhenServersStartOrStop(false); gui.setStatus("Servers are stopped"); } else if (!serversAreRunning && runningNew) { //Servers started running gui.updateGUIElementsWhenServersStartOrStop(true); } serversAreRunning = runningNew; } /** * Goes through all servers and looks for any running servers * * @return

Whether at least one server is running

*/ private static boolean serversRunning() { int serversRunning = 0; for (Collection collection : controller.getCurrentProfile().getCollections()) { if (collection.getServer().isStarted() || (collection.getServer().getProcess() != null && collection.getServer().getProcess().isAlive())) { serversRunning++; } } return serversRunning > 0; } }