Almost completed SQL handling

This commit is contained in:
BuildTools committed 2020-12-10 22:42:51 +00:00
1 parent b95629a986
commit cf1ffdb611
9 files changed
+488 -89

No files matched your search

@@ -29,7 +29,7 @@ public abstract class Database {
public void initialize() {
connection = getSQLConnection();
try {
PreparedStatement ps = connection.prepareStatement("SELECT * FROM " + Main.settings.getPlayerTable() + " WHERE uuid = ?");
PreparedStatement ps = connection.prepareStatement("SELECT * FROM " + Main.settings.getPlayerTable() + " WHERE `uuid`=?");
ResultSet rs = ps.executeQuery();
close(ps, rs);
@@ -139,6 +139,61 @@ public abstract class Database {
}
}
// Delete a warp
public void deleteWarp(String warpName) {
Connection conn = null;
PreparedStatement ps = null;
try {
conn = getSQLConnection();
// Delete the warp with the given name
ps = conn.prepareStatement("DELETE FROM " + Main.settings.getWarpsTable() + " WHERE `name`=?;");
ps.setString(1, warpName);
ps.executeUpdate();
} 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);
}
}
}
// Delete a warp
public void deleteHome(String homeName, int playerID) {
Connection conn = null;
PreparedStatement ps = null;
try {
conn = getSQLConnection();
// Delete the home with the given name and player ID
ps = conn.prepareStatement("DELETE FROM " + Main.settings.getHomesTable() + " WHERE `name`=? AND `player_id`=?;");
ps.setString(1, homeName);
ps.setInt(2, playerID);
ps.executeUpdate();
} 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);
}
}
}
// Insert a player into the database
public void addPlayer(Player p) {
Connection conn = null;
@@ -147,13 +202,12 @@ public abstract class Database {
try {
conn = getSQLConnection();
ps = conn.prepareStatement("INSERT INTO " + Main.settings.getPlayerTable() + " (uuid,username,home_count,home_slots,rtp_cooldown,is_teleporting) VALUES(?,?,?,?,?,?);");
ps = conn.prepareStatement("INSERT INTO " + Main.settings.getPlayerTable() + " (uuid,username,home_slots,rtp_cooldown,is_teleporting) VALUES(?,?,?,?,?);");
ps.setString(1, p.getUniqueId().toString());
ps.setString(2, p.getName());
ps.setInt(3, 0);
ps.setInt(4, 5); //TODO: Update default home slots to be based on permission & config
ps.setInt(5, 0);
ps.setBoolean(6, false);
ps.setInt(3, 5); //TODO: Update default home slots to be based on permission & config
ps.setInt(4, 0);
ps.setBoolean(5, false);
ps.executeUpdate();
} catch (SQLException ex) {
@@ -170,6 +224,32 @@ public abstract class Database {
}
}
public void setPlayerTeleporting(int playerID, boolean value) {
Connection conn = null;
PreparedStatement ps = null;
try {
conn = getSQLConnection();
// Set the home location ID to the new teleport point for the given home
ps = conn.prepareStatement("UPDATE " + Main.settings.getPlayerTable() + " SET `is_teleporting`=? WHERE `player_id`=?;");
ps.setBoolean(1, value);
ps.setInt(2, playerID);
ps.executeUpdate();
} 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);
}
}
}
// Delete a teleportation point from SQL
public void deleteTeleportationPoint(int locationID) {
Connection conn = null;
@@ -196,6 +276,176 @@ public abstract class Database {
}
}
// Update the location of a home
public void setHomeTeleportPoint(String homeName, int ownerID, TeleportationPoint point) {
Connection conn = null;
PreparedStatement ps = null;
try {
conn = getSQLConnection();
// Add the teleportation point
addTeleportationPoint(point, conn, ps);
// Set the home location ID to the new teleport point for the given home
ps = conn.prepareStatement("UPDATE " + Main.settings.getHomesTable() + " SET `location_id`=(SELECT last_insert_rowid()) WHERE `name`=? AND `player_id`=?;");
ps.setString(1, homeName);
ps.setInt(2, ownerID);
ps.executeUpdate();
} 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);
}
}
}
// Update the location of a home
public void setWarpTeleportPoint(String warpName, TeleportationPoint point) {
Connection conn = null;
PreparedStatement ps = null;
try {
conn = getSQLConnection();
// Add the teleportation point
addTeleportationPoint(point, conn, ps);
// Set the warp location ID to the new teleport point
ps = conn.prepareStatement("UPDATE " + Main.settings.getWarpsTable() + " SET `location_id`=(SELECT last_insert_rowid()) WHERE `name`=?;");
ps.setString(1, warpName);
ps.executeUpdate();
} 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);
}
}
}
// Update the description of a home
public void setHomeDescription(String homeName, int ownerID, String newDescription) {
Connection conn = null;
PreparedStatement ps = null;
try {
conn = getSQLConnection();
// Update the home description
ps = conn.prepareStatement("UPDATE " + Main.settings.getHomesTable() + " SET `description`=? WHERE `name`=? AND `player_id`=?;");
ps.setString(1, newDescription);
ps.setString(2, homeName);
ps.setInt(3, ownerID);
ps.executeUpdate();
} 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);
}
}
}
// Update the description of a warp
public void setWarpDescription(String warpName, String newDescription) {
Connection conn = null;
PreparedStatement ps = null;
try {
conn = getSQLConnection();
// Update the warp description
ps = conn.prepareStatement("UPDATE " + Main.settings.getWarpsTable() + " SET `description`=? WHERE `name`=?;");
ps.setString(1, newDescription);
ps.setString(2, warpName);
ps.executeUpdate();
} 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);
}
}
}
// Update the name of a home
public void setHomeName(String oldHomeName, int ownerID, String newHomeName) {
Connection conn = null;
PreparedStatement ps = null;
try {
conn = getSQLConnection();
// Update the home name
ps = conn.prepareStatement("UPDATE " + Main.settings.getHomesTable() + " SET `name`=? WHERE `name`=? AND `player_id`=?;");
ps.setString(1, newHomeName);
ps.setString(2, oldHomeName);
ps.setInt(3, ownerID);
ps.executeUpdate();
} 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);
}
}
}
// Update the name of a warp
public void setWarpName(String oldWarpName, String newWarpName) {
Connection conn = null;
PreparedStatement ps = null;
try {
conn = getSQLConnection();
// Update the warp name
ps = conn.prepareStatement("UPDATE " + Main.settings.getWarpsTable() + " SET `name`=? WHERE `name`=?;");
ps.setString(1, newWarpName);
ps.setString(2, oldWarpName);
ps.executeUpdate();
} 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);
}
}
}
// Update a player's destination teleport point
public void setTeleportationLastOrDest(int playerID, TeleportationPoint point, String type) {
Connection conn = null;