Skip to content

Commit

Permalink
old world backup no longer got counted
Browse files Browse the repository at this point in the history
  • Loading branch information
Hadron67 committed May 25, 2020
1 parent cc83df4 commit 58fe53f
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 47 deletions.
11 changes: 11 additions & 0 deletions src/main/java/com/hadroncfy/fibersync/backup/BackupFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<BackupEntry> entries){
int c = 0;
for (BackupEntry e: entries){
if (!e.getInfo().isOldWorld){
c++;
}
}
return c;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Expand Down
80 changes: 37 additions & 43 deletions src/main/java/com/hadroncfy/fibersync/command/BackupCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,25 +54,29 @@ public static void register(CommandDispatcher<ServerCommandSource> cd) {
final LiteralArgumentBuilder<ServerCommandSource> 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);
}

Expand Down Expand Up @@ -107,7 +111,7 @@ private static int reload(CommandContext<ServerCommandSource> 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;
}
Expand Down Expand Up @@ -220,7 +224,7 @@ private static synchronized int setLocked(CommandContext<ServerCommandSource> 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);
Expand Down Expand Up @@ -270,7 +274,7 @@ private static String getEmptyBackupName(String prefix, List<BackupEntry> 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;
Expand Down Expand Up @@ -302,32 +306,25 @@ private static synchronized int create(CommandContext<ServerCommandSource> 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);
Expand Down Expand Up @@ -372,14 +369,9 @@ private static synchronized int back(CommandContext<ServerCommandSource> 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<BackupEntry> 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)){
Expand Down Expand Up @@ -410,8 +402,10 @@ private static synchronized int back(CommandContext<ServerCommandSource> 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();
Expand Down
8 changes: 5 additions & 3 deletions src/main/java/com/hadroncfy/fibersync/config/Formats.java
Original file line number Diff line number Diff line change
Expand Up @@ -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] 删除失败:此存档已锁定"),
Expand All @@ -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] 文件复制完成");
}

0 comments on commit 58fe53f

Please sign in to comment.