From 58fe53f0c66f64ba3ae6b77ba8027f94470223b7 Mon Sep 17 00:00:00 2001 From: Hadron67 <604700341@qq.com> Date: Mon, 25 May 2020 17:42:54 +0800 Subject: [PATCH] old world backup no longer got counted --- .../fibersync/backup/BackupFactory.java | 11 +++ .../fibersync/backup/BackupInfo.java | 2 +- .../fibersync/command/BackupCommand.java | 80 +++++++++---------- .../hadroncfy/fibersync/config/Formats.java | 8 +- 4 files changed, 54 insertions(+), 47 deletions(-) diff --git a/src/main/java/com/hadroncfy/fibersync/backup/BackupFactory.java b/src/main/java/com/hadroncfy/fibersync/backup/BackupFactory.java index a2c1870..41ee680 100644 --- a/src/main/java/com/hadroncfy/fibersync/backup/BackupFactory.java +++ b/src/main/java/com/hadroncfy/fibersync/backup/BackupFactory.java @@ -8,6 +8,7 @@ import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; +import java.util.Collection; import java.util.Date; import java.util.List; import java.util.UUID; @@ -68,4 +69,14 @@ public BackupEntry create(String levelName, String name, String description, UUI public long totalSize(){ return FileUtil.totalSize(dir.get()); } + + public static int getBackupCount(Collection entries){ + int c = 0; + for (BackupEntry e: entries){ + if (!e.getInfo().isOldWorld){ + c++; + } + } + return c; + } } \ No newline at end of file diff --git a/src/main/java/com/hadroncfy/fibersync/backup/BackupInfo.java b/src/main/java/com/hadroncfy/fibersync/backup/BackupInfo.java index 7430868..a928fa9 100644 --- a/src/main/java/com/hadroncfy/fibersync/backup/BackupInfo.java +++ b/src/main/java/com/hadroncfy/fibersync/backup/BackupInfo.java @@ -15,7 +15,7 @@ public class BackupInfo { public String name; public Date date; public String description; - public boolean locked; + public boolean locked, isOldWorld; public UUID creator; public void refresh(String description, UUID creator){ diff --git a/src/main/java/com/hadroncfy/fibersync/command/BackupCommand.java b/src/main/java/com/hadroncfy/fibersync/command/BackupCommand.java index 6971529..73c9ba9 100644 --- a/src/main/java/com/hadroncfy/fibersync/command/BackupCommand.java +++ b/src/main/java/com/hadroncfy/fibersync/command/BackupCommand.java @@ -54,25 +54,29 @@ public static void register(CommandDispatcher cd) { final LiteralArgumentBuilder b = literal(NAME) .then(literal("list").executes(BackupCommand::list)) .then(literal("create") - .then(argument("name", StringArgumentType.word()).suggests(BackupCommand::suggestBackups) - .then(argument("description", MessageArgumentType.message()) - .executes(BackupCommand::create)))) + .then(argument("name", StringArgumentType.word()).suggests(BackupCommand::suggestUnlockedBackups) + .then(argument("description", MessageArgumentType.message()).executes(BackupCommand::create)))) .then(literal("make").executes(BackupCommand::create) - .then(argument("description", MessageArgumentType.message()).executes(BackupCommand::create))) - .then(literal("back").then(argument("name", StringArgumentType.word()) - .suggests(BackupCommand::suggestBackups).executes(BackupCommand::back))) + .then(argument("description", MessageArgumentType.message()).executes(BackupCommand::create))) + .then(literal("back") + .then(argument("name", StringArgumentType.word()) + .suggests(BackupCommand::suggestBackups) + .executes(BackupCommand::back))) .then(literal("confirm") - .then(argument("code", IntegerArgumentType.integer()).executes(BackupCommand::confirm))) + .then(argument("code", IntegerArgumentType.integer()).executes(BackupCommand::confirm))) .then(literal("cancel").executes(BackupCommand::cancel)) .then(literal("reload").executes(BackupCommand::reload)) - .then(literal("delete").then(argument("name", StringArgumentType.word()) + .then(literal("delete") + .then(argument("name", StringArgumentType.word()) .suggests(BackupCommand::suggestUnlockedBackups).executes(BackupCommand::delete))) - .then(literal("lock").requires(BackupCommand::canLock) + .then(literal("lock") + .requires(BackupCommand::canLock) .then(argument("name", StringArgumentType.word()) - .suggests(BackupCommand::suggestUnlockedBackups).executes(ctx -> setLocked(ctx, true)))) - .then(literal("unlock").requires(BackupCommand::canLock) - .then(argument("name", StringArgumentType.word()).suggests(BackupCommand::suggestLockedBackups) - .executes(ctx -> setLocked(ctx, false)))); + .suggests(BackupCommand::suggestUnlockedBackups).executes(ctx -> setLocked(ctx, true)))) + .then(literal("unlock") + .requires(BackupCommand::canLock) + .then(argument("name", StringArgumentType.word()) + .suggests(BackupCommand::suggestLockedBackups).executes(ctx -> setLocked(ctx, false)))); cd.register(b); } @@ -107,7 +111,7 @@ private static int reload(CommandContext ctx) { FibersyncMod.loadConfig(); src.sendFeedback(getFormat().reloadedConfig, true); return 0; - } catch (Exception e) { + } catch (Throwable e) { src.sendError(render(getFormat().failedToCopyLevelFiles, e.toString())); return 1; } @@ -220,7 +224,7 @@ private static synchronized int setLocked(CommandContext ct server.getPlayerManager() .broadcastChatMessage(render(locked ? getFormat().lockedBackup : getFormat().unlockedBackup, src.getName(), entry.getInfo().name), false); - } catch (Exception e) { + } catch (Throwable e) { e.printStackTrace(); server.getPlayerManager().broadcastChatMessage( render(getFormat().failedToWriteInfo, src.getName(), e.toString()), false); @@ -270,7 +274,7 @@ private static String getEmptyBackupName(String prefix, List entrie int i = 1; out: while (true){ - name = prefix + "-" + i++; + name = prefix + i++; for (BackupEntry e: entries){ if (e.getInfo().name.equals(name)){ continue out; @@ -302,32 +306,25 @@ private static synchronized int create(CommandContext ctx) final int maxBackups = getConfig().maxBackupCount; final String description = tryGetArg(() -> MessageArgumentType.getMessage(ctx, "description").asString(), () -> ""); - BackupEntry b, b2 = null; - try { - final String name = StringArgumentType.getString(ctx, "name"); - b = cctx.getBackupFactory().create(server.getLevelName(), name, description, getSourceUUID(ctx)); - if (b.exists()){ - b2 = b; - } - else if (maxBackups != -1 && entries.size() >= maxBackups){ - src.sendError(getFormat().backupCountFull); - return 0; - } + BackupEntry b2 = null; + + final String name = tryGetArg(() -> StringArgumentType.getString(ctx, "name"), () -> getEmptyBackupName("b", entries)); + final BackupEntry selected = cctx.getBackupFactory().create(server.getLevelName(), name, description, getSourceUUID(ctx)); + if (selected.exists()){ + b2 = selected; } - catch(IllegalArgumentException e){ - if (maxBackups != -1 && entries.size() >= maxBackups){ - b2 = getOldestUnlockedEntry(entries); - if (b2 == null){ - src.sendError(getFormat().allBackupsLocked); - return 1; - } + else if (maxBackups != -1 && BackupFactory.getBackupCount(entries) >= maxBackups){ + b2 = getOldestUnlockedEntry(entries); + if (b2 == null){ + src.sendError(getFormat().allBackupsLocked); + return 1; } - b = cctx.getBackupFactory().create(server.getLevelName(), getEmptyBackupName("backup", entries), description, getSourceUUID(ctx)); } - final BackupEntry selected = b, overwrite = b2; + + + final BackupEntry overwrite = b2; final String senderName = src.getName(); - final String name = selected.getInfo().name; final Runnable btask = () -> { if (cctx.tryBeginTask(src)){ server.getPlayerManager().broadcastChatMessage(render(getFormat().creatingBackup, senderName, name), false); @@ -372,14 +369,9 @@ private static synchronized int back(CommandContext ctx){ return 0; } - final int maxBackups = getConfig().maxBackupCount; final BackupEntry currentWorld = cctx.getBackupFactory().create(server.getLevelName(), getConfig().oldWorldName, getConfig().oldWorldDescription, getSourceUUID(ctx)); currentWorld.getInfo().locked = true; - final List entries = cctx.getBackupFactory().getBackups(server.getLevelName()); - if (!currentWorld.exists() && maxBackups != -1 && entries.size() >= maxBackups){ - src.sendError(getFormat().backupCountFullWhenRollback); - return 0; - } + currentWorld.getInfo().isOldWorld = true; cctx.getConfirmationManager().submit(src.getName(), src, (s) -> { if (cctx.tryBeginTask(src)){ @@ -410,8 +402,10 @@ private static synchronized int back(CommandContext ctx){ CompletableFuture.runAsync(() -> { try { doCopy(server, autoBackup, currentWorld); + server.getPlayerManager().broadcastChatMessage(getFormat().copiedFromTempDir, false); } catch (Throwable e1) { e1.printStackTrace(); + server.getPlayerManager().broadcastChatMessage(render(getFormat().failedToCopyFromTempDir, e1.toString()), false); } finally { cctx.endTask(); diff --git a/src/main/java/com/hadroncfy/fibersync/config/Formats.java b/src/main/java/com/hadroncfy/fibersync/config/Formats.java index 967d651..f6fa38e 100644 --- a/src/main/java/com/hadroncfy/fibersync/config/Formats.java +++ b/src/main/java/com/hadroncfy/fibersync/config/Formats.java @@ -60,13 +60,13 @@ private static Text empty(){ creatingBackupTitle = new LiteralText("创建存档") .append(new LiteralText("$1").setStyle(new Style().setColor(Formatting.GREEN))), failedToCopyLevelFiles = red("[Fibersync] 复制存档文件失败:$1"), - countDownTitle = new LiteralText("准备回档:$1"), + countDownTitle = empty().append(new LiteralText("准备回档:").setStyle(new Style().setColor(Formatting.GREEN))) + .append(new LiteralText("$1").setStyle(new Style().setBold(true))), rollbackAborted = new LiteralText("[Fibersync] $1:已取消回档"), nothingToAbort = red("[Fibersync] 无可取消的操作"), reloadedConfig = new LiteralText("[Fibersync] 已加载配置"), failedToLoadConfig = red("[Fibersync] 加载配置失败:$1"), backupCountFull = red("[Fibersync] 已超出最大备份数"), - backupCountFullWhenRollback = red("[Fibersync] 已超出最大备份数,不能备份当前世界"), deletedBackup = new LiteralText("[Fibersync] $1:已删除备份$2"), failedToDeletedBackup = red("[Fibersync] $1:删除备份$2失败:$3"), backupLocked = red("[Fibersync] 删除失败:此存档已锁定"), @@ -78,5 +78,7 @@ private static Text empty(){ deletingBackup = new LiteralText("[Fibersync] $1:正在删除存档$2"), deletingBackupTitle = new LiteralText("删除存档") .append(new LiteralText("$1").setStyle(new Style().setColor(Formatting.GREEN))), - failedToRetrieveList = red("[Fibersync] 获取存档列表时出错:$1"); + failedToRetrieveList = red("[Fibersync] 获取存档列表时出错:$1"), + failedToCopyFromTempDir = red("[Fibersync] 从临时目录复制文件时出错:$1"), + copiedFromTempDir = new LiteralText("[Fibersync] 文件复制完成"); } \ No newline at end of file