112 lines
4.2 KiB
Java
112 lines
4.2 KiB
Java
import net.knarcraft.serverlauncher.profile.Collection;
|
|
import net.knarcraft.serverlauncher.profile.Profile;
|
|
import net.knarcraft.serverlauncher.server.ServerType;
|
|
import net.knarcraft.serverlauncher.userinterface.GUI;
|
|
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;
|
|
//Java 8 required.
|
|
|
|
/**
|
|
* A software for managing Minecraft servers.
|
|
*
|
|
* @author Kristian Knarvik <kristian.knarvik@knett.no>
|
|
* @version 0.0.0.1
|
|
* @since 0.0.0.1
|
|
*/
|
|
|
|
class Main {
|
|
public static void main(String[] args) {
|
|
EventQueue.invokeLater(() -> {
|
|
try {
|
|
setup();
|
|
new GUI();
|
|
new ServerConsoles();
|
|
Profile.addProfile("Default");
|
|
//TODO: replace with profiles loading generating a default profile if empty.
|
|
|
|
ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor();
|
|
exec.scheduleAtFixedRate(() -> {
|
|
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);
|
|
updatePlayerList(readText, collection);
|
|
}
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
}
|
|
}, 10, 250, TimeUnit.MILLISECONDS);
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
});
|
|
}
|
|
|
|
private static void setup() {
|
|
try {
|
|
ServerType.loadServerTypes();
|
|
} catch (ConfigurationException e) {
|
|
e.printStackTrace();
|
|
System.exit(1);
|
|
}
|
|
}
|
|
|
|
private static void updatePlayerList(String text, Collection collection) {
|
|
if (!collection.getServer().typeName().equals("Bungee")) { //Bungee servers are not to be treated as normal servers.
|
|
String joinedPlayer = stringBetween(text, "[Server thread/INFO]: ", " joined the game");
|
|
if (!joinedPlayer.equals("")) {
|
|
if (!collection.getServer().hasPlayer(joinedPlayer)) {
|
|
collection.getServer().addPlayer(joinedPlayer);
|
|
}
|
|
} else {
|
|
joinedPlayer = stringBetween(text, "UUID of player ", " is ");
|
|
if (!joinedPlayer.equals("")) {
|
|
if (!collection.getServer().hasPlayer(joinedPlayer)) {
|
|
collection.getServer().addPlayer(joinedPlayer);
|
|
}
|
|
}
|
|
}
|
|
if (joinedPlayer.equals("")) {
|
|
String leftPlayer = stringBetween(text, "INFO]: ", " lost connection");
|
|
if (!leftPlayer.equals("")) {
|
|
if (collection.getServer().hasPlayer(leftPlayer)) {
|
|
collection.getServer().removePlayer(leftPlayer);
|
|
}
|
|
} else {
|
|
leftPlayer = stringBetween(text, "[Server thread/INFO]: ", " left the game");
|
|
if (!leftPlayer.equals("")) {
|
|
if (collection.getServer().hasPlayer(leftPlayer)) {
|
|
collection.getServer().removePlayer(leftPlayer);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Finds a substring between two substrings in a string.
|
|
*
|
|
* @param string The string containing the substrings
|
|
* @param start The substring before the wanted substring
|
|
* @param end The substring after the wanted substring
|
|
* @return The wanted substring.
|
|
*/
|
|
private static String stringBetween(String string, String start, String end) {
|
|
if (!string.contains(start)) {
|
|
return "";
|
|
}
|
|
int startPos = string.indexOf(start) + start.length();
|
|
return string.substring(startPos, string.indexOf(end, startPos));
|
|
}
|
|
} |