Implement delete command
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Bea 2022-10-24 21:11:50 +02:00
parent efd506eb74
commit ae2e2e5d82
3 changed files with 81 additions and 3 deletions

View File

@ -32,10 +32,11 @@ public class NounsAddSubCommand
int id = Cache.dbManager.getPronounId(pronoun);
sender.sendMessage("New pronoun " + pronoun + " added with id " + id + "! Format is " + format);
return true;
} else {
sender.sendMessage("Error creating new pronoun! Check console for details.");
return true;
}
return true;
}
}

View File

@ -0,0 +1,36 @@
package wtf.beatrice.nounspicker.commands.subcommands;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;
import wtf.beatrice.nounspicker.utils.Cache;
public class NounsDeleteSubCommand
{
public static boolean run(@NotNull CommandSender sender,
@NotNull String[] args)
{
if(args.length < 2) {
sender.sendMessage("usage: /nouns delete <pronoun>");
return true;
}
String pronoun = args[1].toLowerCase();
if(!Cache.dbManager.isPronounValid(pronoun))
{
sender.sendMessage("The pronoun " + pronoun + " doesn't exist!");
return true;
}
if(Cache.dbManager.deletePronoun(pronoun))
{
sender.sendMessage("Pronoun deleted successfully and removed from all users!");
return true;
} else {
sender.sendMessage("Could not delete pronoun " + pronoun + "! Check console for details.");
return true;
}
}
}

View File

@ -61,8 +61,8 @@ public class DatabaseManager
newTables.add("CREATE TABLE IF NOT EXISTS players (" +
"player text NOT NULL," +
"main_pronoun_id integer," +
"secondary_pronoun_id integer" +
"main_pronoun_id INTEGER DEFAULT -1," +
"secondary_pronoun_id INTEGER DEFAULT -1" +
");");
newTables.add("CREATE TABLE IF NOT EXISTS pronouns (" +
@ -169,6 +169,47 @@ public class DatabaseManager
return -1;
}
public boolean deletePronoun(String pronoun) {
// get the pronoun id (so we can remove it from users)
int pronounId = getPronounId(pronoun);
// delete it from the list of pronouns
String query = "DELETE FROM pronouns " +
"WHERE pronoun = ?;";
try (PreparedStatement preparedStatement = dbConnection.prepareStatement(query)) {
preparedStatement.setString(1, pronoun);
preparedStatement.execute();
} catch (SQLException e) {
logger.err(e.getMessage());
return false;
}
// set players who had this pronoun to -1, which means no pronoun
List<String> queries = new ArrayList<>();
queries.add("UPDATE players " +
"SET main_pronoun_id = -1 " +
"WHERE main_pronoun_id = " + pronounId + ";");
queries.add("UPDATE players " +
"SET secondary_pronoun_id = -1 " +
"WHERE secondary_pronoun_id = " + pronounId + ";");
for(String q : queries) {
try(Statement stmt = dbConnection.createStatement()) {
stmt.execute(q);
} catch (SQLException e) {
logger.err(e.getMessage());
return false;
}
}
// accounce success
return true;
}
}