Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions src/main/java/net/neganote/monilabs/utils/PackSwitchUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Properties;

public class PackSwitchUtil {

Expand All @@ -18,6 +21,7 @@ public static void switchToNormal(String cwd) {
public static void switchToHard(String cwd) {
copyFiles(Path.of(cwd, File.separator, "config-overrides", File.separator, "hardmode"),
Path.of(cwd, File.separator, "config"));
setServerDifficulty(cwd, "peaceful");

createModeFile("hard");
}
Expand All @@ -26,6 +30,7 @@ public static void switchToExpert(String cwd) {
switchToHard(cwd);
copyFiles(Path.of(cwd, File.separator, "config-overrides", File.separator, "expert"),
Path.of(cwd, File.separator, "config"));
setServerDifficulty(cwd, "peaceful");

createModeFile("expert");
}
Expand Down Expand Up @@ -74,4 +79,31 @@ public static boolean createModeFile(String contents) {
}
return true;
}

public static void setServerDifficulty(String cwd, String difficulty) {
Path propertiesPath = Path.of(cwd, "server.properties");

if (!Files.exists(propertiesPath)) {
return;
}

Properties properties = new Properties();

try (InputStream in = Files.newInputStream(propertiesPath)) {
properties.load(in);
} catch (IOException e) {
System.err.println("Failed to read server.properties: " + e.getMessage());
throw new RuntimeException();
}

properties.setProperty("difficulty", difficulty);

try (OutputStream out = Files.newOutputStream(propertiesPath)) {
properties.store(out, "Minecraft server properties");
System.out.println("Server difficulty set to " + difficulty);
} catch (IOException e) {
System.err.println("Failed to write server.properties: " + e.getMessage());
throw new RuntimeException();
}
}
}