diff --git a/src/main/java/wtf/beatrice/nounspicker/commands/subcommands/NounsAddSubCommand.java b/src/main/java/wtf/beatrice/nounspicker/commands/subcommands/NounsAddSubCommand.java index ada8168..fda8cba 100644 --- a/src/main/java/wtf/beatrice/nounspicker/commands/subcommands/NounsAddSubCommand.java +++ b/src/main/java/wtf/beatrice/nounspicker/commands/subcommands/NounsAddSubCommand.java @@ -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; } } diff --git a/src/main/java/wtf/beatrice/nounspicker/commands/subcommands/NounsDeleteSubCommand.java b/src/main/java/wtf/beatrice/nounspicker/commands/subcommands/NounsDeleteSubCommand.java new file mode 100644 index 0000000..2a2be1f --- /dev/null +++ b/src/main/java/wtf/beatrice/nounspicker/commands/subcommands/NounsDeleteSubCommand.java @@ -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 "); + 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; + } + + } +} diff --git a/src/main/java/wtf/beatrice/nounspicker/utils/DatabaseManager.java b/src/main/java/wtf/beatrice/nounspicker/utils/DatabaseManager.java index 145f670..bc77fa1 100644 --- a/src/main/java/wtf/beatrice/nounspicker/utils/DatabaseManager.java +++ b/src/main/java/wtf/beatrice/nounspicker/utils/DatabaseManager.java @@ -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 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; + } + }