Registered /plan db uninstalled command

This commit is contained in:
Risto Lahtela 2020-06-20 21:18:59 +03:00
parent a9b4432795
commit 74296543e7
3 changed files with 43 additions and 130 deletions

View File

@ -253,6 +253,7 @@ public class PlanCommand {
.subcommand(moveCommand()) .subcommand(moveCommand())
.subcommand(clearCommand()) .subcommand(clearCommand())
.subcommand(removeCommand()) .subcommand(removeCommand())
.subcommand(uninstalled())
.inDepthDescription("Use different database subcommands to change the data in some way") .inDepthDescription("Use different database subcommands to change the data in some way")
.build(); .build();
} }
@ -313,4 +314,15 @@ public class PlanCommand {
.onCommand((sender, arguments) -> databaseCommands.onRemove(commandName, sender, arguments)) .onCommand((sender, arguments) -> databaseCommands.onRemove(commandName, sender, arguments))
.build(); .build();
} }
private Subcommand uninstalled() {
return Subcommand.builder()
.aliases("uninstalled")
.requirePermission("plan.data.uninstalled")
.requiredArgument("server", "Name, ID or UUID of a server")
.description("Set a server as uninstalled in the database.")
.inDepthDescription("Marks a server in Plan database as uninstalled so that it will not show up in server queries.")
.onCommand(databaseCommands::onUninstalled)
.build();
}
} }

View File

@ -22,6 +22,8 @@ import com.djrapitops.plan.delivery.formatting.Formatter;
import com.djrapitops.plan.delivery.formatting.Formatters; import com.djrapitops.plan.delivery.formatting.Formatters;
import com.djrapitops.plan.exceptions.database.DBOpException; import com.djrapitops.plan.exceptions.database.DBOpException;
import com.djrapitops.plan.identification.Identifiers; import com.djrapitops.plan.identification.Identifiers;
import com.djrapitops.plan.identification.Server;
import com.djrapitops.plan.identification.ServerInfo;
import com.djrapitops.plan.query.QuerySvc; import com.djrapitops.plan.query.QuerySvc;
import com.djrapitops.plan.settings.locale.Locale; import com.djrapitops.plan.settings.locale.Locale;
import com.djrapitops.plan.settings.locale.lang.CommandLang; import com.djrapitops.plan.settings.locale.lang.CommandLang;
@ -30,9 +32,11 @@ import com.djrapitops.plan.storage.database.DBSystem;
import com.djrapitops.plan.storage.database.DBType; import com.djrapitops.plan.storage.database.DBType;
import com.djrapitops.plan.storage.database.Database; import com.djrapitops.plan.storage.database.Database;
import com.djrapitops.plan.storage.database.SQLiteDB; import com.djrapitops.plan.storage.database.SQLiteDB;
import com.djrapitops.plan.storage.database.queries.objects.ServerQueries;
import com.djrapitops.plan.storage.database.transactions.BackupCopyTransaction; 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.RemoveEverythingTransaction;
import com.djrapitops.plan.storage.database.transactions.commands.RemovePlayerTransaction; import com.djrapitops.plan.storage.database.transactions.commands.RemovePlayerTransaction;
import com.djrapitops.plan.storage.database.transactions.commands.SetServerAsUninstalledTransaction;
import com.djrapitops.plan.storage.file.PlanFiles; import com.djrapitops.plan.storage.file.PlanFiles;
import com.djrapitops.plan.utilities.logging.ErrorContext; import com.djrapitops.plan.utilities.logging.ErrorContext;
import com.djrapitops.plan.utilities.logging.ErrorLogger; import com.djrapitops.plan.utilities.logging.ErrorLogger;
@ -56,6 +60,7 @@ public class DatabaseCommands {
private final DBSystem dbSystem; private final DBSystem dbSystem;
private final SQLiteDB.Factory sqliteFactory; private final SQLiteDB.Factory sqliteFactory;
private final QuerySvc queryService; private final QuerySvc queryService;
private final ServerInfo serverInfo;
private final Identifiers identifiers; private final Identifiers identifiers;
private final PluginStatusCommands statusCommands; private final PluginStatusCommands statusCommands;
private final ErrorLogger errorLogger; private final ErrorLogger errorLogger;
@ -71,6 +76,7 @@ public class DatabaseCommands {
DBSystem dbSystem, DBSystem dbSystem,
SQLiteDB.Factory sqliteFactory, SQLiteDB.Factory sqliteFactory,
QuerySvc queryService, QuerySvc queryService,
ServerInfo serverInfo,
Formatters formatters, Formatters formatters,
Identifiers identifiers, Identifiers identifiers,
PluginStatusCommands statusCommands, PluginStatusCommands statusCommands,
@ -83,6 +89,7 @@ public class DatabaseCommands {
this.dbSystem = dbSystem; this.dbSystem = dbSystem;
this.sqliteFactory = sqliteFactory; this.sqliteFactory = sqliteFactory;
this.queryService = queryService; this.queryService = queryService;
this.serverInfo = serverInfo;
this.identifiers = identifiers; this.identifiers = identifiers;
this.statusCommands = statusCommands; this.statusCommands = statusCommands;
this.errorLogger = errorLogger; this.errorLogger = errorLogger;
@ -359,4 +366,28 @@ public class DatabaseCommands {
errorLogger.log(L.ERROR, e, ErrorContext.builder().related(sender, database.getType().getName(), playerToRemove).build()); errorLogger.log(L.ERROR, e, ErrorContext.builder().related(sender, database.getType().getName(), playerToRemove).build());
} }
} }
private void ensureDatabaseIsOpen() {
Database.State dbState = dbSystem.getDatabase().getState();
if (dbState != Database.State.OPEN) {
throw new IllegalArgumentException(locale.getString(CommandLang.FAIL_DATABASE_NOT_OPEN, dbState.name()));
}
}
public void onUninstalled(CMDSender sender, Arguments arguments) {
ensureDatabaseIsOpen();
String identifier = arguments.concatenate(" ");
Server server = dbSystem.getDatabase()
.query(ServerQueries.fetchServerMatchingIdentifier(identifier))
.filter(s -> !s.isProxy())
.orElseThrow(() -> new IllegalArgumentException("Server '" + identifier + "' was not found from the database."));
if (server.getUuid().equals(serverInfo.getServerUUID())) {
throw new IllegalArgumentException(locale.getString(ManageLang.UNINSTALLING_SAME_SERVER));
}
dbSystem.getDatabase().executeTransaction(new SetServerAsUninstalledTransaction(server.getUuid()));
sender.send(locale.getString(ManageLang.PROGRESS_SUCCESS));
sender.send("§aIf the server is still installed, it will automatically set itself as installed in the database.");
}
} }

