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
+382 -274

No files matched your search

@@ -29,7 +29,7 @@ public abstract class Database {
public void initialize() { public void initialize() {
connection = getSQLConnection(); connection = getSQLConnection();
try { 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(); ResultSet rs = ps.executeQuery();
close(ps, rs); 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 // Insert a teleportation point
public void addTeleportationPoint(TeleportationPoint point, Connection conn, PreparedStatement ps) { public void addTeleportationPoint(TeleportationPoint point, Connection conn) {
try { 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(1, point.getWorldName());
ps.setString(2, point.getWorldName()); ps.setString(2, point.getServer());
ps.setDouble(3, point.getX()); ps.setDouble(3, point.getX());
ps.setDouble(4, point.getY()); ps.setDouble(4, point.getY());
ps.setDouble(5, point.getZ()); ps.setDouble(5, point.getZ());
@@ -87,9 +63,9 @@ public abstract class Database {
try { try {
conn = getSQLConnection(); 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.setInt(1, playerID);
ps.setString(2, home.getName()); ps.setString(2, home.getName());
ps.setString(3, home.getDescription()); ps.setString(3, home.getDescription());
@@ -118,10 +94,10 @@ public abstract class Database {
conn = getSQLConnection(); conn = getSQLConnection();
// Add the teleportation point // Add the teleportation point
addTeleportationPoint(warp, conn, ps); addTeleportationPoint(warp, conn);
// Insert the warp with the location_id of the last inserted teleport point // 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(1, warp.getName());
ps.setString(2, warp.getDescription()); ps.setString(2, warp.getDescription());
ps.executeUpdate(); ps.executeUpdate();
@@ -148,7 +124,7 @@ public abstract class Database {
conn = getSQLConnection(); conn = getSQLConnection();
// Delete the warp with the given name // 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.setString(1, warpName);
ps.executeUpdate(); ps.executeUpdate();
@@ -175,7 +151,7 @@ public abstract class Database {
conn = getSQLConnection(); conn = getSQLConnection();
// Delete the home with the given name and player ID // 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.setString(1, homeName);
ps.setInt(2, playerID); ps.setInt(2, playerID);
ps.executeUpdate(); ps.executeUpdate();
@@ -202,7 +178,7 @@ public abstract class Database {
try { try {
conn = getSQLConnection(); 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(1, p.getUniqueId().toString());
ps.setString(2, p.getName()); ps.setString(2, p.getName());
ps.setInt(3, 5); //TODO: Update default home slots to be based on permission & config 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(); conn = getSQLConnection();
// Set the home location ID to the new teleport point for the given home // 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.setBoolean(1, value);
ps.setInt(2, playerID); ps.setInt(2, playerID);
ps.executeUpdate(); ps.executeUpdate();
@@ -258,7 +234,7 @@ public abstract class Database {
try { try {
conn = getSQLConnection(); 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.setInt(1, locationID);
ps.executeUpdate(); ps.executeUpdate();
@@ -285,10 +261,10 @@ public abstract class Database {
conn = getSQLConnection(); conn = getSQLConnection();
// Add the teleportation point // 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 // 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.setString(1, homeName);
ps.setInt(2, ownerID); ps.setInt(2, ownerID);
ps.executeUpdate(); ps.executeUpdate();
@@ -315,10 +291,10 @@ public abstract class Database {
conn = getSQLConnection(); conn = getSQLConnection();
// Add the teleportation point // Add the teleportation point
addTeleportationPoint(point, conn, ps); addTeleportationPoint(point, conn);
// Set the warp location ID to the new teleport point // 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.setString(1, warpName);
ps.executeUpdate(); ps.executeUpdate();
} catch (SQLException ex) { } catch (SQLException ex) {
@@ -343,7 +319,7 @@ public abstract class Database {
conn = getSQLConnection(); conn = getSQLConnection();
// Update the home description // 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(1, newDescription);
ps.setString(2, homeName); ps.setString(2, homeName);
ps.setInt(3, ownerID); ps.setInt(3, ownerID);
@@ -371,7 +347,7 @@ public abstract class Database {
conn = getSQLConnection(); conn = getSQLConnection();
// Update the warp description // 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(1, newDescription);
ps.setString(2, warpName); ps.setString(2, warpName);
ps.executeUpdate(); ps.executeUpdate();
@@ -398,7 +374,7 @@ public abstract class Database {
conn = getSQLConnection(); conn = getSQLConnection();
// Update the home name // 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(1, newHomeName);
ps.setString(2, oldHomeName); ps.setString(2, oldHomeName);
ps.setInt(3, ownerID); ps.setInt(3, ownerID);
@@ -427,7 +403,7 @@ public abstract class Database {
conn = getSQLConnection(); conn = getSQLConnection();
// Update the warp name // 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(1, newWarpName);
ps.setString(2, oldWarpName); ps.setString(2, oldWarpName);
@@ -455,10 +431,10 @@ public abstract class Database {
conn = getSQLConnection(); conn = getSQLConnection();
// Add the teleportation point // 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 // 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.setInt(1, playerID);
ps.executeUpdate(); ps.executeUpdate();
} catch (SQLException ex) { } catch (SQLException ex) {
@@ -46,6 +46,9 @@ public final class Main extends JavaPlugin {
// Load the config // Load the config
configManager.loadConfig(); configManager.loadConfig();
// Load the messages (in the right language)
messageManager.loadMessages(Main.settings.getLanguage());
// Set up data storage // Set up data storage
dataManager.setupStorage(settings.getStorageType()); dataManager.setupStorage(settings.getStorageType());
@@ -9,11 +9,6 @@ public class Settings {
String storageType; String storageType;
String playerTable;
String locationsTable;
String homesTable;
String warpsTable;
int maximumHomes; int maximumHomes;
int teleportRequestExpiryTime; int teleportRequestExpiryTime;
int teleportWarmupTime; int teleportWarmupTime;
@@ -30,11 +25,6 @@ public class Settings {
this.doSpawnCommand = configFile.getBoolean("spawn_command.enabled"); 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.maximumHomes = configFile.getInt("general.max_sethomes");
this.teleportRequestExpiryTime = configFile.getInt("general.teleport_request_expiry_time"); this.teleportRequestExpiryTime = configFile.getInt("general.teleport_request_expiry_time");
this.teleportWarmupTime = configFile.getInt("general.teleport_warmup_time"); this.teleportWarmupTime = configFile.getInt("general.teleport_warmup_time");
@@ -69,22 +59,6 @@ public class Settings {
return doBungee; 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 getMaximumHomes() { return maximumHomes; }
public int getTeleportRequestExpiryTime() { return teleportRequestExpiryTime; } public int getTeleportRequestExpiryTime() { return teleportRequestExpiryTime; }
@@ -1,6 +1,7 @@
package me.william278.huskhomes2; package me.william278.huskhomes2;
import me.william278.huskhomes2.Data.SQLite.Database; 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.Data.SQLite.SQLite;
import me.william278.huskhomes2.Objects.Home; import me.william278.huskhomes2.Objects.Home;
import me.william278.huskhomes2.Objects.TeleportationPoint; import me.william278.huskhomes2.Objects.TeleportationPoint;
@@ -8,10 +9,13 @@ import me.william278.huskhomes2.Objects.Warp;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.UUID; import java.util.UUID;
import java.util.logging.Level;
// This class handles the saving of data; whether that be through SQLite or SQL // This class handles the saving of data; whether that be through SQLite or SQL
public class dataManager { 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" + "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 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() { private static void initializeSQLite() {
sqliteDatabase = new SQLite(Main.getInstance()); sqliteDatabase = new SQLite(Main.getInstance());
sqliteDatabase.load(); sqliteDatabase.load();
@@ -72,21 +87,53 @@ public class dataManager {
} }
public static ResultSet queryDatabase(String query) { // Return a player's ID from their username
if (Main.settings.getStorageType().equalsIgnoreCase("mysql")) { private static Integer getPlayerId(String playerUsername) {
return null; 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 { } else {
return sqliteDatabase.queryDatabase(query); 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 // Return an integer from the player table from a player ID
private static Integer getPlayerInteger(String playerName, String column) { public static Integer getPlayerInteger(Integer playerID, String column) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs;
try { try {
Integer playerID = getPlayerId(playerName); conn = getConnection();
String query = "SELECT * FROM " + Main.settings.getHomesTable() + " WHERE `player_id`=?;"; ps = conn.prepareStatement("SELECT * FROM huskhomes_player_data WHERE `player_id`=?;");
query = query.replace("?", playerID.toString()); ps.setInt(1, playerID);
ResultSet rs = queryDatabase(query); rs = ps.executeQuery();
if (rs != null) { if (rs != null) {
if (rs.next()) { if (rs.next()) {
return rs.getInt(column); return rs.getInt(column);
@@ -94,63 +141,45 @@ public class dataManager {
return null; return null;
} }
} else { } 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; return null;
} }
} catch (SQLException e) { } catch (SQLException ex) {
Bukkit.getLogger().severe("An SQL exception occurred returning a value for " + playerName); plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex);
return null; } 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) { 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 { try {
Integer playerID = getPlayerId(p.getName()); conn = getConnection();
String query = "SELECT * FROM " + Main.settings.getHomesTable() + " WHERE `player_id`=?;"; ps = conn.prepareStatement("SELECT * FROM huskhomes_player_data WHERE `player_id`=?;");
query = query.replace("?", playerID.toString()); ps.setInt(1, playerID);
ResultSet rs = queryDatabase(query); rs = ps.executeQuery();
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);
if (rs != null) { if (rs != null) {
if (rs.next()) { if (rs.next()) {
return rs.getString(column); return rs.getString(column);
@@ -158,22 +187,46 @@ public class dataManager {
return null; return null;
} }
} else { } 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; return null;
} }
} catch (SQLException e) { } catch (SQLException ex) {
Bukkit.getLogger().severe("An SQL exception occurred returning a player UUID!"); plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex);
return null; } 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) { public static Boolean getPlayerTeleporting(Player p) {
try { Connection conn = null;
PreparedStatement ps = null;
Integer playerID = getPlayerId(p.getName()); Integer playerID = getPlayerId(p.getName());
String query = "SELECT * FROM " + Main.settings.getHomesTable() + " WHERE `player_id`=?;"; ResultSet rs;
query = query.replace("?", playerID.toString());
ResultSet rs = queryDatabase(query); try {
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 != null) {
if (rs.next()) { if (rs.next()) {
return rs.getBoolean("is_teleporting"); return rs.getBoolean("is_teleporting");
@@ -181,13 +234,23 @@ public class dataManager {
return null; return null;
} }
} else { } else {
Bukkit.getLogger().severe("Result set for a player returned null; perhaps player ID was null");
return null; return null;
} }
} catch (SQLException e) { } catch (SQLException ex) {
Bukkit.getLogger().severe("An SQL exception occurred checking " + p.getName() + " is teleporting"); plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex);
return null; } 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 // Return a player's UUID
public static String getPlayerUUID(int playerID) { public static String getPlayerUUID(int playerID) {
@@ -219,19 +282,20 @@ public class dataManager {
return getPlayerInteger(p, "rtp_cooldown"); 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) { public static ArrayList<Home> getPlayerHomes(String playerName) {
try { Connection conn = null;
PreparedStatement ps = null;
Integer playerID = getPlayerId(playerName); Integer playerID = getPlayerId(playerName);
if (playerID == null) { ResultSet rs;
Bukkit.getLogger().severe("Player ID returned null!!");
return new ArrayList<>(); try {
} conn = getConnection();
String query = "SELECT * FROM " + Main.settings.getHomesTable() + " WHERE `player_id`=?;"; ps = conn.prepareStatement("SELECT * FROM huskhomes_home_data WHERE `player_id`=?;");
query = query.replace("?", Integer.toString(playerID)); ps.setInt(1, playerID);
ResultSet rs = queryDatabase(query); rs = ps.executeQuery();
ArrayList<Home> playerHomes = new ArrayList<>();
if (rs != null) { if (rs != null) {
ArrayList<Home> playerHomes = new ArrayList<>();
while (rs.next()) { while (rs.next()) {
int locationID = rs.getInt("location_id"); int locationID = rs.getInt("location_id");
TeleportationPoint teleportationPoint = getTeleportationPoint(locationID); TeleportationPoint teleportationPoint = getTeleportationPoint(locationID);
@@ -245,21 +309,36 @@ public class dataManager {
} }
return playerHomes; return playerHomes;
} else { } else {
Bukkit.getLogger().severe("Result set of a player's homes returned null; perhaps player ID was null");
return null; return null;
} }
} catch (SQLException e) { } catch (SQLException ex) {
Bukkit.getLogger().severe("An SQL exception occurred returning a home!"); plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex);
return null; } 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() { public static ArrayList<Home> getPublicHomes() {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs;
try { try {
String query = "SELECT * FROM " + Main.settings.getHomesTable() + " WHERE `public`;"; conn = getConnection();
ResultSet rs = queryDatabase(query); ps = conn.prepareStatement("SELECT * FROM huskhomes_home_data WHERE `public`;");
ArrayList<Home> publicHomes = new ArrayList<>(); rs = ps.executeQuery();
if (rs != null) { if (rs != null) {
ArrayList<Home> publicHomes = new ArrayList<>();
while (rs.next()) { while (rs.next()) {
int playerID = rs.getInt("player_id"); int playerID = rs.getInt("player_id");
int locationID = rs.getInt("location_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)); publicHomes.add(new Home(teleportationPoint, getPlayerUsername(playerID), getPlayerUUID(playerID), rs.getString("name"), rs.getString("description"), true));
} }
} }
} else {
return null;
}
return publicHomes; return publicHomes;
} catch (SQLException e) { } else {
Bukkit.getLogger().severe("An SQL exception occurred returning a home!"); Bukkit.getLogger().severe("Result set of public returns returned null!");
return null; 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 // Return an array of all the warps
public static ArrayList<Warp> getWarps() { public static ArrayList<Warp> getWarps() {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs;
try { try {
String query = "SELECT * FROM " + Main.settings.getWarpsTable() + ";"; conn = getConnection();
ResultSet rs = queryDatabase(query); ps = conn.prepareStatement("SELECT * FROM huskhomes_warp_data;");
rs = ps.executeQuery();
ArrayList<Warp> warps = new ArrayList<>(); ArrayList<Warp> warps = new ArrayList<>();
if (rs != null) { if (rs != null) {
while (rs.next()) { while (rs.next()) {
@@ -294,22 +388,37 @@ public class dataManager {
rs.getString("description"))); rs.getString("description")));
} }
} }
} else {
Bukkit.getLogger().severe("Result set of warps returned null!");
} }
return warps; return warps;
} catch (SQLException e) { } catch (SQLException ex) {
Bukkit.getLogger().severe("An SQL exception occurred returning a list of warps!"); plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex);
return null; } 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) // Return a warp with a given name (warp names are unique)
public static Warp getWarp(String name) { public static Warp getWarp(String name) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs;
try { try {
String query = "SELECT * FROM " + Main.settings.getWarpsTable() + " WHERE `name`=?;"; conn = getConnection();
query = query.replace("?", "'" + name + "'"); ps = conn.prepareStatement("SELECT * FROM huskhomes_warp_data WHERE `name`=?;");
ResultSet rs = queryDatabase(query); ps.setString(1, name);
rs = ps.executeQuery();
if (rs != null) { if (rs != null) {
if (rs.next()) {
int locationID = rs.getInt("location_id"); int locationID = rs.getInt("location_id");
TeleportationPoint teleportationPoint = getTeleportationPoint(locationID); TeleportationPoint teleportationPoint = getTeleportationPoint(locationID);
if (teleportationPoint != null) { if (teleportationPoint != null) {
@@ -317,34 +426,45 @@ public class dataManager {
rs.getString("name"), rs.getString("name"),
rs.getString("description")); rs.getString("description"));
} else { } else {
Bukkit.getLogger().severe("An error occurred returning a warp!"); Bukkit.getLogger().severe("An error occurred returning a warp from the table!");
return null; return null;
} }
} else { } else {
Bukkit.getLogger().severe("Warp returned null!");
return null; return null;
} }
} else { } catch (SQLException ex) {
return null; 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);
} }
} catch (SQLException e) {
Bukkit.getLogger().severe("An SQL exception occurred returning a warp");
e.printStackTrace();
return null;
} }
return null;
} }
// Return a home with a given owner username and home name // Return a home with a given owner username and home name
public static Home getHome(String ownerUsername, String homeName) { public static Home getHome(String ownerUsername, String homeName) {
try { Connection conn = null;
PreparedStatement ps = null;
Integer playerID = getPlayerId(ownerUsername); Integer playerID = getPlayerId(ownerUsername);
String query = "SELECT * FROM " + Main.settings.getHomesTable() + " WHERE `player_id`=%1% AND `name`=%2%;"; ResultSet rs;
query = query.replace("%1%", Integer.toString(playerID));
query = query.replace("%2%", "'" + homeName + "'"); ResultSet rs = queryDatabase(query); try {
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 != null) {
if (rs.next()) {
int locationID = rs.getInt("location_id"); int locationID = rs.getInt("location_id");
TeleportationPoint teleportationPoint = getTeleportationPoint(locationID); TeleportationPoint teleportationPoint = getTeleportationPoint(locationID);
if (teleportationPoint != null && playerID != null) { if (teleportationPoint != null) {
return new Home(teleportationPoint, return new Home(teleportationPoint,
ownerUsername, ownerUsername,
getPlayerUUID(playerID), getPlayerUUID(playerID),
@@ -356,98 +476,152 @@ public class dataManager {
return null; return null;
} }
} else { } else {
Bukkit.getLogger().severe("Home returned null; perhaps player ID was null?");
return null; return null;
} }
} else { } catch (SQLException ex) {
Bukkit.getLogger().severe("An error occurred retrieving a home from the table!"); plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex);
return null; } finally {
try {
if (ps != null)
ps.close();
if (conn != null)
conn.close();
} catch (SQLException ex) {
plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionClose(), ex);
} }
} catch (SQLException e) {
Bukkit.getLogger().severe("An SQL exception occurred returning a home!");
e.printStackTrace();
return null;
} }
return null;
} }
public static boolean warpExists(String warpName) { public static Boolean warpExists(String warpName) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs;
try { try {
String query = "SELECT * FROM " + Main.settings.getWarpsTable() + " WHERE `name`=?;"; conn = getConnection();
query = query.replace("?", "'" + warpName + "'"); ps = conn.prepareStatement("SELECT * FROM huskhomes_warp_data WHERE `name`=?;");
ResultSet rs = queryDatabase(query); ps.setString(1, warpName);
rs = ps.executeQuery();
if (rs != null) { if (rs != null) {
return rs.next(); return rs.next();
} else { } 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; return false;
} }
} catch (SQLException e) { } catch (SQLException ex) {
Bukkit.getLogger().severe("An SQL exception occurred in checking if warp data exists in the table."); plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex);
e.printStackTrace(); } finally {
return false; 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 owner, String homeName) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs;
Integer playerID = getPlayerId(owner.getName());
public static boolean homeExists(Player p, String homeName) {
Integer playerID = getPlayerId(p.getName());
try { try {
String query = "SELECT * FROM " + Main.settings.getHomesTable() + " WHERE `player_id`=%1% AND `name`=%2%;"; conn = getConnection();
query = query.replace("%1%", Integer.toString(playerID)); ps = conn.prepareStatement("SELECT * FROM huskhomes_home_data WHERE `player_id`=? AND `name`=?;");
query = query.replace("%2%", "'" + homeName + "'"); ps.setInt(1, playerID);
ResultSet rs = queryDatabase(query); ps.setString(2, homeName);
rs = ps.executeQuery();
if (rs != null) { if (rs != null) {
return rs.next(); return rs.next();
} else { } 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; return false;
} }
} catch (SQLException e) { } catch (SQLException ex) {
Bukkit.getLogger().severe("An SQL exception occurred in checking if player data exists in the table."); plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex);
e.printStackTrace(); } finally {
return false; 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 player) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs;
String playerUUID = player.getUniqueId().toString();
public static boolean playerExists(Player p) {
try { try {
UUID uuid = p.getUniqueId(); conn = getConnection();
String uuidString = uuid.toString(); ps = conn.prepareStatement("SELECT * FROM huskhomes_player_data WHERE `user_uuid`=?;");
String query = "SELECT * FROM " + Main.settings.getPlayerTable() + " WHERE `user_uuid`=?;"; ps.setString(1, playerUUID);
query = query.replace("?", "'" + uuidString + "'"); rs = ps.executeQuery();
ResultSet rs = queryDatabase(query);
if (rs != null) { if (rs != null) {
return rs.next(); return rs.next();
} else { } 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; return false;
} }
} catch (SQLException e) { } catch (SQLException ex) {
Bukkit.getLogger().severe("An SQL exception occurred in determining if a player exists in the player table."); plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex);
e.printStackTrace(); } finally {
return false; 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 getTeleportationPoint(Integer locationID) {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs;
private static Integer getPlayerId(String playerUsername) {
try { try {
String query = "SELECT * FROM " + Main.settings.getPlayerTable() + " WHERE `username`=?;"; conn = getConnection();
query = query.replace("?", "'" + playerUsername + "'"); ps = conn.prepareStatement("SELECT * FROM huskhomes_location_data WHERE `location_id`=?;");
Bukkit.getLogger().info(query); ps.setInt(1, locationID);
ResultSet rs = queryDatabase(query); rs = ps.executeQuery();
if (rs != null) {
if (rs.next()) { if (rs.next()) {
return rs.getInt("player_id"); 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 { } else {
Bukkit.getLogger().info("Could not find player ID for " + playerUsername); Bukkit.getLogger().severe("An SQL exception occurred in retrieving a teleportation location.");
return null; return null;
} }
} else { } catch (SQLException ex) {
Bukkit.getLogger().severe("An SQL exception occurred in retrieving a player ID."); plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex);
return null; } finally {
try {
if (ps != null)
ps.close();
if (conn != null)
conn.close();
} catch (SQLException ex) {
plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionClose(), ex);
} }
} catch (SQLException e) {
Bukkit.getLogger().severe("An SQL exception occurred returning a player ID!");
return null;
} }
return null;
} }
public static TeleportationPoint getPlayerDestination(Player p) { 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) { public static void setupStorage(String storageType) {
if (storageType.equalsIgnoreCase("sqlite")) { if (storageType.equalsIgnoreCase("sqlite")) {
initializeSQLite(); 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() + "]"); TextComponent clickableHome = new TextComponent("[" + home.getName() + "]");
clickableHome.setColor(ChatColor.ITALIC); clickableHome.setColor(ChatColor.ITALIC);
clickableHome.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, ("/edithome " + home.getName()))); 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; return clickableHome;
} }
@@ -40,7 +40,7 @@ public class homeListHandler {
TextComponent clickableHome = new TextComponent("[" + home.getName() + "]"); TextComponent clickableHome = new TextComponent("[" + home.getName() + "]");
clickableHome.setColor(ChatColor.ITALIC); clickableHome.setColor(ChatColor.ITALIC);
clickableHome.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, ("/phome " + home.getOwnerUsername() + "." + home.getName()))); 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; 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" + "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" + "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)"); "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) { if (defaultMessageFile != null) {
YamlConfiguration yamlConfiguration = YamlConfiguration.loadConfiguration(new InputStreamReader(defaultMessageFile, StandardCharsets.UTF_8)); YamlConfiguration yamlConfiguration = YamlConfiguration.loadConfiguration(new InputStreamReader(defaultMessageFile, StandardCharsets.UTF_8));
config.setDefaults(yamlConfiguration); config.setDefaults(yamlConfiguration);
@@ -16,7 +16,7 @@ public class teleportManager {
String server = teleportationPoint.getServer(); String server = teleportationPoint.getServer();
if (!Main.settings.doBungee() || server.equals(Main.settings.getServerID())) { if (!Main.settings.doBungee() || server.equals(Main.settings.getServerID())) {
p.teleport(teleportationPoint.getLocation()); p.teleport(teleportationPoint.getLocation());
messageManager.sendMessage(p, "teleport_success"); messageManager.sendMessage(p, "teleporting_complete");
} else if (Main.settings.doBungee()) { } else if (Main.settings.doBungee()) {
dataManager.setPlayerDestinationLocation(p, teleportationPoint); dataManager.setPlayerDestinationLocation(p, teleportationPoint);
pluginMessageHandler.sendPlayer(p, server); pluginMessageHandler.sendPlayer(p, server);
@@ -26,6 +26,7 @@ public class teleportManager {
public static void queueTimedTeleport(Player player, String targetPlayer) { public static void queueTimedTeleport(Player player, String targetPlayer) {
if (player.hasPermission("huskhomes.bypass_warmup_timers")) { if (player.hasPermission("huskhomes.bypass_warmup_timers")) {
dataManager.setPlayerLastPosition(player, new TeleportationPoint(player.getLocation(), Main.settings.getServerID())); dataManager.setPlayerLastPosition(player, new TeleportationPoint(player.getLocation(), Main.settings.getServerID()));
setPlayerDestinationFromTargetPlayer(player, targetPlayer);
teleportPlayer(player); teleportPlayer(player);
return; return;
} }
@@ -36,6 +37,7 @@ public class teleportManager {
public static void queueTimedTeleport(Player player, TeleportationPoint point) { public static void queueTimedTeleport(Player player, TeleportationPoint point) {
if (player.hasPermission("huskhomes.bypass_warmup_timers")) { if (player.hasPermission("huskhomes.bypass_warmup_timers")) {
dataManager.setPlayerLastPosition(player, new TeleportationPoint(player.getLocation(), Main.settings.getServerID())); dataManager.setPlayerLastPosition(player, new TeleportationPoint(player.getLocation(), Main.settings.getServerID()));
dataManager.setPlayerDestinationLocation(player, point);
teleportPlayer(player); teleportPlayer(player);
return; return;
} }
-5
View File
@@ -17,8 +17,3 @@ spawn_command:
pitch: pitch:
data_storage_options: data_storage_options:
storage_type: 'SQLite' storage_type: 'SQLite'
table_names:
player_data: 'huskhomes_player_data'
locations_data: 'huskhomes_location_data'
homes_data: 'huskhomes_home_data'
warps_data: 'huskhomes_warp_data'