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.
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;
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+/
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;;
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.server.ServerType;
import net.knarcraft.serverlauncher.userinterface.GUI;
@ -5,6 +6,7 @@ import net.knarcraft.serverlauncher.userinterface.ServerConsoles;
import javax.naming.ConfigurationException;
import java.awt.*;
import java.io.IOException;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
@ -30,8 +32,19 @@ class Main {
ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor();
exec.scheduleAtFixedRate(() -> {
//TODO: Read from consoles and insert into ServerConsoles.
}, 0, 5, TimeUnit.SECONDS);
for (Collection collection : GUI.getGUI().currentProfile().getCollections()) {
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) {
e.printStackTrace();
}

View File

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

View File

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

View File

@ -34,6 +34,7 @@ public class Server {
private String maxRam;
private Process process;
private BufferedWriter writer;
private BufferedReader reader;
public Server(String name, GUI window, ServerTab serverTab) {
this.name = name;
@ -179,8 +180,10 @@ public class Server {
builder.directory(new File(this.path));
builder.redirectErrorStream(true);
this.process = builder.start();
OutputStream stdin = this.process.getOutputStream ();
OutputStream stdin = this.process.getOutputStream();
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");
return true;
} 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.
* 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 {
if (this.writer != null) {
this.writer.write(command);
if (this.process != null && this.writer != null) {
this.writer.write(command + "\n");
this.writer.flush();
this.writer = null;
}
}
}

View File

@ -1,7 +1,14 @@
package net.knarcraft.serverlauncher.userinterface;
import net.knarcraft.serverlauncher.profile.Profile;
import javax.swing.*;
import javax.swing.text.DefaultCaret;
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
@ -11,15 +18,13 @@ import java.awt.*;
* @version 0.0.0.1
* @since 0.0.0.1
*/
public class Console {
public class Console implements ActionListener {
private final JTextField textInput;
private final JTextArea textOutput;
private final String name;
void output(String text) {
this.textOutput.setText(text);
}
public Console(JTabbedPane tab, String name) {
Console(JTabbedPane tab, String name) {
this.name = name;
JPanel panel = new JPanel();
tab.addTab(name, null, panel, null);
panel.setLayout(new BorderLayout(0, 0));
@ -27,10 +32,28 @@ public class Console {
textInput = new JTextField();
panel.add(textInput, BorderLayout.SOUTH);
textInput.setColumns(10);
textInput.addActionListener(this);
textOutput = new JTextArea();
JScrollPane scroll = new JScrollPane(textOutput);
panel.add(scroll, BorderLayout.CENTER);
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) {
for (int i = 0; i < this.serverTabs.size(); i++) {
if(this.serverTabs.get(i) == tab) {
serversPane.remove(i);
currentProfile().removeCollection(i);
this.serversPane.remove(i);
this.currentProfile().removeCollection(i);
ServerConsoles.getGUI().removeTab(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.
*/
private void initialize() {
/*try {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | UnsupportedLookAndFeelException | InstantiationException | IllegalAccessException e) {
e.printStackTrace();
}*/
}
frame = new JFrame("Minecraft server launcher");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
@ -475,8 +476,7 @@ public class GUI implements ActionListener {
currentProfile().sendCommand(selectedServerValue, "reload");
}
} else if (e.getSource() == btnServerConsoles) {
//TODO: Make server consoles window, and toggle visibility on this action.
ServerConsoles.getGUI().show();
ServerConsoles.show();
}
}

View File

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