working on expanding database
This commit is contained in:
parent
491e426ce9
commit
b74b813636
16 changed files with 569 additions and 60 deletions
28
pom.xml
28
pom.xml
|
@ -30,18 +30,18 @@
|
||||||
</relocation>
|
</relocation>
|
||||||
</relocations>
|
</relocations>
|
||||||
<artifactSet>
|
<artifactSet>
|
||||||
<excludes>
|
<includes>
|
||||||
<exclude>org.spigotmc:spigot-api</exclude>
|
<include>com.github.cryptomorin:XSeries</include>
|
||||||
<exclude>com.comphenix.protocol:ProtocolLib</exclude>
|
<include>org.xerial:sqlite-jdbc</include>
|
||||||
<exclude>org.jetbrains:annotations</exclude>
|
<include>com.zaxxer:HikariCP</include>
|
||||||
<exclude>net.bytebuddy:byte-buddy</exclude>
|
</includes>
|
||||||
</excludes>
|
|
||||||
</artifactSet>
|
</artifactSet>
|
||||||
<filters>
|
<filters>
|
||||||
<filter>
|
<filter>
|
||||||
<artifact>*:*</artifact>
|
<artifact>*:*</artifact>
|
||||||
<excludes>
|
<excludes>
|
||||||
<exclude>META-INF/*.MF</exclude>
|
<exclude>META-INF/*.MF</exclude>
|
||||||
|
<exclude>META-INF/*.md</exclude>
|
||||||
<exclude>META-INF</exclude>
|
<exclude>META-INF</exclude>
|
||||||
<exclude>sqlite-jdbc.properties</exclude>
|
<exclude>sqlite-jdbc.properties</exclude>
|
||||||
</excludes>
|
</excludes>
|
||||||
|
@ -113,5 +113,21 @@
|
||||||
<version>2.11.1</version>
|
<version>2.11.1</version>
|
||||||
<scope>provided</scope>
|
<scope>provided</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.zaxxer</groupId>
|
||||||
|
<artifactId>HikariCP</artifactId>
|
||||||
|
<version>5.0.1</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.junit.jupiter</groupId>
|
||||||
|
<artifactId>junit-jupiter</artifactId>
|
||||||
|
<version>5.9.0-M1</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.github.seeseemelk</groupId>
|
||||||
|
<artifactId>MockBukkit-v1.17</artifactId>
|
||||||
|
<version>1.13.0</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</project>
|
</project>
|
|
@ -19,6 +19,7 @@
|
||||||
|
|
||||||
package net.tylermurphy.hideAndSeek;
|
package net.tylermurphy.hideAndSeek;
|
||||||
|
|
||||||
|
import jdk.jpackage.internal.IOUtils;
|
||||||
import net.tylermurphy.hideAndSeek.configuration.Config;
|
import net.tylermurphy.hideAndSeek.configuration.Config;
|
||||||
import net.tylermurphy.hideAndSeek.configuration.Items;
|
import net.tylermurphy.hideAndSeek.configuration.Items;
|
||||||
import net.tylermurphy.hideAndSeek.configuration.Localization;
|
import net.tylermurphy.hideAndSeek.configuration.Localization;
|
||||||
|
@ -34,7 +35,9 @@ import org.bukkit.Bukkit;
|
||||||
import org.bukkit.command.Command;
|
import org.bukkit.command.Command;
|
||||||
import org.bukkit.command.CommandSender;
|
import org.bukkit.command.CommandSender;
|
||||||
import org.bukkit.event.Listener;
|
import org.bukkit.event.Listener;
|
||||||
|
import org.bukkit.plugin.PluginDescriptionFile;
|
||||||
import org.bukkit.plugin.java.JavaPlugin;
|
import org.bukkit.plugin.java.JavaPlugin;
|
||||||
|
import org.bukkit.plugin.java.JavaPluginLoader;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
@ -47,13 +50,26 @@ public class Main extends JavaPlugin implements Listener {
|
||||||
private static Main instance;
|
private static Main instance;
|
||||||
private static int version;
|
private static int version;
|
||||||
|
|
||||||
private Database database;
|
private final Database database;
|
||||||
private Board board;
|
private final Board board;
|
||||||
|
|
||||||
private Game game;
|
private Game game;
|
||||||
|
|
||||||
public void onEnable() {
|
public Main() {
|
||||||
|
super();
|
||||||
instance = this;
|
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();
|
this.registerListeners();
|
||||||
|
|
||||||
|
@ -63,9 +79,6 @@ public class Main extends JavaPlugin implements Listener {
|
||||||
|
|
||||||
CommandHandler.registerCommands();
|
CommandHandler.registerCommands();
|
||||||
|
|
||||||
board = new Board();
|
|
||||||
database = new Database();
|
|
||||||
|
|
||||||
game = new Game(board);
|
game = new Game(board);
|
||||||
|
|
||||||
getServer().getScheduler().runTaskTimer(this, this::onTick,0,1).getTaskId();
|
getServer().getScheduler().runTaskTimer(this, this::onTick,0,1).getTaskId();
|
||||||
|
|
|
@ -20,7 +20,7 @@
|
||||||
package net.tylermurphy.hideAndSeek.command;
|
package net.tylermurphy.hideAndSeek.command;
|
||||||
|
|
||||||
import net.tylermurphy.hideAndSeek.Main;
|
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.ChatColor;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
|
|
||||||
|
|
|
@ -20,7 +20,7 @@
|
||||||
package net.tylermurphy.hideAndSeek.command;
|
package net.tylermurphy.hideAndSeek.command;
|
||||||
|
|
||||||
import net.tylermurphy.hideAndSeek.Main;
|
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.ChatColor;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
|
|
||||||
|
|
|
@ -21,48 +21,51 @@ package net.tylermurphy.hideAndSeek.database;
|
||||||
|
|
||||||
import com.google.common.io.ByteStreams;
|
import com.google.common.io.ByteStreams;
|
||||||
import net.tylermurphy.hideAndSeek.Main;
|
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.ByteArrayInputStream;
|
||||||
import java.io.File;
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.nio.ByteBuffer;
|
import java.nio.ByteBuffer;
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
import java.sql.DriverManager;
|
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
public class Database {
|
public class Database {
|
||||||
|
|
||||||
private final File databaseFile = new File(Main.getInstance().getDataFolder(), "database.db");
|
private final GameDataTable playerInfo;
|
||||||
private final PlayerInfoTable playerInfo;
|
private final NameDataTable nameInfo;
|
||||||
private final SQLiteConfig config;
|
private final DatabaseConnection connection;
|
||||||
|
|
||||||
public Database(){
|
public Database(){
|
||||||
try {
|
|
||||||
Class.forName("org.sqlite.JDBC");
|
connection = new SQLiteConnection();
|
||||||
} catch (ClassNotFoundException e) {
|
|
||||||
Main.getInstance().getLogger().severe(e.getMessage());
|
playerInfo = new GameDataTable(this);
|
||||||
throw new RuntimeException(e.getMessage());
|
|
||||||
|
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;
|
return playerInfo;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public NameDataTable getNameData() { return nameInfo; }
|
||||||
|
|
||||||
protected Connection connect() {
|
protected Connection connect() {
|
||||||
Connection conn = null;
|
Connection conn = null;
|
||||||
try {
|
try {
|
||||||
String url = "jdbc:sqlite:"+databaseFile;
|
conn = connection.connect();
|
||||||
conn = DriverManager.getConnection(url, config.toProperties());
|
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
Main.getInstance().getLogger().severe(e.getMessage());
|
Main.getInstance().getLogger().severe(e.getMessage());
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
/*
|
/*
|
||||||
* This file is part of Kenshins Hide and Seek
|
* 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
|
* 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
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
@ -20,6 +20,7 @@
|
||||||
package net.tylermurphy.hideAndSeek.database;
|
package net.tylermurphy.hideAndSeek.database;
|
||||||
|
|
||||||
import net.tylermurphy.hideAndSeek.Main;
|
import net.tylermurphy.hideAndSeek.Main;
|
||||||
|
import net.tylermurphy.hideAndSeek.database.util.PlayerInfo;
|
||||||
import net.tylermurphy.hideAndSeek.game.Board;
|
import net.tylermurphy.hideAndSeek.game.Board;
|
||||||
import net.tylermurphy.hideAndSeek.game.util.WinType;
|
import net.tylermurphy.hideAndSeek.game.util.WinType;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
@ -27,12 +28,12 @@ import org.jetbrains.annotations.Nullable;
|
||||||
import java.sql.*;
|
import java.sql.*;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
public class PlayerInfoTable {
|
public class GameDataTable {
|
||||||
|
|
||||||
private final Map<UUID, PlayerInfo> CACHE = new HashMap<>();
|
private final Map<UUID, PlayerInfo> CACHE = new HashMap<>();
|
||||||
private final Database database;
|
private final Database database;
|
||||||
|
|
||||||
protected PlayerInfoTable(Database database) {
|
protected GameDataTable(Database database) {
|
||||||
|
|
||||||
String sql = "CREATE TABLE IF NOT EXISTS hs_data (\n"
|
String sql = "CREATE TABLE IF NOT EXISTS hs_data (\n"
|
||||||
+ " uuid BINARY(16) PRIMARY KEY,\n"
|
+ " uuid BINARY(16) PRIMARY KEY,\n"
|
||||||
|
@ -170,30 +171,48 @@ public class PlayerInfoTable {
|
||||||
|
|
||||||
public void addWins(Board board, List<UUID> uuids, List<UUID> winners, Map<String,Integer> hider_kills, Map<String,Integer> hider_deaths, Map<String,Integer> seeker_kills, Map<String,Integer> seeker_deaths, WinType type) {
|
public void addWins(Board board, List<UUID> uuids, List<UUID> winners, Map<String,Integer> hider_kills, Map<String,Integer> hider_deaths, Map<String,Integer> seeker_kills, Map<String,Integer> seeker_deaths, WinType type) {
|
||||||
for(UUID uuid : uuids) {
|
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);
|
PlayerInfo info = getInfo(uuid);
|
||||||
if(info == null){
|
if(info == null){
|
||||||
info = new PlayerInfo(uuid, 0, 0, 0, 0, 0, 0, 0, 0);
|
info = new PlayerInfo(uuid, 0, 0, 0, 0, 0, 0, 0, 0);
|
||||||
}
|
}
|
||||||
try(Connection connection = database.connect(); PreparedStatement statement = connection.prepareStatement(sql)) {
|
updateInfo(
|
||||||
statement.setBytes(1, database.encodeUUID(uuid));
|
database.encodeUUID(info.getUniqueId()),
|
||||||
statement.setInt(2, info.getHiderWins() + (winners.contains(uuid) && type == WinType.HIDER_WIN ? 1 : 0));
|
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));
|
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));
|
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));
|
info.getSeekerGames() + (board.getFirstSeeker().getUniqueId().equals(uuid) ? 1 : 0),
|
||||||
statement.setInt(6, info.getHiderKills() + hider_kills.getOrDefault(uuid.toString(), 0));
|
info.getHiderKills() + hider_kills.getOrDefault(uuid.toString(), 0),
|
||||||
statement.setInt(7, info.getSeekerKills() + seeker_kills.getOrDefault(uuid.toString(), 0));
|
info.getSeekerKills() + seeker_kills.getOrDefault(uuid.toString(), 0),
|
||||||
statement.setInt(8, info.getHiderDeaths() + hider_deaths.getOrDefault(uuid.toString(), 0));
|
info.getHiderDeaths() + hider_deaths.getOrDefault(uuid.toString(), 0),
|
||||||
statement.setInt(9, info.getSeekerDeaths() + seeker_deaths.getOrDefault(uuid.toString(), 0));
|
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -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 <http://www.gnu.org/licenses/>.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
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<LegacyPlayerInfo> 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -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 <http://www.gnu.org/licenses/>.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -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 <http://www.gnu.org/licenses/>.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
package net.tylermurphy.hideAndSeek.database.connections;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
|
||||||
|
public interface DatabaseConnection {
|
||||||
|
|
||||||
|
Connection connect() throws SQLException;
|
||||||
|
|
||||||
|
}
|
|
@ -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 <http://www.gnu.org/licenses/>.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -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 <http://www.gnu.org/licenses/>.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -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 <http://www.gnu.org/licenses/>.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
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; }
|
||||||
|
|
||||||
|
}
|
|
@ -1,7 +1,7 @@
|
||||||
/*
|
/*
|
||||||
* This file is part of Kenshins Hide and Seek
|
* 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
|
* 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
|
* 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;
|
import java.util.UUID;
|
||||||
|
|
|
@ -23,6 +23,9 @@ public class JoinLeaveHandler implements Listener {
|
||||||
|
|
||||||
@EventHandler(priority = EventPriority.HIGHEST)
|
@EventHandler(priority = EventPriority.HIGHEST)
|
||||||
public void onPlayerJoin(PlayerJoinEvent event) {
|
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());
|
Main.getInstance().getBoard().remove(event.getPlayer());
|
||||||
removeItems(event.getPlayer());
|
removeItems(event.getPlayer());
|
||||||
if (Main.getInstance().getGame().isNotSetup()) return;
|
if (Main.getInstance().getGame().isNotSetup()) return;
|
||||||
|
|
|
@ -3,7 +3,7 @@ package net.tylermurphy.hideAndSeek.util;
|
||||||
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
|
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
|
||||||
import net.tylermurphy.hideAndSeek.Main;
|
import net.tylermurphy.hideAndSeek.Main;
|
||||||
import net.tylermurphy.hideAndSeek.database.Database;
|
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.bukkit.OfflinePlayer;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
|
|
42
src/test/java/MainTest.java
Normal file
42
src/test/java/MainTest.java
Normal file
|
@ -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 <http://www.gnu.org/licenses/>.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in a new issue