Split up code into steps
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
package org.strassburger.mcrun.steps;
|
||||
|
||||
import org.apache.maven.plugin.logging.Log;
|
||||
import org.strassburger.mcrun.config.ServerInstallConfig;
|
||||
import org.strassburger.mcrun.downloader.ServerJarDownloadException;
|
||||
import org.strassburger.mcrun.downloader.ServerJarDownloader;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.StandardCopyOption;
|
||||
|
||||
public class ServerInstallStep extends Step<ServerInstallConfig> {
|
||||
public ServerInstallStep(Log log, ServerInstallConfig config) {
|
||||
super(log, config);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() throws StepExecutionException {
|
||||
copyPlugin();
|
||||
downloadServerJar();
|
||||
acceptEula();
|
||||
}
|
||||
|
||||
private void copyPlugin() throws StepExecutionException {
|
||||
File pluginsDir = new File(getConfig().serverDirectory(), "plugins");
|
||||
pluginsDir.mkdirs();
|
||||
try {
|
||||
Files.copy(getConfig().pluginJar().toPath(),
|
||||
new File(pluginsDir, getConfig().pluginJar().getName()).toPath(),
|
||||
StandardCopyOption.REPLACE_EXISTING);
|
||||
} catch (IOException e) {
|
||||
throw new StepExecutionException("Failed to copy plugin jar file: " + getConfig().pluginJar().getAbsolutePath(), e);
|
||||
}
|
||||
}
|
||||
|
||||
private void downloadServerJar() throws StepExecutionException {
|
||||
try {
|
||||
ServerJarDownloader jarDownloader = ServerJarDownloader.forServerSoftware(getConfig().serverSoftware())
|
||||
.withLog(getLog()).withServerDirectory(getConfig().serverDirectory())
|
||||
.withServerJarName(getConfig().serverJarName()).withMcVersion(getConfig().mcVersion())
|
||||
.build();
|
||||
jarDownloader.run();
|
||||
} catch (ServerJarDownloadException e) {
|
||||
throw new StepExecutionException("Failed to download server jar", e);
|
||||
}
|
||||
}
|
||||
|
||||
private void acceptEula() throws StepExecutionException {
|
||||
if (!getConfig().acceptEula()) return;
|
||||
File eula = new File(getConfig().serverDirectory(), "eula.txt");
|
||||
if (eula.exists()) return;
|
||||
try {
|
||||
Files.writeString(eula.toPath(), "eula=true\n");
|
||||
getLog().info("eula.txt created and accepted.");
|
||||
} catch (IOException e) {
|
||||
throw new StepExecutionException("Failed to write eula.txt: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package org.strassburger.mcrun.steps;
|
||||
|
||||
import org.apache.maven.plugin.logging.Log;
|
||||
import org.strassburger.mcrun.config.ServerRunStepConfig;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class ServerRunStep extends Step<ServerRunStepConfig> {
|
||||
public ServerRunStep(Log log, ServerRunStepConfig config) {
|
||||
super(log, config);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() throws StepExecutionException {
|
||||
startServerProcess();
|
||||
}
|
||||
|
||||
private List<String> buildCommand() {
|
||||
List<String> cmd = new ArrayList<>();
|
||||
cmd.add("java");
|
||||
cmd.add("-Xmx" + getConfig().memory());
|
||||
cmd.add("-Xms" + getConfig().memory());
|
||||
if (getConfig().remoteDebug() && getConfig().debugPort() > 0) {
|
||||
cmd.add(String.format("-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:%d", getConfig().debugPort()));
|
||||
}
|
||||
cmd.addAll(getConfig().jvmArgs());
|
||||
cmd.add("-jar");
|
||||
cmd.add(getConfig().serverJarName());
|
||||
cmd.add("--nogui");
|
||||
if (getConfig().port() != null) {
|
||||
cmd.add("--port");
|
||||
cmd.add(String.valueOf(getConfig().port()));
|
||||
}
|
||||
return cmd;
|
||||
}
|
||||
|
||||
private void startServerProcess() throws StepExecutionException {
|
||||
List<String> command = buildCommand();
|
||||
getLog().info("Starting server with command: " + String.join(" ", command));
|
||||
|
||||
ProcessBuilder pb = new ProcessBuilder(command);
|
||||
pb.directory(getConfig().serverDirectory());
|
||||
pb.inheritIO();
|
||||
Process process;
|
||||
try {
|
||||
process = pb.start();
|
||||
process.waitFor();
|
||||
} catch (IOException | InterruptedException e) {
|
||||
throw new StepExecutionException("Failed to start server: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package org.strassburger.mcrun.steps;
|
||||
|
||||
import org.apache.maven.plugin.logging.Log;
|
||||
|
||||
public abstract class Step<T> {
|
||||
private final Log log;
|
||||
private final T config;
|
||||
|
||||
public Step(Log log, T config) {
|
||||
this.log = log;
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
public abstract void run() throws StepExecutionException;
|
||||
|
||||
protected Log getLog() {
|
||||
return log;
|
||||
}
|
||||
|
||||
protected T getConfig() {
|
||||
return config;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package org.strassburger.mcrun.steps;
|
||||
|
||||
public class StepExecutionException extends RuntimeException {
|
||||
public StepExecutionException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public StepExecutionException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user