Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 59 additions & 26 deletions me/justicepro/spigotgui/Core/SpigotGUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -100,6 +101,8 @@ public class SpigotGUI extends JFrame {

private JSpinner maxRam;
private JSpinner minRam;

private JCheckBox chckbxUseServerHomeDir;

private JLabel lblStatus;

Expand All @@ -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();

Expand Down Expand Up @@ -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()) {

Expand Down Expand Up @@ -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);
Expand All @@ -1875,14 +1880,20 @@ 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);

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");
Expand All @@ -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);
}

}
Expand All @@ -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();
Expand All @@ -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)
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

}
Expand Down
26 changes: 13 additions & 13 deletions me/justicepro/spigotgui/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);
Expand Down
31 changes: 20 additions & 11 deletions me/justicepro/spigotgui/ServerSettings.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package me.justicepro.spigotgui;

import java.io.File;
import java.io.Serializable;

public class ServerSettings implements Serializable {
Expand All @@ -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() {
Expand All @@ -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) {
Expand All @@ -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;
}
Expand All @@ -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);
}

}