Made it possible to mark servers as uninstalled

This commit is contained in:
Rsl1122 2018-08-12 12:00:43 +03:00
parent 07a0855432
commit 2dd8dd3ee1
8 changed files with 127 additions and 5 deletions

View File

@ -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);
}

View File

@ -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<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;
}
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<Server> getServer(String[] args) {
if (args.length >= 1 && ConnectionSystem.getInstance().isServerAvailable()) {
Map<UUID, Server> bukkitServers = Database.getActive().fetch().getBukkitServers();
String serverIdentifier = getGivenIdentifier(args);
for (Map.Entry<UUID, Server> 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();
}
}

View File

@ -69,4 +69,6 @@ public interface SaveOperations {
void webUser(WebUser webUser);
void ping(UUID uuid, Ping ping);
void setAsUninstalled(UUID serverUUID);
}

View File

@ -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);
}
}

View File

@ -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<UUID, Server> getBukkitServers() {
String sql = Select.from(tableName, "*")
.where(Col.NAME + "!=?")
.and(Col.INSTALLED + "=?")
.toString();
return query(new QueryStatement<Map<UUID, Server>>(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"),

View File

@ -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;

View File

@ -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;

View File

@ -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;