mirror of
https://github.com/plan-player-analytics/Plan.git
synced 2025-02-05 23:11:56 +01:00
More commands moved
This commit is contained in:
parent
326e07a022
commit
1945db8045
@ -16,10 +16,7 @@
|
||||
*/
|
||||
package com.djrapitops.plan.commands;
|
||||
|
||||
import com.djrapitops.plan.commands.subcommands.Confirmation;
|
||||
import com.djrapitops.plan.commands.subcommands.LinkCommands;
|
||||
import com.djrapitops.plan.commands.subcommands.PluginStatusCommands;
|
||||
import com.djrapitops.plan.commands.subcommands.RegistrationCommands;
|
||||
import com.djrapitops.plan.commands.subcommands.*;
|
||||
import com.djrapitops.plan.commands.use.Arguments;
|
||||
import com.djrapitops.plan.commands.use.CMDSender;
|
||||
import com.djrapitops.plan.commands.use.CommandWithSubcommands;
|
||||
@ -46,6 +43,7 @@ public class PlanCommand {
|
||||
private final LinkCommands linkCommands;
|
||||
private final RegistrationCommands registrationCommands;
|
||||
private final PluginStatusCommands statusCommands;
|
||||
private final DatabaseCommands databaseCommands;
|
||||
private final ErrorLogger errorLogger;
|
||||
|
||||
@Inject
|
||||
@ -57,6 +55,7 @@ public class PlanCommand {
|
||||
LinkCommands linkCommands,
|
||||
RegistrationCommands registrationCommands,
|
||||
PluginStatusCommands statusCommands,
|
||||
DatabaseCommands databaseCommands,
|
||||
ErrorLogger errorLogger
|
||||
) {
|
||||
this.commandName = commandName;
|
||||
@ -66,6 +65,7 @@ public class PlanCommand {
|
||||
this.linkCommands = linkCommands;
|
||||
this.registrationCommands = registrationCommands;
|
||||
this.statusCommands = statusCommands;
|
||||
this.databaseCommands = databaseCommands;
|
||||
this.errorLogger = errorLogger;
|
||||
}
|
||||
|
||||
@ -89,6 +89,7 @@ public class PlanCommand {
|
||||
|
||||
.subcommand(registerCommand())
|
||||
.subcommand(unregisterCommand())
|
||||
.subcommand(webUsersCommand())
|
||||
|
||||
.subcommand(acceptCommand())
|
||||
.subcommand(cancelCommand())
|
||||
@ -96,6 +97,7 @@ public class PlanCommand {
|
||||
.subcommand(infoCommand())
|
||||
.subcommand(reloadCommand())
|
||||
.subcommand(disableCommand())
|
||||
.subcommand(databaseCommand())
|
||||
.exceptionHandler(this::handleException)
|
||||
.build();
|
||||
}
|
||||
@ -228,4 +230,62 @@ public class PlanCommand {
|
||||
.onCommand(statusCommands::onDisable)
|
||||
.build();
|
||||
}
|
||||
|
||||
private Subcommand webUsersCommand() {
|
||||
return Subcommand.builder()
|
||||
.aliases("webusers", "users")
|
||||
.requirePermission("plan.register.other")
|
||||
.description("List all web users")
|
||||
.inDepthDescription("Lists web users as a table.")
|
||||
.onCommand(linkCommands::onWebUsersCommand)
|
||||
.build();
|
||||
}
|
||||
|
||||
private Subcommand databaseCommand() {
|
||||
return CommandWithSubcommands.builder()
|
||||
.aliases("database", "db")
|
||||
.requirePermission("plan.data.base")
|
||||
.optionalArgument("[subcommand]", "Use the command without subcommand to see help.")
|
||||
.description("Manage Plan database")
|
||||
.colorScheme(colors)
|
||||
.subcommand(backupCommand())
|
||||
.subcommand(restoreCommand())
|
||||
.inDepthDescription("Use different database subcommands to change the data in some way")
|
||||
.build();
|
||||
}
|
||||
|
||||
private Subcommand backupCommand() {
|
||||
return Subcommand.builder()
|
||||
.aliases("backup")
|
||||
.requirePermission("plan.data.backup")
|
||||
.optionalArgument("MySQL/SQlite/H2", "Type of the database to backup. Current database is used if not specified.")
|
||||
.description("Backup data of a database to a file")
|
||||
.inDepthDescription("Uses SQLite to backup one of the usable databases to a file.")
|
||||
.onCommand(databaseCommands::onBackup)
|
||||
.build();
|
||||
}
|
||||
|
||||
private Subcommand restoreCommand() {
|
||||
return Subcommand.builder()
|
||||
.aliases("restore")
|
||||
.requirePermission("plan.data.restore")
|
||||
.requiredArgument("backup-file", "Name of the backup file (case sensitive)")
|
||||
.optionalArgument("MySQL/SQlite/H2", "Type of the database to restore to. Current database is used if not specified.")
|
||||
.description("Restore data from a file to a database")
|
||||
.inDepthDescription("Uses SQLite to backup file and overwrites contents of the target database.")
|
||||
.onCommand((sender, arguments) -> databaseCommands.onRestore(commandName, sender, arguments))
|
||||
.build();
|
||||
}
|
||||
|
||||
private Subcommand moveCommand() {
|
||||
return Subcommand.builder()
|
||||
.aliases("move")
|
||||
.requirePermission("plan.data.move")
|
||||
.requiredArgument("MySQL/SQlite/H2", "Type of the database to move data from.")
|
||||
.requiredArgument("MySQL/SQlite/H2", "Type of the database to move data to. Can not be same as previous.")
|
||||
.description("Move data between databases")
|
||||
.inDepthDescription("Overwrites contents in the other database with the contents in another.")
|
||||
.onCommand((sender, arguments) -> databaseCommands.onMove(commandName, sender, arguments))
|
||||
.build();
|
||||
}
|
||||
}
|
||||
|
@ -16,9 +16,6 @@
|
||||
*/
|
||||
package com.djrapitops.plan.commands;
|
||||
|
||||
import com.djrapitops.plan.commands.subcommands.WebUserCommand;
|
||||
import com.djrapitops.plan.commands.subcommands.manage.ManageRawDataCommand;
|
||||
import com.djrapitops.plan.commands.subcommands.manage.ManageUninstalledCommand;
|
||||
import com.djrapitops.plan.settings.Permissions;
|
||||
import com.djrapitops.plan.settings.locale.Locale;
|
||||
import com.djrapitops.plan.settings.locale.lang.DeepHelpLang;
|
||||
@ -26,7 +23,6 @@ import com.djrapitops.plugin.command.ColorScheme;
|
||||
import com.djrapitops.plugin.command.CommandNode;
|
||||
import com.djrapitops.plugin.command.CommandType;
|
||||
import com.djrapitops.plugin.command.TreeCmdNode;
|
||||
import dagger.Lazy;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Named;
|
||||
@ -42,32 +38,21 @@ import javax.inject.Singleton;
|
||||
@Singleton
|
||||
public class PlanProxyCommand extends TreeCmdNode {
|
||||
|
||||
private final Lazy<WebUserCommand> webUserCommand;
|
||||
private final ManageRawDataCommand rawDataCommand;
|
||||
private final ManageUninstalledCommand uninstalledCommand;
|
||||
|
||||
private boolean commandsRegistered;
|
||||
|
||||
@Inject
|
||||
public PlanProxyCommand(
|
||||
@Named("mainCommandName") String mainCommandName,
|
||||
ColorScheme colorScheme,
|
||||
Locale locale,
|
||||
Locale locale
|
||||
// Group 1
|
||||
// Group 2
|
||||
Lazy<WebUserCommand> webUserCommand,
|
||||
// Group 3
|
||||
ManageRawDataCommand rawDataCommand,
|
||||
ManageUninstalledCommand uninstalledCommand
|
||||
) {
|
||||
super(mainCommandName, Permissions.MANAGE.getPermission(), CommandType.CONSOLE, null);
|
||||
this.uninstalledCommand = uninstalledCommand;
|
||||
|
||||
commandsRegistered = false;
|
||||
|
||||
this.webUserCommand = webUserCommand;
|
||||
this.rawDataCommand = rawDataCommand;
|
||||
|
||||
getHelpCommand().setPermission(Permissions.MANAGE.getPermission());
|
||||
setColorScheme(colorScheme);
|
||||
setInDepthHelp(locale.getArray(DeepHelpLang.PLAN));
|
||||
@ -86,11 +71,11 @@ public class PlanProxyCommand extends TreeCmdNode {
|
||||
CommandNode[] webGroup = {
|
||||
// registerCommand,
|
||||
// unregisterCommand,
|
||||
webUserCommand.get()
|
||||
// webUserCommand.get()
|
||||
};
|
||||
CommandNode[] manageGroup = {
|
||||
rawDataCommand,
|
||||
uninstalledCommand,
|
||||
// rawDataCommand,
|
||||
// uninstalledCommand,
|
||||
//reloadCommand,
|
||||
//disableCommand
|
||||
};
|
||||
|
@ -0,0 +1,243 @@
|
||||
/*
|
||||
* 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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.djrapitops.plan.commands.subcommands;
|
||||
|
||||
import com.djrapitops.plan.commands.use.Arguments;
|
||||
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.settings.locale.Locale;
|
||||
import com.djrapitops.plan.settings.locale.lang.CommandLang;
|
||||
import com.djrapitops.plan.settings.locale.lang.ManageLang;
|
||||
import com.djrapitops.plan.storage.database.DBSystem;
|
||||
import com.djrapitops.plan.storage.database.DBType;
|
||||
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.file.PlanFiles;
|
||||
import com.djrapitops.plan.utilities.logging.ErrorContext;
|
||||
import com.djrapitops.plan.utilities.logging.ErrorLogger;
|
||||
import com.djrapitops.plugin.command.ColorScheme;
|
||||
import com.djrapitops.plugin.logging.L;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Singleton;
|
||||
import java.io.File;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
@Singleton
|
||||
public class DatabaseCommands {
|
||||
|
||||
private final Locale locale;
|
||||
private final Confirmation confirmation;
|
||||
private final ColorScheme colors;
|
||||
private final PlanFiles files;
|
||||
private final DBSystem dbSystem;
|
||||
private final SQLiteDB.Factory sqliteFactory;
|
||||
private final ErrorLogger errorLogger;
|
||||
|
||||
private final Formatter<Long> timestamp;
|
||||
|
||||
@Inject
|
||||
public DatabaseCommands(
|
||||
Locale locale,
|
||||
Confirmation confirmation,
|
||||
ColorScheme colors,
|
||||
PlanFiles files,
|
||||
DBSystem dbSystem,
|
||||
SQLiteDB.Factory sqliteFactory,
|
||||
Formatters formatters,
|
||||
ErrorLogger errorLogger
|
||||
) {
|
||||
this.locale = locale;
|
||||
this.confirmation = confirmation;
|
||||
this.colors = colors;
|
||||
this.files = files;
|
||||
this.dbSystem = dbSystem;
|
||||
this.sqliteFactory = sqliteFactory;
|
||||
this.errorLogger = errorLogger;
|
||||
|
||||
this.timestamp = formatters.iso8601NoClockLong();
|
||||
}
|
||||
|
||||
public void onBackup(CMDSender sender, Arguments arguments) {
|
||||
String dbName = arguments.get(0)
|
||||
.orElse(dbSystem.getDatabase().getType().getName())
|
||||
.toLowerCase();
|
||||
|
||||
if (!DBType.exists(dbName)) {
|
||||
throw new IllegalArgumentException(locale.getString(ManageLang.FAIL_INCORRECT_DB, dbName));
|
||||
}
|
||||
|
||||
Database fromDB = dbSystem.getActiveDatabaseByName(dbName);
|
||||
if (fromDB.getState() != Database.State.OPEN) fromDB.init();
|
||||
|
||||
performBackup(sender, arguments, dbName, fromDB);
|
||||
sender.send(locale.getString(ManageLang.PROGRESS_SUCCESS));
|
||||
}
|
||||
|
||||
public void performBackup(CMDSender sender, Arguments arguments, String dbName, Database fromDB) {
|
||||
Database toDB = null;
|
||||
try {
|
||||
String timeStamp = timestamp.apply(System.currentTimeMillis());
|
||||
String fileName = dbName + "-backup-" + timeStamp;
|
||||
sender.send("Creating a backup file '" + fileName + ".db' with contents of " + dbName);
|
||||
toDB = sqliteFactory.usingFileCalled(fileName);
|
||||
toDB.init();
|
||||
toDB.executeTransaction(new BackupCopyTransaction(fromDB, toDB)).get();
|
||||
} catch (DBOpException | ExecutionException e) {
|
||||
errorLogger.log(L.ERROR, e, ErrorContext.builder().related(sender, arguments).build());
|
||||
} catch (InterruptedException e) {
|
||||
toDB.close();
|
||||
Thread.currentThread().interrupt();
|
||||
} finally {
|
||||
if (toDB != null) {
|
||||
toDB.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void onRestore(String mainCommand, CMDSender sender, Arguments arguments) {
|
||||
String backupDbName = arguments.get(0)
|
||||
.orElseThrow(() -> new IllegalArgumentException(locale.getString(CommandLang.FAIL_REQ_ARGS, 1, "<backup-file>")));
|
||||
|
||||
boolean containsDBFileExtension = backupDbName.endsWith(".db");
|
||||
File backupDBFile = files.getFileFromPluginFolder(backupDbName + (containsDBFileExtension ? "" : ".db"));
|
||||
|
||||
if (!backupDBFile.exists()) {
|
||||
throw new IllegalArgumentException(locale.getString(ManageLang.FAIL_FILE_NOT_FOUND, backupDBFile.getAbsolutePath()));
|
||||
}
|
||||
|
||||
String dbName = arguments.get(1)
|
||||
.orElse(dbSystem.getDatabase().getType().getName())
|
||||
.toLowerCase();
|
||||
if (!DBType.exists(dbName)) {
|
||||
throw new IllegalArgumentException(locale.getString(ManageLang.FAIL_INCORRECT_DB, dbName));
|
||||
}
|
||||
|
||||
Database toDB = dbSystem.getActiveDatabaseByName(dbName);
|
||||
|
||||
// Check against restoring from database.db as it is active database
|
||||
if (backupDbName.contains("database") && toDB instanceof SQLiteDB) {
|
||||
throw new IllegalArgumentException(locale.getString(ManageLang.FAIL_SAME_DB));
|
||||
}
|
||||
|
||||
if (toDB.getState() != Database.State.OPEN) toDB.init();
|
||||
|
||||
if (sender.isPlayer()) {
|
||||
sender.buildMessage()
|
||||
.addPart(colors.getMainColor() + "You are about to overwrite data in Plan " + toDB.getType().getName() + " with data in " + backupDBFile.toPath()).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 overwrite data in Plan " + toDB.getType().getName() + " with data in " + backupDBFile.toPath()).newLine()
|
||||
.addPart("Confirm: ").addPart("§a/" + mainCommand + " accept")
|
||||
.addPart(" ")
|
||||
.addPart("§c/" + mainCommand + " cancel")
|
||||
.send();
|
||||
}
|
||||
|
||||
confirmation.confirm(sender, choice -> {
|
||||
if (choice) {
|
||||
performRestore(sender, backupDBFile, toDB);
|
||||
} else {
|
||||
sender.send(colors.getMainColor() + "Cancelled. No data was changed.");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public void performRestore(CMDSender sender, File backupDBFile, Database toDB) {
|
||||
try {
|
||||
SQLiteDB fromDB = sqliteFactory.usingFile(backupDBFile);
|
||||
fromDB.init();
|
||||
|
||||
sender.send("Writing to " + toDB.getType().getName() + "..");
|
||||
toDB.executeTransaction(new BackupCopyTransaction(fromDB, toDB)).get();
|
||||
sender.send(locale.getString(ManageLang.PROGRESS_SUCCESS));
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
} catch (DBOpException | ExecutionException e) {
|
||||
errorLogger.log(L.ERROR, e, ErrorContext.builder().related(backupDBFile, toDB.getType(), toDB.getState()).build());
|
||||
sender.send(locale.getString(ManageLang.PROGRESS_FAIL, e.getMessage()));
|
||||
}
|
||||
}
|
||||
|
||||
public void onMove(String mainCommand, CMDSender sender, Arguments arguments) {
|
||||
DBType fromDB = arguments.get(0).flatMap(DBType::getForName)
|
||||
.orElseThrow(() -> new IllegalArgumentException(locale.getString(ManageLang.FAIL_INCORRECT_DB, arguments.get(0).orElse("<MySQL/SQLite/H2>"))));
|
||||
|
||||
DBType toDB = arguments.get(1).flatMap(DBType::getForName)
|
||||
.orElseThrow(() -> new IllegalArgumentException(locale.getString(ManageLang.FAIL_INCORRECT_DB, arguments.get(0).orElse("<MySQL/SQLite/H2>"))));
|
||||
|
||||
if (fromDB == toDB) {
|
||||
throw new IllegalArgumentException(locale.getString(ManageLang.FAIL_SAME_DB));
|
||||
}
|
||||
|
||||
if (sender.isPlayer()) {
|
||||
sender.buildMessage()
|
||||
.addPart(colors.getMainColor() + "You are about to overwrite data in Plan " + toDB.getName() + " with data in " + fromDB.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 overwrite data in Plan " + toDB.getName() + " with data in " + fromDB.getName()).newLine()
|
||||
.addPart("Confirm: ").addPart("§a/" + mainCommand + " accept")
|
||||
.addPart(" ")
|
||||
.addPart("§c/" + mainCommand + " cancel")
|
||||
.send();
|
||||
}
|
||||
|
||||
confirmation.confirm(sender, choice -> {
|
||||
if (choice) {
|
||||
performMove(sender, fromDB, toDB);
|
||||
} else {
|
||||
sender.send(colors.getMainColor() + "Cancelled. No data was changed.");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void performMove(CMDSender sender, DBType fromDB, DBType toDB) {
|
||||
Database fromDatabase = dbSystem.getActiveDatabaseByType(fromDB);
|
||||
Database toDatabase = dbSystem.getActiveDatabaseByType(toDB);
|
||||
fromDatabase.init();
|
||||
toDatabase.init();
|
||||
|
||||
try {
|
||||
sender.send("Writing to " + toDB.getName() + "..");
|
||||
|
||||
fromDatabase.executeTransaction(new BackupCopyTransaction(fromDatabase, toDatabase)).get();
|
||||
|
||||
sender.send(locale.getString(ManageLang.PROGRESS_SUCCESS));
|
||||
|
||||
boolean movingToCurrentDB = toDatabase.getType() == dbSystem.getDatabase().getType();
|
||||
if (movingToCurrentDB) {
|
||||
sender.send(locale.getString(ManageLang.HOTSWAP_REMINDER, toDatabase.getType().getConfigName()));
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
} catch (Exception e) {
|
||||
errorLogger.log(L.ERROR, e, ErrorContext.builder().related(sender, fromDB.getName() + "->" + toDB.getName()).build());
|
||||
sender.send(locale.getString(ManageLang.PROGRESS_FAIL, e.getMessage()));
|
||||
}
|
||||
}
|
||||
}
|
@ -30,6 +30,7 @@ import com.djrapitops.plan.storage.database.DBSystem;
|
||||
import com.djrapitops.plan.storage.database.Database;
|
||||
import com.djrapitops.plan.storage.database.queries.objects.ServerQueries;
|
||||
import com.djrapitops.plan.storage.database.queries.objects.UserIdentifierQueries;
|
||||
import com.djrapitops.plan.storage.database.queries.objects.WebUserQueries;
|
||||
import com.djrapitops.plugin.command.ColorScheme;
|
||||
|
||||
import javax.inject.Inject;
|
||||
@ -199,4 +200,28 @@ public class LinkCommands {
|
||||
.orElseThrow(() -> new IllegalArgumentException("Server is not connected to a network. The link redirects to server page."));
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of webusers subcommand, used to list webusers.
|
||||
*
|
||||
* @param sender Sender of command.
|
||||
* @param arguments Given arguments.
|
||||
*/
|
||||
public void onWebUsersCommand(CMDSender sender, Arguments arguments) {
|
||||
ensureDatabaseIsOpen();
|
||||
String m = colors.getMainColor();
|
||||
String s = colors.getSecondaryColor();
|
||||
String t = colors.getTertiaryColor();
|
||||
String usersListed = dbSystem.getDatabase()
|
||||
.query(WebUserQueries.fetchAllUsers())
|
||||
.stream().sorted()
|
||||
.map(user -> m + user.getUsername() + ":" + t + user.getLinkedTo() + ":" + s + user.getPermissionLevel() + "\n")
|
||||
.collect(StringBuilder::new, StringBuilder::append, StringBuilder::append)
|
||||
.toString();
|
||||
sender.buildMessage()
|
||||
.addPart(t + '>' + m + " Servers").newLine()
|
||||
.addPart(sender.getFormatter().table(
|
||||
t + "username:linked to:permission level\n" + usersListed, ":"))
|
||||
.send();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,78 +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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.djrapitops.plan.commands.subcommands;
|
||||
|
||||
import com.djrapitops.plan.commands.subcommands.manage.*;
|
||||
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.DeepHelpLang;
|
||||
import com.djrapitops.plugin.command.ColorScheme;
|
||||
import com.djrapitops.plugin.command.CommandNode;
|
||||
import com.djrapitops.plugin.command.CommandType;
|
||||
import com.djrapitops.plugin.command.TreeCmdNode;
|
||||
import dagger.Lazy;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Named;
|
||||
|
||||
/**
|
||||
* This SubCommand is used to manage the the plugin's database and components.
|
||||
*
|
||||
* @author Rsl1122
|
||||
*/
|
||||
public class ManageCommand extends TreeCmdNode {
|
||||
|
||||
@Inject
|
||||
public ManageCommand(ColorScheme colorScheme, Locale locale, @Named("mainCommand") Lazy<CommandNode> parent,
|
||||
// Group 1
|
||||
ManageRawDataCommand rawDataCommand,
|
||||
ManageMoveCommand moveCommand,
|
||||
ManageBackupCommand backupCommand,
|
||||
ManageRemoveCommand removeCommand,
|
||||
ManageRestoreCommand restoreCommand,
|
||||
ManageHotSwapCommand hotSwapCommand,
|
||||
ManageClearCommand clearCommand,
|
||||
// Group 2
|
||||
ManageImportCommand importCommand,
|
||||
ManageExportCommand exportCommand,
|
||||
ManageDisableCommand disableCommand,
|
||||
ManageUninstalledCommand uninstalledCommand
|
||||
) {
|
||||
super("manage|m", Permissions.MANAGE.getPermission(), CommandType.CONSOLE, parent.get());
|
||||
super.setColorScheme(colorScheme);
|
||||
|
||||
setShortHelp(locale.getString(CmdHelpLang.MANAGE));
|
||||
setInDepthHelp(locale.getArray(DeepHelpLang.MANAGE));
|
||||
CommandNode[] databaseGroup = {
|
||||
rawDataCommand,
|
||||
moveCommand,
|
||||
backupCommand,
|
||||
restoreCommand,
|
||||
hotSwapCommand,
|
||||
removeCommand,
|
||||
clearCommand,
|
||||
};
|
||||
CommandNode[] pluginGroup = {
|
||||
importCommand,
|
||||
exportCommand,
|
||||
disableCommand,
|
||||
uninstalledCommand
|
||||
};
|
||||
setNodeGroups(databaseGroup, pluginGroup);
|
||||
}
|
||||
}
|
@ -133,9 +133,9 @@ public class RegistrationCommands {
|
||||
}
|
||||
|
||||
private int getPermissionLevel(CMDSender sender) {
|
||||
final String permAnalyze = Permissions.ANALYZE.getPerm();
|
||||
final String permInspectOther = Permissions.INSPECT_OTHER.getPerm();
|
||||
final String permInspect = Permissions.INSPECT.getPerm();
|
||||
final String permAnalyze = "plan.server";
|
||||
final String permInspectOther = "plan.player.other";
|
||||
final String permInspect = "plan.player.self";
|
||||
if (sender.hasPermission(permAnalyze)) {
|
||||
return 0;
|
||||
}
|
||||
|
@ -1,63 +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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.djrapitops.plan.commands.subcommands;
|
||||
|
||||
import com.djrapitops.plan.commands.subcommands.webuser.WebCheckCommand;
|
||||
import com.djrapitops.plan.commands.subcommands.webuser.WebDeleteCommand;
|
||||
import com.djrapitops.plan.commands.subcommands.webuser.WebLevelCommand;
|
||||
import com.djrapitops.plan.commands.subcommands.webuser.WebListUsersCommand;
|
||||
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.DeepHelpLang;
|
||||
import com.djrapitops.plugin.command.ColorScheme;
|
||||
import com.djrapitops.plugin.command.CommandNode;
|
||||
import com.djrapitops.plugin.command.CommandType;
|
||||
import com.djrapitops.plugin.command.TreeCmdNode;
|
||||
import dagger.Lazy;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import javax.inject.Named;
|
||||
|
||||
/**
|
||||
* Web subcommand used to manage Web users.
|
||||
*
|
||||
* @author Rsl1122
|
||||
*/
|
||||
public class WebUserCommand extends TreeCmdNode {
|
||||
|
||||
@Inject
|
||||
public WebUserCommand(ColorScheme colorScheme, Locale locale, @Named("mainCommand") Lazy<CommandNode> parent,
|
||||
WebLevelCommand levelCommand,
|
||||
WebListUsersCommand listUsersCommand,
|
||||
WebCheckCommand checkCommand,
|
||||
WebDeleteCommand deleteCommand
|
||||
) {
|
||||
super("webuser|web", Permissions.MANAGE_WEB.getPerm(), CommandType.CONSOLE, parent.get());
|
||||
super.setColorScheme(colorScheme);
|
||||
|
||||
setShortHelp(locale.getString(CmdHelpLang.WEB));
|
||||
setInDepthHelp(locale.getArray(DeepHelpLang.WEB));
|
||||
CommandNode[] webGroup = {
|
||||
levelCommand,
|
||||
listUsersCommand,
|
||||
checkCommand,
|
||||
deleteCommand
|
||||
};
|
||||
setNodeGroups(webGroup);
|
||||
}
|
||||
}
|
@ -1,156 +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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.djrapitops.plan.commands.subcommands.manage;
|
||||
|
||||
import com.djrapitops.plan.delivery.formatting.Formatter;
|
||||
import com.djrapitops.plan.delivery.formatting.Formatters;
|
||||
import com.djrapitops.plan.exceptions.database.DBInitException;
|
||||
import com.djrapitops.plan.exceptions.database.DBOpException;
|
||||
import com.djrapitops.plan.processing.Processing;
|
||||
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.DBType;
|
||||
import com.djrapitops.plan.storage.database.Database;
|
||||
import com.djrapitops.plan.storage.database.SQLiteDB;
|
||||
import com.djrapitops.plan.storage.database.queries.ServerAggregateQueries;
|
||||
import com.djrapitops.plan.storage.database.transactions.BackupCopyTransaction;
|
||||
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.concurrent.ExecutionException;
|
||||
|
||||
/**
|
||||
* This command is used to backup a database to a .db file.
|
||||
*
|
||||
* @author Rsl1122
|
||||
*/
|
||||
@Singleton
|
||||
public class ManageBackupCommand extends CommandNode {
|
||||
|
||||
private final Locale locale;
|
||||
private final Processing processing;
|
||||
private final DBSystem dbSystem;
|
||||
private final SQLiteDB.Factory sqliteFactory;
|
||||
private final ErrorLogger errorLogger;
|
||||
|
||||
private final Formatter<Long> iso8601LongFormatter;
|
||||
|
||||
@Inject
|
||||
public ManageBackupCommand(
|
||||
Locale locale,
|
||||
Processing processing,
|
||||
DBSystem dbSystem,
|
||||
SQLiteDB.Factory sqliteFactory,
|
||||
Formatters formatters,
|
||||
ErrorLogger errorLogger
|
||||
) {
|
||||
super("backup", Permissions.MANAGE.getPermission(), CommandType.CONSOLE);
|
||||
|
||||
this.locale = locale;
|
||||
this.processing = processing;
|
||||
this.dbSystem = dbSystem;
|
||||
this.sqliteFactory = sqliteFactory;
|
||||
this.errorLogger = errorLogger;
|
||||
|
||||
this.iso8601LongFormatter = formatters.iso8601NoClockLong();
|
||||
|
||||
setShortHelp(locale.getString(CmdHelpLang.MANAGE_BACKUP));
|
||||
setInDepthHelp(locale.getArray(DeepHelpLang.MANAGE_BACKUP));
|
||||
setArguments("<DB>");
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCommand(Sender sender, String commandLabel, String[] args) {
|
||||
try {
|
||||
Verify.isTrue(args.length >= 1,
|
||||
() -> new IllegalArgumentException(locale.getString(CommandLang.FAIL_REQ_ONE_ARG, Arrays.toString(this.getArguments()))));
|
||||
|
||||
String dbName = args[0].toLowerCase();
|
||||
|
||||
boolean isCorrectDB = DBType.exists(dbName);
|
||||
Verify.isTrue(isCorrectDB,
|
||||
() -> new IllegalArgumentException(locale.getString(ManageLang.FAIL_INCORRECT_DB, dbName)));
|
||||
|
||||
Database database = dbSystem.getActiveDatabaseByName(dbName);
|
||||
database.init();
|
||||
|
||||
runBackupTask(sender, args, database);
|
||||
} catch (DBInitException e) {
|
||||
sender.sendMessage(locale.getString(ManageLang.PROGRESS_FAIL, e.getMessage()));
|
||||
}
|
||||
}
|
||||
|
||||
private void runBackupTask(Sender sender, String[] args, Database database) {
|
||||
processing.submitCritical(() -> {
|
||||
try {
|
||||
Database.State dbState = database.getState();
|
||||
if (dbState != Database.State.OPEN) {
|
||||
sender.sendMessage(locale.getString(CommandLang.WARN_DATABASE_NOT_OPEN, dbState.name()));
|
||||
}
|
||||
sender.sendMessage(locale.getString(ManageLang.PROGRESS_START));
|
||||
createNewBackup(args[0], database);
|
||||
sender.sendMessage(locale.getString(ManageLang.PROGRESS_SUCCESS));
|
||||
} catch (Exception e) {
|
||||
errorLogger.log(L.ERROR, ManageBackupCommand.class, e);
|
||||
sender.sendMessage(locale.getString(ManageLang.PROGRESS_FAIL, e.getMessage()));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new backup sqlite file with the data of given database.
|
||||
*
|
||||
* @param dbName Name of database (mysql/sqlite)
|
||||
* @param copyFromDB Database you want to backup.
|
||||
*/
|
||||
private void createNewBackup(String dbName, Database copyFromDB) {
|
||||
Integer userCount = copyFromDB.query(ServerAggregateQueries.baseUserCount());
|
||||
if (userCount <= 0) {
|
||||
return;
|
||||
}
|
||||
Database backupDB = null;
|
||||
try {
|
||||
String timeStamp = iso8601LongFormatter.apply(System.currentTimeMillis());
|
||||
String fileName = dbName + "-backup-" + timeStamp;
|
||||
backupDB = sqliteFactory.usingFileCalled(fileName);
|
||||
backupDB.init();
|
||||
backupDB.executeTransaction(new BackupCopyTransaction(copyFromDB, backupDB)).get();
|
||||
} catch (DBOpException | ExecutionException e) {
|
||||
errorLogger.log(L.ERROR, this.getClass(), e);
|
||||
} catch (InterruptedException e) {
|
||||
backupDB.close();
|
||||
Thread.currentThread().interrupt();
|
||||
} finally {
|
||||
if (backupDB != null) {
|
||||
backupDB.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -1,127 +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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.djrapitops.plan.commands.subcommands.manage;
|
||||
|
||||
import com.djrapitops.plan.processing.Processing;
|
||||
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.DBType;
|
||||
import com.djrapitops.plan.storage.database.Database;
|
||||
import com.djrapitops.plan.storage.database.transactions.BackupCopyTransaction;
|
||||
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;
|
||||
|
||||
/**
|
||||
* This manage SubCommand is used to move all data from one database to another.
|
||||
* <p>
|
||||
* Destination database will be cleared.
|
||||
*
|
||||
* @author Rsl1122
|
||||
*/
|
||||
@Singleton
|
||||
public class ManageMoveCommand extends CommandNode {
|
||||
|
||||
private final Locale locale;
|
||||
private final Processing processing;
|
||||
private final DBSystem dbSystem;
|
||||
private final ErrorLogger errorLogger;
|
||||
|
||||
@Inject
|
||||
public ManageMoveCommand(
|
||||
Locale locale,
|
||||
Processing processing,
|
||||
DBSystem dbSystem,
|
||||
ErrorLogger errorLogger
|
||||
) {
|
||||
super("move", Permissions.MANAGE.getPermission(), CommandType.PLAYER_OR_ARGS);
|
||||
|
||||
this.locale = locale;
|
||||
this.processing = processing;
|
||||
this.dbSystem = dbSystem;
|
||||
this.errorLogger = errorLogger;
|
||||
|
||||
setArguments("<fromDB>", "<toDB>", "[-a]");
|
||||
setShortHelp(locale.getString(CmdHelpLang.MANAGE_MOVE));
|
||||
setInDepthHelp(locale.getArray(DeepHelpLang.MANAGE_MOVE));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCommand(Sender sender, String commandLabel, String[] args) {
|
||||
Verify.isTrue(args.length >= 2,
|
||||
() -> new IllegalArgumentException(locale.getString(CommandLang.FAIL_REQ_ARGS, 2, Arrays.toString(this.getArguments()))));
|
||||
|
||||
DBType fromDB = DBType.getForName(args[0])
|
||||
.orElseThrow(() -> new IllegalArgumentException(locale.getString(ManageLang.FAIL_INCORRECT_DB, args[0])));
|
||||
|
||||
DBType toDB = DBType.getForName(args[1])
|
||||
.orElseThrow(() -> new IllegalArgumentException(locale.getString(ManageLang.FAIL_INCORRECT_DB, args[1])));
|
||||
|
||||
Verify.isFalse(fromDB == toDB,
|
||||
() -> new IllegalArgumentException(locale.getString(ManageLang.FAIL_SAME_DB)));
|
||||
|
||||
if (!Verify.contains("-a", args)) {
|
||||
sender.sendMessage(locale.getString(ManageLang.CONFIRMATION, locale.getString(ManageLang.CONFIRM_OVERWRITE, toDB.getConfigName())));
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
final Database fromDatabase = dbSystem.getActiveDatabaseByType(fromDB);
|
||||
final Database toDatabase = dbSystem.getActiveDatabaseByType(toDB);
|
||||
fromDatabase.init();
|
||||
toDatabase.init();
|
||||
|
||||
runMoveTask(fromDatabase, toDatabase, sender);
|
||||
} catch (Exception e) {
|
||||
sender.sendMessage(locale.getString(ManageLang.PROGRESS_FAIL, e.getMessage()));
|
||||
}
|
||||
}
|
||||
|
||||
private void runMoveTask(final Database fromDatabase, final Database toDatabase, Sender sender) {
|
||||
processing.submitCritical(() -> {
|
||||
try {
|
||||
sender.sendMessage(locale.getString(ManageLang.PROGRESS_START));
|
||||
|
||||
toDatabase.executeTransaction(new BackupCopyTransaction(fromDatabase, toDatabase)).get();
|
||||
|
||||
sender.sendMessage(locale.getString(ManageLang.PROGRESS_SUCCESS));
|
||||
|
||||
boolean movingToCurrentDB = toDatabase.getType() == dbSystem.getDatabase().getType();
|
||||
if (movingToCurrentDB) {
|
||||
sender.sendMessage(locale.getString(ManageLang.HOTSWAP_REMINDER, toDatabase.getType().getConfigName()));
|
||||
}
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
} catch (Exception e) {
|
||||
errorLogger.log(L.ERROR, this.getClass(), e);
|
||||
sender.sendMessage(locale.getString(ManageLang.PROGRESS_FAIL, e.getMessage()));
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
@ -1,143 +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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.djrapitops.plan.commands.subcommands.manage;
|
||||
|
||||
import com.djrapitops.plan.processing.Processing;
|
||||
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.DBType;
|
||||
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.file.PlanFiles;
|
||||
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 java.io.File;
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* This manage SubCommand is used to restore a backup.db file in the
|
||||
* /plugins/Plan folder.
|
||||
*
|
||||
* @author Rsl1122
|
||||
*/
|
||||
public class ManageRestoreCommand extends CommandNode {
|
||||
|
||||
private final Locale locale;
|
||||
private final Processing processing;
|
||||
private final DBSystem dbSystem;
|
||||
private final ErrorLogger errorLogger;
|
||||
private final SQLiteDB.Factory sqliteFactory;
|
||||
private final PlanFiles files;
|
||||
|
||||
@Inject
|
||||
public ManageRestoreCommand(
|
||||
Locale locale,
|
||||
Processing processing,
|
||||
DBSystem dbSystem,
|
||||
SQLiteDB.Factory sqliteFactory,
|
||||
PlanFiles files,
|
||||
ErrorLogger errorLogger
|
||||
) {
|
||||
super("restore", Permissions.MANAGE.getPermission(), CommandType.CONSOLE);
|
||||
|
||||
this.locale = locale;
|
||||
this.processing = processing;
|
||||
this.dbSystem = dbSystem;
|
||||
this.sqliteFactory = sqliteFactory;
|
||||
this.files = files;
|
||||
this.errorLogger = errorLogger;
|
||||
|
||||
setArguments("<Filename.db>", "<dbTo>", "[-a]");
|
||||
setShortHelp(locale.getString(CmdHelpLang.MANAGE_RESTORE));
|
||||
setInDepthHelp(locale.getArray(DeepHelpLang.MANAGE_RESTORE));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCommand(Sender sender, String commandLabel, String[] args) {
|
||||
Verify.isTrue(args.length >= 2,
|
||||
() -> new IllegalArgumentException(locale.getString(CommandLang.FAIL_REQ_ARGS, 2, Arrays.toString(this.getArguments()))));
|
||||
|
||||
String backupDbName = args[0];
|
||||
|
||||
String dbName = args[1].toLowerCase();
|
||||
boolean isCorrectDB = DBType.exists(dbName);
|
||||
Verify.isTrue(isCorrectDB,
|
||||
() -> new IllegalArgumentException(locale.getString(ManageLang.FAIL_INCORRECT_DB, dbName)));
|
||||
|
||||
try {
|
||||
Database database = dbSystem.getActiveDatabaseByName(dbName);
|
||||
|
||||
Verify.isFalse(backupDbName.contains("database") && database instanceof SQLiteDB,
|
||||
() -> new IllegalArgumentException(locale.getString(ManageLang.FAIL_SAME_DB)));
|
||||
database.init();
|
||||
|
||||
if (!Verify.contains("-a", args)) {
|
||||
sender.sendMessage(locale.getString(ManageLang.CONFIRMATION, locale.getString(ManageLang.CONFIRM_OVERWRITE, database.getType().getName())));
|
||||
return;
|
||||
}
|
||||
|
||||
runRestoreTask(backupDbName, sender, database);
|
||||
} catch (Exception e) {
|
||||
sender.sendMessage(locale.getString(ManageLang.PROGRESS_FAIL, e.getMessage()));
|
||||
}
|
||||
}
|
||||
|
||||
private void runRestoreTask(String backupDbName, Sender sender, Database database) {
|
||||
processing.submitCritical(() -> {
|
||||
try {
|
||||
boolean containsDBFileExtension = backupDbName.endsWith(".db");
|
||||
File backupDBFile = files.getFileFromPluginFolder(backupDbName + (containsDBFileExtension ? "" : ".db"));
|
||||
|
||||
if (!backupDBFile.exists()) {
|
||||
sender.sendMessage(locale.getString(ManageLang.FAIL_FILE_NOT_FOUND, backupDBFile.getAbsolutePath()));
|
||||
return;
|
||||
}
|
||||
|
||||
SQLiteDB backupDB = sqliteFactory.usingFile(backupDBFile);
|
||||
backupDB.init();
|
||||
|
||||
Database.State dbState = database.getState();
|
||||
if (dbState != Database.State.OPEN) {
|
||||
sender.sendMessage(locale.getString(CommandLang.WARN_DATABASE_NOT_OPEN, dbState.name()));
|
||||
}
|
||||
|
||||
sender.sendMessage(locale.getString(ManageLang.PROGRESS_START));
|
||||
|
||||
database.executeTransaction(new BackupCopyTransaction(backupDB, database)).get();
|
||||
|
||||
sender.sendMessage(locale.getString(ManageLang.PROGRESS_SUCCESS));
|
||||
} catch (InterruptedException e) {
|
||||
Thread.currentThread().interrupt();
|
||||
} catch (Exception e) {
|
||||
errorLogger.log(L.ERROR, this.getClass(), e);
|
||||
sender.sendMessage(locale.getString(ManageLang.PROGRESS_FAIL, e.getMessage()));
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
@ -1,102 +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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.djrapitops.plan.commands.subcommands.webuser;
|
||||
|
||||
import com.djrapitops.plan.delivery.domain.auth.User;
|
||||
import com.djrapitops.plan.processing.Processing;
|
||||
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.ManageLang;
|
||||
import com.djrapitops.plan.storage.database.DBSystem;
|
||||
import com.djrapitops.plan.storage.database.Database;
|
||||
import com.djrapitops.plan.storage.database.queries.objects.WebUserQueries;
|
||||
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.Optional;
|
||||
|
||||
/**
|
||||
* Subcommand for checking WebUser permission level.
|
||||
*
|
||||
* @author Rsl1122
|
||||
*/
|
||||
@Singleton
|
||||
public class WebCheckCommand extends CommandNode {
|
||||
|
||||
private final Locale locale;
|
||||
private final Processing processing;
|
||||
private final DBSystem dbSystem;
|
||||
private final ErrorLogger errorLogger;
|
||||
|
||||
@Inject
|
||||
public WebCheckCommand(
|
||||
Locale locale,
|
||||
Processing processing,
|
||||
DBSystem dbSystem,
|
||||
ErrorLogger errorLogger
|
||||
) {
|
||||
super("check", Permissions.MANAGE_WEB.getPerm(), CommandType.PLAYER_OR_ARGS);
|
||||
|
||||
this.locale = locale;
|
||||
this.processing = processing;
|
||||
this.dbSystem = dbSystem;
|
||||
this.errorLogger = errorLogger;
|
||||
|
||||
setShortHelp(locale.getString(CmdHelpLang.WEB_CHECK));
|
||||
setArguments("<username>");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCommand(Sender sender, String commandLabel, String[] args) {
|
||||
Database.State dbState = dbSystem.getDatabase().getState();
|
||||
if (dbState != Database.State.OPEN) {
|
||||
sender.sendMessage(locale.getString(CommandLang.FAIL_DATABASE_NOT_OPEN, dbState.name()));
|
||||
return;
|
||||
}
|
||||
|
||||
Verify.isTrue(args.length >= 1,
|
||||
() -> new IllegalArgumentException(locale.getString(CommandLang.FAIL_REQ_ONE_ARG, Arrays.toString(this.getArguments()))));
|
||||
|
||||
String user = args[0];
|
||||
|
||||
processing.submitNonCritical(() -> {
|
||||
try {
|
||||
Database db = dbSystem.getDatabase();
|
||||
Optional<User> found = db.query(WebUserQueries.fetchUser(user));
|
||||
if (!found.isPresent()) {
|
||||
sender.sendMessage(locale.getString(CommandLang.FAIL_WEB_USER_NOT_EXISTS));
|
||||
return;
|
||||
}
|
||||
User info = found.get();
|
||||
sender.sendMessage(locale.getString(CommandLang.WEB_USER_LIST, info.getUsername(), info.getPermissionLevel()));
|
||||
} catch (Exception e) {
|
||||
errorLogger.log(L.ERROR, this.getClass(), e);
|
||||
sender.sendMessage(locale.getString(ManageLang.PROGRESS_FAIL, e.getMessage()));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
@ -1,106 +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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.djrapitops.plan.commands.subcommands.webuser;
|
||||
|
||||
import com.djrapitops.plan.delivery.domain.auth.User;
|
||||
import com.djrapitops.plan.delivery.webserver.auth.FailReason;
|
||||
import com.djrapitops.plan.processing.Processing;
|
||||
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.ManageLang;
|
||||
import com.djrapitops.plan.storage.database.DBSystem;
|
||||
import com.djrapitops.plan.storage.database.Database;
|
||||
import com.djrapitops.plan.storage.database.queries.objects.WebUserQueries;
|
||||
import com.djrapitops.plan.storage.database.transactions.commands.RemoveWebUserTransaction;
|
||||
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.Optional;
|
||||
|
||||
/**
|
||||
* Subcommand for deleting a WebUser.
|
||||
*
|
||||
* @author Rsl1122
|
||||
*/
|
||||
@Singleton
|
||||
public class WebDeleteCommand extends CommandNode {
|
||||
|
||||
private final Locale locale;
|
||||
private final Processing processing;
|
||||
private final DBSystem dbSystem;
|
||||
private final ErrorLogger errorLogger;
|
||||
|
||||
@Inject
|
||||
public WebDeleteCommand(
|
||||
Locale locale,
|
||||
Processing processing,
|
||||
DBSystem dbSystem,
|
||||
ErrorLogger errorLogger
|
||||
) {
|
||||
super("delete|remove", Permissions.MANAGE_WEB.getPerm(), CommandType.PLAYER_OR_ARGS);
|
||||
|
||||
this.locale = locale;
|
||||
this.processing = processing;
|
||||
this.dbSystem = dbSystem;
|
||||
this.errorLogger = errorLogger;
|
||||
|
||||
setShortHelp(locale.getString(CmdHelpLang.WEB_DELETE));
|
||||
setArguments("<username>");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCommand(Sender sender, String commandLabel, String[] args) {
|
||||
Database.State dbState = dbSystem.getDatabase().getState();
|
||||
if (dbState != Database.State.OPEN) {
|
||||
sender.sendMessage(locale.getString(CommandLang.FAIL_DATABASE_NOT_OPEN, dbState.name()));
|
||||
return;
|
||||
}
|
||||
|
||||
Verify.isTrue(args.length >= 1,
|
||||
() -> new IllegalArgumentException(locale.getString(CommandLang.FAIL_REQ_ONE_ARG, Arrays.toString(this.getArguments()))));
|
||||
|
||||
String user = args[0];
|
||||
|
||||
processing.submitNonCritical(() -> {
|
||||
try {
|
||||
Database db = dbSystem.getDatabase();
|
||||
Optional<User> found = db.query(WebUserQueries.fetchUser(user));
|
||||
if (!found.isPresent()) {
|
||||
sender.sendMessage("§c" + locale.getString(FailReason.USER_DOES_NOT_EXIST));
|
||||
return;
|
||||
}
|
||||
sender.sendMessage(locale.getString(ManageLang.PROGRESS_START));
|
||||
db.executeTransaction(new RemoveWebUserTransaction(user))
|
||||
.get(); // Wait for completion
|
||||
sender.sendMessage(locale.getString(ManageLang.PROGRESS_SUCCESS));
|
||||
} catch (Exception e) {
|
||||
errorLogger.log(L.ERROR, this.getClass(), e);
|
||||
sender.sendMessage(locale.getString(ManageLang.PROGRESS_FAIL, e.getMessage()));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
@ -1,52 +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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.djrapitops.plan.commands.subcommands.webuser;
|
||||
|
||||
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.plugin.command.CommandNode;
|
||||
import com.djrapitops.plugin.command.CommandType;
|
||||
import com.djrapitops.plugin.command.Sender;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
/**
|
||||
* Subcommand for info about permission levels.
|
||||
*
|
||||
* @author Rsl1122
|
||||
*/
|
||||
public class WebLevelCommand extends CommandNode {
|
||||
|
||||
private final Locale locale;
|
||||
|
||||
@Inject
|
||||
public WebLevelCommand(Locale locale) {
|
||||
super("level", Permissions.MANAGE_WEB.getPerm(), CommandType.CONSOLE);
|
||||
|
||||
this.locale = locale;
|
||||
|
||||
setShortHelp(locale.getString(CmdHelpLang.WEB_LEVEL));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCommand(Sender sender, String commandLabel, String[] args) {
|
||||
sender.sendMessage(locale.getArray(CommandLang.WEB_PERMISSION_LEVELS));
|
||||
}
|
||||
|
||||
}
|
@ -1,92 +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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.djrapitops.plan.commands.subcommands.webuser;
|
||||
|
||||
import com.djrapitops.plan.delivery.domain.auth.User;
|
||||
import com.djrapitops.plan.processing.Processing;
|
||||
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.ManageLang;
|
||||
import com.djrapitops.plan.storage.database.DBSystem;
|
||||
import com.djrapitops.plan.storage.database.Database;
|
||||
import com.djrapitops.plan.storage.database.queries.objects.WebUserQueries;
|
||||
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 javax.inject.Inject;
|
||||
import javax.inject.Singleton;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Subcommand for checking WebUser list.
|
||||
*
|
||||
* @author Rsl1122
|
||||
*/
|
||||
@Singleton
|
||||
public class WebListUsersCommand extends CommandNode {
|
||||
|
||||
private final Locale locale;
|
||||
private final Processing processing;
|
||||
private final DBSystem dbSystem;
|
||||
private final ErrorLogger errorLogger;
|
||||
|
||||
@Inject
|
||||
public WebListUsersCommand(
|
||||
Locale locale,
|
||||
Processing processing,
|
||||
DBSystem dbSystem,
|
||||
ErrorLogger errorLogger
|
||||
) {
|
||||
super("list", Permissions.MANAGE_WEB.getPerm(), CommandType.CONSOLE);
|
||||
|
||||
this.locale = locale;
|
||||
this.processing = processing;
|
||||
this.dbSystem = dbSystem;
|
||||
this.errorLogger = errorLogger;
|
||||
|
||||
setShortHelp(locale.getString(CmdHelpLang.WEB_LIST));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCommand(Sender sender, String commandLabel, String[] args) {
|
||||
Database.State dbState = dbSystem.getDatabase().getState();
|
||||
if (dbState != Database.State.OPEN) {
|
||||
sender.sendMessage(locale.getString(CommandLang.FAIL_DATABASE_NOT_OPEN, dbState.name()));
|
||||
return;
|
||||
}
|
||||
|
||||
processing.submitNonCritical(() -> {
|
||||
try {
|
||||
List<User> users = dbSystem.getDatabase().query(WebUserQueries.fetchAllUsers());
|
||||
sender.sendMessage(locale.getString(CommandLang.HEADER_WEB_USERS, users.size()));
|
||||
for (User user : users) {
|
||||
sender.sendMessage(locale.getString(CommandLang.WEB_USER_LIST, user.getUsername(), user.getPermissionLevel()));
|
||||
}
|
||||
sender.sendMessage(">");
|
||||
} catch (Exception e) {
|
||||
errorLogger.log(L.ERROR, this.getClass(), e);
|
||||
sender.sendMessage(locale.getString(ManageLang.PROGRESS_FAIL, e.getMessage()));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user