View File

@ -1,130 +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.exceptions.database.DBOpException;
import com.djrapitops.plan.identification.Server;
import com.djrapitops.plan.identification.ServerInfo;
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.Database;
import com.djrapitops.plan.storage.database.queries.objects.ServerQueries;
import com.djrapitops.plan.storage.database.transactions.commands.SetServerAsUninstalledTransaction;
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.Optional;
import java.util.UUID;
/**
* This SubCommand is used to set a server as uninstalled on Plan.
*
* @author Rsl1122
*/
@Singleton
public class ManageUninstalledCommand extends CommandNode {
private final Locale locale;
private final Processing processing;
private final DBSystem dbSystem;
private final ErrorLogger errorLogger;
private final ServerInfo serverInfo;
@Inject
public ManageUninstalledCommand(
Locale locale,
Processing processing,
DBSystem dbSystem,
ServerInfo serverInfo,
ErrorLogger errorLogger
) {
super("uninstalled", Permissions.MANAGE.getPermission(), CommandType.ALL_WITH_ARGS);
this.locale = locale;
this.processing = processing;
this.dbSystem = dbSystem;
this.serverInfo = serverInfo;
this.errorLogger = errorLogger;
setShortHelp(locale.getString(CmdHelpLang.MANAGE_UNINSTALLED));
setInDepthHelp(locale.getArray(DeepHelpLang.MANAGE_UNINSTALLED));
setArguments("[server/id]");
}
@Override
public void onCommand(Sender sender, String commandLabel, String[] args) {
sender.sendMessage(locale.getString(ManageLang.PROGRESS_START));
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 {
Optional<Server> serverOptional = getServer(args);
if (!serverOptional.isPresent()) {
sender.sendMessage(locale.getString(ManageLang.PROGRESS_FAIL, locale.getString(ManageLang.NO_SERVER)));
return;
}
Server server = serverOptional.get();
UUID serverUUID = server.getUuid();
if (serverInfo.getServerUUID().equals(serverUUID)) {
sender.sendMessage(locale.getString(ManageLang.UNINSTALLING_SAME_SERVER));
return;
}
dbSystem.getDatabase().executeTransaction(new SetServerAsUninstalledTransaction(serverUUID));
sender.sendMessage(locale.getString(ManageLang.PROGRESS_SUCCESS));
} catch (DBOpException e) {
sender.sendMessage("§cError occurred: " + e.toString());
errorLogger.log(L.ERROR, this.getClass(), e);
}
});
}
private Optional<Server> getServer(String[] args) {
if (args.length >= 1) {
String serverIdentifier = getGivenIdentifier(args);
return dbSystem.getDatabase().query(ServerQueries.fetchServerMatchingIdentifier(serverIdentifier))
.filter(Server::isNotProxy);
}
return Optional.empty();
}
private String getGivenIdentifier(String[] args) {
StringBuilder idBuilder = new StringBuilder(args[0]);
if (args.length > 1) {
for (int i = 1; i < args.length; i++) {
idBuilder.append(" ").append(args[i]);
}
}
return idBuilder.toString();
}
}