From b74b8136365ca2542242023fc91e370873d6eabf Mon Sep 17 00:00:00 2001 From: Tyler Murphy Date: Tue, 17 May 2022 16:36:35 -0400 Subject: [PATCH] working on expanding database --- pom.xml | 28 ++++- .../net/tylermurphy/hideAndSeek/Main.java | 27 +++-- .../tylermurphy/hideAndSeek/command/Top.java | 2 +- .../tylermurphy/hideAndSeek/command/Wins.java | 2 +- .../hideAndSeek/database/Database.java | 43 ++++---- ...layerInfoTable.java => GameDataTable.java} | 63 +++++++---- .../hideAndSeek/database/LegacyTable.java | 104 ++++++++++++++++++ .../hideAndSeek/database/NameDataTable.java | 99 +++++++++++++++++ .../connections/DatabaseConnection.java | 29 +++++ .../database/connections/MySQLConnection.java | 61 ++++++++++ .../connections/SQLiteConnection.java | 64 +++++++++++ .../database/util/LegacyPlayerInfo.java | 56 ++++++++++ .../database/{ => util}/PlayerInfo.java | 4 +- .../game/listener/JoinLeaveHandler.java | 3 + .../hideAndSeek/util/PAPIExpansion.java | 2 +- src/test/java/MainTest.java | 42 +++++++ 16 files changed, 569 insertions(+), 60 deletions(-) rename src/main/java/net/tylermurphy/hideAndSeek/database/{PlayerInfoTable.java => GameDataTable.java} (74%) create mode 100644 src/main/java/net/tylermurphy/hideAndSeek/database/LegacyTable.java create mode 100644 src/main/java/net/tylermurphy/hideAndSeek/database/NameDataTable.java create mode 100644 src/main/java/net/tylermurphy/hideAndSeek/database/connections/DatabaseConnection.java create mode 100644 src/main/java/net/tylermurphy/hideAndSeek/database/connections/MySQLConnection.java create mode 100644 src/main/java/net/tylermurphy/hideAndSeek/database/connections/SQLiteConnection.java create mode 100644 src/main/java/net/tylermurphy/hideAndSeek/database/util/LegacyPlayerInfo.java rename src/main/java/net/tylermurphy/hideAndSeek/database/{ => util}/PlayerInfo.java (95%) create mode 100644 src/test/java/MainTest.java diff --git a/pom.xml b/pom.xml index 8073d7a..fff9f6c 100644 --- a/pom.xml +++ b/pom.xml @@ -30,18 +30,18 @@ - - org.spigotmc:spigot-api - com.comphenix.protocol:ProtocolLib - org.jetbrains:annotations - net.bytebuddy:byte-buddy - + + com.github.cryptomorin:XSeries + org.xerial:sqlite-jdbc + com.zaxxer:HikariCP + *:* META-INF/*.MF + META-INF/*.md META-INF sqlite-jdbc.properties @@ -113,5 +113,21 @@ 2.11.1 provided + + com.zaxxer + HikariCP + 5.0.1 + + + org.junit.jupiter + junit-jupiter + 5.9.0-M1 + + + com.github.seeseemelk + MockBukkit-v1.17 + 1.13.0 + test + \ No newline at end of file diff --git a/src/main/java/net/tylermurphy/hideAndSeek/Main.java b/src/main/java/net/tylermurphy/hideAndSeek/Main.java index 2737e81..126deaa 100644 --- a/src/main/java/net/tylermurphy/hideAndSeek/Main.java +++ b/src/main/java/net/tylermurphy/hideAndSeek/Main.java @@ -19,6 +19,7 @@ package net.tylermurphy.hideAndSeek; +import jdk.jpackage.internal.IOUtils; import net.tylermurphy.hideAndSeek.configuration.Config; import net.tylermurphy.hideAndSeek.configuration.Items; import net.tylermurphy.hideAndSeek.configuration.Localization; @@ -34,7 +35,9 @@ import org.bukkit.Bukkit; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; import org.bukkit.event.Listener; +import org.bukkit.plugin.PluginDescriptionFile; import org.bukkit.plugin.java.JavaPlugin; +import org.bukkit.plugin.java.JavaPluginLoader; import org.jetbrains.annotations.NotNull; import java.io.File; @@ -47,13 +50,26 @@ public class Main extends JavaPlugin implements Listener { private static Main instance; private static int version; - private Database database; - private Board board; + private final Database database; + private final Board board; + private Game game; - public void onEnable() { - + public Main() { + super(); instance = this; + board = new Board(); + database = new Database(); + } + + protected Main(JavaPluginLoader loader, PluginDescriptionFile description, File dataFolder, File file) { + super(loader, description, dataFolder, file); + instance = this; + board = new Board(); + database = new Database(); + } + + public void onEnable() { this.registerListeners(); @@ -63,9 +79,6 @@ public class Main extends JavaPlugin implements Listener { CommandHandler.registerCommands(); - board = new Board(); - database = new Database(); - game = new Game(board); getServer().getScheduler().runTaskTimer(this, this::onTick,0,1).getTaskId(); diff --git a/src/main/java/net/tylermurphy/hideAndSeek/command/Top.java b/src/main/java/net/tylermurphy/hideAndSeek/command/Top.java index e0aa2fb..caa751b 100644 --- a/src/main/java/net/tylermurphy/hideAndSeek/command/Top.java +++ b/src/main/java/net/tylermurphy/hideAndSeek/command/Top.java @@ -20,7 +20,7 @@ package net.tylermurphy.hideAndSeek.command; import net.tylermurphy.hideAndSeek.Main; -import net.tylermurphy.hideAndSeek.database.PlayerInfo; +import net.tylermurphy.hideAndSeek.database.util.PlayerInfo; import org.bukkit.ChatColor; import org.bukkit.entity.Player; diff --git a/src/main/java/net/tylermurphy/hideAndSeek/command/Wins.java b/src/main/java/net/tylermurphy/hideAndSeek/command/Wins.java index bb69cd4..8a0b6dc 100644 --- a/src/main/java/net/tylermurphy/hideAndSeek/command/Wins.java +++ b/src/main/java/net/tylermurphy/hideAndSeek/command/Wins.java @@ -20,7 +20,7 @@ package net.tylermurphy.hideAndSeek.command; import net.tylermurphy.hideAndSeek.Main; -import net.tylermurphy.hideAndSeek.database.PlayerInfo; +import net.tylermurphy.hideAndSeek.database.util.PlayerInfo; import org.bukkit.ChatColor; import org.bukkit.entity.Player; diff --git a/src/main/java/net/tylermurphy/hideAndSeek/database/Database.java b/src/main/java/net/tylermurphy/hideAndSeek/database/Database.java index ce06244..0eceb98 100644 --- a/src/main/java/net/tylermurphy/hideAndSeek/database/Database.java +++ b/src/main/java/net/tylermurphy/hideAndSeek/database/Database.java @@ -21,48 +21,51 @@ package net.tylermurphy.hideAndSeek.database; import com.google.common.io.ByteStreams; import net.tylermurphy.hideAndSeek.Main; -import org.sqlite.SQLiteConfig; +import net.tylermurphy.hideAndSeek.database.connections.DatabaseConnection; +import net.tylermurphy.hideAndSeek.database.connections.SQLiteConnection; import java.io.ByteArrayInputStream; -import java.io.File; import java.io.IOException; import java.io.InputStream; import java.nio.ByteBuffer; import java.sql.Connection; -import java.sql.DriverManager; import java.sql.SQLException; import java.util.UUID; public class Database { - private final File databaseFile = new File(Main.getInstance().getDataFolder(), "database.db"); - private final PlayerInfoTable playerInfo; - private final SQLiteConfig config; + private final GameDataTable playerInfo; + private final NameDataTable nameInfo; + private final DatabaseConnection connection; public Database(){ - try { - Class.forName("org.sqlite.JDBC"); - } catch (ClassNotFoundException e) { - Main.getInstance().getLogger().severe(e.getMessage()); - throw new RuntimeException(e.getMessage()); + + connection = new SQLiteConnection(); + + playerInfo = new GameDataTable(this); + + nameInfo = new NameDataTable(this); + + LegacyTable legacyTable = new LegacyTable(this); + if(legacyTable.exists()){ + if(legacyTable.copyData()){ + if(!legacyTable.drop()){ + Main.getInstance().getLogger().severe("Failed to drop old legacy table: player_info. Some data may be duplicated!"); + } + } } - - config = new SQLiteConfig(); - config.setSynchronous(SQLiteConfig.SynchronousMode.NORMAL); - config.setTempStore(SQLiteConfig.TempStore.MEMORY); - - playerInfo = new PlayerInfoTable(this); } - public PlayerInfoTable getGameData(){ + public GameDataTable getGameData(){ return playerInfo; } + public NameDataTable getNameData() { return nameInfo; } + protected Connection connect() { Connection conn = null; try { - String url = "jdbc:sqlite:"+databaseFile; - conn = DriverManager.getConnection(url, config.toProperties()); + conn = connection.connect(); } catch (SQLException e) { Main.getInstance().getLogger().severe(e.getMessage()); e.printStackTrace(); diff --git a/src/main/java/net/tylermurphy/hideAndSeek/database/PlayerInfoTable.java b/src/main/java/net/tylermurphy/hideAndSeek/database/GameDataTable.java similarity index 74% rename from src/main/java/net/tylermurphy/hideAndSeek/database/PlayerInfoTable.java rename to src/main/java/net/tylermurphy/hideAndSeek/database/GameDataTable.java index 500cdc0..128614d 100644 --- a/src/main/java/net/tylermurphy/hideAndSeek/database/PlayerInfoTable.java +++ b/src/main/java/net/tylermurphy/hideAndSeek/database/GameDataTable.java @@ -1,7 +1,7 @@ /* * This file is part of Kenshins Hide and Seek * - * Copyright (c) 2021 Tyler Murphy. + * Copyright (c) 2021-2022 Tyler Murphy. * * Kenshins Hide and Seek free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -20,6 +20,7 @@ package net.tylermurphy.hideAndSeek.database; import net.tylermurphy.hideAndSeek.Main; +import net.tylermurphy.hideAndSeek.database.util.PlayerInfo; import net.tylermurphy.hideAndSeek.game.Board; import net.tylermurphy.hideAndSeek.game.util.WinType; import org.jetbrains.annotations.Nullable; @@ -27,12 +28,12 @@ import org.jetbrains.annotations.Nullable; import java.sql.*; import java.util.*; -public class PlayerInfoTable { +public class GameDataTable { private final Map CACHE = new HashMap<>(); private final Database database; - protected PlayerInfoTable(Database database) { + protected GameDataTable(Database database) { String sql = "CREATE TABLE IF NOT EXISTS hs_data (\n" + " uuid BINARY(16) PRIMARY KEY,\n" @@ -170,30 +171,48 @@ public class PlayerInfoTable { public void addWins(Board board, List uuids, List winners, Map hider_kills, Map hider_deaths, Map seeker_kills, Map seeker_deaths, WinType type) { for(UUID uuid : uuids) { - String sql = "INSERT OR REPLACE INTO hs_data (uuid, hider_wins, seeker_wins, hider_games, seeker_games, hider_kills, seeker_kills, hider_deaths, seeker_deaths) VALUES (?,?,?,?,?,?,?,?,?)"; PlayerInfo info = getInfo(uuid); if(info == null){ info = new PlayerInfo(uuid, 0, 0, 0, 0, 0, 0, 0, 0); } - try(Connection connection = database.connect(); PreparedStatement statement = connection.prepareStatement(sql)) { - statement.setBytes(1, database.encodeUUID(uuid)); - statement.setInt(2, info.getHiderWins() + (winners.contains(uuid) && type == WinType.HIDER_WIN ? 1 : 0)); - statement.setInt(3, info.getSeekerWins() + (winners.contains(uuid) && type == WinType.SEEKER_WIN ? 1 : 0)); - statement.setInt(4, info.getHiderGames() + (board.isHider(uuid) || (board.isSeeker(uuid) && !board.getFirstSeeker().getUniqueId().equals(uuid)) ? 1 : 0)); - statement.setInt(5, info.getSeekerGames() + (board.getFirstSeeker().getUniqueId().equals(uuid) ? 1 : 0)); - statement.setInt(6, info.getHiderKills() + hider_kills.getOrDefault(uuid.toString(), 0)); - statement.setInt(7, info.getSeekerKills() + seeker_kills.getOrDefault(uuid.toString(), 0)); - statement.setInt(8, info.getHiderDeaths() + hider_deaths.getOrDefault(uuid.toString(), 0)); - statement.setInt(9, info.getSeekerDeaths() + seeker_deaths.getOrDefault(uuid.toString(), 0)); - statement.execute(); - } catch (SQLException e) { - Main.getInstance().getLogger().severe("SQL Error: " + e.getMessage()); - e.printStackTrace(); - return; - } finally { - CACHE.remove(uuid); - } + updateInfo( + database.encodeUUID(info.getUniqueId()), + info.getHiderWins() + (winners.contains(uuid) && type == WinType.HIDER_WIN ? 1 : 0), + info.getSeekerWins() + (winners.contains(uuid) && type == WinType.SEEKER_WIN ? 1 : 0), + info.getHiderGames() + (board.isHider(uuid) || (board.isSeeker(uuid) && !board.getFirstSeeker().getUniqueId().equals(uuid)) ? 1 : 0), + info.getSeekerGames() + (board.getFirstSeeker().getUniqueId().equals(uuid) ? 1 : 0), + info.getHiderKills() + hider_kills.getOrDefault(uuid.toString(), 0), + info.getSeekerKills() + seeker_kills.getOrDefault(uuid.toString(), 0), + info.getHiderDeaths() + hider_deaths.getOrDefault(uuid.toString(), 0), + info.getSeekerDeaths() + seeker_deaths.getOrDefault(uuid.toString(), 0) + ); } } + protected boolean updateInfo(byte[] uuid, int hider_wins, int seeker_wins, int hider_games, int seeker_games, int hider_kills, int seeker_kills, int hider_deaths, int seeker_deaths){ + boolean success; + String sql = "INSERT OR REPLACE INTO hs_data (uuid, hider_wins, seeker_wins, hider_games, seeker_games, hider_kills, seeker_kills, hider_deaths, seeker_deaths) VALUES (?,?,?,?,?,?,?,?,?)"; + try(Connection connection = database.connect(); PreparedStatement statement = connection.prepareStatement(sql)) { + statement.setBytes(1, uuid); + statement.setInt(2, hider_wins); + statement.setInt(3, seeker_wins); + statement.setInt(4, hider_games); + statement.setInt(5, seeker_games); + statement.setInt(6, hider_kills); + statement.setInt(7, seeker_kills); + statement.setInt(8, hider_deaths); + statement.setInt(9, seeker_deaths); + statement.execute(); + statement.close(); + success = true; + } catch (SQLException e) { + Main.getInstance().getLogger().severe("SQL Error: " + e.getMessage()); + e.printStackTrace(); + success = false; + } finally { + CACHE.remove(uuid); + } + return success; + } + } diff --git a/src/main/java/net/tylermurphy/hideAndSeek/database/LegacyTable.java b/src/main/java/net/tylermurphy/hideAndSeek/database/LegacyTable.java new file mode 100644 index 0000000..580c2f5 --- /dev/null +++ b/src/main/java/net/tylermurphy/hideAndSeek/database/LegacyTable.java @@ -0,0 +1,104 @@ +/* + * This file is part of Kenshins Hide and Seek + * + * Copyright (c) 2022 Tyler Murphy. + * + * Kenshins Hide and Seek free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * he Free Software Foundation version 3. + * + * Kenshins Hide and Seek is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +package net.tylermurphy.hideAndSeek.database; + +import net.tylermurphy.hideAndSeek.Main; +import net.tylermurphy.hideAndSeek.database.Database; +import net.tylermurphy.hideAndSeek.database.util.LegacyPlayerInfo; + +import java.sql.Connection; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; +import java.util.ArrayList; +import java.util.List; + +public class LegacyTable { + + private final Database database; + private final boolean exists; + + protected LegacyTable(Database database) { + + String sql = "SELECT * FROM player_info LIMIT 1;"; + + boolean check; + try(Connection connection = database.connect(); Statement statement = connection.createStatement()) { + ResultSet resultSet = statement.executeQuery(sql); + check = resultSet.next(); + } catch (SQLException e) { + check = false; + } + + this.exists = check; + this.database = database; + } + + public boolean exists(){ + return exists; + } + + public boolean copyData(){ + String sql = "SELECT * FROM player_info;"; + List legacyPlayerInfoList = new ArrayList<>(); + try(Connection connection = database.connect(); Statement statement = connection.createStatement()) { + ResultSet resultSet = statement.executeQuery(sql); + while(resultSet.next()){ + legacyPlayerInfoList.add(new LegacyPlayerInfo( + resultSet.getBytes("uuid"), + resultSet.getInt("wins"), + resultSet.getInt("hider_wins"), + resultSet.getInt("seeker_wins"), + resultSet.getInt("games_played") + )); + } + resultSet.close(); + } catch (SQLException e) { + e.printStackTrace(); + return false; + } + for(LegacyPlayerInfo legacyInfo : legacyPlayerInfoList){ + database.getGameData().updateInfo( + legacyInfo.getUniqueId(), + legacyInfo.getHiderWins(), + legacyInfo.getSeekerWins(), + legacyInfo.getGamesPlayer() - legacyInfo.getSeekerWins(), + legacyInfo.getSeekerWins(), + 0, + 0, + 0, + 0 + ); + } + return true; + } + + public boolean drop(){ + String sql = "DROP table player_info"; + try(Connection connection = database.connect(); Statement statement = connection.createStatement()) { + statement.execute(sql); + return true; + } catch (SQLException e) { + e.printStackTrace(); + return false; + } + } + +} diff --git a/src/main/java/net/tylermurphy/hideAndSeek/database/NameDataTable.java b/src/main/java/net/tylermurphy/hideAndSeek/database/NameDataTable.java new file mode 100644 index 0000000..cbf410a --- /dev/null +++ b/src/main/java/net/tylermurphy/hideAndSeek/database/NameDataTable.java @@ -0,0 +1,99 @@ +/* + * This file is part of Kenshins Hide and Seek + * + * Copyright (c) 2022 Tyler Murphy. + * + * Kenshins Hide and Seek free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * he Free Software Foundation version 3. + * + * Kenshins Hide and Seek is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +package net.tylermurphy.hideAndSeek.database; + +import net.tylermurphy.hideAndSeek.Main; +import org.jetbrains.annotations.Nullable; + +import java.sql.*; +import java.util.UUID; + +public class NameDataTable { + + private final Database database; + + protected NameDataTable(Database database) { + + String sql = "CREATE TABLE IF NOT EXISTS hs_names (\n" + + " uuid BINARY(16) NOT NULL,\n" + + " name VARCHAR(48) NOT NULL,\n" + + " PRIMARY KEY (uuid,name)\n" + + ");"; + + try(Connection connection = database.connect(); Statement statement = connection.createStatement()) { + statement.executeUpdate(sql); + } catch (SQLException e) { + Main.getInstance().getLogger().severe("SQL Error: " + e.getMessage()); + e.printStackTrace(); + } + + this.database = database; + } + + @Nullable + public String getName(UUID uuid) { + String sql = "SELECT * FROM hs_names WHERE uuid = ?;"; + try(Connection connection = database.connect(); PreparedStatement statement = connection.prepareStatement(sql)) { + statement.setBytes(1, database.encodeUUID(uuid)); + ResultSet rs = statement.executeQuery(); + if (rs.next()) { + return rs.getString("name"); + } + rs.close(); + } catch (SQLException e) { + Main.getInstance().getLogger().severe("SQL Error: " + e.getMessage()); + e.printStackTrace(); + } + return null; + } + + @Nullable + public UUID getUUID(String name) { + String sql = "SELECT * FROM hs_names WHERE name = ?;"; + try(Connection connection = database.connect(); PreparedStatement statement = connection.prepareStatement(sql)) { + statement.setString(1, name); + ResultSet rs = statement.executeQuery(); + if (rs.next()) { + return database.decodeUUID(rs.getBytes("uuid")); + } + rs.close(); + } catch (SQLException e) { + Main.getInstance().getLogger().severe("SQL Error: " + e.getMessage()); + e.printStackTrace(); + } + return null; + } + + public boolean update(UUID uuid, String name){ + String sql = "INSERT OR REPLACE INTO hs_names (uuid, name) VALUES (?,?)"; + try(Connection connection = database.connect(); PreparedStatement statement = connection.prepareStatement(sql)) { + statement.setBytes(1, database.encodeUUID(uuid)); + statement.setString(2, name); + statement.execute(); + statement.close(); + return true; + } catch (SQLException e) { + Main.getInstance().getLogger().severe("SQL Error: " + e.getMessage()); + e.printStackTrace(); + return false; + } + } + +} diff --git a/src/main/java/net/tylermurphy/hideAndSeek/database/connections/DatabaseConnection.java b/src/main/java/net/tylermurphy/hideAndSeek/database/connections/DatabaseConnection.java new file mode 100644 index 0000000..466bda5 --- /dev/null +++ b/src/main/java/net/tylermurphy/hideAndSeek/database/connections/DatabaseConnection.java @@ -0,0 +1,29 @@ +/* + * This file is part of Kenshins Hide and Seek + * + * Copyright (c) 2022 Tyler Murphy. + * + * Kenshins Hide and Seek free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * he Free Software Foundation version 3. + * + * Kenshins Hide and Seek is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +package net.tylermurphy.hideAndSeek.database.connections; + +import java.sql.Connection; +import java.sql.SQLException; + +public interface DatabaseConnection { + + Connection connect() throws SQLException; + +} diff --git a/src/main/java/net/tylermurphy/hideAndSeek/database/connections/MySQLConnection.java b/src/main/java/net/tylermurphy/hideAndSeek/database/connections/MySQLConnection.java new file mode 100644 index 0000000..c29d1b3 --- /dev/null +++ b/src/main/java/net/tylermurphy/hideAndSeek/database/connections/MySQLConnection.java @@ -0,0 +1,61 @@ +/* + * This file is part of Kenshins Hide and Seek + * + * Copyright (c) 2022 Tyler Murphy. + * + * Kenshins Hide and Seek free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * he Free Software Foundation version 3. + * + * Kenshins Hide and Seek is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +package net.tylermurphy.hideAndSeek.database.connections; + +import com.zaxxer.hikari.HikariConfig; +import com.zaxxer.hikari.HikariDataSource; + +import java.sql.Connection; +import java.sql.SQLException; + +public class MySQLConnection implements DatabaseConnection { + + private final HikariConfig config; + private final HikariDataSource ds; + + public MySQLConnection(){ + + String host = "to be implemented"; + String port = "to be implemented"; + String user = "to be implemented"; + String pass = "to be implemented"; + + config = new HikariConfig(); + + config.setJdbcUrl("jdbc:mariadb://"+host+":"+port+"/kenbot"); + config.addDataSourceProperty("cachePrepStmts", "true"); + config.addDataSourceProperty("prepStmtCacheSize", "250"); + config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048"); + config.addDataSourceProperty("user", user); + config.addDataSourceProperty("password",pass); + config.addDataSourceProperty("autoCommit", "true"); + config.setAutoCommit(true); + config.setMaximumPoolSize(20); + + ds = new HikariDataSource(config); + + } + + @Override + public Connection connect() throws SQLException { + return ds.getConnection(); + } + +} diff --git a/src/main/java/net/tylermurphy/hideAndSeek/database/connections/SQLiteConnection.java b/src/main/java/net/tylermurphy/hideAndSeek/database/connections/SQLiteConnection.java new file mode 100644 index 0000000..1a03106 --- /dev/null +++ b/src/main/java/net/tylermurphy/hideAndSeek/database/connections/SQLiteConnection.java @@ -0,0 +1,64 @@ +/* + * This file is part of Kenshins Hide and Seek + * + * Copyright (c) 2022 Tyler Murphy. + * + * Kenshins Hide and Seek free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * he Free Software Foundation version 3. + * + * Kenshins Hide and Seek is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +package net.tylermurphy.hideAndSeek.database.connections; + +import net.tylermurphy.hideAndSeek.Main; +import org.sqlite.SQLiteConfig; + +import java.io.File; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.SQLException; + +public class SQLiteConnection implements DatabaseConnection { + + private final File databaseFile; + private final SQLiteConfig config; + + public SQLiteConnection(){ + + try { + Class.forName("org.sqlite.JDBC"); + } catch (ClassNotFoundException e) { + Main.getInstance().getLogger().severe(e.getMessage()); + throw new RuntimeException(e.getMessage()); + } + + databaseFile = new File(Main.getInstance().getDataFolder(), "database.db"); + + config = new SQLiteConfig(); + config.setSynchronous(SQLiteConfig.SynchronousMode.NORMAL); + config.setTempStore(SQLiteConfig.TempStore.MEMORY); + } + + @Override + public Connection connect() { + Connection conn = null; + try { + String url = "jdbc:sqlite:"+databaseFile; + conn = DriverManager.getConnection(url, config.toProperties()); + } catch (SQLException e) { + Main.getInstance().getLogger().severe(e.getMessage()); + e.printStackTrace(); + } + return conn; + } + +} diff --git a/src/main/java/net/tylermurphy/hideAndSeek/database/util/LegacyPlayerInfo.java b/src/main/java/net/tylermurphy/hideAndSeek/database/util/LegacyPlayerInfo.java new file mode 100644 index 0000000..5b59779 --- /dev/null +++ b/src/main/java/net/tylermurphy/hideAndSeek/database/util/LegacyPlayerInfo.java @@ -0,0 +1,56 @@ +/* + * This file is part of Kenshins Hide and Seek + * + * Copyright (c) 2021-2022 Tyler Murphy. + * + * Kenshins Hide and Seek free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * he Free Software Foundation version 3. + * + * Kenshins Hide and Seek is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +package net.tylermurphy.hideAndSeek.database.util; + +import java.util.UUID; + +public class LegacyPlayerInfo { + + private final byte[] uniqueId; + private final int totalWins; + private final int hiderWins; + private final int seekerWins; + private final int gamesPlayed; + + public LegacyPlayerInfo(byte[] uniqueId, int totalWins, int hiderWins, int seekerWins, int gamesPlayed) { + this.uniqueId = uniqueId; + this.totalWins = totalWins; + this.hiderWins = hiderWins; + this.seekerWins = seekerWins; + this.gamesPlayed = gamesPlayed; + } + + public byte[] getUniqueId() { + return uniqueId; + } + + public int getTotalWins() { return totalWins; } + + public int getHiderWins() { + return hiderWins; + } + + public int getSeekerWins() { + return seekerWins; + } + + public int getGamesPlayer() { return gamesPlayed; } + +} diff --git a/src/main/java/net/tylermurphy/hideAndSeek/database/PlayerInfo.java b/src/main/java/net/tylermurphy/hideAndSeek/database/util/PlayerInfo.java similarity index 95% rename from src/main/java/net/tylermurphy/hideAndSeek/database/PlayerInfo.java rename to src/main/java/net/tylermurphy/hideAndSeek/database/util/PlayerInfo.java index 5c74e76..96fe9d8 100644 --- a/src/main/java/net/tylermurphy/hideAndSeek/database/PlayerInfo.java +++ b/src/main/java/net/tylermurphy/hideAndSeek/database/util/PlayerInfo.java @@ -1,7 +1,7 @@ /* * This file is part of Kenshins Hide and Seek * - * Copyright (c) 2021 Tyler Murphy. + * Copyright (c) 2021-2022 Tyler Murphy. * * Kenshins Hide and Seek free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -17,7 +17,7 @@ * */ -package net.tylermurphy.hideAndSeek.database; +package net.tylermurphy.hideAndSeek.database.util; import java.util.UUID; diff --git a/src/main/java/net/tylermurphy/hideAndSeek/game/listener/JoinLeaveHandler.java b/src/main/java/net/tylermurphy/hideAndSeek/game/listener/JoinLeaveHandler.java index 37865b0..094ff68 100644 --- a/src/main/java/net/tylermurphy/hideAndSeek/game/listener/JoinLeaveHandler.java +++ b/src/main/java/net/tylermurphy/hideAndSeek/game/listener/JoinLeaveHandler.java @@ -23,6 +23,9 @@ public class JoinLeaveHandler implements Listener { @EventHandler(priority = EventPriority.HIGHEST) public void onPlayerJoin(PlayerJoinEvent event) { + if(!Main.getInstance().getDatabase().getNameData().update(event.getPlayer().getUniqueId(), event.getPlayer().getDisplayName())){ + Main.getInstance().getLogger().warning("Failed to save name data for user: " + event.getPlayer().getDisplayName()); + } Main.getInstance().getBoard().remove(event.getPlayer()); removeItems(event.getPlayer()); if (Main.getInstance().getGame().isNotSetup()) return; diff --git a/src/main/java/net/tylermurphy/hideAndSeek/util/PAPIExpansion.java b/src/main/java/net/tylermurphy/hideAndSeek/util/PAPIExpansion.java index c1f8c2a..72d1f4d 100644 --- a/src/main/java/net/tylermurphy/hideAndSeek/util/PAPIExpansion.java +++ b/src/main/java/net/tylermurphy/hideAndSeek/util/PAPIExpansion.java @@ -3,7 +3,7 @@ package net.tylermurphy.hideAndSeek.util; import me.clip.placeholderapi.expansion.PlaceholderExpansion; import net.tylermurphy.hideAndSeek.Main; import net.tylermurphy.hideAndSeek.database.Database; -import net.tylermurphy.hideAndSeek.database.PlayerInfo; +import net.tylermurphy.hideAndSeek.database.util.PlayerInfo; import org.bukkit.OfflinePlayer; import org.jetbrains.annotations.NotNull; diff --git a/src/test/java/MainTest.java b/src/test/java/MainTest.java new file mode 100644 index 0000000..9997e04 --- /dev/null +++ b/src/test/java/MainTest.java @@ -0,0 +1,42 @@ +/* + * This file is part of Kenshins Hide and Seek + * + * Copyright (c) 2022 Tyler Murphy. + * + * Kenshins Hide and Seek free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * he Free Software Foundation version 3. + * + * Kenshins Hide and Seek is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +import be.seeseemelk.mockbukkit.MockBukkit; +import be.seeseemelk.mockbukkit.ServerMock; +import net.tylermurphy.hideAndSeek.Main; +import org.junit.After; +import org.junit.Before; + +public class MainTest { + + private ServerMock server; + private Main plugin; + + @Before + public void setUp() { + server = MockBukkit.mock(); + plugin = MockBukkit.load(Main.class); + } + + @After + public void tearDown() { + MockBukkit.unmock(); + } + +}