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

This commit is contained in:
Bea 2022-10-24 21:57:11 +02:00
parent e6534c54e1
commit 5b5faa6b5f
4 changed files with 59 additions and 1 deletions

View File

@ -8,6 +8,7 @@ import wtf.beatrice.nounspicker.NounsPicker;
import wtf.beatrice.nounspicker.commands.subcommands.NounsCreateSubCommand; import wtf.beatrice.nounspicker.commands.subcommands.NounsCreateSubCommand;
import wtf.beatrice.nounspicker.commands.subcommands.NounsDeleteSubCommand; import wtf.beatrice.nounspicker.commands.subcommands.NounsDeleteSubCommand;
import wtf.beatrice.nounspicker.commands.subcommands.NounsSetSubCommand; import wtf.beatrice.nounspicker.commands.subcommands.NounsSetSubCommand;
import wtf.beatrice.nounspicker.commands.subcommands.NounsUpdateSubCmd;
public class NounsCommand implements CommandExecutor public class NounsCommand implements CommandExecutor
{ {
@ -39,6 +40,8 @@ public class NounsCommand implements CommandExecutor
return NounsCreateSubCommand.run(sender, args); return NounsCreateSubCommand.run(sender, args);
case "delete": case "delete":
return NounsDeleteSubCommand.run(sender, args); return NounsDeleteSubCommand.run(sender, args);
case "update":
return NounsUpdateSubCmd.run(sender, args);
} }

View File

@ -11,7 +11,6 @@ public class NounsCreateSubCommand
@NotNull String[] args) @NotNull String[] args)
{ {
if(args.length < 3) if(args.length < 3)
{ {
sender.sendMessage("usage: /nouns create <pronoun> <format>"); sender.sendMessage("usage: /nouns create <pronoun> <format>");

View File

@ -0,0 +1,39 @@
package wtf.beatrice.nounspicker.commands.subcommands;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;
import wtf.beatrice.nounspicker.utils.Cache;
public class NounsUpdateSubCmd
{
public static boolean run(@NotNull CommandSender sender,
@NotNull String[] args) {
if (args.length < 3) {
sender.sendMessage("usage: /nouns update <pronoun> <format>");
return true;
}
String pronoun = args[1].toLowerCase();
if(!Cache.dbManager.isPronounValid(pronoun)) {
sender.sendMessage("Invalid pronoun " + pronoun + "!");
return true;
}
int pronounId = Cache.dbManager.getPronounId(pronoun);
String oldFormat = Cache.dbManager.getPronounFormat(pronounId);
String newFormat = args[2];
if(Cache.dbManager.updatePronounFormat(pronounId, newFormat)) {
sender.sendMessage("Format of pronoun " + pronoun + " updated from " + oldFormat + " to " + newFormat + "!");
return true;
} else {
sender.sendMessage("Error updating pronoun format! Please check console for details.");
return true;
}
}
}

View File

@ -287,5 +287,22 @@ public class DatabaseManager
return false; return false;
} }
public boolean updatePronounFormat(int pronounId, String newFormat) {
String query = "UPDATE pronouns SET format=? " +
"WHERE id=?;";
try(PreparedStatement pStatement = dbConnection.prepareStatement(query)) {
pStatement.setString(1, newFormat);
pStatement.setInt(2, pronounId);
pStatement.executeUpdate();
return true;
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
} }