Homes now function

This commit is contained in:
William278 committed 2020-12-12 13:48:51 +00:00
1 parent 252cb37983
commit d20baa41d1
8 files changed
+402 -294

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 `user_uuid`=?");
PreparedStatement ps = connection.prepareStatement("SELECT * FROM huskhomes_player_data WHERE `user_uuid`=?");
ResultSet rs = ps.executeQuery();
close(ps, rs);
@@ -38,37 +38,13 @@ public abstract class Database {
}
}
public ResultSet queryDatabase(String query) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs;
try {
conn = getSQLConnection();
ps = conn.prepareStatement(query);
rs = ps.executeQuery();
return rs;
} 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 null;
}
// Insert a teleportation point
public void addTeleportationPoint(TeleportationPoint point, Connection conn, PreparedStatement ps) {
public void addTeleportationPoint(TeleportationPoint point, Connection conn) {
try {
ps = conn.prepareStatement("INSERT INTO " + Main.settings.getLocationsTable() + " (world,server,x,y,z,yaw,pitch) VALUES(?,?,?,?,?,?,?);");
PreparedStatement ps = conn.prepareStatement("INSERT INTO huskhomes_location_data (world,server,x,y,z,yaw,pitch) VALUES(?,?,?,?,?,?,?);");
ps.setString(1, point.getServer());
ps.setString(2, point.getWorldName());
ps.setString(1, point.getWorldName());
ps.setString(2, point.getServer());
ps.setDouble(3, point.getX());
ps.setDouble(4, point.getY());
ps.setDouble(5, point.getZ());
@@ -87,9 +63,9 @@ public abstract class Database {
try {
conn = getSQLConnection();
addTeleportationPoint(home, conn, ps);
addTeleportationPoint(home, conn);
ps = conn.prepareStatement("INSERT INTO " + Main.settings.getHomesTable() + " (player_id,location_id,name,description,public) VALUES(?,(SELECT last_insert_rowid()),?,?,?);");
ps = conn.prepareStatement("INSERT INTO huskhomes_home_data (player_id,location_id,name,description,public) VALUES(?,(SELECT last_insert_rowid()),?,?,?);");
ps.setInt(1, playerID);
ps.setString(2, home.getName());
ps.setString(3, home.getDescription());
@@ -118,10 +94,10 @@ public abstract class Database {
conn = getSQLConnection();
// Add the teleportation point
addTeleportationPoint(warp, conn, ps);
addTeleportationPoint(warp, conn);
// Insert the warp with the location_id of the last inserted teleport point
ps = conn.prepareStatement("INSERT INTO " + Main.settings.getWarpsTable() + " (location_id,name,description) VALUES((SELECT last_insert_rowid()),?,?);");
ps = conn.prepareStatement("INSERT INTO huskhomes_warp_data (location_id,name,description) VALUES((SELECT last_insert_rowid()),?,?);");
ps.setString(1, warp.getName());
ps.setString(2, warp.getDescription());
ps.executeUpdate();
@@ -148,7 +124,7 @@ public abstract class Database {
conn = getSQLConnection();
// Delete the warp with the given name
ps = conn.prepareStatement("DELETE FROM " + Main.settings.getWarpsTable() + " WHERE `name`=?;");
ps = conn.prepareStatement("DELETE FROM huskhomes_warp_data WHERE `name`=?;");
ps.setString(1, warpName);
ps.executeUpdate();
@@ -175,7 +151,7 @@ public abstract class Database {
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 = conn.prepareStatement("DELETE FROM huskhomes_home_data WHERE `name`=? AND `player_id`=?;");
ps.setString(1, homeName);
ps.setInt(2, playerID);
ps.executeUpdate();
@@ -202,7 +178,7 @@ public abstract class Database {
try {
conn = getSQLConnection();
ps = conn.prepareStatement("INSERT INTO " + Main.settings.getPlayerTable() + " (user_uuid,username,home_slots,rtp_cooldown,is_teleporting) VALUES(?,?,?,?,?);");
ps = conn.prepareStatement("INSERT INTO huskhomes_player_data (user_uuid,username,home_slots,rtp_cooldown,is_teleporting) VALUES(?,?,?,?,?);");
ps.setString(1, p.getUniqueId().toString());
ps.setString(2, p.getName());
ps.setInt(3, 5); //TODO: Update default home slots to be based on permission & config
@@ -232,7 +208,7 @@ public abstract class Database {
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 = conn.prepareStatement("UPDATE huskhomes_player_data SET `is_teleporting`=? WHERE `player_id`=?;");
ps.setBoolean(1, value);
ps.setInt(2, playerID);
ps.executeUpdate();
@@ -258,7 +234,7 @@ public abstract class Database {
try {
conn = getSQLConnection();
ps = conn.prepareStatement("DELETE FROM " + Main.settings.getPlayerTable() + " WHERE `location_id`=?;");
ps = conn.prepareStatement("DELETE FROM huskhomes_location_data WHERE `location_id`=?;");
ps.setInt(1, locationID);
ps.executeUpdate();
@@ -285,10 +261,10 @@ public abstract class Database {
conn = getSQLConnection();
// Add the teleportation point
addTeleportationPoint(point, conn, ps);
addTeleportationPoint(point, conn);
// 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 = conn.prepareStatement("UPDATE huskhomes_home_data SET `location_id`=(SELECT last_insert_rowid()) WHERE `name`=? AND `player_id`=?;");
ps.setString(1, homeName);
ps.setInt(2, ownerID);
ps.executeUpdate();
@@ -315,10 +291,10 @@ public abstract class Database {
conn = getSQLConnection();
// Add the teleportation point
addTeleportationPoint(point, conn, ps);
addTeleportationPoint(point, conn);
// 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 = conn.prepareStatement("UPDATE huskhomes_warp_data SET `location_id`=(SELECT last_insert_rowid()) WHERE `name`=?;");
ps.setString(1, warpName);
ps.executeUpdate();
} catch (SQLException ex) {
@@ -343,7 +319,7 @@ public abstract class Database {
conn = getSQLConnection();
// Update the home description
ps = conn.prepareStatement("UPDATE " + Main.settings.getHomesTable() + " SET `description`=? WHERE `name`=? AND `player_id`=?;");
ps = conn.prepareStatement("UPDATE huskhomes_home_data SET `description`=? WHERE `name`=? AND `player_id`=?;");
ps.setString(1, newDescription);
ps.setString(2, homeName);
ps.setInt(3, ownerID);
@@ -371,7 +347,7 @@ public abstract class Database {
conn = getSQLConnection();
// Update the warp description
ps = conn.prepareStatement("UPDATE " + Main.settings.getWarpsTable() + " SET `description`=? WHERE `name`=?;");
ps = conn.prepareStatement("UPDATE huskhomes_warp_data SET `description`=? WHERE `name`=?;");
ps.setString(1, newDescription);
ps.setString(2, warpName);
ps.executeUpdate();
@@ -398,7 +374,7 @@ public abstract class Database {
conn = getSQLConnection();
// Update the home name
ps = conn.prepareStatement("UPDATE " + Main.settings.getHomesTable() + " SET `name`=? WHERE `name`=? AND `player_id`=?;");
ps = conn.prepareStatement("UPDATE huskhomes_home_data SET `name`=? WHERE `name`=? AND `player_id`=?;");
ps.setString(1, newHomeName);
ps.setString(2, oldHomeName);
ps.setInt(3, ownerID);
@@ -427,7 +403,7 @@ public abstract class Database {
conn = getSQLConnection();
// Update the warp name
ps = conn.prepareStatement("UPDATE " + Main.settings.getWarpsTable() + " SET `name`=? WHERE `name`=?;");
ps = conn.prepareStatement("UPDATE huskhomes_warp_data SET `name`=? WHERE `name`=?;");
ps.setString(1, newWarpName);
ps.setString(2, oldWarpName);
@@ -455,10 +431,10 @@ public abstract class Database {
conn = getSQLConnection();
// Add the teleportation point
addTeleportationPoint(point, conn, ps);
addTeleportationPoint(point, conn);
// Set the destination location with the location_id of the last inserted teleport point
ps = conn.prepareStatement("UPDATE " + Main.settings.getPlayerTable() + " SET `" + type + "_location_id`=(SELECT last_insert_rowid()) WHERE `player_id`=?;");
ps = conn.prepareStatement("UPDATE huskhomes_player_data SET `" + type + "_location_id`=(SELECT last_insert_rowid()) WHERE `player_id`=?;");
ps.setInt(1, playerID);
ps.executeUpdate();
} catch (SQLException ex) {
@@ -46,6 +46,9 @@ public final class Main extends JavaPlugin {
// Load the config
configManager.loadConfig();
// Load the messages (in the right language)
messageManager.loadMessages(Main.settings.getLanguage());
// Set up data storage
dataManager.setupStorage(settings.getStorageType());
@@ -9,11 +9,6 @@ public class Settings {
String storageType;
String playerTable;
String locationsTable;
String homesTable;
String warpsTable;
int maximumHomes;
int teleportRequestExpiryTime;
int teleportWarmupTime;
@@ -30,11 +25,6 @@ public class Settings {
this.doSpawnCommand = configFile.getBoolean("spawn_command.enabled");
this.playerTable = configFile.getString("data_storage_options.table_names.player_data");
this.locationsTable = configFile.getString("data_storage_options.table_names.location_data");
this.homesTable = configFile.getString("data_storage_options.table_names.home_data");
this.warpsTable = configFile.getString("data_storage_options.table_names.warp_data");
this.maximumHomes = configFile.getInt("general.max_sethomes");
this.teleportRequestExpiryTime = configFile.getInt("general.teleport_request_expiry_time");
this.teleportWarmupTime = configFile.getInt("general.teleport_warmup_time");
@@ -69,22 +59,6 @@ public class Settings {
return doBungee;
}
public String getPlayerTable() {
return playerTable;
}
public String getLocationsTable() {
return locationsTable;
}
public String getHomesTable() {
return homesTable;
}
public String getWarpsTable() {
return warpsTable;
}
public int getMaximumHomes() { return maximumHomes; }
public int getTeleportRequestExpiryTime() { return teleportRequestExpiryTime; }
@@ -1,6 +1,7 @@
package me.william278.huskhomes2;
import me.william278.huskhomes2.Data.SQLite.Database;
import me.william278.huskhomes2.Data.SQLite.Errors.Errors;
import me.william278.huskhomes2.Data.SQLite.SQLite;
import me.william278.huskhomes2.Objects.Home;
import me.william278.huskhomes2.Objects.TeleportationPoint;
@@ -8,10 +9,13 @@ import me.william278.huskhomes2.Objects.Warp;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.UUID;
import java.util.logging.Level;
// This class handles the saving of data; whether that be through SQLite or SQL
public class dataManager {
@@ -61,8 +65,19 @@ public class dataManager {
"FOREIGN KEY (`location_id`) REFERENCES huskhomes_location_data (`location_id`) ON DELETE CASCADE ON UPDATE NO ACTION" +
");";
private static Main plugin = Main.getInstance();
private static Database sqliteDatabase;
private static Connection getConnection() {
if (Main.settings.getStorageType().equalsIgnoreCase("mysql")) {
return null; //TODO: Add mySQL support
} else {
initializeSQLite();
return sqliteDatabase.getSQLConnection();
}
}
private static void initializeSQLite() {
sqliteDatabase = new SQLite(Main.getInstance());
sqliteDatabase.load();
@@ -72,21 +87,53 @@ public class dataManager {
}
public static ResultSet queryDatabase(String query) {
if (Main.settings.getStorageType().equalsIgnoreCase("mysql")) {
return null;
} else {
return sqliteDatabase.queryDatabase(query);
// Return a player's ID from their username
private static Integer getPlayerId(String playerUsername) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs;
try {
conn = getConnection();
ps = conn.prepareStatement("SELECT * FROM huskhomes_player_data WHERE `username`=?;");
ps.setString(1, playerUsername);
rs = ps.executeQuery();
if (rs != null) {
if (rs.next()) {
return rs.getInt("player_id");
} else {
return null;
}
} else {
Bukkit.getLogger().severe("Result set for a player returned null while fetching player ID");
return null;
}
} 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 null;
}
// Return an integer from the player table
private static Integer getPlayerInteger(String playerName, String column) {
// Return an integer from the player table from a player ID
public static Integer getPlayerInteger(Integer playerID, String column) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs;
try {
Integer playerID = getPlayerId(playerName);
String query = "SELECT * FROM " + Main.settings.getHomesTable() + " WHERE `player_id`=?;";
query = query.replace("?", playerID.toString());
ResultSet rs = queryDatabase(query);
conn = getConnection();
ps = conn.prepareStatement("SELECT * FROM huskhomes_player_data WHERE `player_id`=?;");
ps.setInt(1, playerID);
rs = ps.executeQuery();
if (rs != null) {
if (rs.next()) {
return rs.getInt(column);
@@ -94,63 +141,45 @@ public class dataManager {
return null;
}
} else {
Bukkit.getLogger().severe("Could not return integer for " + playerName + " because the player ID was null");
Bukkit.getLogger().severe("Result set for a player returned null; perhaps player ID was null");
return null;
}
} catch (SQLException e) {
Bukkit.getLogger().severe("An SQL exception occurred returning a value for " + playerName);
return null;
} 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 null;
}
// Return an integer from the player table
// Return an integer from the player table from a player name
public static Integer getPlayerInteger(String playerName, String column) {
return getPlayerInteger(getPlayerId(playerName), column);
}
// Return an integer from the player table from a player object
public static Integer getPlayerInteger(Player p, String column) {
return getPlayerInteger(getPlayerId(p.getName()), column);
}
// Return an integer from the player table from a player ID
public static String getPlayerString(Integer playerID, String column) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs;
try {
Integer playerID = getPlayerId(p.getName());
String query = "SELECT * FROM " + Main.settings.getHomesTable() + " WHERE `player_id`=?;";
query = query.replace("?", playerID.toString());
ResultSet rs = queryDatabase(query);
if (rs != null) {
if (rs.next()) {
return rs.getInt(column);
} else {
return null;
}
} else {
Bukkit.getLogger().severe("Could not return integer for " + p.getName() + " because the player ID was null");
return null;
}
} catch (SQLException e) {
Bukkit.getLogger().severe("An SQL exception occurred returning a value for " + p.getName());
return null;
}
}
public static void createPlayer(Player p) {
if (Main.settings.getStorageType().equalsIgnoreCase("mysql")) {
} else {
sqliteDatabase.addPlayer(p);
}
}
// Get a string value from a player's data row
public static String getPlayerString(Player p, String column) {
Integer playerID = getPlayerId(p.getName());
if (playerID != null) {
return getPlayerString(playerID, column);
} else {
Bukkit.getLogger().severe("Could not return string for " + p.getName() + " because the player ID was null");
return null;
}
}
// Get a string value from a player with given ID's data row
public static String getPlayerString(int playerID, String column) {
try {
String query = "SELECT * FROM " + Main.settings.getPlayerTable() + " WHERE `player_id`=?;";
query = query.replace("?", Integer.toString(playerID));
ResultSet rs = queryDatabase(query);
conn = getConnection();
ps = conn.prepareStatement("SELECT * FROM huskhomes_player_data WHERE `player_id`=?;");
ps.setInt(1, playerID);
rs = ps.executeQuery();
if (rs != null) {
if (rs.next()) {
return rs.getString(column);
@@ -158,22 +187,46 @@ public class dataManager {
return null;
}
} else {
Bukkit.getLogger().severe("An error occurred retrieving a string");
Bukkit.getLogger().severe("Result set for a player returned null; perhaps player ID was null");
return null;
}
} catch (SQLException e) {
Bukkit.getLogger().severe("An SQL exception occurred returning a player UUID!");
return null;
} 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 null;
}
// Return how many home slots a player has
// Return an integer from the player table from a player name
public static String getPlayerString(String playerName, String column) {
return getPlayerString(getPlayerId(playerName), column);
}
// Return an integer from the player table from a player object
public static String getPlayerString(Player p, String column) {
return getPlayerString(getPlayerId(p.getName()), column);
}
// Return an integer from the player table from a player ID
public static Boolean getPlayerTeleporting(Player p) {
Connection conn = null;
PreparedStatement ps = null;
Integer playerID = getPlayerId(p.getName());
ResultSet rs;
try {
Integer playerID = getPlayerId(p.getName());
String query = "SELECT * FROM " + Main.settings.getHomesTable() + " WHERE `player_id`=?;";
query = query.replace("?", playerID.toString());
ResultSet rs = queryDatabase(query);
conn = getConnection();
ps = conn.prepareStatement("SELECT * FROM huskhomes_player_data WHERE `player_id`=?;");
ps.setInt(1, playerID);
rs = ps.executeQuery();
if (rs != null) {
if (rs.next()) {
return rs.getBoolean("is_teleporting");
@@ -181,12 +234,22 @@ public class dataManager {
return null;
}
} else {
Bukkit.getLogger().severe("Result set for a player returned null; perhaps player ID was null");
return null;
}
} catch (SQLException e) {
Bukkit.getLogger().severe("An SQL exception occurred checking " + p.getName() + " is teleporting");
return null;
} 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 null;
}
// Return a player's UUID
@@ -219,19 +282,20 @@ public class dataManager {
return getPlayerInteger(p, "rtp_cooldown");
}
// Return an array of a player's homes
// Return a player's homes.
public static ArrayList<Home> getPlayerHomes(String playerName) {
Connection conn = null;
PreparedStatement ps = null;
Integer playerID = getPlayerId(playerName);
ResultSet rs;
try {
Integer playerID = getPlayerId(playerName);
if (playerID == null) {
Bukkit.getLogger().severe("Player ID returned null!!");
return new ArrayList<>();
}
String query = "SELECT * FROM " + Main.settings.getHomesTable() + " WHERE `player_id`=?;";
query = query.replace("?", Integer.toString(playerID));
ResultSet rs = queryDatabase(query);
ArrayList<Home> playerHomes = new ArrayList<>();
conn = getConnection();
ps = conn.prepareStatement("SELECT * FROM huskhomes_home_data WHERE `player_id`=?;");
ps.setInt(1, playerID);
rs = ps.executeQuery();
if (rs != null) {
ArrayList<Home> playerHomes = new ArrayList<>();
while (rs.next()) {
int locationID = rs.getInt("location_id");
TeleportationPoint teleportationPoint = getTeleportationPoint(locationID);
@@ -245,21 +309,36 @@ public class dataManager {
}
return playerHomes;
} else {
Bukkit.getLogger().severe("Result set of a player's homes returned null; perhaps player ID was null");
return null;
}
} catch (SQLException e) {
Bukkit.getLogger().severe("An SQL exception occurred returning a home!");
return null;
} 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 null;
}
// Return an array of all the public homes
// Return all the public homes
public static ArrayList<Home> getPublicHomes() {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs;
try {
String query = "SELECT * FROM " + Main.settings.getHomesTable() + " WHERE `public`;";
ResultSet rs = queryDatabase(query);
ArrayList<Home> publicHomes = new ArrayList<>();
conn = getConnection();
ps = conn.prepareStatement("SELECT * FROM huskhomes_home_data WHERE `public`;");
rs = ps.executeQuery();
if (rs != null) {
ArrayList<Home> publicHomes = new ArrayList<>();
while (rs.next()) {
int playerID = rs.getInt("player_id");
int locationID = rs.getInt("location_id");
@@ -268,21 +347,36 @@ public class dataManager {
publicHomes.add(new Home(teleportationPoint, getPlayerUsername(playerID), getPlayerUUID(playerID), rs.getString("name"), rs.getString("description"), true));
}
}
return publicHomes;
} else {
Bukkit.getLogger().severe("Result set of public returns returned null!");
return null;
}
return publicHomes;
} catch (SQLException e) {
Bukkit.getLogger().severe("An SQL exception occurred returning a home!");
return null;
} 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 null;
}
// Return an array of all the warps
public static ArrayList<Warp> getWarps() {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs;
try {
String query = "SELECT * FROM " + Main.settings.getWarpsTable() + ";";
ResultSet rs = queryDatabase(query);
conn = getConnection();
ps = conn.prepareStatement("SELECT * FROM huskhomes_warp_data;");
rs = ps.executeQuery();
ArrayList<Warp> warps = new ArrayList<>();
if (rs != null) {
while (rs.next()) {
@@ -294,160 +388,240 @@ public class dataManager {
rs.getString("description")));
}
}
} else {
Bukkit.getLogger().severe("Result set of warps returned null!");
}
return warps;
} catch (SQLException e) {
Bukkit.getLogger().severe("An SQL exception occurred returning a list of warps!");
return null;
} 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 null;
}
// Return a warp with a given name (warp names are unique)
public static Warp getWarp(String name) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs;
try {
String query = "SELECT * FROM " + Main.settings.getWarpsTable() + " WHERE `name`=?;";
query = query.replace("?", "'" + name + "'");
ResultSet rs = queryDatabase(query);
conn = getConnection();
ps = conn.prepareStatement("SELECT * FROM huskhomes_warp_data WHERE `name`=?;");
ps.setString(1, name);
rs = ps.executeQuery();
if (rs != null) {
if (rs.next()) {
int locationID = rs.getInt("location_id");
TeleportationPoint teleportationPoint = getTeleportationPoint(locationID);
if (teleportationPoint != null) {
return new Warp(teleportationPoint,
rs.getString("name"),
rs.getString("description"));
} else {
Bukkit.getLogger().severe("An error occurred returning a warp!");
return null;
}
int locationID = rs.getInt("location_id");
TeleportationPoint teleportationPoint = getTeleportationPoint(locationID);
if (teleportationPoint != null) {
return new Warp(teleportationPoint,
rs.getString("name"),
rs.getString("description"));
} else {
Bukkit.getLogger().severe("An error occurred returning a warp from the table!");
return null;
}
} else {
Bukkit.getLogger().severe("Warp returned null!");
return null;
}
} catch (SQLException e) {
Bukkit.getLogger().severe("An SQL exception occurred returning a warp");
e.printStackTrace();
return null;
} 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 null;
}
// Return a home with a given owner username and home name
public static Home getHome(String ownerUsername, String homeName) {
Connection conn = null;
PreparedStatement ps = null;
Integer playerID = getPlayerId(ownerUsername);
ResultSet rs;
try {
Integer playerID = getPlayerId(ownerUsername);
String query = "SELECT * FROM " + Main.settings.getHomesTable() + " WHERE `player_id`=%1% AND `name`=%2%;";
query = query.replace("%1%", Integer.toString(playerID));
query = query.replace("%2%", "'" + homeName + "'"); ResultSet rs = queryDatabase(query);
conn = getConnection();
ps = conn.prepareStatement("SELECT * FROM huskhomes_home_data WHERE `player_id`=? AND `name`=?;");
ps.setInt(1, playerID);
ps.setString(2, homeName);
rs = ps.executeQuery();
if (rs != null) {
if (rs.next()) {
int locationID = rs.getInt("location_id");
TeleportationPoint teleportationPoint = getTeleportationPoint(locationID);
if (teleportationPoint != null && playerID != null) {
return new Home(teleportationPoint,
ownerUsername,
getPlayerUUID(playerID),
rs.getString("name"),
rs.getString("description"),
rs.getBoolean("public"));
} else {
Bukkit.getLogger().severe("An error occurred retrieving a home from the table!");
return null;
}
int locationID = rs.getInt("location_id");
TeleportationPoint teleportationPoint = getTeleportationPoint(locationID);
if (teleportationPoint != null) {
return new Home(teleportationPoint,
ownerUsername,
getPlayerUUID(playerID),
rs.getString("name"),
rs.getString("description"),
rs.getBoolean("public"));
} else {
Bukkit.getLogger().severe("An error occurred retrieving a home from the table!");
return null;
}
} else {
Bukkit.getLogger().severe("An error occurred retrieving a home from the table!");
Bukkit.getLogger().severe("Home returned null; perhaps player ID was null?");
return null;
}
} catch (SQLException e) {
Bukkit.getLogger().severe("An SQL exception occurred returning a home!");
e.printStackTrace();
return null;
} 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 null;
}
public static boolean warpExists(String warpName) {
public static Boolean warpExists(String warpName) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs;
try {
String query = "SELECT * FROM " + Main.settings.getWarpsTable() + " WHERE `name`=?;";
query = query.replace("?", "'" + warpName + "'");
ResultSet rs = queryDatabase(query);
conn = getConnection();
ps = conn.prepareStatement("SELECT * FROM huskhomes_warp_data WHERE `name`=?;");
ps.setString(1, warpName);
rs = ps.executeQuery();
if (rs != null) {
return rs.next();
} else {
Bukkit.getLogger().severe("An SQL exception occurred in retrieving warp data from the table.");
Bukkit.getLogger().severe("An SQL exception occurred in retrieving if a warp exists from the table.");
return false;
}
} catch (SQLException e) {
Bukkit.getLogger().severe("An SQL exception occurred in checking if warp data exists in the table.");
e.printStackTrace();
return false;
} 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 null;
}
public static boolean homeExists(Player p, String homeName) {
Integer playerID = getPlayerId(p.getName());
public static Boolean homeExists(Player owner, String homeName) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs;
Integer playerID = getPlayerId(owner.getName());
try {
String query = "SELECT * FROM " + Main.settings.getHomesTable() + " WHERE `player_id`=%1% AND `name`=%2%;";
query = query.replace("%1%", Integer.toString(playerID));
query = query.replace("%2%", "'" + homeName + "'");
ResultSet rs = queryDatabase(query);
conn = getConnection();
ps = conn.prepareStatement("SELECT * FROM huskhomes_home_data WHERE `player_id`=? AND `name`=?;");
ps.setInt(1, playerID);
ps.setString(2, homeName);
rs = ps.executeQuery();
if (rs != null) {
return rs.next();
} else {
Bukkit.getLogger().severe("An SQL exception occurred in retrieving player data from the table.");
Bukkit.getLogger().severe("An SQL exception occurred in retrieving if a home exists from the table.");
return false;
}
} catch (SQLException e) {
Bukkit.getLogger().severe("An SQL exception occurred in checking if player data exists in the table.");
e.printStackTrace();
return false;
} 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 null;
}
public static boolean playerExists(Player p) {
public static Boolean playerExists(Player player) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs;
String playerUUID = player.getUniqueId().toString();
try {
UUID uuid = p.getUniqueId();
String uuidString = uuid.toString();
String query = "SELECT * FROM " + Main.settings.getPlayerTable() + " WHERE `user_uuid`=?;";
query = query.replace("?", "'" + uuidString + "'");
ResultSet rs = queryDatabase(query);
conn = getConnection();
ps = conn.prepareStatement("SELECT * FROM huskhomes_player_data WHERE `user_uuid`=?;");
ps.setString(1, playerUUID);
rs = ps.executeQuery();
if (rs != null) {
return rs.next();
} else {
Bukkit.getLogger().severe("An SQL exception retrieving SQL data from the player table in determining if they exist.");
Bukkit.getLogger().severe("An SQL exception occurred in retrieving if a player exists from the table.");
return false;
}
} catch (SQLException e) {
Bukkit.getLogger().severe("An SQL exception occurred in determining if a player exists in the player table.");
e.printStackTrace();
return false;
} 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 null;
}
private static Integer getPlayerId(String playerUsername) {
public static TeleportationPoint getTeleportationPoint(Integer locationID) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs;
try {
String query = "SELECT * FROM " + Main.settings.getPlayerTable() + " WHERE `username`=?;";
query = query.replace("?", "'" + playerUsername + "'");
Bukkit.getLogger().info(query);
ResultSet rs = queryDatabase(query);
if (rs != null) {
if (rs.next()) {
return rs.getInt("player_id");
} else {
Bukkit.getLogger().info("Could not find player ID for " + playerUsername);
return null;
}
conn = getConnection();
ps = conn.prepareStatement("SELECT * FROM huskhomes_location_data WHERE `location_id`=?;");
ps.setInt(1, locationID);
rs = ps.executeQuery();
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 {
Bukkit.getLogger().severe("An SQL exception occurred in retrieving a player ID.");
Bukkit.getLogger().severe("An SQL exception occurred in retrieving a teleportation location.");
return null;
}
} catch (SQLException e) {
Bukkit.getLogger().severe("An SQL exception occurred returning a player ID!");
return null;
} 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 null;
}
public static TeleportationPoint getPlayerDestination(Player p) {
@@ -600,29 +774,6 @@ public class dataManager {
}
}
public static TeleportationPoint getTeleportationPoint(int locationID) {
try {
String query = "SELECT * FROM " + Main.settings.getLocationsTable() + " WHERE `location_id`=" + (locationID) + ";";
ResultSet rs = queryDatabase(query);
if (rs != null) {
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;
}
} else {
Bukkit.getLogger().severe("An error occurred returning a transportation point!");
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.equalsIgnoreCase("sqlite")) {
initializeSQLite();
@@ -634,4 +785,11 @@ public class dataManager {
}
}
public static void createPlayer(Player p) {
if (Main.settings.getStorageType().equalsIgnoreCase("mySQL")) {
} else {
sqliteDatabase.addPlayer(p);
}
}
}
@@ -32,7 +32,7 @@ public class homeListHandler {
TextComponent clickableHome = new TextComponent("[" + home.getName() + "]");
clickableHome.setColor(ChatColor.ITALIC);
clickableHome.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, ("/edithome " + home.getName())));
clickableHome.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new Text(new ComponentBuilder(home.getOwnerUsername()).color(ChatColor.GREEN).italic(false).append(": ").append(home.getDescription()).color(net.md_5.bungee.api.ChatColor.GRAY).italic(false).create())));
clickableHome.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new Text(new ComponentBuilder(home.getDescription()).color(net.md_5.bungee.api.ChatColor.GRAY).italic(false).create())));
return clickableHome;
}
@@ -40,7 +40,7 @@ public class homeListHandler {
TextComponent clickableHome = new TextComponent("[" + home.getName() + "]");
clickableHome.setColor(ChatColor.ITALIC);
clickableHome.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, ("/phome " + home.getOwnerUsername() + "." + home.getName())));
clickableHome.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new Text(new ComponentBuilder(home.getDescription()).color(net.md_5.bungee.api.ChatColor.GRAY).italic(false).create())));
clickableHome.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new Text(new ComponentBuilder(home.getOwnerUsername()).color(ChatColor.GREEN).italic(false).append(": ").append(home.getDescription()).color(net.md_5.bungee.api.ChatColor.GRAY).italic(false).create())));
return clickableHome;
}
@@ -80,7 +80,7 @@ public class messageManager {
"2. Use an ampersand symbol and hashtag (&#) followed by a 6 digit hex code to add custom colors.\n" +
"3. Example 1: \"§cHello!\" will display \"Hello!\" in light red.\n" +
"4. Example 2: \"&#32CD32Hello\" will display \"Hello!\" in a bright green color (hex: #32CD32)");
InputStream defaultMessageFile = plugin.getResource("Languages/" + language);
InputStream defaultMessageFile = plugin.getResource("Languages/" + language + ".yml");
if (defaultMessageFile != null) {
YamlConfiguration yamlConfiguration = YamlConfiguration.loadConfiguration(new InputStreamReader(defaultMessageFile, StandardCharsets.UTF_8));
config.setDefaults(yamlConfiguration);
@@ -16,7 +16,7 @@ public class teleportManager {
String server = teleportationPoint.getServer();
if (!Main.settings.doBungee() || server.equals(Main.settings.getServerID())) {
p.teleport(teleportationPoint.getLocation());
messageManager.sendMessage(p, "teleport_success");
messageManager.sendMessage(p, "teleporting_complete");
} else if (Main.settings.doBungee()) {
dataManager.setPlayerDestinationLocation(p, teleportationPoint);
pluginMessageHandler.sendPlayer(p, server);
@@ -26,6 +26,7 @@ public class teleportManager {
public static void queueTimedTeleport(Player player, String targetPlayer) {
if (player.hasPermission("huskhomes.bypass_warmup_timers")) {
dataManager.setPlayerLastPosition(player, new TeleportationPoint(player.getLocation(), Main.settings.getServerID()));
setPlayerDestinationFromTargetPlayer(player, targetPlayer);
teleportPlayer(player);
return;
}
@@ -36,6 +37,7 @@ public class teleportManager {
public static void queueTimedTeleport(Player player, TeleportationPoint point) {
if (player.hasPermission("huskhomes.bypass_warmup_timers")) {
dataManager.setPlayerLastPosition(player, new TeleportationPoint(player.getLocation(), Main.settings.getServerID()));
dataManager.setPlayerDestinationLocation(player, point);
teleportPlayer(player);
return;
}