Implemented server name tab completion

This commit is contained in:
Rsl1122 2020-07-13 21:03:02 +03:00 committed by Risto Lahtela
parent 9b599f13de
commit 14e009cb67
2 changed files with 46 additions and 1 deletions

View File

@ -22,7 +22,11 @@ import com.djrapitops.plan.commands.use.CMDSender;
import com.djrapitops.plan.commands.use.CommandWithSubcommands;
import com.djrapitops.plan.commands.use.Subcommand;
import com.djrapitops.plan.gathering.importing.ImportSystem;
import com.djrapitops.plan.identification.Server;
import com.djrapitops.plan.settings.locale.Locale;
import com.djrapitops.plan.storage.database.DBSystem;
import com.djrapitops.plan.storage.database.queries.objects.ServerQueries;
import com.djrapitops.plan.utilities.java.Lists;
import com.djrapitops.plan.utilities.logging.ErrorContext;
import com.djrapitops.plan.utilities.logging.ErrorLogger;
import com.djrapitops.plugin.command.ColorScheme;
@ -42,6 +46,7 @@ public class PlanCommand {
private final ColorScheme colors;
private final Confirmation confirmation;
private final ImportSystem importSystem;
private final DBSystem dbSystem;
private final LinkCommands linkCommands;
private final RegistrationCommands registrationCommands;
private final PluginStatusCommands statusCommands;
@ -56,6 +61,7 @@ public class PlanCommand {
ColorScheme colors,
Confirmation confirmation,
ImportSystem importSystem,
DBSystem dbSystem,
LinkCommands linkCommands,
RegistrationCommands registrationCommands,
PluginStatusCommands statusCommands,
@ -68,6 +74,7 @@ public class PlanCommand {
this.colors = colors;
this.confirmation = confirmation;
this.importSystem = importSystem;
this.dbSystem = dbSystem;
this.linkCommands = linkCommands;
this.registrationCommands = registrationCommands;
this.statusCommands = statusCommands;
@ -114,7 +121,9 @@ public class PlanCommand {
}
public List<String> serverNames(CMDSender sender, Arguments arguments) {
return Collections.emptyList(); // TODO
String asString = arguments.concatenate(" ");
List<Server> servers = dbSystem.getDatabase().query(ServerQueries.findMatchingServers(asString));
return Lists.map(servers, Server::getIdentifiableName);
}
private List<String> playerNames(CMDSender sender, Arguments arguments) {

View File

@ -137,4 +137,40 @@ public class ServerQueries {
}
};
}
public static Query<List<Server>> findMatchingServers(String identifier) {
String sql = SELECT + '*' + FROM + ServerTable.TABLE_NAME +
" WHERE (LOWER(" + ServerTable.SERVER_UUID + ") LIKE LOWER(%?%)" +
OR + "LOWER(" + ServerTable.NAME + ") LIKE LOWER(%?%)" +
OR + ServerTable.SERVER_ID + "=?" +
OR + ServerTable.SERVER_ID + "=?)" +
AND + ServerTable.INSTALLED + "=?" +
" LIMIT 1";
return new QueryStatement<List<Server>>(sql) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setString(1, identifier);
statement.setString(2, identifier);
statement.setInt(3, NumberUtils.isParsable(identifier) ? Integer.parseInt(identifier) : -1);
String id = identifier.startsWith("Server ") ? identifier.substring(7) : identifier;
statement.setInt(4, NumberUtils.isParsable(id) ? Integer.parseInt(id) : -1);
statement.setBoolean(5, true);
}
@Override
public List<Server> processResults(ResultSet set) throws SQLException {
List<Server> matches = new ArrayList<>();
while (set.next()) {
matches.add(new Server(
set.getInt(ServerTable.SERVER_ID),
UUID.fromString(set.getString(ServerTable.SERVER_UUID)),
set.getString(ServerTable.NAME),
set.getString(ServerTable.WEB_ADDRESS),
set.getInt(ServerTable.MAX_PLAYERS)
));
}
return matches;
}
};
}
}