From 7f82664ae7c80aa1249285e004981c6b39316cef Mon Sep 17 00:00:00 2001 From: szymon Date: Sat, 5 Dec 2020 22:17:22 +0100 Subject: [PATCH] Added Delete command. Rearranged command package --- .../textile_backup/TextileBackup.java | 8 +- .../commands/CommandExceptions.java | 39 +++++++++ .../commands/FileSuggestionProvider.java | 67 +++++++++++++++ .../BlacklistCommand.java | 2 +- .../commands/manage/DeleteCommand.java | 84 +++++++++++++++++++ .../ListBackupsCommand.java | 2 +- .../WhitelistCommand.java | 2 +- .../restore/RestoreBackupCommand.java | 57 ++----------- 8 files changed, 203 insertions(+), 58 deletions(-) create mode 100644 src/main/java/net/szum123321/textile_backup/commands/CommandExceptions.java create mode 100644 src/main/java/net/szum123321/textile_backup/commands/FileSuggestionProvider.java rename src/main/java/net/szum123321/textile_backup/commands/{permission => manage}/BlacklistCommand.java (98%) create mode 100644 src/main/java/net/szum123321/textile_backup/commands/manage/DeleteCommand.java rename src/main/java/net/szum123321/textile_backup/commands/{restore => manage}/ListBackupsCommand.java (97%) rename src/main/java/net/szum123321/textile_backup/commands/{permission => manage}/WhitelistCommand.java (98%) diff --git a/src/main/java/net/szum123321/textile_backup/TextileBackup.java b/src/main/java/net/szum123321/textile_backup/TextileBackup.java index 9887ca3..c8d75a9 100644 --- a/src/main/java/net/szum123321/textile_backup/TextileBackup.java +++ b/src/main/java/net/szum123321/textile_backup/TextileBackup.java @@ -28,10 +28,11 @@ import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents; import net.minecraft.server.command.ServerCommandSource; import net.szum123321.textile_backup.commands.create.CleanupCommand; import net.szum123321.textile_backup.commands.create.StartBackupCommand; -import net.szum123321.textile_backup.commands.permission.BlacklistCommand; -import net.szum123321.textile_backup.commands.permission.WhitelistCommand; +import net.szum123321.textile_backup.commands.manage.BlacklistCommand; +import net.szum123321.textile_backup.commands.manage.DeleteCommand; +import net.szum123321.textile_backup.commands.manage.WhitelistCommand; import net.szum123321.textile_backup.commands.restore.KillRestoreCommand; -import net.szum123321.textile_backup.commands.restore.ListBackupsCommand; +import net.szum123321.textile_backup.commands.manage.ListBackupsCommand; import net.szum123321.textile_backup.commands.restore.RestoreBackupCommand; import net.szum123321.textile_backup.core.ActionInitiator; import net.szum123321.textile_backup.core.Utilities; @@ -108,6 +109,7 @@ public class TextileBackup implements ModInitializer { .then(BlacklistCommand.register()) .then(RestoreBackupCommand.register()) .then(ListBackupsCommand.register()) + .then(DeleteCommand.register()) .then(KillRestoreCommand.register()) )); } diff --git a/src/main/java/net/szum123321/textile_backup/commands/CommandExceptions.java b/src/main/java/net/szum123321/textile_backup/commands/CommandExceptions.java new file mode 100644 index 0000000..42accf0 --- /dev/null +++ b/src/main/java/net/szum123321/textile_backup/commands/CommandExceptions.java @@ -0,0 +1,39 @@ +/* + * A simple backup mod for Fabric + * Copyright (C) 2020 Szum123321 + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.szum123321.textile_backup.commands; + +import com.mojang.brigadier.exceptions.DynamicCommandExceptionType; +import net.minecraft.text.LiteralText; +import net.minecraft.text.MutableText; + +import java.time.format.DateTimeParseException; + +public class CommandExceptions { + public static final DynamicCommandExceptionType DATE_TIME_PARSE_COMMAND_EXCEPTION_TYPE = new DynamicCommandExceptionType(o -> { + DateTimeParseException e = (DateTimeParseException)o; + + MutableText message = new LiteralText("An exception occurred while trying to parse:\n") + .append(e.getParsedString()) + .append("\n"); + + for (int i = 0; i < e.getErrorIndex(); i++) message.append(" "); + + return message.append("^"); + }); +} diff --git a/src/main/java/net/szum123321/textile_backup/commands/FileSuggestionProvider.java b/src/main/java/net/szum123321/textile_backup/commands/FileSuggestionProvider.java new file mode 100644 index 0000000..eb2534f --- /dev/null +++ b/src/main/java/net/szum123321/textile_backup/commands/FileSuggestionProvider.java @@ -0,0 +1,67 @@ +/* + * A simple backup mod for Fabric + * Copyright (C) 2020 Szum123321 + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.szum123321.textile_backup.commands; + +import com.mojang.brigadier.LiteralMessage; +import com.mojang.brigadier.context.CommandContext; +import com.mojang.brigadier.exceptions.CommandSyntaxException; +import com.mojang.brigadier.suggestion.SuggestionProvider; +import com.mojang.brigadier.suggestion.Suggestions; +import com.mojang.brigadier.suggestion.SuggestionsBuilder; +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.server.command.ServerCommandSource; +import net.szum123321.textile_backup.Statics; +import net.szum123321.textile_backup.core.restore.RestoreHelper; +import org.lwjgl.system.CallbackI; + +import java.util.concurrent.CompletableFuture; + +public final class FileSuggestionProvider implements SuggestionProvider { + private static final FileSuggestionProvider INSTANCE = new FileSuggestionProvider(); + + public static FileSuggestionProvider Instance() { + return INSTANCE; + } + + @Override + public CompletableFuture getSuggestions(CommandContext ctx, SuggestionsBuilder builder) throws CommandSyntaxException { + String remaining = builder.getRemaining(); + + for (RestoreHelper.RestoreableFile file : RestoreHelper.getAvailableBackups(ctx.getSource().getMinecraftServer())) { + String formattedCreationTime = file.getCreationTime().format(Statics.defaultDateTimeFormatter); + + if (formattedCreationTime.startsWith(remaining)) { + if (ctx.getSource().getEntity() instanceof PlayerEntity) { //was typed by player + if (file.getComment() != null) { + builder.suggest(formattedCreationTime, new LiteralMessage("Comment: " + file.getComment())); + } else { + builder.suggest(formattedCreationTime); + } + } else { //was typed from server console + if (file.getComment() != null) { + builder.suggest(file.getCreationTime() + "#" + file.getComment()); + } else { + builder.suggest(formattedCreationTime); + } + } + } + } + return builder.buildFuture(); + } +} diff --git a/src/main/java/net/szum123321/textile_backup/commands/permission/BlacklistCommand.java b/src/main/java/net/szum123321/textile_backup/commands/manage/BlacklistCommand.java similarity index 98% rename from src/main/java/net/szum123321/textile_backup/commands/permission/BlacklistCommand.java rename to src/main/java/net/szum123321/textile_backup/commands/manage/BlacklistCommand.java index cc6baf4..b8960b3 100644 --- a/src/main/java/net/szum123321/textile_backup/commands/permission/BlacklistCommand.java +++ b/src/main/java/net/szum123321/textile_backup/commands/manage/BlacklistCommand.java @@ -1,4 +1,4 @@ -package net.szum123321.textile_backup.commands.permission; +package net.szum123321.textile_backup.commands.manage; import com.mojang.brigadier.builder.LiteralArgumentBuilder; import com.mojang.brigadier.context.CommandContext; diff --git a/src/main/java/net/szum123321/textile_backup/commands/manage/DeleteCommand.java b/src/main/java/net/szum123321/textile_backup/commands/manage/DeleteCommand.java new file mode 100644 index 0000000..48ce7ac --- /dev/null +++ b/src/main/java/net/szum123321/textile_backup/commands/manage/DeleteCommand.java @@ -0,0 +1,84 @@ +/* + * A simple backup mod for Fabric + * Copyright (C) 2020 Szum123321 + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.szum123321.textile_backup.commands.manage; + +import com.mojang.brigadier.arguments.StringArgumentType; +import com.mojang.brigadier.builder.LiteralArgumentBuilder; +import com.mojang.brigadier.exceptions.CommandSyntaxException; +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.server.command.CommandManager; +import net.minecraft.server.command.ServerCommandSource; +import net.szum123321.textile_backup.commands.CommandExceptions; +import net.szum123321.textile_backup.Statics; +import net.szum123321.textile_backup.commands.FileSuggestionProvider; +import net.szum123321.textile_backup.core.Utilities; + +import java.io.File; +import java.time.LocalDateTime; +import java.time.format.DateTimeParseException; +import java.util.Arrays; +import java.util.Optional; + +public class DeleteCommand { + public static LiteralArgumentBuilder register() { + return CommandManager.literal("delete") + .then(CommandManager.argument("file", StringArgumentType.word()) + .suggests(FileSuggestionProvider.Instance()) + .executes(ctx -> execute(ctx.getSource(), StringArgumentType.getString(ctx, "file"))) + ); + } + + private static int execute(ServerCommandSource source, String fileName) throws CommandSyntaxException { + LocalDateTime dateTime; + + try { + dateTime = LocalDateTime.from(Statics.defaultDateTimeFormatter.parse(fileName)); + } catch (DateTimeParseException e) { + throw CommandExceptions.DATE_TIME_PARSE_COMMAND_EXCEPTION_TYPE.create(e); + } + + File root = Utilities.getBackupRootPath(Utilities.getLevelName(source.getMinecraftServer())); + + Optional optionalFile = Arrays.stream(root.listFiles()) + .filter(Utilities::isValidBackup) + .filter(file -> Utilities.getFileCreationTime(file).orElse(LocalDateTime.MIN).equals(dateTime)) + .findFirst(); + + if(optionalFile.isPresent()) { + if(Statics.untouchableFile == null || (Statics.untouchableFile != null && !Statics.untouchableFile.equals(optionalFile.get()))) { + if(optionalFile.get().delete()) { + Statics.LOGGER.sendInfo(source, "File {} successfully deleted!", optionalFile.get().getName()); + + if(source.getEntity() instanceof PlayerEntity) + Statics.LOGGER.info("Player {} deleted {}.", source.getPlayer().getName(), optionalFile.get().getName()); + } else { + Statics.LOGGER.sendError(source, "Something went wrong while deleting file!"); + } + } else { + Statics.LOGGER.sendError(source, "Couldn't delete the file because it's being restored right now."); + Statics.LOGGER.sendHint(source, "If you want to abort restoration then use: /backup killR"); + } + } else { + Statics.LOGGER.sendError(source, "Couldn't find file by this name."); + Statics.LOGGER.sendHint(source, "Maybe try /backup list"); + } + + return 0; + } +} diff --git a/src/main/java/net/szum123321/textile_backup/commands/restore/ListBackupsCommand.java b/src/main/java/net/szum123321/textile_backup/commands/manage/ListBackupsCommand.java similarity index 97% rename from src/main/java/net/szum123321/textile_backup/commands/restore/ListBackupsCommand.java rename to src/main/java/net/szum123321/textile_backup/commands/manage/ListBackupsCommand.java index 390d32e..076770e 100644 --- a/src/main/java/net/szum123321/textile_backup/commands/restore/ListBackupsCommand.java +++ b/src/main/java/net/szum123321/textile_backup/commands/manage/ListBackupsCommand.java @@ -16,7 +16,7 @@ * along with this program. If not, see . */ -package net.szum123321.textile_backup.commands.restore; +package net.szum123321.textile_backup.commands.manage; import com.mojang.brigadier.builder.LiteralArgumentBuilder; import net.minecraft.server.command.CommandManager; diff --git a/src/main/java/net/szum123321/textile_backup/commands/permission/WhitelistCommand.java b/src/main/java/net/szum123321/textile_backup/commands/manage/WhitelistCommand.java similarity index 98% rename from src/main/java/net/szum123321/textile_backup/commands/permission/WhitelistCommand.java rename to src/main/java/net/szum123321/textile_backup/commands/manage/WhitelistCommand.java index e2bf0a2..8f5cc8f 100644 --- a/src/main/java/net/szum123321/textile_backup/commands/permission/WhitelistCommand.java +++ b/src/main/java/net/szum123321/textile_backup/commands/manage/WhitelistCommand.java @@ -1,4 +1,4 @@ -package net.szum123321.textile_backup.commands.permission; +package net.szum123321.textile_backup.commands.manage; import com.mojang.brigadier.builder.LiteralArgumentBuilder; import com.mojang.brigadier.context.CommandContext; diff --git a/src/main/java/net/szum123321/textile_backup/commands/restore/RestoreBackupCommand.java b/src/main/java/net/szum123321/textile_backup/commands/restore/RestoreBackupCommand.java index d2cb2d0..ad516f3 100644 --- a/src/main/java/net/szum123321/textile_backup/commands/restore/RestoreBackupCommand.java +++ b/src/main/java/net/szum123321/textile_backup/commands/restore/RestoreBackupCommand.java @@ -18,22 +18,15 @@ package net.szum123321.textile_backup.commands.restore; -import com.mojang.brigadier.LiteralMessage; import com.mojang.brigadier.arguments.StringArgumentType; import com.mojang.brigadier.builder.LiteralArgumentBuilder; -import com.mojang.brigadier.context.CommandContext; import com.mojang.brigadier.exceptions.CommandSyntaxException; -import com.mojang.brigadier.exceptions.DynamicCommandExceptionType; -import com.mojang.brigadier.suggestion.SuggestionProvider; -import com.mojang.brigadier.suggestion.Suggestions; -import com.mojang.brigadier.suggestion.SuggestionsBuilder; -import net.minecraft.entity.player.PlayerEntity; import net.minecraft.server.command.CommandManager; import net.minecraft.server.command.ServerCommandSource; -import net.minecraft.text.LiteralText; -import net.minecraft.text.MutableText; +import net.szum123321.textile_backup.commands.CommandExceptions; import net.szum123321.textile_backup.Statics; +import net.szum123321.textile_backup.commands.FileSuggestionProvider; import net.szum123321.textile_backup.core.restore.RestoreContext; import net.szum123321.textile_backup.core.restore.RestoreHelper; @@ -41,32 +34,19 @@ import javax.annotation.Nullable; import java.time.LocalDateTime; import java.time.format.DateTimeParseException; import java.util.Optional; -import java.util.concurrent.CompletableFuture; public class RestoreBackupCommand { - private final static DynamicCommandExceptionType DATE_TIME_PARSE_COMMAND_EXCEPTION_TYPE = new DynamicCommandExceptionType(o -> { - DateTimeParseException e = (DateTimeParseException)o; - - MutableText message = new LiteralText("An exception occurred while trying to parse:\n") - .append(e.getParsedString()) - .append("\n"); - - for (int i = 0; i < e.getErrorIndex(); i++) message.append(" "); - - return message.append("^"); - }); - public static LiteralArgumentBuilder register() { return CommandManager.literal("restore") .then(CommandManager.argument("file", StringArgumentType.word()) - .suggests(new FileSuggestionProvider()) + .suggests(FileSuggestionProvider.Instance()) .executes(ctx -> execute( StringArgumentType.getString(ctx, "file"), null, ctx.getSource() )) ).then(CommandManager.argument("file", StringArgumentType.word()) - .suggests(new FileSuggestionProvider()) + .suggests(FileSuggestionProvider.Instance()) .then(CommandManager.argument("comment", StringArgumentType.word()) .executes(ctx -> execute( StringArgumentType.getString(ctx, "file"), @@ -92,7 +72,7 @@ public class RestoreBackupCommand { try { dateTime = LocalDateTime.from(Statics.defaultDateTimeFormatter.parse(file)); } catch (DateTimeParseException e) { - throw DATE_TIME_PARSE_COMMAND_EXCEPTION_TYPE.create(e); + throw CommandExceptions.DATE_TIME_PARSE_COMMAND_EXCEPTION_TYPE.create(e); } Optional backupFile = RestoreHelper.findFileAndLockIfPresent(dateTime, source.getMinecraftServer()); @@ -123,31 +103,4 @@ public class RestoreBackupCommand { } } - private static final class FileSuggestionProvider implements SuggestionProvider { - @Override - public CompletableFuture getSuggestions(CommandContext ctx, SuggestionsBuilder builder) throws CommandSyntaxException { - String remaining = builder.getRemaining(); - - for(RestoreHelper.RestoreableFile file : RestoreHelper.getAvailableBackups(ctx.getSource().getMinecraftServer())) { - String formattedCreationTime = file.getCreationTime().format(Statics.defaultDateTimeFormatter); - - if(formattedCreationTime.startsWith(remaining)) { - if(ctx.getSource().getEntity() instanceof PlayerEntity) { //was typed by player - if(file.getComment() != null) { - builder.suggest(formattedCreationTime, new LiteralMessage("Comment: " + file.getComment())); - } else { - builder.suggest(formattedCreationTime); - } - } else { //was typed from server console - if(file.getComment() != null) { - builder.suggest(file.getCreationTime() + "#" + file.getComment()); - } else { - builder.suggest(formattedCreationTime); - } - } - } - } - return builder.buildFuture(); - } - } }