From a9b443279550b2df33b7b7b3a690a2decd69d19f Mon Sep 17 00:00:00 2001 From: Risto Lahtela <24460436+Rsl1122@users.noreply.github.com> Date: Sat, 20 Jun 2020 21:08:19 +0300 Subject: [PATCH] Registered /plan db remove command --- .../djrapitops/plan/commands/PlanCommand.java | 12 ++ .../commands/subcommands/Confirmation.java | 1 + .../subcommands/DatabaseCommands.java | 57 +++++++ .../manage/ManageRemoveCommand.java | 139 ------------------ 4 files changed, 70 insertions(+), 139 deletions(-) delete mode 100644 Plan/common/src/main/java/com/djrapitops/plan/commands/subcommands/manage/ManageRemoveCommand.java diff --git a/Plan/common/src/main/java/com/djrapitops/plan/commands/PlanCommand.java b/Plan/common/src/main/java/com/djrapitops/plan/commands/PlanCommand.java index 2f799421c..987d61390 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/commands/PlanCommand.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/commands/PlanCommand.java @@ -252,6 +252,7 @@ public class PlanCommand { .subcommand(restoreCommand()) .subcommand(moveCommand()) .subcommand(clearCommand()) + .subcommand(removeCommand()) .inDepthDescription("Use different database subcommands to change the data in some way") .build(); } @@ -301,4 +302,15 @@ public class PlanCommand { .onCommand((sender, arguments) -> databaseCommands.onClear(commandName, sender, arguments)) .build(); } + + private Subcommand removeCommand() { + return Subcommand.builder() + .aliases("remove") + .requirePermission("plan.data.remove") + .requiredArgument("name/uuid", "Identifier for a player that will be removed from current database.") + .description("Remove player's data from Current database.") + .inDepthDescription("Removes all data linked to a player from the Current database.") + .onCommand((sender, arguments) -> databaseCommands.onRemove(commandName, sender, arguments)) + .build(); + } } diff --git a/Plan/common/src/main/java/com/djrapitops/plan/commands/subcommands/Confirmation.java b/Plan/common/src/main/java/com/djrapitops/plan/commands/subcommands/Confirmation.java index 80f8bd6be..0c01ec9b8 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/commands/subcommands/Confirmation.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/commands/subcommands/Confirmation.java @@ -28,6 +28,7 @@ public class Confirmation { } public void confirm(CMDSender sender, Consumer confirmation) { + if (awaiting.getIfPresent(sender) != null) onCancel(sender); awaiting.put(sender, confirmation); } diff --git a/Plan/common/src/main/java/com/djrapitops/plan/commands/subcommands/DatabaseCommands.java b/Plan/common/src/main/java/com/djrapitops/plan/commands/subcommands/DatabaseCommands.java index c3c7b1064..33df23656 100644 --- a/Plan/common/src/main/java/com/djrapitops/plan/commands/subcommands/DatabaseCommands.java +++ b/Plan/common/src/main/java/com/djrapitops/plan/commands/subcommands/DatabaseCommands.java @@ -21,6 +21,7 @@ import com.djrapitops.plan.commands.use.CMDSender; import com.djrapitops.plan.delivery.formatting.Formatter; import com.djrapitops.plan.delivery.formatting.Formatters; import com.djrapitops.plan.exceptions.database.DBOpException; +import com.djrapitops.plan.identification.Identifiers; import com.djrapitops.plan.query.QuerySvc; import com.djrapitops.plan.settings.locale.Locale; import com.djrapitops.plan.settings.locale.lang.CommandLang; @@ -31,6 +32,7 @@ import com.djrapitops.plan.storage.database.Database; import com.djrapitops.plan.storage.database.SQLiteDB; import com.djrapitops.plan.storage.database.transactions.BackupCopyTransaction; import com.djrapitops.plan.storage.database.transactions.commands.RemoveEverythingTransaction; +import com.djrapitops.plan.storage.database.transactions.commands.RemovePlayerTransaction; import com.djrapitops.plan.storage.file.PlanFiles; import com.djrapitops.plan.utilities.logging.ErrorContext; import com.djrapitops.plan.utilities.logging.ErrorLogger; @@ -41,6 +43,7 @@ import javax.inject.Inject; import javax.inject.Singleton; import java.io.File; import java.util.Collections; +import java.util.UUID; import java.util.concurrent.ExecutionException; @Singleton @@ -53,6 +56,7 @@ public class DatabaseCommands { private final DBSystem dbSystem; private final SQLiteDB.Factory sqliteFactory; private final QuerySvc queryService; + private final Identifiers identifiers; private final PluginStatusCommands statusCommands; private final ErrorLogger errorLogger; @@ -68,6 +72,7 @@ public class DatabaseCommands { SQLiteDB.Factory sqliteFactory, QuerySvc queryService, Formatters formatters, + Identifiers identifiers, PluginStatusCommands statusCommands, ErrorLogger errorLogger ) { @@ -78,6 +83,7 @@ public class DatabaseCommands { this.dbSystem = dbSystem; this.sqliteFactory = sqliteFactory; this.queryService = queryService; + this.identifiers = identifiers; this.statusCommands = statusCommands; this.errorLogger = errorLogger; @@ -302,4 +308,55 @@ public class DatabaseCommands { errorLogger.log(L.ERROR, e, ErrorContext.builder().related(sender, fromDB.getName()).build()); } } + + public void onRemove(String mainCommand, CMDSender sender, Arguments arguments) { + String identifier = arguments.concatenate(" "); + UUID playerUUID = identifiers.getPlayerUUID(identifier); + if (playerUUID == null) { + throw new IllegalArgumentException("Player '" + identifier + "' was not found, they have no UUID."); + } + + Database database = dbSystem.getDatabase(); + + if (sender.isPlayer()) { + sender.buildMessage() + .addPart(colors.getMainColor() + "You are about to remove data of " + playerUUID + " from " + database.getType().getName()).newLine() + .addPart("Confirm: ").addPart("§2§l[\u2714]").command("/" + mainCommand + " accept") + .addPart(" ") + .addPart("§4§l[\u2718]").command("/" + mainCommand + " cancel") + .send(); + } else { + sender.buildMessage() + .addPart(colors.getMainColor() + "You are about to remove data of " + playerUUID + " from " + database.getType().getName()).newLine() + .addPart("Confirm: ").addPart("§a/" + mainCommand + " accept") + .addPart(" ") + .addPart("§c/" + mainCommand + " cancel") + .send(); + } + + confirmation.confirm(sender, choice -> { + if (choice) { + performRemoval(sender, database, playerUUID); + } else { + sender.send(colors.getMainColor() + "Cancelled. No data was changed."); + } + }); + } + + private void performRemoval(CMDSender sender, Database database, UUID playerToRemove) { + try { + sender.send("Removing data of " + playerToRemove + " from " + database.getType().getName() + ".."); + + queryService.playerRemoved(playerToRemove); + database.executeTransaction(new RemovePlayerTransaction(playerToRemove)) + .get(); // Wait for completion + + sender.send(locale.getString(ManageLang.PROGRESS_SUCCESS)); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } catch (DBOpException | ExecutionException e) { + sender.send(locale.getString(ManageLang.PROGRESS_FAIL, e.getMessage())); + errorLogger.log(L.ERROR, e, ErrorContext.builder().related(sender, database.getType().getName(), playerToRemove).build()); + } + } } diff --git a/Plan/common/src/main/java/com/djrapitops/plan/commands/subcommands/manage/ManageRemoveCommand.java b/Plan/common/src/main/java/com/djrapitops/plan/commands/subcommands/manage/ManageRemoveCommand.java deleted file mode 100644 index 33eda8507..000000000 --- a/Plan/common/src/main/java/com/djrapitops/plan/commands/subcommands/manage/ManageRemoveCommand.java +++ /dev/null @@ -1,139 +0,0 @@ -/* - * This file is part of Player Analytics (Plan). - * - * Plan is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License v3 as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Plan 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 Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with Plan. If not, see . - */ -package com.djrapitops.plan.commands.subcommands.manage; - -import com.djrapitops.plan.exceptions.database.DBOpException; -import com.djrapitops.plan.identification.UUIDUtility; -import com.djrapitops.plan.processing.Processing; -import com.djrapitops.plan.query.QuerySvc; -import com.djrapitops.plan.settings.Permissions; -import com.djrapitops.plan.settings.locale.Locale; -import com.djrapitops.plan.settings.locale.lang.CmdHelpLang; -import com.djrapitops.plan.settings.locale.lang.CommandLang; -import com.djrapitops.plan.settings.locale.lang.DeepHelpLang; -import com.djrapitops.plan.settings.locale.lang.ManageLang; -import com.djrapitops.plan.storage.database.DBSystem; -import com.djrapitops.plan.storage.database.Database; -import com.djrapitops.plan.storage.database.queries.PlayerFetchQueries; -import com.djrapitops.plan.storage.database.transactions.commands.RemovePlayerTransaction; -import com.djrapitops.plan.utilities.MiscUtils; -import com.djrapitops.plan.utilities.logging.ErrorLogger; -import com.djrapitops.plugin.command.CommandNode; -import com.djrapitops.plugin.command.CommandType; -import com.djrapitops.plugin.command.Sender; -import com.djrapitops.plugin.logging.L; -import com.djrapitops.plugin.utilities.Verify; - -import javax.inject.Inject; -import javax.inject.Singleton; -import java.util.Arrays; -import java.util.UUID; -import java.util.concurrent.ExecutionException; - -/** - * This manage subcommand is used to remove a single player's data from the - * dbSystem. - * - * @author Rsl1122 - */ -@Singleton -public class ManageRemoveCommand extends CommandNode { - - private final Locale locale; - private final Processing processing; - private final DBSystem dbSystem; - private final QuerySvc queryService; - private final UUIDUtility uuidUtility; - private final ErrorLogger errorLogger; - - @Inject - public ManageRemoveCommand( - Locale locale, - Processing processing, - DBSystem dbSystem, - QuerySvc queryService, - UUIDUtility uuidUtility, - ErrorLogger errorLogger - ) { - super("remove|delete", Permissions.MANAGE.getPermission(), CommandType.PLAYER_OR_ARGS); - - this.locale = locale; - this.processing = processing; - this.dbSystem = dbSystem; - this.queryService = queryService; - this.uuidUtility = uuidUtility; - this.errorLogger = errorLogger; - - setArguments("", "[-a]"); - setShortHelp(locale.getString(CmdHelpLang.MANAGE_REMOVE)); - setInDepthHelp(locale.getArray(DeepHelpLang.MANAGE_REMOVE)); - } - - @Override - public void onCommand(Sender sender, String commandLabel, String[] args) { - Verify.isTrue(args.length >= 1, - () -> new IllegalArgumentException(locale.getString(CommandLang.FAIL_REQ_ONE_ARG, Arrays.toString(this.getArguments())))); - - String playerName = MiscUtils.getPlayerName(args, sender, Permissions.MANAGE); - - if (playerName == null) { - sender.sendMessage(locale.getString(CommandLang.FAIL_NO_PERMISSION)); - return; - } - - runRemoveTask(playerName, sender, args); - } - - private void runRemoveTask(String playerName, Sender sender, String[] args) { - processing.submitCritical(() -> { - try { - UUID playerUUID = uuidUtility.getUUIDOf(playerName); - - if (playerUUID == null) { - sender.sendMessage(locale.getString(CommandLang.FAIL_USERNAME_NOT_VALID)); - return; - } - - Database db = dbSystem.getDatabase(); - if (!db.query(PlayerFetchQueries.isPlayerRegistered(playerUUID))) { - sender.sendMessage(locale.getString(CommandLang.FAIL_USERNAME_NOT_KNOWN)); - return; - } - - if (!Verify.contains("-a", args)) { - sender.sendMessage( - locale.getString(ManageLang.CONFIRMATION, - locale.getString(ManageLang.CONFIRM_REMOVAL, db.getType().getName()) - ) - ); - return; - } - - sender.sendMessage(locale.getString(ManageLang.PROGRESS_START)); - queryService.playerRemoved(playerUUID); - db.executeTransaction(new RemovePlayerTransaction(playerUUID)) - .get(); // Wait for completion - sender.sendMessage(locale.getString(ManageLang.PROGRESS_SUCCESS)); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - } catch (DBOpException | ExecutionException e) { - errorLogger.log(L.ERROR, this.getClass(), e); - sender.sendMessage(locale.getString(ManageLang.PROGRESS_FAIL, e.getMessage())); - } - }); - } -}