Added Delete command.
Rearranged command package
This commit is contained in:
parent
d29b9049a4
commit
7f82664ae7
@ -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())
|
||||
));
|
||||
}
|
||||
|
@ -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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
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("^");
|
||||
});
|
||||
}
|
@ -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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
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<ServerCommandSource> {
|
||||
private static final FileSuggestionProvider INSTANCE = new FileSuggestionProvider();
|
||||
|
||||
public static FileSuggestionProvider Instance() {
|
||||
return INSTANCE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompletableFuture<Suggestions> getSuggestions(CommandContext<ServerCommandSource> 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();
|
||||
}
|
||||
}
|
@ -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;
|
@ -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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
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<ServerCommandSource> 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<File> 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;
|
||||
}
|
||||
}
|
@ -16,7 +16,7 @@
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
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;
|
@ -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;
|
@ -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<ServerCommandSource> 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<RestoreHelper.RestoreableFile> backupFile = RestoreHelper.findFileAndLockIfPresent(dateTime, source.getMinecraftServer());
|
||||
@ -123,31 +103,4 @@ public class RestoreBackupCommand {
|
||||
}
|
||||
}
|
||||
|
||||
private static final class FileSuggestionProvider implements SuggestionProvider<ServerCommandSource> {
|
||||
@Override
|
||||
public CompletableFuture<Suggestions> getSuggestions(CommandContext<ServerCommandSource> 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user