From 9d902a8453dc7c11721940d720be7186b3d0c8e8 Mon Sep 17 00:00:00 2001 From: BuildTools Date: Tue, 8 Dec 2020 17:56:03 +0000 Subject: [PATCH] Updated SQL handling --- .../huskhomes2/Data/SQLite/Database.java | 57 ++------- .../huskhomes2/Data/SQLite/SQLite.java | 51 +------- .../william278/huskhomes2/Objects/Home.java | 8 ++ .../me/william278/huskhomes2/dataManager.java | 116 +++++++++++++++++- 4 files changed, 138 insertions(+), 94 deletions(-) diff --git a/src/main/java/me/william278/huskhomes2/Data/SQLite/Database.java b/src/main/java/me/william278/huskhomes2/Data/SQLite/Database.java index 89c5994b..8171a19e 100644 --- a/src/main/java/me/william278/huskhomes2/Data/SQLite/Database.java +++ b/src/main/java/me/william278/huskhomes2/Data/SQLite/Database.java @@ -14,9 +14,7 @@ import org.bukkit.entity.Player; public abstract class Database { Main plugin; Connection connection; - // The name of the table we created back in SQLite class. - public String table = "table_name"; - public int tokens = 0; + public Database(Main instance){ plugin = instance; } @@ -28,31 +26,24 @@ public abstract class Database { public void initialize(){ connection = getSQLConnection(); try{ - PreparedStatement ps = connection.prepareStatement("SELECT * FROM " + table + " WHERE player = ?"); + PreparedStatement ps = connection.prepareStatement("SELECT * FROM " + Main.settings.getPlayerTable() + " WHERE uuid = ?"); ResultSet rs = ps.executeQuery(); close(ps,rs); } catch (SQLException ex) { - plugin.getLogger().log(Level.SEVERE, "Unable to retreive connection", ex); + plugin.getLogger().log(Level.SEVERE, "Unable to retrieve connection", ex); } } - // These are the methods you can use to get things out of your database. You of course can make new ones to return different things in the database. - // This returns the number of people the player killed. - public Integer getTokens(String string) { + public ResultSet queryDatabase(String query) { Connection conn = null; PreparedStatement ps = null; - ResultSet rs = null; + ResultSet rs; try { conn = getSQLConnection(); - ps = conn.prepareStatement("SELECT * FROM " + table + " WHERE player = '"+string+"';"); - + ps = conn.prepareStatement(query); rs = ps.executeQuery(); - while(rs.next()){ - if(rs.getString("player").equalsIgnoreCase(string.toLowerCase())){ // Tell database to search for the player you sent into the method. e.g getTokens(sam) It will look for sam. - return rs.getInt("kills"); // Return the players ammount of kills. If you wanted to get total (just a random number for an example for you guys) You would change this to total! - } - } + return rs; } catch (SQLException ex) { plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex); } finally { @@ -65,37 +56,11 @@ public abstract class Database { plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionClose(), ex); } } - return 0; + return null; } - // Exact same method here, Except as mentioned above i am looking for total! - public Integer getTotal(String string) { - Connection conn = null; - PreparedStatement ps = null; - ResultSet rs = null; - try { - conn = getSQLConnection(); - ps = conn.prepareStatement("SELECT * FROM " + table + " WHERE player = '"+string+"';"); - rs = ps.executeQuery(); - while(rs.next()){ - if(rs.getString("player").equalsIgnoreCase(string.toLowerCase())){ - return rs.getInt("total"); - } - } - } catch (SQLException ex) { - plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex); - } finally { - try { - if (ps != null) - ps.close(); - if (conn != null) - conn.close(); - } catch (SQLException ex) { - plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionClose(), ex); - } - } - return 0; - } + + // Now we need methods to save things to the database public void setTokens(Player player, Integer tokens, Integer total) { @@ -133,7 +98,7 @@ public abstract class Database { } - public void close(PreparedStatement ps,ResultSet rs){ + public void close(PreparedStatement ps, ResultSet rs){ try { if (ps != null) ps.close(); diff --git a/src/main/java/me/william278/huskhomes2/Data/SQLite/SQLite.java b/src/main/java/me/william278/huskhomes2/Data/SQLite/SQLite.java index 8f74efda..f7a2ede7 100644 --- a/src/main/java/me/william278/huskhomes2/Data/SQLite/SQLite.java +++ b/src/main/java/me/william278/huskhomes2/Data/SQLite/SQLite.java @@ -1,6 +1,7 @@ package me.william278.huskhomes2.Data.SQLite; import me.william278.huskhomes2.Main; +import me.william278.huskhomes2.dataManager; import java.io.File; import java.io.IOException; @@ -18,48 +19,6 @@ public class SQLite extends Database { super(instance); } - public String createPlayerTable = "CREATE TABLE IF NOT EXISTS " + Main.settings.getPlayerTable() + " (" + - "`player_id` integer PRIMARY KEY," + - "`uuid` text NOT NULL UNIQUE," + - "`username` text NOT NULL," + - "`home_count` integer NOT NULL," + - "`home_slots` integer NOT NULL," + - "`rtp_cooldown` integer NOT NULL," + - "`home_count` integer NOT NULL" + - ");"; - - public String createLocationsTable = "CREATE TABLE IF NOT EXISTS " + Main.settings.getLocationsTable() + " (" + - "`location_id` integer PRIMARY KEY," + - "`world` text NOT NULL," + - "`server` text NOT NULL," + - "`x` double NOT NULL," + - "`y` double NOT NULL," + - "`z` double NOT NULL," + - "`yaw` float NOT NULL," + - "`pitch` float NOT NULL" + - ");"; - - public String createHomesTable = "CREATE TABLE IF NOT EXISTS " + Main.settings.getHomesTable() + " (" + - "`player_id` integer NOT NULL," + - "`location_id` integer NOT NULL," + - "`name` text NOT NULL," + - "`description` double NOT NULL," + - "`public` boolean NOT NULL," + - - "PRIMARY KEY (`player_id`, `location_id`)," + - "FOREIGN KEY (`player_id`) REFERENCES " + Main.settings.getPlayerTable() + " (`player_id`) ON DELETE CASCADE ON UPDATE NO ACTION," + - "FOREIGN KEY (`location_id`) REFERENCES " + Main.settings.getLocationsTable() + " (`location_id`) ON DELETE CASCADE ON UPDATE NO ACTION," + - ");"; - - public String createWarpsTable = "CREATE TABLE IF NOT EXISTS " + Main.settings.getWarpsTable() + " (" + - "`location_id` integer NOT NULL," + - "`name` text NOT NULL," + - "`description` double NOT NULL," + - - "PRIMARY KEY (`location_id`)," + - "FOREIGN KEY (`location_id`) REFERENCES " + Main.settings.getLocationsTable() + " (`location_id`) ON DELETE CASCADE ON UPDATE NO ACTION," + - ");"; - // SQL creation stuff, You can leave the blow stuff untouched. public Connection getSQLConnection() { File dataFolder = new File(plugin.getDataFolder(), databaseName + ".db"); @@ -89,10 +48,10 @@ public class SQLite extends Database { connection = getSQLConnection(); try { Statement s = connection.createStatement(); - s.executeUpdate(createPlayerTable); - s.executeUpdate(createLocationsTable); - s.executeUpdate(createHomesTable); - s.executeUpdate(createWarpsTable); + s.executeUpdate(dataManager.createPlayerTable); + s.executeUpdate(dataManager.createLocationsTable); + s.executeUpdate(dataManager.createHomesTable); + s.executeUpdate(dataManager.createWarpsTable); s.close(); } catch (SQLException e) { e.printStackTrace(); diff --git a/src/main/java/me/william278/huskhomes2/Objects/Home.java b/src/main/java/me/william278/huskhomes2/Objects/Home.java index 2fe598d5..e68ff538 100644 --- a/src/main/java/me/william278/huskhomes2/Objects/Home.java +++ b/src/main/java/me/william278/huskhomes2/Objects/Home.java @@ -24,6 +24,14 @@ public class Home extends TeleportationPoint { this.isPublic = isPublic; } + public Home(TeleportationPoint teleportationPoint, String ownerUsername, String ownerUUID, String name, String description, boolean isPublic) { + super(teleportationPoint.worldName, teleportationPoint.x, teleportationPoint.y, teleportationPoint.z, teleportationPoint.yaw, teleportationPoint.pitch, teleportationPoint.server); + this.ownerUsername = ownerUsername; + this.ownerUUID = UUID.fromString(ownerUUID); + this.name = name; + this.isPublic = isPublic; + } + public boolean isPublic() { return isPublic; } diff --git a/src/main/java/me/william278/huskhomes2/dataManager.java b/src/main/java/me/william278/huskhomes2/dataManager.java index 130cf658..8afb8087 100644 --- a/src/main/java/me/william278/huskhomes2/dataManager.java +++ b/src/main/java/me/william278/huskhomes2/dataManager.java @@ -2,11 +2,58 @@ package me.william278.huskhomes2; import me.william278.huskhomes2.Data.SQLite.Database; import me.william278.huskhomes2.Data.SQLite.SQLite; +import me.william278.huskhomes2.Objects.Home; +import me.william278.huskhomes2.Objects.TeleportationPoint; import org.bukkit.Bukkit; +import java.sql.ResultSet; +import java.sql.SQLException; + // This class handles the saving of data; whether that be through SQLite or SQL public class dataManager { + public static String createPlayerTable = "CREATE TABLE IF NOT EXISTS " + Main.settings.getPlayerTable() + " (" + + "`player_id` integer PRIMARY KEY," + + "`uuid` text NOT NULL UNIQUE," + + "`username` text NOT NULL," + + "`home_count` integer NOT NULL," + + "`home_slots` integer NOT NULL," + + "`rtp_cooldown` integer NOT NULL," + + "`home_count` integer NOT NULL" + + ");"; + + public static String createLocationsTable = "CREATE TABLE IF NOT EXISTS " + Main.settings.getLocationsTable() + " (" + + "`location_id` integer PRIMARY KEY," + + "`world` text NOT NULL," + + "`server` text NOT NULL," + + "`x` double NOT NULL," + + "`y` double NOT NULL," + + "`z` double NOT NULL," + + "`yaw` float NOT NULL," + + "`pitch` float NOT NULL" + + ");"; + + public static String createHomesTable = "CREATE TABLE IF NOT EXISTS " + Main.settings.getHomesTable() + " (" + + "`player_id` integer NOT NULL," + + "`location_id` integer NOT NULL," + + "`name` text NOT NULL," + + "`description` double NOT NULL," + + "`public` boolean NOT NULL," + + + "PRIMARY KEY (`player_id`, `location_id`)," + + "FOREIGN KEY (`player_id`) REFERENCES " + Main.settings.getPlayerTable() + " (`player_id`) ON DELETE CASCADE ON UPDATE NO ACTION," + + "FOREIGN KEY (`location_id`) REFERENCES " + Main.settings.getLocationsTable() + " (`location_id`) ON DELETE CASCADE ON UPDATE NO ACTION," + + ");"; + + public static String createWarpsTable = "CREATE TABLE IF NOT EXISTS " + Main.settings.getWarpsTable() + " (" + + "`location_id` integer NOT NULL," + + "`name` text NOT NULL," + + "`description` double NOT NULL," + + + "PRIMARY KEY (`location_id`)," + + "FOREIGN KEY (`location_id`) REFERENCES " + Main.settings.getLocationsTable() + " (`location_id`) ON DELETE CASCADE ON UPDATE NO ACTION," + + ");"; + private static Database sqliteDatabase; private static void initializeSQLite() { @@ -18,10 +65,75 @@ public class dataManager { } + public ResultSet queryDatabase(String query) { + if (Main.settings.getStorageType().equalsIgnoreCase("mysql")) { + return null; + } else { + return sqliteDatabase.queryDatabase(query); + } + } + + public Home getHome(String ownerUsername, String homeName) { + try { + Integer playerID = getPlayerId(ownerUsername); + ResultSet rs = queryDatabase("SELECT * FROM " + Main.settings.getPlayerTable() + " WHERE `player_id`=" + (playerID) + " AND `name`=" + homeName +";"); + if (rs.next()) { + return new Home(getTransportationPoint(rs.getInt("location_id")), ownerUsername, getPlayerUUID(playerID), rs.getString("name"), rs.getString("description"), rs.getBoolean("public")); + } else { + return null; + } + } catch (SQLException e) { + Bukkit.getLogger().severe("An SQL exception occurred returning a home!"); + return null; + } + } + + private Integer getPlayerId(String playerUsername) { + try { + ResultSet rs = queryDatabase("SELECT * FROM " + Main.settings.getPlayerTable() + " WHERE `username`=" + (playerUsername) + ";"); + if (rs.next()) { + return rs.getInt("player_id"); + } else { + return null; + } + } catch (SQLException e) { + Bukkit.getLogger().severe("An SQL exception occurred returning a player ID!"); + return null; + } + } + + public String getPlayerUUID(int playerID) { + try { + ResultSet rs = queryDatabase("SELECT * FROM " + Main.settings.getLocationsTable() + " WHERE `player_id`=" + (playerID) + ";"); + if (rs.next()) { + return rs.getString("uuid"); + } else { + return null; + } + } catch (SQLException e) { + Bukkit.getLogger().severe("An SQL exception occurred returning a player UUID!"); + return null; + } + } + + public TeleportationPoint getTransportationPoint(int locationID) { + try { + ResultSet rs = queryDatabase("SELECT * FROM " + Main.settings.getLocationsTable() + " WHERE `location_id`=" + (locationID) + ";"); + if (rs.next()) { + return new TeleportationPoint(rs.getString("world"), rs.getDouble("x"), rs.getDouble("y"), rs.getDouble("z"), rs.getFloat("yaw"), rs.getFloat("pitch"), rs.getString("server")); + } else { + return null; + } + } catch (SQLException e) { + Bukkit.getLogger().severe("An SQL exception occurred returning a transportation point!"); + return null; + } + } + public static void setupStorage(String storageType) { - if (storageType.toLowerCase().equals("sqlite")) { + if (storageType.equalsIgnoreCase("sqlite")) { initializeSQLite(); - } else if (storageType.toLowerCase().equals("mysql")) { + } else if (storageType.equalsIgnoreCase("mysql")) { initializeMySQL(); } else { Bukkit.getLogger().warning("Invalid storage method set in config.yml; defaulting to SQLite");