It is possible to write to server consoles

This commit is contained in:
Kristian Knarvik 2018-01-31 21:24:54 +01:00
parent 30263d7753
commit 2591f3ed05
8 changed files with 86 additions and 36 deletions

View File

@ -1,8 +1,8 @@
Vanilla;Latest,1.12,1.11.2,1.10.2,1.9.4,1.8.9,1.7.10,1.6.4,1.5.2,1.4.7,1.3.2,1.2.5;https://launchermeta.mojang.com/mc/game/version_manifest.json;"release":";";https://s3.amazonaws.com/Minecraft.Download/versions/;/minecraft_server. Vanilla;Latest,1.12.2,1.11.2,1.10.2,1.9.4,1.8.9,1.7.10,1.6.4,1.5.2,1.4.7,1.3.2,1.2.5;https://launchermeta.mojang.com/mc/game/version_manifest.json;"release":";";https://s3.amazonaws.com/Minecraft.Download/versions/;/minecraft_server.
Snapshot;Latest;https://launchermeta.mojang.com/mc/game/version_manifest.json;"snapshot":";";https://s3.amazonaws.com/Minecraft.Download/versions/;/minecraft_server. Snapshot;Latest;https://launchermeta.mojang.com/mc/game/version_manifest.json;"snapshot":";";https://s3.amazonaws.com/Minecraft.Download/versions/;/minecraft_server.
SpongeVanilla;1.12.2,1.11.2,1.10.2,1.8.9;https://dl-api.spongepowered.org/v1/org.spongepowered/spongevanilla/downloads?type=stable&minecraft=;"version":";",;https://repo.spongepowered.org/maven/org/spongepowered/spongevanilla/;/spongevanilla- SpongeVanilla;1.12.2,1.11.2,1.10.2,1.8.9;https://dl-api.spongepowered.org/v1/org.spongepowered/spongevanilla/downloads?type=stable&minecraft=;"version":";",;https://repo.spongepowered.org/maven/org/spongepowered/spongevanilla/;/spongevanilla-
Bungee;Latest;https://ci.md-5.net/job/BungeeCord/lastSuccessfulBuild/artifact/bootstrap/target/;Artifacts of BungeeCord #; ;http://ci.md-5.net/job/BungeeCord/lastSuccessfulBuild/artifact/bootstrap/target/BungeeCord.jar; Bungee;Latest;https://ci.md-5.net/job/BungeeCord/lastSuccessfulBuild/artifact/bootstrap/target/;Artifacts of BungeeCord #; ;http://ci.md-5.net/job/BungeeCord/lastSuccessfulBuild/artifact/bootstrap/target/BungeeCord.jar;
Spigot;1.12.2,1.11.2,1.10.2,1.9.4,1.9,1.8.8,1.7.10,1.6.4,1.5.2,1.4.7;https://knarcraft.net/Api/Download/bungeeminecraftserverlauncher/jars/Spigot/ Spigot;1.12.2,1.11.2,1.10.2,1.9.4,1.9,1.8.8,1.7.10,1.6.4,1.5.2,1.4.7;https://knarcraft.net/Api/Download/bungeeminecraftserverlauncher/jars/Spigot/
MCPCplus;1.6.4,1.6.2,1.5.2,1.4.7;https://knarcraft.net/Api/Download/bungeeminecraftserverlauncher/jars/MCPC+/ MCPCplus;1.6.4,1.6.2,1.5.2,1.4.7;https://knarcraft.net/Api/Download/bungeeminecraftserverlauncher/jars/MCPC+/
Craftbukkit;1.12,1.11.2,1.10.2,1.9.4,1.8.8,1.7.10,1.6.4,1.5.2,1.4.6,1.3.2,1.2.5,1.1,1.0;https://knarcraft.net/Api/Download/bungeeminecraftserverlauncher/jars/Bukkit/ Craftbukkit;1.12.2,1.11.2,1.10.2,1.9.4,1.8.8,1.7.10,1.6.4,1.5.2,1.4.6,1.3.2,1.2.5,1.1,1.0;https://knarcraft.net/Api/Download/bungeeminecraftserverlauncher/jars/Bukkit/
Custom;; Custom;;
Can't render this file because it contains an unexpected character in line 1 and column 154.

View File

