//========== Player Management ==========\\
private Map<String, ImageIcon> createPlayerImageMap() {
Map<String, ImageIcon> map = new HashMap<>();
try {
for (Player player : listOfPlayers) {
if (player.isAFK()) {
map.put(player.toString(), new ImageIcon(this.getClass().getResource("/eu/m4gkbeatz/bukkitui/resources/player_icons/player_afk.png")));
} else {
map.put(player.toString(), new ImageIcon(this.getClass().getResource("/eu/m4gkbeatz/bukkitui/resources/player_icons/player_online.png")));
}
}
} catch (Exception ex) {}
return map;
}
private void loadPlayers() {
DefaultListModel model = new DefaultListModel();
int index = 0;
for (Player player : listOfPlayers) {
model.add(index, player.toString());
index++;
}
}
//========== Internal Classes ===========\\
/**
* Custom list cell renderer for player list.
* @author beatsleigher
*/
public class PlayerListCellRenderer extends DefaultListCellRenderer {
@Override
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
JLabel label = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
label.setIcon(imageMap.get((String) value));
label.setHorizontalTextPosition(JLabel.RIGHT);
return label;
}
}
//========== Read Console Output ===========\\
private void monitorServer() {
new Thread() {
@Override
public void run() {
try {
String line = "";
while ((line = processReader.readLine()) != null && runServer) {
if (!line.contains("[JVM]")) {
consoleLog.append(line + "\n");
System.out.print("Craftbukkit Output: " + line + "\n");
if (line.toLowerCase().contains("done")) {
serverStatusLabel.setIcon(ONLINE);
serverStatusLabel.setText("ONLINE");
serverState = ServerState.ONLINE;
monitorServerRuntime();
}
if (line.toLowerCase().contains("logged in")) {
String[] arr = line.split("[INFO] "), arr0 = arr[1].split(" ");
String[] ip0 = arr0[1].split("[/"), ip1 = ip0[1].split("]");
System.out.println("User logged in: " + arr0[0]);
System.out.println("User IP-Address: " + ip1[0]);
listOfPlayers.add(new Player(arr0[0], false, ip1[0]));
}
if (line.toLowerCase().contains("disconnect.")) {
String[] arr = line.split("[INFO] "), arr0 = arr[1].split(" ");
System.out.println(arr0[0] + " has left.");
for (Player player : listOfPlayers) {
if (player.toString().equals(arr0[0]))
listOfPlayers.remove(player);
}
}
imageMap = createPlayerImageMap();
loadPlayers();
} else {
if (line.contains("processCPULoad=")) {
System.out.println("JVM Info: " + line);
String[] load = line.split("=");
cpuUsageProgressBar.setValue(Integer.valueOf(load[1]));
cpuUsageProgressBar.setString(load[1]);
continue;
}
if (line.contains("maxMem=")) {
String[] mem = line.split("=");
System.out.println("JVM Info: " + line);
maxMemLabel.setText("Max Memory: " + mem[1] + "MB");
usedMemProgressBar.setMaximum(Integer.valueOf(mem[1]));
}
if (line.contains("totalMem=")) {
String[] mem = line.split("=");
System.out.println("JVM Info: " + line);
totalMemLabel.setText("Total Memory: " + mem[1] + "MB");
}
if (line.contains("usedMem=")) {
String[] mem = line.split("=");
System.out.println("JVM Info: " + line);
usedMemProgressBar.setString("used Memory: " + mem[1] + "MB");
usedMemProgressBar.setValue(Integer.valueOf(mem[1]));
}
}
}
serverStatusLabel.setIcon(OFFLINE);
serverStatusLabel.setText("OFFLINE");
} catch (Exception ex) {
serverStatusLabel.setIcon(OFFLINE);
JOptionPane.showMessageDialog(null, "ERROR: An error occured while trying to read from the server!\n" + ex.toString() + "\nThe server will now be destroyed.", "Destroying Process!", JOptionPane.ERROR_MESSAGE);
killServer();
}
}
}.start();
}