diff --git a/me/justicepro/spigotgui/Core/SpigotGUI.java b/me/justicepro/spigotgui/Core/SpigotGUI.java index ecc13d3..9af7a4a 100644 --- a/me/justicepro/spigotgui/Core/SpigotGUI.java +++ b/me/justicepro/spigotgui/Core/SpigotGUI.java @@ -22,6 +22,7 @@ import java.net.URISyntaxException; import java.net.URL; import java.nio.file.Files; +import java.nio.file.Paths; import java.nio.file.StandardCopyOption; import java.util.ArrayList; import java.util.Collections; @@ -100,6 +101,8 @@ public class SpigotGUI extends JFrame { private JSpinner maxRam; private JSpinner minRam; + + private JCheckBox chckbxUseServerHomeDir; private JLabel lblStatus; @@ -124,7 +127,7 @@ public class SpigotGUI extends JFrame { private JCheckBox chckbxConsoleForsay; - public static File jarFile; + public static String jarFilePath; public static ServerHandler serverHandler = new ServerHandler(); @@ -200,7 +203,7 @@ public void windowClosing(WindowEvent arg0) { String theme = themeBox.getItemAt(themeBox.getSelectedIndex()); - Settings s = new Settings(new ServerSettings(minRam.getValue(), maxRam.getValue(), customArgsTxt.getText(), customSwitchesTxt.getText(), jarFile), settings.getTheme(), fontSpinner.getValue()); + Settings s = new Settings(new ServerSettings(minRam.getValue(), maxRam.getValue(), customArgsTxt.getText(), customSwitchesTxt.getText(), jarFilePath, chckbxUseServerHomeDir.isSelected()), settings.getTheme(), fontSpinner.getValue()); for (Theme t : Theme.values()) { @@ -1854,6 +1857,8 @@ public void actionPerformed(ActionEvent e) { JLabel lblMaxRam = new JLabel("Max Ram"); lblMaxRam.setHorizontalAlignment(SwingConstants.LEFT); + chckbxUseServerHomeDir = new JCheckBox("Use server's home directory"); + customArgsTxt = new JTextField(); customArgsTxt.setColumns(10); @@ -1875,6 +1880,7 @@ public void stateChanged(ChangeEvent arg0) { fontSpinner.setValue(settings.getFontSize()); customArgsTxt.setText(serverSettings.getCustomArgs()); customSwitchesTxt.setText(serverSettings.getCustomSwitches()); + chckbxUseServerHomeDir.setSelected(serverSettings.getUseServerHomeDirectory()); JLabel lblCustomArgs = new JLabel("Custom Arguments"); lblCustomArgs.setHorizontalAlignment(SwingConstants.CENTER); @@ -1882,7 +1888,12 @@ public void stateChanged(ChangeEvent arg0) { JLabel lblCustomSwitches = new JLabel("Custom Switches"); lblCustomSwitches.setHorizontalAlignment(SwingConstants.CENTER); - JLabel lblJarFile = new JLabel("Server File: server.jar"); + String jarFileLabelText = "Server File: server.jar"; + jarFilePath = settings.getServerSettings().getJarFilePath(); + if (jarFilePath != null && !jarFilePath.isEmpty()){ + jarFileLabelText = "Server File: " + jarFilePath; + } + JLabel lblJarFile = new JLabel(jarFileLabelText); lblJarFile.setHorizontalAlignment(SwingConstants.LEFT); JButton btnSetJarFile = new JButton("Set Server File"); @@ -1899,8 +1910,8 @@ public void actionPerformed(ActionEvent e) { int result = fileChooser.showOpenDialog(null); if (result==JFileChooser.APPROVE_OPTION) { - jarFile = fileChooser.getSelectedFile(); - lblJarFile.setText("Server File: " + jarFile.getAbsolutePath()); + jarFilePath = fileChooser.getSelectedFile().getAbsolutePath(); + lblJarFile.setText("Server File: " + jarFilePath); } } @@ -1913,7 +1924,15 @@ public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent arg0) { FileEditor fileEditor = new FileEditor(); try { - fileEditor.openFile(new File("server.properties")); + String settingsPath = ""; + + if (chckbxUseServerHomeDir.isSelected()) { + settingsPath = Paths.get(jarFilePath).getParent().toString(); + } + + File settingsFile = new File(Paths.get(settingsPath).resolve("server.properties").toString()); + + fileEditor.openFile(settingsFile); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); @@ -1927,6 +1946,8 @@ public void actionPerformed(ActionEvent arg0) { gl_panel_2.createParallelGroup(Alignment.LEADING) .addGroup(gl_panel_2.createSequentialGroup() .addComponent(btnSetJarFile, GroupLayout.PREFERRED_SIZE, 110, GroupLayout.PREFERRED_SIZE) + .addPreferredGap(ComponentPlacement.RELATED, 350, Short.MAX_VALUE) + .addComponent(chckbxUseServerHomeDir) .addPreferredGap(ComponentPlacement.RELATED, 462, Short.MAX_VALUE) .addComponent(btnEditServerproperties)) .addComponent(lblJarFile, GroupLayout.PREFERRED_SIZE, 596, GroupLayout.PREFERRED_SIZE) @@ -1981,7 +2002,8 @@ public void actionPerformed(ActionEvent arg0) { .addPreferredGap(ComponentPlacement.RELATED) .addGroup(gl_panel_2.createParallelGroup(Alignment.TRAILING) .addComponent(btnSetJarFile, GroupLayout.PREFERRED_SIZE, 20, GroupLayout.PREFERRED_SIZE) - .addComponent(btnEditServerproperties))) + .addComponent(btnEditServerproperties) + .addComponent(chckbxUseServerHomeDir))) ); panel_2.setLayout(gl_panel_2); @@ -2367,44 +2389,54 @@ public void startServer() throws IOException { public void startServer(String args, String switches) throws IOException { consoleTextArea.setText(""); - File eula = new File("eula.txt"); - - if (!eula.exists()) { + + File jarFile; + + if (jarFilePath == null || jarFilePath.isEmpty()) { - int result = JOptionPane.showOptionDialog(null, - "Do you agree to the Minecraft Eula? (https://account.mojang.com/documents/minecraft_eula)", "Message", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, null, null); + jarFile = new File("server.jar"); - if (result==JOptionPane.YES_OPTION) { - Files.copy(getClass().getResourceAsStream("/eula.txt"), eula.toPath(), StandardCopyOption.REPLACE_EXISTING); - }else { - JOptionPane.showMessageDialog(null, "You must agree to the eula to run a server."); + if (!jarFile.exists()) { + JOptionPane.showMessageDialog(null, "There is no selected jar file. Look at Server Settings."); return; } + }else { + jarFile = new File(jarFilePath); + } + + if (!jarFile.exists()) { + JOptionPane.showMessageDialog(null, "The selected jar file does not exist."); + return; + } + + String eulaPath = ""; + + if (chckbxUseServerHomeDir.isSelected()) { + eulaPath = jarFile.getParentFile().getAbsolutePath(); } + + File eula = new File(Paths.get(eulaPath).resolve("eula.txt").toString()); - if (jarFile==null) { + if (!eula.exists()) { - File file = new File("server.jar"); + int result = JOptionPane.showOptionDialog(null, + "Do you agree to the Minecraft Eula? (https://account.mojang.com/documents/minecraft_eula)", "Message", JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, null, null); - if (file.exists()) { - jarFile = file; + if (result==JOptionPane.YES_OPTION) { + Files.copy(getClass().getResourceAsStream("/eula.txt"), eula.toPath(), StandardCopyOption.REPLACE_EXISTING); }else { - JOptionPane.showMessageDialog(null, "There is no selected jar file. Look at Server Settings."); + JOptionPane.showMessageDialog(null, "You must agree to the eula to run a server."); return; } } - if (!jarFile.exists()) { - JOptionPane.showMessageDialog(null, "The selected jar file does not exist."); - return; - } - if (server != null) { if (!server.isRunning()) { server = new Server(jarFile, "nogui " + args, switches); + server.useServerHomeDir = chckbxUseServerHomeDir.isSelected(); try { server.start(); } catch (IOException | ProcessException e) { @@ -2418,6 +2450,7 @@ public void startServer(String args, String switches) throws IOException { }else { server = new Server(jarFile, "nogui " + args, switches); + server.useServerHomeDir = chckbxUseServerHomeDir.isSelected(); try { server.start(); } catch (IOException | ProcessException e) { diff --git a/me/justicepro/spigotgui/RemoteAdmin/PacketHandlers/ServerHandler.java b/me/justicepro/spigotgui/RemoteAdmin/PacketHandlers/ServerHandler.java index 4af48de..8f5e0f9 100644 --- a/me/justicepro/spigotgui/RemoteAdmin/PacketHandlers/ServerHandler.java +++ b/me/justicepro/spigotgui/RemoteAdmin/PacketHandlers/ServerHandler.java @@ -200,41 +200,30 @@ public void onPacketRecievedServer(Packet packet, RConnection connection, RServe } if (eula.exists()) { - - if (SpigotGUI.jarFile == null) { - + + if (SpigotGUI.jarFilePath == null || SpigotGUI.jarFilePath.isEmpty()) { File file = new File("server.jar"); - if (file.exists()) { - SpigotGUI.jarFile = file; + if (!file.exists()) { + System.out.println("Unable to locate server jar file"); + return; } - } - + + boolean isServerRunning = false; if (SpigotGUI.server != null) { System.out.println("Server_Start is not null"); - if (!SpigotGUI.server.isRunning() && SpigotGUI.jarFile != null) { - System.out.println("Server_Starting"); - try { - SpigotGUI.instance.startServer(serverStart.getArguments(), serverStart.getSwitches()); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - - } - + isServerRunning = SpigotGUI.server.isRunning(); }else { System.out.println("Server_Start is null"); - if (SpigotGUI.jarFile != null) { - System.out.println("Server_Starting"); - try { - SpigotGUI.instance.startServer(serverStart.getArguments(), serverStart.getSwitches()); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - + } + if (!isServerRunning) { + System.out.println("Server_Starting"); + try { + SpigotGUI.instance.startServer(serverStart.getArguments(), serverStart.getSwitches()); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); } } diff --git a/me/justicepro/spigotgui/Server.java b/me/justicepro/spigotgui/Server.java index 3ce27fe..9c9839e 100644 --- a/me/justicepro/spigotgui/Server.java +++ b/me/justicepro/spigotgui/Server.java @@ -13,10 +13,13 @@ public class Server { private Process process; + public boolean useServerHomeDir; + public Server(File jar, String arguments, String switches) { this.jar = jar; this.arguments = arguments; this.switches = switches; + this.useServerHomeDir = false; } public Thread start(String arguments, String switches) throws IOException, ProcessException { @@ -30,21 +33,18 @@ public Thread start(String arguments, String switches) throws IOException, Proce } }else { System.out.println("Started Server"); - - if (isWindows) { - process = Runtime.getRuntime().exec("cmd"); - - //PrintWriter output = new PrintWriter(process.getOutputStream(), true); - -// output.println("java " + switches + " -jar \"" + jar.getAbsolutePath() + "\" " + arguments + " & exit"); - + File jarPath = jar.getParentFile(); + ProcessBuilder processBuilder; + if (isWindows) { + processBuilder = new ProcessBuilder("cmd"); }else { - process = Runtime.getRuntime().exec("sh"); - - //PrintWriter output = new PrintWriter(process.getOutputStream(), true); - -// output.println("java " + switches + " -jar \"" + jar.getAbsolutePath() + "\" " + arguments + " & exit"); + processBuilder = new ProcessBuilder("sh"); + } + if (useServerHomeDir) { + process = processBuilder.directory(jarPath).start(); + }else { + process = processBuilder.start(); } PrintWriter output = new PrintWriter(process.getOutputStream(), true); diff --git a/me/justicepro/spigotgui/ServerSettings.java b/me/justicepro/spigotgui/ServerSettings.java index d1b2b3b..83f0011 100644 --- a/me/justicepro/spigotgui/ServerSettings.java +++ b/me/justicepro/spigotgui/ServerSettings.java @@ -1,6 +1,5 @@ package me.justicepro.spigotgui; -import java.io.File; import java.io.Serializable; public class ServerSettings implements Serializable { @@ -9,15 +8,17 @@ public class ServerSettings implements Serializable { private Object maxRam; private String customArgs; private String customSwitches; - private File jarFile; + private String jarFilePath; + private boolean useServerHomeDirectory; - public ServerSettings(Object minRam, Object maxRam, String customArgs, String customSwitches, File jarFile) { + public ServerSettings(Object minRam, Object maxRam, String customArgs, String customSwitches, String jarFilePath, boolean useServerHomeDirectory) { this.minRam = minRam; this.maxRam = maxRam; this.customArgs = customArgs; this.customSwitches = customSwitches; - this.jarFile = jarFile; + this.jarFilePath = jarFilePath; + this.useServerHomeDirectory = useServerHomeDirectory; } public Object getMinRam() { @@ -36,8 +37,12 @@ public String getCustomSwitches() { return customSwitches; } - public File getJarFile() { - return jarFile; + public String getJarFilePath() { + return jarFilePath; + } + + public boolean getUseServerHomeDirectory() { + return useServerHomeDirectory; } public void setCustomArgs(String customArgs) { @@ -48,10 +53,6 @@ public void setCustomSwitches(String customSwitches) { this.customSwitches = customSwitches; } - public void setJarFile(File jarFile) { - this.jarFile = jarFile; - } - public void setMaxRam(Object maxRam) { this.maxRam = maxRam; } @@ -60,8 +61,16 @@ public void setMinRam(Object minRam) { this.minRam = minRam; } + public void setJarFilePath(String jarFilePath) { + this.jarFilePath = jarFilePath; + } + + public void setUseServerHomeDirectory(boolean useServerHomeDirectory) { + this.useServerHomeDirectory = useServerHomeDirectory; + } + public static ServerSettings getDefault() { - return new ServerSettings(1024, 1024, "", "", new File("server.jar")); + return new ServerSettings(1024, 1024, "", "", "server.jar", false); } } \ No newline at end of file