package de.creeperfans.crw;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
import java.awt.Desktop;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Roman
*/
public class ConsoleRW implements Runnable, HttpHandler {
private Process process;
private int port;
private HttpServer server;
private boolean running;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
new Thread(new ConsoleRW()).start();
}
@Override
public void run() {
try {
process = Runtime.getRuntime().exec("java -Xmx1024M -Xms1024M -jar craftbukkit.jar");
port = 12345;
running = true;
server = HttpServer.create(new InetSocketAddress(port), 10);
server.createContext("/", this);
server.setExecutor(null);
server.start();
setUp();
update();
Desktop.getDesktop().browse(new URI("http://localhost:" + port + "/"));
while (running) {
update();
try {
Thread.sleep(3000);
} catch (InterruptedException ex) {
Logger.getLogger(ConsoleRW.class.getName()).log(Level.SEVERE, null, ex);
}
}
} catch (URISyntaxException ex) {
Logger.getLogger(ConsoleRW.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(ConsoleRW.class.getName()).log(Level.SEVERE, null, ex);
}
}
@Override
public void handle(HttpExchange he) throws IOException {
StringBuilder html = new StringBuilder();
html.append("<html><head><title>Minecraft Server</title></head><body><h1>Minecraft Server</h1><br><h2>Players</h2><br><ul>");
for (String player : playerList) {
html.append("<li>");
html.append(player);
html.append("</li>");
}
html.append("</ul>");
he.sendResponseHeaders(200, html.toString().length());
OutputStream os = he.getResponseBody();
os.write(html.toString().getBytes());
os.close();
}
private boolean setup = true;
private void setUp() throws IOException {
System.out.println("==SetUp==");
new StreamReaderThread(process.getInputStream(), new ReaderListener() {
@Override
public void dataAvailable(String data) {
if (data.equals(">") || setup) {
return;
}
playerList.add(data);
System.out.println(data);
}
}).start();
new StreamReaderThread(process.getErrorStream(), new ReaderListener() {
@Override
public void dataAvailable(String data) {
if (!setup) {
//return;
}
System.out.println(data.replaceAll("[0-9][0-9]:[0-9][0-9]:[0-9][0-9] \\[([a-zA-Z]*)\\] (.*)", "$1 >> $2"));
if (data.matches("[0-2][0-9]:[0-9][0-9]:[0-9][0-9] \\[INFO\\] Done .*")) {
setup = false;
}
}
}).start();
while (setup) {
try {
Thread.sleep(250);
} catch (InterruptedException ex) {
Logger.getLogger(ConsoleRW.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
private List<String> playerList = new ArrayList<String>();
private void update() throws IOException {
System.out.println("==Update==");
updatePlayerList();
}
private void updatePlayerList() throws IOException {
System.out.println("> Updating player list...");
//mein code (kann auch deiner sein :D)
BufferedOutputStream os = (BufferedOutputStream) process.getOutputStream();
os.write("list\n".getBytes());
os.flush();
//mein code ende
try {
Thread.sleep(250);
} catch (InterruptedException ex) {
Logger.getLogger(ConsoleRW.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println("> Player list updated!");
}
}