mirror of
https://github.com/darksaid98/HuskHomes.git
synced 2026-12-03 22:16:34 +00:00
Almost completed SQL handling
This commit is contained in:
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() {
|
public void initialize() {
|
||||||
connection = getSQLConnection();
|
connection = getSQLConnection();
|
||||||
try {
|
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();
|
ResultSet rs = ps.executeQuery();
|
||||||
close(ps, rs);
|
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
|
// Insert a player into the database
|
||||||
public void addPlayer(Player p) {
|
public void addPlayer(Player p) {
|
||||||
Connection conn = null;
|
Connection conn = null;
|
||||||
@@ -147,13 +202,12 @@ public abstract class Database {
|
|||||||
try {
|
try {
|
||||||
conn = getSQLConnection();
|
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(1, p.getUniqueId().toString());
|
||||||
ps.setString(2, p.getName());
|
ps.setString(2, p.getName());
|
||||||
ps.setInt(3, 0);
|
ps.setInt(3, 5); //TODO: Update default home slots to be based on permission & config
|
||||||
ps.setInt(4, 5); //TODO: Update default home slots to be based on permission & config
|
ps.setInt(4, 0);
|
||||||
ps.setInt(5, 0);
|
ps.setBoolean(5, false);
|
||||||
ps.setBoolean(6, false);
|
|
||||||
|
|
||||||
ps.executeUpdate();
|
ps.executeUpdate();
|
||||||
} catch (SQLException ex) {
|
} 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
|
// Delete a teleportation point from SQL
|
||||||
public void deleteTeleportationPoint(int locationID) {
|
public void deleteTeleportationPoint(int locationID) {
|
||||||
Connection conn = null;
|
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
|
// Update a player's destination teleport point
|
||||||
public void setTeleportationLastOrDest(int playerID, TeleportationPoint point, String type) {
|
public void setTeleportationLastOrDest(int playerID, TeleportationPoint point, String type) {
|
||||||
Connection conn = null;
|
Connection conn = null;
|
||||||
|
|||||||
@@ -0,0 +1,28 @@
|
|||||||
|
package me.william278.huskhomes2.Objects;
|
||||||
|
|
||||||
|
import me.william278.huskhomes2.Main;
|
||||||
|
import me.william278.huskhomes2.dataManager;
|
||||||
|
import org.bukkit.entity.Player;
|
||||||
|
|
||||||
|
public class HomeConditionsMet {
|
||||||
|
|
||||||
|
boolean conditionsMet;
|
||||||
|
String conditionsNotMetReason;
|
||||||
|
|
||||||
|
public HomeConditionsMet (Player player) {
|
||||||
|
if (dataManager.getPlayerHomeCount(player) > (Main.settings.getMaximumHomes()-1)) {
|
||||||
|
conditionsMet = false;
|
||||||
|
conditionsNotMetReason = "error_set_home_maximum_homes";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
conditionsMet = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean areConditionsMet() {
|
||||||
|
return conditionsMet;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getConditionsNotMetReason() {
|
||||||
|
return conditionsNotMetReason;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -14,6 +14,8 @@ public class Settings {
|
|||||||
String homesTable;
|
String homesTable;
|
||||||
String warpsTable;
|
String warpsTable;
|
||||||
|
|
||||||
|
int maximumHomes;
|
||||||
|
|
||||||
boolean doBungee;
|
boolean doBungee;
|
||||||
|
|
||||||
private void setSettings(FileConfiguration configFile) {
|
private void setSettings(FileConfiguration configFile) {
|
||||||
@@ -28,6 +30,8 @@ public class Settings {
|
|||||||
this.homesTable = configFile.getString("data_storage_options.table_names.home_data");
|
this.homesTable = configFile.getString("data_storage_options.table_names.home_data");
|
||||||
this.warpsTable = configFile.getString("data_storage_options.table_names.warp_data");
|
this.warpsTable = configFile.getString("data_storage_options.table_names.warp_data");
|
||||||
|
|
||||||
|
this.maximumHomes = configFile.getInt("limits.max_sethomes");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Settings(FileConfiguration configFile) {
|
public Settings(FileConfiguration configFile) {
|
||||||
@@ -69,4 +73,6 @@ public class Settings {
|
|||||||
public String getWarpsTable() {
|
public String getWarpsTable() {
|
||||||
return warpsTable;
|
return warpsTable;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int getMaximumHomes() { return maximumHomes; }
|
||||||
}
|
}
|
||||||
@@ -19,12 +19,9 @@ public class dataManager {
|
|||||||
"`player_id` integer NOT NULL," +
|
"`player_id` integer NOT NULL," +
|
||||||
"`uuid` text NOT NULL UNIQUE," +
|
"`uuid` text NOT NULL UNIQUE," +
|
||||||
"`username` text NOT NULL," +
|
"`username` text NOT NULL," +
|
||||||
"`home_count` integer NOT NULL," +
|
|
||||||
"`home_slots` integer NOT NULL," +
|
"`home_slots` integer NOT NULL," +
|
||||||
"`rtp_cooldown` integer NOT NULL," +
|
"`rtp_cooldown` integer NOT NULL," +
|
||||||
"`is_teleporting` boolean NOT NULL," +
|
"`is_teleporting` boolean NOT NULL," +
|
||||||
"`dest_type` text," + // Destination can be PLAYER or LOCATION.
|
|
||||||
"`dest_player` integer," + // These values below can be null if needed
|
|
||||||
"`dest_location_id` integer," +
|
"`dest_location_id` integer," +
|
||||||
"`last_location_id` integer," +
|
"`last_location_id` integer," +
|
||||||
|
|
||||||
@@ -76,7 +73,7 @@ public class dataManager {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public ResultSet queryDatabase(String query) {
|
public static ResultSet queryDatabase(String query) {
|
||||||
if (Main.settings.getStorageType().equalsIgnoreCase("mysql")) {
|
if (Main.settings.getStorageType().equalsIgnoreCase("mysql")) {
|
||||||
return null;
|
return null;
|
||||||
} else {
|
} else {
|
||||||
@@ -84,8 +81,85 @@ public class dataManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Return an integer from the player table
|
||||||
|
public static Integer getPlayerInteger(Player p, String column) {
|
||||||
|
try {
|
||||||
|
Integer playerID = getPlayerId(p.getName());
|
||||||
|
ResultSet rs = queryDatabase("SELECT * FROM " + Main.settings.getHomesTable() + " WHERE `player_id`=" + playerID + ";");
|
||||||
|
if (rs.next()) {
|
||||||
|
return rs.getInt(column);
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
Bukkit.getLogger().severe("An SQL exception occurred returning a value for " + p.getName());
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get a string value from a player's data row
|
||||||
|
public static String getPlayerString(Player p, String column) {
|
||||||
|
return getPlayerString(getPlayerId(p.getName()), column);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get a string value from a player with given ID's data row
|
||||||
|
public static String getPlayerString(int playerID, String column) {
|
||||||
|
try {
|
||||||
|
ResultSet rs = queryDatabase("SELECT * FROM " + Main.settings.getPlayerTable() + " WHERE `player_id`=" + (playerID) + ";");
|
||||||
|
if (rs.next()) {
|
||||||
|
return rs.getString(column);
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
Bukkit.getLogger().severe("An SQL exception occurred returning a player UUID!");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return how many home slots a player has
|
||||||
|
public static Boolean getPlayerTeleporting(Player p) {
|
||||||
|
try {
|
||||||
|
Integer playerID = getPlayerId(p.getName());
|
||||||
|
ResultSet rs = queryDatabase("SELECT * FROM " + Main.settings.getHomesTable() + " WHERE `player_id`=" + playerID + ";");
|
||||||
|
if (rs.next()) {
|
||||||
|
return rs.getBoolean("is_teleporting");
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
} catch (SQLException e) {
|
||||||
|
Bukkit.getLogger().severe("An SQL exception occurred checking " + p.getName() + " is teleporting");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return a player's UUID
|
||||||
|
public static String getPlayerUUID(int playerID) {
|
||||||
|
return getPlayerString(playerID, "uuid");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return a player's username
|
||||||
|
public static String getPlayerUsername(int playerID) {
|
||||||
|
return getPlayerString(playerID, "username");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return how many homes the player has set
|
||||||
|
public static int getPlayerHomeCount(Player p) {
|
||||||
|
return getPlayerHomes(p.getName()).size();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return how many home slots a player has
|
||||||
|
public static Integer getPlayerHomeSlots(Player p) {
|
||||||
|
return getPlayerInteger(p, "home_slots");
|
||||||
|
}
|
||||||
|
|
||||||
|
// Return how many home slots a player has
|
||||||
|
public static Integer getPlayerRtpCooldown(Player p) {
|
||||||
|
return getPlayerInteger(p, "rtp_cooldown");
|
||||||
|
}
|
||||||
|
|
||||||
// Return an array of a player's homes
|
// Return an array of a player's homes
|
||||||
public ArrayList<Home> getPlayerHomes(String playerName) {
|
public static ArrayList<Home> getPlayerHomes(String playerName) {
|
||||||
try {
|
try {
|
||||||
Integer playerID = getPlayerId(playerName);
|
Integer playerID = getPlayerId(playerName);
|
||||||
ResultSet rs = queryDatabase("SELECT * FROM " + Main.settings.getHomesTable() + " WHERE `player_id`=" + playerID + ";");
|
ResultSet rs = queryDatabase("SELECT * FROM " + Main.settings.getHomesTable() + " WHERE `player_id`=" + playerID + ";");
|
||||||
@@ -102,7 +176,7 @@ public class dataManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Return an array of all the public homes
|
// Return an array of all the public homes
|
||||||
public ArrayList<Home> getPublicHomes() {
|
public static ArrayList<Home> getPublicHomes() {
|
||||||
try {
|
try {
|
||||||
ResultSet rs = queryDatabase("SELECT * FROM " + Main.settings.getHomesTable() + " WHERE `public`;");
|
ResultSet rs = queryDatabase("SELECT * FROM " + Main.settings.getHomesTable() + " WHERE `public`;");
|
||||||
ArrayList<Home> publicHomes = new ArrayList<>();
|
ArrayList<Home> publicHomes = new ArrayList<>();
|
||||||
@@ -119,7 +193,7 @@ public class dataManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Return an array of all the warps
|
// Return an array of all the warps
|
||||||
public ArrayList<Warp> getWarps() {
|
public static ArrayList<Warp> getWarps() {
|
||||||
try {
|
try {
|
||||||
ResultSet rs = queryDatabase("SELECT * FROM " + Main.settings.getWarpsTable() + ";");
|
ResultSet rs = queryDatabase("SELECT * FROM " + Main.settings.getWarpsTable() + ";");
|
||||||
ArrayList<Warp> warps = new ArrayList<>();
|
ArrayList<Warp> warps = new ArrayList<>();
|
||||||
@@ -135,7 +209,7 @@ public class dataManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Return a warp with a given name (warp names are unique)
|
// Return a warp with a given name (warp names are unique)
|
||||||
public Warp getWarp(String name) {
|
public static Warp getWarp(String name) {
|
||||||
try {
|
try {
|
||||||
ResultSet rs = queryDatabase("SELECT * FROM " + Main.settings.getWarpsTable() + " WHERE `name`=" + name + ";");
|
ResultSet rs = queryDatabase("SELECT * FROM " + Main.settings.getWarpsTable() + " WHERE `name`=" + name + ";");
|
||||||
if (rs.next()) {
|
if (rs.next()) {
|
||||||
@@ -151,13 +225,18 @@ public class dataManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Return a home with a given owner username and home name
|
// Return a home with a given owner username and home name
|
||||||
public Home getHome(String ownerUsername, String homeName) {
|
public static Home getHome(String ownerUsername, String homeName) {
|
||||||
try {
|
try {
|
||||||
Integer playerID = getPlayerId(ownerUsername);
|
Integer playerID = getPlayerId(ownerUsername);
|
||||||
ResultSet rs = queryDatabase("SELECT * FROM " + Main.settings.getHomesTable() + " WHERE `player_id`=" + (playerID) + " AND `name`=" + homeName +";");
|
ResultSet rs = queryDatabase("SELECT * FROM " + Main.settings.getHomesTable() + " WHERE `player_id`=" + (playerID) + " AND `name`=" + homeName +";");
|
||||||
if (rs.next()) {
|
if (rs.next()) {
|
||||||
int locationID = rs.getInt("location_id");
|
int locationID = rs.getInt("location_id");
|
||||||
return new Home(getTeleportationPoint(locationID), ownerUsername, getPlayerUUID(playerID), rs.getString("name"), rs.getString("description"), rs.getBoolean("public"));
|
return new Home(getTeleportationPoint(locationID),
|
||||||
|
ownerUsername,
|
||||||
|
getPlayerUUID(playerID),
|
||||||
|
rs.getString("name"),
|
||||||
|
rs.getString("description"),
|
||||||
|
rs.getBoolean("public"));
|
||||||
} else {
|
} else {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@@ -167,7 +246,7 @@ public class dataManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean playerExists(Player p) {
|
public static boolean playerExists(Player p) {
|
||||||
try {
|
try {
|
||||||
ResultSet rs = queryDatabase("SELECT * FROM " + Main.settings.getPlayerTable() + " WHERE `uuid`=" + p.getUniqueId().toString() + ";");
|
ResultSet rs = queryDatabase("SELECT * FROM " + Main.settings.getPlayerTable() + " WHERE `uuid`=" + p.getUniqueId().toString() + ";");
|
||||||
return rs.next();
|
return rs.next();
|
||||||
@@ -178,7 +257,7 @@ public class dataManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private Integer getPlayerId(String playerUsername) {
|
private static Integer getPlayerId(String playerUsername) {
|
||||||
try {
|
try {
|
||||||
ResultSet rs = queryDatabase("SELECT * FROM " + Main.settings.getPlayerTable() + " WHERE `username`=" + (playerUsername) + ";");
|
ResultSet rs = queryDatabase("SELECT * FROM " + Main.settings.getPlayerTable() + " WHERE `username`=" + (playerUsername) + ";");
|
||||||
if (rs.next()) {
|
if (rs.next()) {
|
||||||
@@ -192,66 +271,18 @@ public class dataManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getPlayerUUID(int playerID) {
|
public static TeleportationPoint getPlayerDestination(Player p) {
|
||||||
try {
|
int locationID = getPlayerInteger(p, "dest_location_id");
|
||||||
ResultSet rs = queryDatabase("SELECT * FROM " + Main.settings.getPlayerTable() + " 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 void deletePlayerDestination(Player p) {
|
|
||||||
int destinationID = getPlayerPositionID(p, "dest");
|
|
||||||
if (Main.settings.getStorageType().equalsIgnoreCase("mySQL")) {
|
|
||||||
|
|
||||||
} else {
|
|
||||||
sqliteDatabase.deleteTeleportationPoint(destinationID);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void deletePlayerLastPosition(Player p) {
|
|
||||||
int lastPositionID = getPlayerPositionID(p, "last");
|
|
||||||
if (Main.settings.getStorageType().equalsIgnoreCase("mySQL")) {
|
|
||||||
|
|
||||||
} else {
|
|
||||||
sqliteDatabase.deleteTeleportationPoint(lastPositionID);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public TeleportationPoint getPlayerDestination(Player p) {
|
|
||||||
int locationID = getPlayerPositionID(p, "dest");
|
|
||||||
return getTeleportationPoint(locationID);
|
return getTeleportationPoint(locationID);
|
||||||
}
|
}
|
||||||
|
|
||||||
public TeleportationPoint getPlayerLastPosition(Player p) {
|
public static TeleportationPoint getPlayerLastPosition(Player p) {
|
||||||
int locationID = getPlayerPositionID(p, "last");
|
int locationID = getPlayerInteger(p, "last_location_id");
|
||||||
return getTeleportationPoint(locationID);
|
return getTeleportationPoint(locationID);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Integer getPlayerPositionID(Player p, String type) {
|
|
||||||
try {
|
|
||||||
String playerName = p.getName();
|
|
||||||
Integer playerID = getPlayerId(playerName);
|
|
||||||
ResultSet rs = queryDatabase("SELECT * FROM " + Main.settings.getPlayerTable() + " WHERE `player_id`=" + playerID + ";");
|
|
||||||
if (rs.next()) {
|
|
||||||
return rs.getInt(type + "_location_id");
|
|
||||||
} else {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
} catch (SQLException e) {
|
|
||||||
Bukkit.getLogger().severe("An SQL exception occurred returning a player's destination location ID!");
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Update a player's last position location on SQL
|
// Update a player's last position location on SQL
|
||||||
public void setPlayerLastPosition(Player p, TeleportationPoint point) {
|
public static void setPlayerLastPosition(Player p, TeleportationPoint point) {
|
||||||
Integer playerID = getPlayerId(p.getName());
|
Integer playerID = getPlayerId(p.getName());
|
||||||
if (playerID != null) {
|
if (playerID != null) {
|
||||||
deletePlayerLastPosition(p);
|
deletePlayerLastPosition(p);
|
||||||
@@ -266,7 +297,7 @@ public class dataManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Update a player's destination location on SQL
|
// Update a player's destination location on SQL
|
||||||
public void setPlayerDestination(Player p, TeleportationPoint point) {
|
public static void setPlayerDestinationLocation(Player p, TeleportationPoint point) {
|
||||||
Integer playerID = getPlayerId(p.getName());
|
Integer playerID = getPlayerId(p.getName());
|
||||||
if (playerID != null) {
|
if (playerID != null) {
|
||||||
deletePlayerDestination(p);
|
deletePlayerDestination(p);
|
||||||
@@ -280,25 +311,67 @@ public class dataManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getPlayerUsername(int playerID) {
|
public static void setPlayerTeleporting(Player p, boolean value) {
|
||||||
try {
|
Integer playerID = getPlayerId(p.getName());
|
||||||
ResultSet rs = queryDatabase("SELECT * FROM " + Main.settings.getPlayerTable() + " WHERE `player_id`=" + (playerID) + ";");
|
if (playerID != null) {
|
||||||
if (rs.next()) {
|
deletePlayerDestination(p);
|
||||||
return rs.getString("username");
|
if (Main.settings.getStorageType().equalsIgnoreCase("mySQL")) {
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
return null;
|
sqliteDatabase.setPlayerTeleporting(playerID, value);
|
||||||
}
|
}
|
||||||
} catch (SQLException e) {
|
} else {
|
||||||
Bukkit.getLogger().severe("An SQL exception occurred returning a player username!");
|
Bukkit.getLogger().warning("Failed to update player destination records for " + p.getName());
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public TeleportationPoint getTeleportationPoint(int locationID) {
|
public static void deletePlayerDestination(Player p) {
|
||||||
|
int destinationID = getPlayerInteger(p, "dest_location_id");
|
||||||
|
if (Main.settings.getStorageType().equalsIgnoreCase("mySQL")) {
|
||||||
|
|
||||||
|
} else {
|
||||||
|
sqliteDatabase.deleteTeleportationPoint(destinationID);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void deletePlayerLastPosition(Player p) {
|
||||||
|
int lastPositionID = getPlayerInteger(p, "last_location_id");
|
||||||
|
if (Main.settings.getStorageType().equalsIgnoreCase("mySQL")) {
|
||||||
|
|
||||||
|
} else {
|
||||||
|
sqliteDatabase.deleteTeleportationPoint(lastPositionID);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void addWarp(Warp warp) {
|
||||||
|
if (Main.settings.getStorageType().equalsIgnoreCase("mySQL")) {
|
||||||
|
|
||||||
|
} else {
|
||||||
|
sqliteDatabase.addWarp(warp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void addHome(Home home, Player p) {
|
||||||
|
Integer playerID = getPlayerId(p.getName());
|
||||||
|
if (playerID != null) {
|
||||||
|
if (Main.settings.getStorageType().equalsIgnoreCase("mySQL")) {
|
||||||
|
|
||||||
|
} else {
|
||||||
|
sqliteDatabase.addHome(home, playerID);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Bukkit.getLogger().warning("Failed to add a home for " + p.getName());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static TeleportationPoint getTeleportationPoint(int locationID) {
|
||||||
try {
|
try {
|
||||||
ResultSet rs = queryDatabase("SELECT * FROM " + Main.settings.getLocationsTable() + " WHERE `location_id`=" + (locationID) + ";");
|
ResultSet rs = queryDatabase("SELECT * FROM " + Main.settings.getLocationsTable() + " WHERE `location_id`=" + (locationID) + ";");
|
||||||
if (rs.next()) {
|
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"));
|
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 {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@@ -319,4 +392,4 @@ public class dataManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -7,12 +7,10 @@ import org.bukkit.configuration.file.FileConfiguration;
|
|||||||
import org.bukkit.configuration.file.YamlConfiguration;
|
import org.bukkit.configuration.file.YamlConfiguration;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
|
|
||||||
import java.awt.*;
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
import java.nio.charset.Charset;
|
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
|
|||||||
@@ -0,0 +1,27 @@
|
|||||||
|
package me.william278.huskhomes2;
|
||||||
|
|
||||||
|
import org.bukkit.entity.Player;
|
||||||
|
import org.bukkit.scheduler.BukkitScheduler;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
|
||||||
|
import static org.bukkit.Bukkit.getServer;
|
||||||
|
|
||||||
|
public class runEverySecond {
|
||||||
|
|
||||||
|
private static Main plugin = Main.getInstance();
|
||||||
|
|
||||||
|
// HashMap mapping teleportation times to players
|
||||||
|
public static HashMap<Player,Integer> teleportationTimer = new HashMap<>();
|
||||||
|
|
||||||
|
public runEverySecond() {
|
||||||
|
BukkitScheduler scheduler = getServer().getScheduler();
|
||||||
|
|
||||||
|
// Run every second
|
||||||
|
scheduler.scheduleSyncRepeatingTask(plugin, () -> {
|
||||||
|
for (Player p : teleportationTimer.keySet()) {
|
||||||
|
|
||||||
|
}
|
||||||
|
}, 0L, 20L);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,13 +1,28 @@
|
|||||||
package me.william278.huskhomes2;
|
package me.william278.huskhomes2;
|
||||||
|
|
||||||
|
import me.william278.huskhomes2.Objects.Home;
|
||||||
|
import me.william278.huskhomes2.Objects.HomeConditionsMet;
|
||||||
import org.bukkit.Location;
|
import org.bukkit.Location;
|
||||||
|
import org.bukkit.entity.Player;
|
||||||
|
|
||||||
// This class handles setting homes, warps and the spawn location.
|
// This class handles setting homes, warps and the spawn location.
|
||||||
public class settingHandler {
|
public class settingHandler {
|
||||||
|
|
||||||
// Set a home at the specified position
|
// Set a home at the specified position
|
||||||
public static void setHome(Location location) {
|
public static void setHome(Location location, Player player, String name) {
|
||||||
|
HomeConditionsMet homeConditionsMet = new HomeConditionsMet(player);
|
||||||
|
if (homeConditionsMet.areConditionsMet()) {
|
||||||
|
dataManager.addHome(new Home(location, Main.settings.getServerID(), player, name, false), player);
|
||||||
|
messageManager.sendMessage(player, "set_home_success", name);
|
||||||
|
} else {
|
||||||
|
if (homeConditionsMet.getConditionsNotMetReason().equals("error_set_home_maximum_homes")) {
|
||||||
|
messageManager.sendMessage(player, "error_set_home_maximum_homes", Integer.toString(Main.settings.getMaximumHomes()));
|
||||||
|
} else {
|
||||||
|
messageManager.sendMessage(player, homeConditionsMet.getConditionsNotMetReason());
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,7 +1,6 @@
|
|||||||
package me.william278.huskhomes2;
|
package me.william278.huskhomes2;
|
||||||
|
|
||||||
import me.william278.huskhomes2.Objects.TeleportationPoint;
|
import me.william278.huskhomes2.Objects.TeleportationPoint;
|
||||||
import org.bukkit.Server;
|
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
|
|
||||||
public class teleportManager {
|
public class teleportManager {
|
||||||
@@ -12,6 +11,7 @@ public class teleportManager {
|
|||||||
p.teleport(teleportationPoint.getLocation());
|
p.teleport(teleportationPoint.getLocation());
|
||||||
messageManager.sendMessage(p, "teleport_success");
|
messageManager.sendMessage(p, "teleport_success");
|
||||||
} else if (Main.settings.doBungee()) {
|
} else if (Main.settings.doBungee()) {
|
||||||
|
dataManager.setPlayerDestinationLocation(p, teleportationPoint);
|
||||||
pluginMessageHandler.sendPlayer(p, server);
|
pluginMessageHandler.sendPlayer(p, server);
|
||||||
} else {
|
} else {
|
||||||
return;
|
return;
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
language: 'en-gb'
|
language: 'en-gb'
|
||||||
|
limits:
|
||||||
|
max_sethomes: 10
|
||||||
bungee_options:
|
bungee_options:
|
||||||
enable_bungee_mode: false
|
enable_bungee_mode: false
|
||||||
server_id: 'server'
|
server_id: 'server'
|
||||||
|
|||||||
Reference in new issue
Block a user