From e2746c1b00d9271767cc9c9ea3dc58f488a1d13e Mon Sep 17 00:00:00 2001 From: keronian Date: Sun, 21 Apr 2019 01:11:15 -0400 Subject: [PATCH 1/2] Server File setting now properly saved to file and restored --- me/justicepro/spigotgui/Core/SpigotGUI.java | 27 +++++++----- .../PacketHandlers/ServerHandler.java | 43 +++++++------------ me/justicepro/spigotgui/ServerSettings.java | 21 +++++---- 3 files changed, 43 insertions(+), 48 deletions(-) diff --git a/me/justicepro/spigotgui/Core/SpigotGUI.java b/me/justicepro/spigotgui/Core/SpigotGUI.java index ecc13d3..983d08e 100644 --- a/me/justicepro/spigotgui/Core/SpigotGUI.java +++ b/me/justicepro/spigotgui/Core/SpigotGUI.java @@ -124,7 +124,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 +200,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), settings.getTheme(), fontSpinner.getValue()); for (Theme t : Theme.values()) { @@ -1882,7 +1882,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 +1904,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); } } @@ -2383,17 +2388,19 @@ public void startServer(String args, String switches) throws IOException { } - if (jarFile==null) { + File jarFile; + + if (jarFilePath == null || jarFilePath.isEmpty()) { - File file = new File("server.jar"); + jarFile = new File("server.jar"); - if (file.exists()) { - jarFile = file; - }else { + 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()) { 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/ServerSettings.java b/me/justicepro/spigotgui/ServerSettings.java index d1b2b3b..3c71737 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,15 @@ public class ServerSettings implements Serializable { private Object maxRam; private String customArgs; private String customSwitches; - private File jarFile; + private String jarFilePath; - public ServerSettings(Object minRam, Object maxRam, String customArgs, String customSwitches, File jarFile) { + public ServerSettings(Object minRam, Object maxRam, String customArgs, String customSwitches, String jarFilePath) { this.minRam = minRam; this.maxRam = maxRam; this.customArgs = customArgs; this.customSwitches = customSwitches; - this.jarFile = jarFile; + this.jarFilePath = jarFilePath; } public Object getMinRam() { @@ -36,8 +35,8 @@ public String getCustomSwitches() { return customSwitches; } - public File getJarFile() { - return jarFile; + public String getJarFilePath() { + return jarFilePath; } public void setCustomArgs(String customArgs) { @@ -48,10 +47,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 +55,12 @@ public void setMinRam(Object minRam) { this.minRam = minRam; } + public void setJarFilePath(String jarFilePath) { + this.jarFilePath = jarFilePath; + } + public static ServerSettings getDefault() { - return new ServerSettings(1024, 1024, "", "", new File("server.jar")); + return new ServerSettings(1024, 1024, "", "", "server.jar"); } } \ No newline at end of file From 4e1da0331fcb1a2d78a717d04421b70e8969504c Mon Sep 17 00:00:00 2001 From: keronian Date: Mon, 22 Apr 2019 21:27:37 -0400 Subject: [PATCH 2/2] Added an option to use the server jar's home directory instead of SpigotGUI's home directory --- me/justicepro/spigotgui/Core/SpigotGUI.java | 64 +++++++++++++++------ me/justicepro/spigotgui/Server.java | 26 ++++----- me/justicepro/spigotgui/ServerSettings.java | 14 ++++- 3 files changed, 70 insertions(+), 34 deletions(-) diff --git a/me/justicepro/spigotgui/Core/SpigotGUI.java b/me/justicepro/spigotgui/Core/SpigotGUI.java index 983d08e..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; @@ -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(), jarFilePath), 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); @@ -1918,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(); @@ -1932,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) @@ -1986,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); @@ -2372,22 +2389,7 @@ 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()) { - - 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 (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."); - return; - } - - } - + File jarFile; if (jarFilePath == null || jarFilePath.isEmpty()) { @@ -2407,11 +2409,34 @@ public void startServer(String args, String switches) throws IOException { 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 (!eula.exists()) { + + 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 (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."); + 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) { @@ -2425,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/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 3c71737..83f0011 100644 --- a/me/justicepro/spigotgui/ServerSettings.java +++ b/me/justicepro/spigotgui/ServerSettings.java @@ -9,14 +9,16 @@ public class ServerSettings implements Serializable { private String customArgs; private String customSwitches; private String jarFilePath; + private boolean useServerHomeDirectory; - public ServerSettings(Object minRam, Object maxRam, String customArgs, String customSwitches, String jarFilePath) { + 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.jarFilePath = jarFilePath; + this.useServerHomeDirectory = useServerHomeDirectory; } public Object getMinRam() { @@ -39,6 +41,10 @@ public String getJarFilePath() { return jarFilePath; } + public boolean getUseServerHomeDirectory() { + return useServerHomeDirectory; + } + public void setCustomArgs(String customArgs) { this.customArgs = customArgs; } @@ -59,8 +65,12 @@ 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, "", "", "server.jar"); + return new ServerSettings(1024, 1024, "", "", "server.jar", false); } } \ No newline at end of file