@ -1,3 +1,4 @@
import net.knarcraft.serverlauncher.profile.Collection;
import net.knarcraft.serverlauncher.profile.Profile; import net.knarcraft.serverlauncher.profile.Profile;
import net.knarcraft.serverlauncher.server.ServerType; import net.knarcraft.serverlauncher.server.ServerType;
import net.knarcraft.serverlauncher.userinterface.GUI; import net.knarcraft.serverlauncher.userinterface.GUI;
@ -5,6 +6,7 @@ import net.knarcraft.serverlauncher.userinterface.ServerConsoles;
import javax.naming.ConfigurationException; import javax.naming.ConfigurationException;
import java.awt.*; import java.awt.*;
import java.io.IOException;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -30,8 +32,19 @@ class Main {
ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor(); ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor();
exec.scheduleAtFixedRate(() -> { exec.scheduleAtFixedRate(() -> {
//TODO: Read from consoles and insert into ServerConsoles. for (Collection collection : GUI.getGUI().currentProfile().getCollections()) {
}, 0, 5, TimeUnit.SECONDS); if (collection.getServer().isEnabled() && collection.getServer().getProcess() != null) {
try {
String readText = collection.getServer().read();
if (!readText.equals("")) {
collection.getServerConsole().output(readText);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}, 10, 250, TimeUnit.MILLISECONDS);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }

View File

@ -8,15 +8,16 @@ import net.knarcraft.serverlauncher.userinterface.Console;
public class Collection { public class Collection {
private Server server; private final Server server;
private ServerTab serverTab; private final ServerTab serverTab;
private Console serverConsole; private final Console serverConsole;
private String name; private String name;
public Collection(String name) { public Collection(String name) {
this.serverTab = new ServerTab(name); this.serverTab = new ServerTab(name);
this.server = new Server(name, GUI.getGUI(), this.serverTab); this.server = new Server(name, GUI.getGUI(), this.serverTab);
this.serverConsole = ServerConsoles.getGUI().addTab(name); this.serverConsole = ServerConsoles.getGUI().addTab(name);
this.name = name;
} }
public String getName() { public String getName() {

View File

@ -59,7 +59,7 @@ public class Profile {
return null; return null;
} }
public boolean collectionExists(String name) { private boolean collectionExists(String name) {
for (Collection collection : this.collections) { for (Collection collection : this.collections) {
if (collection.getName().equals(name)) { if (collection.getName().equals(name)) {
return true; return true;
@ -134,8 +134,9 @@ public class Profile {
} }
} }
} else { } else {
Server target = getCollection(serverName).getServer(); Collection collection = getCollection(serverName);
if (target != null) { if (collection != null) {
Server target = collection.getServer();
try { try {
target.sendCommand(command); target.sendCommand(command);
} catch (IOException e) { } catch (IOException e) {
@ -181,9 +182,9 @@ public class Profile {
} }
public void save() { public void save() {
for (int i = 0; i < this.collections.size(); i++) { for (Collection collection : this.collections) {
Server server = collections.get(i).getServer(); Server server = collection.getServer();
ServerTab serverTab = collections.get(i).getServerTab(); ServerTab serverTab = collection.getServerTab();
server.setPath(serverTab.getPath()); server.setPath(serverTab.getPath());
server.setMaxRam(serverTab.getMaxRam()); server.setMaxRam(serverTab.getMaxRam());
server.setType(ServerType.getByName(serverTab.getType())); server.setType(ServerType.getByName(serverTab.getType()));

View File

@ -34,6 +34,7 @@ public class Server {
private String maxRam; private String maxRam;
private Process process; private Process process;
private BufferedWriter writer; private BufferedWriter writer;
private BufferedReader reader;
public Server(String name, GUI window, ServerTab serverTab) { public Server(String name, GUI window, ServerTab serverTab) {
this.name = name; this.name = name;
@ -181,6 +182,8 @@ public class Server {
this.process = builder.start(); this.process = builder.start();
OutputStream stdin = this.process.getOutputStream(); OutputStream stdin = this.process.getOutputStream();
this.writer = new BufferedWriter(new OutputStreamWriter(stdin)); this.writer = new BufferedWriter(new OutputStreamWriter(stdin));
InputStream stdout = this.process.getInputStream();
this.reader = new BufferedReader (new InputStreamReader(stdout));
GUI.getGUI().setStatus("Servers are running"); GUI.getGUI().setStatus("Servers are running");
return true; return true;
} catch (IOException e) { } catch (IOException e) {
@ -192,6 +195,13 @@ public class Server {
} }
} }
public String read() throws IOException {
String line;
if ((line = reader.readLine()) != null) {
return line;
} else {return "";}
}
/** /**
* Downloads necessary .jar file for the server. * Downloads necessary .jar file for the server.
* This is unfortunately hardcoded since there is no golden standard, and we only host some jars ourselves. * This is unfortunately hardcoded since there is no golden standard, and we only host some jars ourselves.
@ -330,10 +340,9 @@ public class Server {
} }
public void sendCommand(String command) throws IOException { public void sendCommand(String command) throws IOException {
if (this.writer != null) { if (this.process != null && this.writer != null) {
this.writer.write(command); this.writer.write(command + "\n");
this.writer.flush(); this.writer.flush();
this.writer = null;
} }
} }
} }

View File

@ -1,7 +1,14 @@
package net.knarcraft.serverlauncher.userinterface; package net.knarcraft.serverlauncher.userinterface;
import net.knarcraft.serverlauncher.profile.Profile;
import javax.swing.*; import javax.swing.*;
import javax.swing.text.DefaultCaret;
import java.awt.*; import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import static javax.swing.text.DefaultCaret.ALWAYS_UPDATE;
/** /**
* Acts as a single writable/readable tab * Acts as a single writable/readable tab
@ -11,15 +18,13 @@ import java.awt.*;
* @version 0.0.0.1 * @version 0.0.0.1
* @since 0.0.0.1 * @since 0.0.0.1
*/ */
public class Console { public class Console implements ActionListener {
private final JTextField textInput; private final JTextField textInput;
private final JTextArea textOutput; private final JTextArea textOutput;
private final String name;
void output(String text) { Console(JTabbedPane tab, String name) {
this.textOutput.setText(text); this.name = name;
}
public Console(JTabbedPane tab, String name) {
JPanel panel = new JPanel(); JPanel panel = new JPanel();
tab.addTab(name, null, panel, null); tab.addTab(name, null, panel, null);
panel.setLayout(new BorderLayout(0, 0)); panel.setLayout(new BorderLayout(0, 0));
@ -27,10 +32,28 @@ public class Console {
textInput = new JTextField(); textInput = new JTextField();
panel.add(textInput, BorderLayout.SOUTH); panel.add(textInput, BorderLayout.SOUTH);
textInput.setColumns(10); textInput.setColumns(10);
textInput.addActionListener(this);
textOutput = new JTextArea(); textOutput = new JTextArea();
JScrollPane scroll = new JScrollPane(textOutput); JScrollPane scroll = new JScrollPane(textOutput);
panel.add(scroll, BorderLayout.CENTER); panel.add(scroll, BorderLayout.CENTER);
textOutput.setEditable(false); textOutput.setEditable(false);
DefaultCaret caret = (DefaultCaret) textOutput.getCaret();
caret.setUpdatePolicy(ALWAYS_UPDATE);
textOutput.setLineWrap(true);
}
public void output(String text) {
this.textOutput.setText(this.textOutput.getText() + "\n" + text);
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == textInput) {
String text = textInput.getText();
Profile profile = GUI.getGUI().currentProfile();
profile.sendCommand(this.name, text);
textInput.setText("");
}
} }
} }

View File

@ -101,17 +101,18 @@ public class GUI implements ActionListener {
} }
/** /**
* Removes a server's tab, removes it from the list of tabs, and removes the server from Profile.java * Removes a server from the profile's collection, the list of tabs, and the actual tabs.
* *
* @param tab The current tab object * @param tab The tab object where remove was clicked
*/ */
public void removeServer(ServerTab tab) { public void removeServer(ServerTab tab) {
for (int i = 0; i < this.serverTabs.size(); i++) { for (int i = 0; i < this.serverTabs.size(); i++) {
if(this.serverTabs.get(i) == tab) { if(this.serverTabs.get(i) == tab) {
serversPane.remove(i); this.serversPane.remove(i);
currentProfile().removeCollection(i); this.currentProfile().removeCollection(i);
ServerConsoles.getGUI().removeTab(i);
this.serverTabs.remove(i); this.serverTabs.remove(i);
i = serverTabs.size(); i = this.serverTabs.size();
} }
} }
@ -121,11 +122,11 @@ public class GUI implements ActionListener {
* Initialize the contents of the frame. * Initialize the contents of the frame.
*/ */
private void initialize() { private void initialize() {
/*try { try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | UnsupportedLookAndFeelException | InstantiationException | IllegalAccessException e) { } catch (ClassNotFoundException | UnsupportedLookAndFeelException | InstantiationException | IllegalAccessException e) {
e.printStackTrace(); e.printStackTrace();
}*/ }
frame = new JFrame("Minecraft server launcher"); frame = new JFrame("Minecraft server launcher");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
@ -475,8 +476,7 @@ public class GUI implements ActionListener {
currentProfile().sendCommand(selectedServerValue, "reload"); currentProfile().sendCommand(selectedServerValue, "reload");
} }
} else if (e.getSource() == btnServerConsoles) { } else if (e.getSource() == btnServerConsoles) {
//TODO: Make server consoles window, and toggle visibility on this action. ServerConsoles.show();
ServerConsoles.getGUI().show();
} }
} }

View File

@ -3,7 +3,6 @@ package net.knarcraft.serverlauncher.userinterface;
import javax.swing.JFrame; import javax.swing.JFrame;
import javax.swing.JTabbedPane; import javax.swing.JTabbedPane;
import java.awt.BorderLayout; import java.awt.BorderLayout;
import java.util.ArrayList;
/** /**
* A parent window for server consoles * A parent window for server consoles
@ -16,8 +15,8 @@ import java.util.ArrayList;
*/ */
public class ServerConsoles { public class ServerConsoles {
private static ServerConsoles serverConsoles; private static ServerConsoles serverConsoles;
private final JFrame frame; private static JFrame frame;
private final JTabbedPane consolesTab; private static JTabbedPane consolesTab;
public ServerConsoles() { public ServerConsoles() {
frame = new JFrame(); frame = new JFrame();
@ -33,10 +32,14 @@ public class ServerConsoles {
return serverConsoles; return serverConsoles;
} }
public void show() { public static void show() {
frame.setVisible(true); frame.setVisible(true);
} }
public void removeTab(int i) {
consolesTab.remove(i);
}
public Console addTab(String name) { public Console addTab(String name) {
return new Console(consolesTab, name); return new Console(consolesTab, name);
} }