Rework command output.

This commit is contained in:
Logan Gorence 2021-12-20 18:51:35 +00:00
parent 42f5f33149
commit 10f6bf98dd
No known key found for this signature in database
GPG Key ID: 9743CEF10935949A
3 changed files with 37 additions and 8 deletions

View File

@ -0,0 +1,7 @@
package cloud.kubelet.foundation;
import net.kyori.adventure.text.format.TextColor;
public final class TextColors {
public static final TextColor AMARANTH_PINK = TextColor.fromHexString("#F7A8B8");
}

View File

@ -1,15 +1,29 @@
package cloud.kubelet.foundation;
import java.io.IOException;
import java.util.List;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.TextColor;
import org.slf4j.Logger;
public class Util {
public static int runProcess(List<String> command) throws IOException, InterruptedException {
return new ProcessBuilder(command).start().waitFor();
}
private static final Component leftBracket = Component.text('[');
private static final Component rightBracket = Component.text(']');
private static final Component whitespace = Component.text(' ');
private static final Component foundationName = Component.text("Foundation");
public static void printFeatureStatus(Logger logger, String feature, boolean state) {
logger.info("{}: {}", feature, state ? "Enabled" : "Disabled");
}
public static Component formatSystemMessage(String message) {
return formatSystemMessage(TextColors.AMARANTH_PINK, message);
}
public static Component formatSystemMessage(TextColor prefixColor, String message) {
return leftBracket
.append(foundationName.color(prefixColor))
.append(rightBracket)
.append(whitespace)
.append(Component.text(message));
}
}

View File

@ -1,6 +1,7 @@
package cloud.kubelet.foundation.command;
import cloud.kubelet.foundation.Foundation;
import cloud.kubelet.foundation.Util;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
@ -15,6 +16,7 @@ import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.TextColor;
import org.bukkit.Server;
import org.bukkit.World;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
@ -61,19 +63,23 @@ public class BackupCommand implements CommandExecutor {
private void runBackup(CommandSender sender) throws IOException {
RUNNING.set(true);
final var server = sender.getServer();
server.sendMessage(Util.formatSystemMessage("Backup started."));
final var backupFile = backupPath.resolve(
String.format("backup-%s.zip", Instant.now().toString())).toFile();
final var zipFileStream = new FileOutputStream(backupFile);
try (zipFileStream; var zipStream = new ZipOutputStream(
new BufferedOutputStream(zipFileStream))) {
final var worlds = sender.getServer().getWorlds();
final var worlds = server.getWorlds();
for (World world : worlds) {
final var name = world.getName();
final var worldPath = world.getWorldFolder().toPath().toString();
sender.sendMessage(String.format("%s: %s", name, worldPath));
// Save the world.
world.save();
// Disable auto saving to prevent any world corruption while creating a ZIP.
world.setAutoSave(false);
try {
@ -96,10 +102,12 @@ public class BackupCommand implements CommandExecutor {
e.printStackTrace();
}
// Re-enable auto saving for this world.
world.setAutoSave(true);
}
} finally {
RUNNING.set(false);
server.sendMessage(Util.formatSystemMessage("Backup finished."));
}
}
}