* Added findFileAndLockIfPresent method to RestoreHelper

* create method in RestoreHelper return AwaitThread
 * Improved RestoreBackupCommand
2.x-1.16
szymon 2020-08-07 00:06:36 +02:00
parent c43ae5b19c
commit 7301a4be0e
2 changed files with 56 additions and 49 deletions

View File

@ -31,72 +31,69 @@ import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.text.LiteralText; import net.minecraft.text.LiteralText;
import net.szum123321.textile_backup.Statics; import net.szum123321.textile_backup.Statics;
import net.szum123321.textile_backup.core.restore.AwaitThread;
import net.szum123321.textile_backup.core.restore.RestoreHelper; import net.szum123321.textile_backup.core.restore.RestoreHelper;
import java.io.File;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter; import java.util.Optional;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
public class RestoreBackupCommand { public class RestoreBackupCommand {
private final static DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd_HH.mm.ss");
public static LiteralArgumentBuilder<ServerCommandSource> register() { public static LiteralArgumentBuilder<ServerCommandSource> register() {
return CommandManager.literal("restore") return CommandManager.literal("restore")
.then(CommandManager.argument("file", StringArgumentType.word()) .then(CommandManager.argument("file", StringArgumentType.word())
.suggests(new FileSuggestionProvider()) .suggests(new FileSuggestionProvider())
.executes(RestoreBackupCommand::execute) .executes(ctx -> execute(
StringArgumentType.getString(ctx, "file"),
null,
ctx.getSource()
))
).then(CommandManager.argument("file", StringArgumentType.word()) ).then(CommandManager.argument("file", StringArgumentType.word())
.suggests(new FileSuggestionProvider()) .suggests(new FileSuggestionProvider())
.then(CommandManager.argument("comment", StringArgumentType.word()) .then(CommandManager.argument("comment", StringArgumentType.word())
.executes(RestoreBackupCommand::executeWithCommand) .executes(ctx -> execute(
StringArgumentType.getString(ctx, "file"),
StringArgumentType.getString(ctx, "comment"),
ctx.getSource()
))
) )
); ).executes(context -> {
ServerCommandSource source = context.getSource();
source.sendFeedback(new LiteralText("To restore given backup you have to provide exact creation time in format:"), false);
source.sendFeedback(new LiteralText("[YEAR]-[MONTH]-[DAY]_[HOUR].[MINUTE].[SECOND]"), false);
source.sendFeedback(new LiteralText("Example: 2020-08-05_10.58.33"), false);
return 1;
});
} }
private static int execute(CommandContext<ServerCommandSource> ctx) { private static int execute(String file, String comment, ServerCommandSource source) {
String file = StringArgumentType.getString(ctx, "file"); LocalDateTime dateTime = LocalDateTime.from(Statics.defaultDateTimeFormatter.parse(file));
LocalDateTime dateTime = LocalDateTime.from(dateTimeFormatter.parse(file));
if(ctx.getSource().getEntity() != null) Optional<File> backupFile = RestoreHelper.findFileAndLockIfPresent(dateTime, source.getMinecraftServer());
Statics.LOGGER.info("Backup restoration was initiated by: {}", ctx.getSource().getName());
else
Statics.LOGGER.info("Backup restoration was initiated form Server Console");
if(Statics.restoreAwaitThread == null || !Statics.restoreAwaitThread.isAlive()) { if(backupFile.isPresent())
Statics.restoreAwaitThread = new AwaitThread( Statics.LOGGER.info("Found file to restore {}", backupFile.get().getName());
Statics.CONFIG.restoreDelay, else {
RestoreHelper.create(dateTime, ctx.getSource().getMinecraftServer(), null) Statics.LOGGER.info("No file created on {} was found!", dateTime.format(Statics.defaultDateTimeFormatter));
); Statics.LOGGER.sendInfo(source, "No file created on {} was found!", dateTime.format(Statics.defaultDateTimeFormatter));
Statics.restoreAwaitThread.start(); return 0;
} else if(Statics.restoreAwaitThread != null && Statics.restoreAwaitThread.isAlive()) {
ctx.getSource().sendFeedback(new LiteralText("Someone has already started another restoration."), false);
} }
return 1; if(source.getEntity() != null)
} Statics.LOGGER.info("Backup restoration was initiated by: {}", source.getName());
private static int executeWithCommand(CommandContext<ServerCommandSource> ctx) {
String file = StringArgumentType.getString(ctx, "file");
String comment = StringArgumentType.getString(ctx, "comment");
LocalDateTime dateTime = LocalDateTime.from(dateTimeFormatter.parse(file));
if(ctx.getSource().getEntity() != null)
Statics.LOGGER.info("Backup restoration was initiated by: {}", ctx.getSource().getName());
else else
Statics.LOGGER.info("Backup restoration was initiated form Server Console"); Statics.LOGGER.info("Backup restoration was initiated form Server Console");
if(Statics.restoreAwaitThread == null || !Statics.restoreAwaitThread.isAlive()) { if(Statics.restoreAwaitThread == null || !Statics.restoreAwaitThread.isAlive()) {
Statics.restoreAwaitThread = new AwaitThread( Statics.restoreAwaitThread = RestoreHelper.create(backupFile.get(), source.getMinecraftServer(), comment);
Statics.CONFIG.restoreDelay,
RestoreHelper.create(dateTime, ctx.getSource().getMinecraftServer(), comment)
);
Statics.restoreAwaitThread.start(); Statics.restoreAwaitThread.start();
} else if(Statics.restoreAwaitThread != null && Statics.restoreAwaitThread.isAlive()) { } else if(Statics.restoreAwaitThread != null && Statics.restoreAwaitThread.isAlive()) {
ctx.getSource().sendFeedback(new LiteralText("Someone has already started another restoration."), false); source.sendFeedback(new LiteralText("Someone has already started another restoration."), false);
return 0;
} }
return 1; return 1;
@ -108,7 +105,7 @@ public class RestoreBackupCommand {
String remaining = builder.getRemaining(); String remaining = builder.getRemaining();
for(RestoreHelper.RestoreableFile file : RestoreHelper.getAvailableBackups(ctx.getSource().getMinecraftServer())) { for(RestoreHelper.RestoreableFile file : RestoreHelper.getAvailableBackups(ctx.getSource().getMinecraftServer())) {
String formattedCreationTime = file.getCreationTime().format(dateTimeFormatter); String formattedCreationTime = file.getCreationTime().format(Statics.defaultDateTimeFormatter);
if(formattedCreationTime.startsWith(remaining)) { if(formattedCreationTime.startsWith(remaining)) {
if(ctx.getSource().getEntity() != null) { //was typed by player if(ctx.getSource().getEntity() != null) { //was typed by player

View File

@ -28,23 +28,34 @@ import java.time.LocalDateTime;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import java.util.Optional;
import java.util.stream.Collectors; import java.util.stream.Collectors;
public class RestoreHelper { public class RestoreHelper {
public static Runnable create(LocalDateTime backupTime, MinecraftServer server, String comment) { public static Optional<File> findFileAndLockIfPresent(LocalDateTime backupTime, MinecraftServer server) {
File backupFile = Arrays.stream(Utilities.getBackupRootPath(Utilities.getLevelName(server)) File root = Utilities.getBackupRootPath(Utilities.getLevelName(server));
.listFiles())
.filter(file -> Utilities.getFileCreationTime(file).isPresent())
.filter(file -> Utilities.getFileCreationTime(file).get().equals(backupTime))
.findFirst()
.orElseThrow(() -> new NoSuchElementException("Couldn't find given backup file!"));
Optional<File> optionalFile = Arrays.stream(root.listFiles())
.filter(File::isFile)
.filter(Utilities::isValid)
.filter(file -> Utilities.getFileCreationTime(file).get().equals(backupTime))
.findFirst();
optionalFile.ifPresent(file -> Statics.untouchableFile = file);
return optionalFile;
}
public static AwaitThread create(File backupFile, MinecraftServer server, String comment) {
server.getPlayerManager().getPlayerList() server.getPlayerManager().getPlayerList()
.forEach(serverPlayerEntity -> serverPlayerEntity.sendMessage(new LiteralText("Warning! The server is going to shut down in " + Statics.CONFIG.restoreDelay + " seconds!"), false)); .forEach(serverPlayerEntity -> serverPlayerEntity.sendMessage(new LiteralText("Warning! The server is going to shut down in " + Statics.CONFIG.restoreDelay + " seconds!"), false));
Statics.globalShutdownBackupFlag.set(false); Statics.globalShutdownBackupFlag.set(false);
return new RestoreBackupRunnable(server, backupFile, comment); return new AwaitThread(
Statics.CONFIG.restoreDelay,
new RestoreBackupRunnable(server, backupFile, comment)
);
} }
public static List<RestoreableFile> getAvailableBackups(MinecraftServer server) { public static List<RestoreableFile> getAvailableBackups(MinecraftServer server) {
@ -52,8 +63,7 @@ public class RestoreHelper {
return Arrays.stream(root.listFiles()) return Arrays.stream(root.listFiles())
.filter(File::isFile) .filter(File::isFile)
.filter(file -> Utilities.getFileExtension(file.getName()).isPresent()) .filter(Utilities::isValid)
.filter(file -> Utilities.getFileCreationTime(file).isPresent())
.map(RestoreableFile::new) .map(RestoreableFile::new)
.collect(Collectors.toList()); .collect(Collectors.toList());
} }