From ea50da980d67b0f7af4c3db5db4b1e5c5c5ea783 Mon Sep 17 00:00:00 2001 From: szymon Date: Thu, 6 Aug 2020 23:53:02 +0200 Subject: [PATCH] Improved list command --- .../commands/restore/ListBackupsCommand.java | 21 ++++++++----------- .../core/restore/RestoreHelper.java | 4 ++++ 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/main/java/net/szum123321/textile_backup/commands/restore/ListBackupsCommand.java b/src/main/java/net/szum123321/textile_backup/commands/restore/ListBackupsCommand.java index 8dc9ec3..0ba8d48 100644 --- a/src/main/java/net/szum123321/textile_backup/commands/restore/ListBackupsCommand.java +++ b/src/main/java/net/szum123321/textile_backup/commands/restore/ListBackupsCommand.java @@ -22,34 +22,31 @@ import com.mojang.brigadier.builder.LiteralArgumentBuilder; import net.minecraft.server.command.CommandManager; import net.minecraft.server.command.ServerCommandSource; import net.minecraft.text.LiteralText; -import net.szum123321.textile_backup.core.Utilities; +import net.szum123321.textile_backup.core.restore.RestoreHelper; -import java.io.File; -import java.util.Arrays; import java.util.Iterator; +import java.util.List; public class ListBackupsCommand { public static LiteralArgumentBuilder register() { return CommandManager.literal("list") - .executes(ctx -> { - StringBuilder builder = new StringBuilder(); + .executes(ctx -> { StringBuilder builder = new StringBuilder(); + List backups = RestoreHelper.getAvailableBackups(ctx.getSource().getMinecraftServer()); - File[] files = Utilities.getBackupRootPath(Utilities.getLevelName(ctx.getSource().getMinecraftServer())).listFiles(); - - if(files.length == 0) { + if(backups.size() == 0) { builder.append("There a no backups available for this world."); - } else if(files.length == 1) { + } else if(backups.size() == 1) { builder.append("There is only one backup available: "); - builder.append(files[0].getName()); + builder.append(backups.get(0).toString()); } else { - Iterator iterator = Arrays.stream(files).iterator(); + Iterator iterator = backups.iterator(); builder.append("Available backups: "); builder.append(iterator.next()); while(iterator.hasNext()) { builder.append(",\n"); - builder.append(iterator.next().getName()); + builder.append(iterator.next().toString()); } } diff --git a/src/main/java/net/szum123321/textile_backup/core/restore/RestoreHelper.java b/src/main/java/net/szum123321/textile_backup/core/restore/RestoreHelper.java index 5e49e8d..fc716f7 100644 --- a/src/main/java/net/szum123321/textile_backup/core/restore/RestoreHelper.java +++ b/src/main/java/net/szum123321/textile_backup/core/restore/RestoreHelper.java @@ -82,5 +82,9 @@ public class RestoreHelper { public String getComment() { return comment; } + + public String toString() { + return this.getCreationTime().format(Statics.defaultDateTimeFormatter) + (comment != null ? "#" + comment : ""); + } } }