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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user