Start implementing add command
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Bea 2022-10-24 20:35:57 +02:00
parent c40a857f8b
commit e7e57506c4
4 changed files with 70 additions and 32 deletions

View File

@ -5,6 +5,7 @@ import org.bukkit.plugin.PluginManager;
import org.bukkit.plugin.java.JavaPlugin;
import wtf.beatrice.nounspicker.commands.NounsCommand;
import wtf.beatrice.nounspicker.objects.PAPIManager;
import wtf.beatrice.nounspicker.utils.Cache;
import wtf.beatrice.nounspicker.utils.ConsoleLogger;
import wtf.beatrice.nounspicker.utils.DatabaseManager;
import wtf.beatrice.nounspicker.utils.FileManager;
@ -56,6 +57,7 @@ public class NounsPicker extends JavaPlugin
if(dbManager.connect() && dbManager.initDb())
{
logger.log("Database connection initialized!");
Cache.dbManager = dbManager;
} else {
logger.err("Error initializing database connection!");
}

View File

@ -0,0 +1,30 @@
package wtf.beatrice.nounspicker.commands.subcommands;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;
import wtf.beatrice.nounspicker.utils.Cache;
public class NounsAddSubCommand
{
public static boolean run(@NotNull CommandSender sender,
@NotNull String[] args)
{
if(args.length < 3)
{
sender.sendMessage("usage: /nouns add <pronoun> <format>");
return true;
}
String pronoun = args[1].toLowerCase();
if(Cache.dbManager.getPronounId(pronoun) >= 0)
{
sender.sendMessage("Pronoun already exists!");
}
return false;
}
}

View File

@ -8,21 +8,5 @@ import java.util.HashMap;
public class Cache
{
// map to store pronouns' "raw" names and how they appear in game.
private static final HashMap<String, String> pronouns = new HashMap<>();
public static @Nullable String getPronoun(String pronoun)
{
return pronouns.get(pronoun);
}
public static void addPronoun(@NotNull String pronoun, @NotNull String appearance)
{
pronouns.put(pronoun, appearance);
}
public static boolean isPronounValid(String pronoun)
{
return getPronoun(pronoun) != null;
}
public static DatabaseManager dbManager;
}

View File

@ -1,9 +1,6 @@
package wtf.beatrice.nounspicker.utils;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
@ -68,10 +65,11 @@ public class DatabaseManager
"secondary_pronoun_id integer" +
");");
newTables.add("CREATE TABLE IF NOT EXISTS pronouns (\" +\n" +
" \"pronoun text,\" +\n" +
" \"id integer\" +\n" +
" \");");
newTables.add("CREATE TABLE IF NOT EXISTS pronouns (" +
"pronoun text," +
"format text," +
"\"id integer" +
");");
for(String sql : newTables)
{
@ -99,14 +97,38 @@ public class DatabaseManager
*
*
* TABLE 2: PRONOUNS
* --------------------------
* | pronoun | id |
* --------------------------
* | she | 1 |
* | her | 2 |
* | he | 3 |
* --------------------------
* --------------------------------------
* | pronoun | id | format |
* --------------------------------------
* | she | 1 | She |
* | her | 2 | Her |
* | he | 3 | He |
* -------------------------------------
*
*/
public int getPronounId(String pronoun) {
String query = "SELECT id " +
"FROM pronouns " +
"WHERE pronoun = ?;";
try (PreparedStatement pStatement = dbConnection.prepareStatement(query)) {
pStatement.setString(1, pronoun);
ResultSet resultSet = pStatement.executeQuery();
while(resultSet.next())
{
return resultSet.getInt("id");
}
} catch (SQLException e)
{
logger.err(e.getMessage());
}
return -1;
}
}