diff --git a/Plan/src/main/java/com/djrapitops/plan/command/commands/ManageCommand.java b/Plan/src/main/java/com/djrapitops/plan/command/commands/ManageCommand.java index 066e4672a..58a7a6020 100644 --- a/Plan/src/main/java/com/djrapitops/plan/command/commands/ManageCommand.java +++ b/Plan/src/main/java/com/djrapitops/plan/command/commands/ManageCommand.java @@ -39,7 +39,8 @@ public class ManageCommand extends TreeCmdNode { new ManageSetupCommand(plugin), new ManageConDebugCommand(plugin), new ManageImportCommand(plugin), - new ManageDisableCommand(plugin) + new ManageDisableCommand(plugin), + new ManageUninstalledCommand(plugin) }; setNodeGroups(databaseGroup, pluginGroup); } diff --git a/Plan/src/main/java/com/djrapitops/plan/command/commands/manage/ManageUninstalledCommand.java b/Plan/src/main/java/com/djrapitops/plan/command/commands/manage/ManageUninstalledCommand.java new file mode 100644 index 000000000..e42715f52 --- /dev/null +++ b/Plan/src/main/java/com/djrapitops/plan/command/commands/manage/ManageUninstalledCommand.java @@ -0,0 +1,96 @@ +package com.djrapitops.plan.command.commands.manage; + +import com.djrapitops.plan.PlanPlugin; +import com.djrapitops.plan.api.exceptions.database.DBOpException; +import com.djrapitops.plan.system.database.databases.Database; +import com.djrapitops.plan.system.info.connection.ConnectionSystem; +import com.djrapitops.plan.system.info.server.Server; +import com.djrapitops.plan.system.info.server.ServerInfo; +import com.djrapitops.plan.system.locale.Locale; +import com.djrapitops.plan.system.locale.lang.CmdHelpLang; +import com.djrapitops.plan.system.locale.lang.DeepHelpLang; +import com.djrapitops.plan.system.locale.lang.ManageLang; +import com.djrapitops.plan.system.processing.Processing; +import com.djrapitops.plan.system.settings.Permissions; +import com.djrapitops.plugin.api.utility.log.Log; +import com.djrapitops.plugin.command.CommandNode; +import com.djrapitops.plugin.command.CommandType; +import com.djrapitops.plugin.command.ISender; + +import java.util.Map; +import java.util.Optional; +import java.util.UUID; + +/** + * This SubCommand is used to set a server as uninstalled on Plan. + * + * @author Rsl1122 + * @since 2.0.0 + */ +public class ManageUninstalledCommand extends CommandNode { + + private final Locale locale; + + public ManageUninstalledCommand(PlanPlugin plugin) { + super("uninstalled", Permissions.MANAGE.getPermission(), CommandType.ALL_WITH_ARGS); + + locale = plugin.getSystem().getLocaleSystem().getLocale(); + + setShortHelp(locale.getString(CmdHelpLang.MANAGE_UNINSTALLED)); + setInDepthHelp(locale.getArray(DeepHelpLang.MANAGE_UNINSTALLED)); + setArguments("[server/id]"); + } + + @Override + public void onCommand(ISender sender, String commandLabel, String[] args) { + sender.sendMessage(locale.getString(ManageLang.PROGRESS_START)); + + Processing.submitNonCritical(() -> { + try { + Optional 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; + } + + Database.getActive().save().setAsUninstalled(serverUUID); + sender.sendMessage(locale.getString(ManageLang.PROGRESS_SUCCESS)); + } catch (DBOpException e) { + sender.sendMessage("§cError occurred: " + e.toString()); + Log.toLog(this.getClass(), e); + } + }); + } + + private Optional getServer(String[] args) { + if (args.length >= 1 && ConnectionSystem.getInstance().isServerAvailable()) { + Map bukkitServers = Database.getActive().fetch().getBukkitServers(); + String serverIdentifier = getGivenIdentifier(args); + for (Map.Entry entry : bukkitServers.entrySet()) { + Server server = entry.getValue(); + + if (Integer.toString(server.getId()).equals(serverIdentifier) + || server.getName().equalsIgnoreCase(serverIdentifier)) { + return Optional.of(server); + } + } + } + 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(); + } +} diff --git a/Plan/src/main/java/com/djrapitops/plan/system/database/databases/operation/SaveOperations.java b/Plan/src/main/java/com/djrapitops/plan/system/database/databases/operation/SaveOperations.java index e841d1c44..82d6b2bb0 100644 --- a/Plan/src/main/java/com/djrapitops/plan/system/database/databases/operation/SaveOperations.java +++ b/Plan/src/main/java/com/djrapitops/plan/system/database/databases/operation/SaveOperations.java @@ -69,4 +69,6 @@ public interface SaveOperations { void webUser(WebUser webUser); void ping(UUID uuid, Ping ping); + + void setAsUninstalled(UUID serverUUID); } \ No newline at end of file diff --git a/Plan/src/main/java/com/djrapitops/plan/system/database/databases/sql/operation/SQLSaveOps.java b/Plan/src/main/java/com/djrapitops/plan/system/database/databases/sql/operation/SQLSaveOps.java index f35686b3e..0bf103c80 100644 --- a/Plan/src/main/java/com/djrapitops/plan/system/database/databases/sql/operation/SQLSaveOps.java +++ b/Plan/src/main/java/com/djrapitops/plan/system/database/databases/sql/operation/SQLSaveOps.java @@ -135,4 +135,9 @@ public class SQLSaveOps extends SQLOps implements SaveOperations { public void ping(UUID uuid, Ping ping) { pingTable.insertPing(uuid, ping); } + + @Override + public void setAsUninstalled(UUID serverUUID) { + serverTable.setAsUninstalled(serverUUID); + } } diff --git a/Plan/src/main/java/com/djrapitops/plan/system/database/databases/sql/tables/ServerTable.java b/Plan/src/main/java/com/djrapitops/plan/system/database/databases/sql/tables/ServerTable.java index 4523a0ece..6df2a8a83 100644 --- a/Plan/src/main/java/com/djrapitops/plan/system/database/databases/sql/tables/ServerTable.java +++ b/Plan/src/main/java/com/djrapitops/plan/system/database/databases/sql/tables/ServerTable.java @@ -55,7 +55,7 @@ public class ServerTable extends Table { .column(Col.SERVER_UUID, Sql.varchar(36)).notNull().unique() .column(Col.NAME, Sql.varchar(100)) .column(Col.WEBSERVER_ADDRESS, Sql.varchar(100)) - .column(Col.INSTALLED, Sql.BOOL).notNull().defaultValue(false) + .column(Col.INSTALLED, Sql.BOOL).notNull().defaultValue(true) .column(Col.MAX_PLAYERS, Sql.INT).notNull().defaultValue("-1") .primaryKey(usingMySQL, Col.SERVER_ID) .toString() @@ -246,12 +246,14 @@ public class ServerTable extends Table { public Map getBukkitServers() { String sql = Select.from(tableName, "*") .where(Col.NAME + "!=?") + .and(Col.INSTALLED + "=?") .toString(); return query(new QueryStatement>(sql, 100) { @Override public void prepare(PreparedStatement statement) throws SQLException { statement.setString(1, "BungeeCord"); + statement.setBoolean(2, true); } @Override @@ -364,6 +366,18 @@ public class ServerTable extends Table { }); } + public void setAsUninstalled(UUID serverUUID) { + String sql = "UPDATE " + tableName + " SET (" + Col.INSTALLED + "=?) WHERE " + Col.SERVER_UUID + "=?"; + + execute(new ExecStatement(sql) { + @Override + public void prepare(PreparedStatement statement) throws SQLException { + statement.setBoolean(1, false); + statement.setString(2, serverUUID.toString()); + } + }); + } + public enum Col implements Column { SERVER_ID("id"), SERVER_UUID("uuid"), diff --git a/Plan/src/main/java/com/djrapitops/plan/system/locale/lang/CmdHelpLang.java b/Plan/src/main/java/com/djrapitops/plan/system/locale/lang/CmdHelpLang.java index ce3dfba9a..4a4826f7d 100644 --- a/Plan/src/main/java/com/djrapitops/plan/system/locale/lang/CmdHelpLang.java +++ b/Plan/src/main/java/com/djrapitops/plan/system/locale/lang/CmdHelpLang.java @@ -40,7 +40,8 @@ public enum CmdHelpLang implements Lang { WEB_LIST("Command Help - /plan web list", "List Web Users"), WEB_CHECK("Command Help - /plan web check", "Inspect a Web User"), WEB_DELETE("Command Help - /plan web delete", "Delete a Web User"), - MANAGE_RAW_DATA("Command Help - /plan manage raw", "View raw JSON of player data"); + MANAGE_RAW_DATA("Command Help - /plan manage raw", "View raw JSON of player data"), + MANAGE_UNINSTALLED("Command Help - /plan manage uninstalled", "Mark a server as uninstalled in the database."); private final String identifier; private final String defaultValue; diff --git a/Plan/src/main/java/com/djrapitops/plan/system/locale/lang/DeepHelpLang.java b/Plan/src/main/java/com/djrapitops/plan/system/locale/lang/DeepHelpLang.java index b4f3657d4..04c3564f9 100644 --- a/Plan/src/main/java/com/djrapitops/plan/system/locale/lang/DeepHelpLang.java +++ b/Plan/src/main/java/com/djrapitops/plan/system/locale/lang/DeepHelpLang.java @@ -32,7 +32,8 @@ public enum DeepHelpLang implements Lang { MANAGE_SETUP("In Depth Help - /plan manage setup ?", "> §2Setup Subcommand\\ Set-up a connection between Bungee and this server for network functionality.\\ BungeeAddress can be found in the enable log on console when Plan enables on Bungee."), WEB_REGISTER("In Depth Help - /plan web register ?", "> §2Register Subcommand\\ Registers a new Web User.\\ Registering a user for another player requires plan.webmanage permission.\\ Passwords are hashed with PBKDF2 (64,000 iterations of SHA1) using a cryptographically-random salt."), - MANAGE_RAW_DATA("In Depth Help - /plan manage raw ?", "> §2Raw Data Subcommand\\ Displays link to raw JSON data page.\\ Not available if Plan webserver is not enabled."); + MANAGE_RAW_DATA("In Depth Help - /plan manage raw ?", "> §2Raw Data Subcommand\\ Displays link to raw JSON data page.\\ Not available if Plan webserver is not enabled."), + MANAGE_UNINSTALLED("In Depth Help - /plan manage uninstalled ?", "> §2Uninstalled Server Subcommand\\ Marks a server as uninstalled in the database.\\ Can not mark the server the command is being used on as uninstalled.\\ Will affect ConnectionSystem."); private final String identifier; private final String defaultValue; diff --git a/Plan/src/main/java/com/djrapitops/plan/system/locale/lang/ManageLang.java b/Plan/src/main/java/com/djrapitops/plan/system/locale/lang/ManageLang.java index 6b9324afb..ffa2e0f58 100644 --- a/Plan/src/main/java/com/djrapitops/plan/system/locale/lang/ManageLang.java +++ b/Plan/src/main/java/com/djrapitops/plan/system/locale/lang/ManageLang.java @@ -28,7 +28,9 @@ public enum ManageLang implements Lang { FAIL_SAME_DB("Manage - Fail Same Database", "> §cCan not operate on to and from the same database!"), FAIL_INCORRECT_DB("Manage - Fail Incorrect Database", "> §c'${0}' is not a supported database."), FAIL_FILE_NOT_FOUND("Manage - Fail File not found", "> §cNo File found at ${0}"), - FAIL_IMPORTER_NOT_FOUND("Manage - Fail No Importer", "§eImporter '${0}' doesn't exist"); + FAIL_IMPORTER_NOT_FOUND("Manage - Fail No Importer", "§eImporter '${0}' doesn't exist"), + NO_SERVER("Manage - Fail", "No server found with given parameters."), + UNINSTALLING_SAME_SERVER("Manage - Fail", "Can not mark this server as uninstalled (You are on it)"); private final String identifier; private final String defaultValue;