mirror of
https://github.com/darksaid98/HuskHomes.git
synced 2026-12-02 22:17:17 +00:00
Major SQL & Plugin Message refactoring
This commit is contained in:
1 parent
4b31b31a36
commit
b3fd814c0c
18 files changed
+1245
-1452
No files matched your search
File diff suppressed because it is too large.
Load diff
@@ -1,511 +0,0 @@
|
||||
package me.william278.huskhomes2.data;
|
||||
|
||||
import me.william278.huskhomes2.HuskHomes;
|
||||
import me.william278.huskhomes2.teleport.points.Home;
|
||||
import me.william278.huskhomes2.teleport.points.TeleportationPoint;
|
||||
import me.william278.huskhomes2.teleport.points.Warp;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.UUID;
|
||||
import java.util.logging.Level;
|
||||
|
||||
/*
|
||||
TODO Should not relay on SQL
|
||||
Instead should contain abstract getters and setters etc, so some alternative way of storing data may exist
|
||||
Current method should possibly go into the SQL class
|
||||
*/
|
||||
public abstract class Database {
|
||||
protected HuskHomes plugin;
|
||||
private Connection connection;
|
||||
|
||||
public Database(HuskHomes instance) {
|
||||
plugin = instance;
|
||||
}
|
||||
|
||||
public abstract Connection getSQLConnection();
|
||||
|
||||
public abstract void load();
|
||||
|
||||
public void initialize() {
|
||||
connection = getSQLConnection();
|
||||
try {
|
||||
PreparedStatement ps = connection.prepareStatement("SELECT * FROM " + HuskHomes.getSettings().getPlayerDataTable() + ";");
|
||||
ResultSet rs = ps.executeQuery();
|
||||
close(ps, rs);
|
||||
|
||||
} catch (SQLException ex) {
|
||||
plugin.getLogger().log(Level.SEVERE, "Unable to retrieve connection", ex);
|
||||
}
|
||||
}
|
||||
|
||||
// Insert a teleportation point, returns generated id
|
||||
public Integer addTeleportationPoint(TeleportationPoint point, Connection conn) {
|
||||
try {
|
||||
PreparedStatement ps = conn.prepareStatement("INSERT INTO " + HuskHomes.getSettings().getLocationsDataTable() + " (world,server,x,y,z,yaw,pitch) VALUES(?,?,?,?,?,?,?);", Statement.RETURN_GENERATED_KEYS);
|
||||
|
||||
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());
|
||||
ps.setFloat(6, point.getYaw());
|
||||
ps.setFloat(7, point.getPitch());
|
||||
ps.executeUpdate();
|
||||
|
||||
ResultSet rs = ps.getGeneratedKeys();
|
||||
if (rs.next()) {
|
||||
return rs.getInt(1);
|
||||
}
|
||||
|
||||
} catch (SQLException ex) {
|
||||
plugin.getLogger().log(Level.SEVERE, Error.SQL_CONNECTION_EXECUTE, ex);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
// Insert a home into the database
|
||||
public void addHome(Home home, int playerID) {
|
||||
Connection conn;
|
||||
PreparedStatement ps;
|
||||
|
||||
try {
|
||||
conn = getSQLConnection();
|
||||
Integer locationID = addTeleportationPoint(home, conn);
|
||||
|
||||
ps = conn.prepareStatement("INSERT INTO " + HuskHomes.getSettings().getHomesDataTable() + " (player_id,location_id,name,description,public) VALUES(?,?,?,?,?);");
|
||||
ps.setInt(1, playerID);
|
||||
ps.setInt(2, locationID);
|
||||
ps.setString(3, home.getName());
|
||||
ps.setString(4, home.getDescription());
|
||||
ps.setBoolean(5, home.isPublic());
|
||||
ps.executeUpdate();
|
||||
} catch (SQLException ex) {
|
||||
plugin.getLogger().log(Level.SEVERE, Error.SQL_CONNECTION_EXECUTE, ex);
|
||||
}
|
||||
}
|
||||
|
||||
// Insert a warp into the database
|
||||
public void addWarp(Warp warp) {
|
||||
Connection conn;
|
||||
PreparedStatement ps;
|
||||
|
||||
try {
|
||||
conn = getSQLConnection();
|
||||
|
||||
// Add the teleportation point
|
||||
Integer locationID = addTeleportationPoint(warp, conn);
|
||||
|
||||
// Insert the warp with the location_id of the last inserted teleport point
|
||||
ps = conn.prepareStatement("INSERT INTO " + HuskHomes.getSettings().getWarpsDataTable() + " (location_id,name,description) VALUES(?,?,?);");
|
||||
ps.setInt(1, locationID);
|
||||
ps.setString(2, warp.getName());
|
||||
ps.setString(3, warp.getDescription());
|
||||
ps.executeUpdate();
|
||||
} catch (SQLException ex) {
|
||||
plugin.getLogger().log(Level.SEVERE, Error.SQL_CONNECTION_EXECUTE, ex);
|
||||
}
|
||||
}
|
||||
|
||||
// Delete a warp
|
||||
public void deleteWarp(String warpName) {
|
||||
Connection conn;
|
||||
PreparedStatement ps;
|
||||
|
||||
try {
|
||||
conn = getSQLConnection();
|
||||
|
||||
// Delete the warp with the given name
|
||||
ps = conn.prepareStatement("DELETE FROM " + HuskHomes.getSettings().getWarpsDataTable() + " WHERE `name`=?;");
|
||||
ps.setString(1, warpName);
|
||||
ps.executeUpdate();
|
||||
|
||||
} catch (SQLException ex) {
|
||||
plugin.getLogger().log(Level.SEVERE, Error.SQL_CONNECTION_EXECUTE, ex);
|
||||
}
|
||||
}
|
||||
|
||||
// Delete a warp
|
||||
// Insert a player into the database
|
||||
|
||||
public void deleteHome(String homeName, String ownerUsername) {
|
||||
Connection conn;
|
||||
PreparedStatement ps;
|
||||
|
||||
try {
|
||||
conn = getSQLConnection();
|
||||
|
||||
// Delete the home with the given name and player ID
|
||||
ps = conn.prepareStatement("DELETE FROM " + HuskHomes.getSettings().getHomesDataTable() + " WHERE `name`=? AND `player_id`=(SELECT `player_id` FROM " + HuskHomes.getSettings().getPlayerDataTable() + " WHERE `username`=?);");
|
||||
ps.setString(1, homeName);
|
||||
ps.setString(2, ownerUsername);
|
||||
ps.executeUpdate();
|
||||
|
||||
} catch (SQLException ex) {
|
||||
plugin.getLogger().log(Level.SEVERE, Error.SQL_CONNECTION_EXECUTE, ex);
|
||||
}
|
||||
}
|
||||
public void addPlayer(Player p) {
|
||||
Connection conn;
|
||||
PreparedStatement ps;
|
||||
|
||||
try {
|
||||
conn = getSQLConnection();
|
||||
|
||||
ps = conn.prepareStatement("INSERT INTO " + HuskHomes.getSettings().getPlayerDataTable() + " (user_uuid,username,home_slots,rtp_cooldown,is_teleporting) VALUES(?,?,?,?,?);");
|
||||
ps.setString(1, p.getUniqueId().toString());
|
||||
ps.setString(2, p.getName());
|
||||
ps.setInt(3, Home.getFreeHomes(p));
|
||||
ps.setInt(4, 0);
|
||||
ps.setBoolean(5, false);
|
||||
|
||||
ps.executeUpdate();
|
||||
} catch (SQLException ex) {
|
||||
plugin.getLogger().log(Level.SEVERE, Error.SQL_CONNECTION_EXECUTE, ex);
|
||||
}
|
||||
}
|
||||
|
||||
// Insert a player into the database
|
||||
public void addPlayer(UUID playerUUID, String playerName) {
|
||||
Connection conn;
|
||||
PreparedStatement ps;
|
||||
|
||||
try {
|
||||
conn = getSQLConnection();
|
||||
|
||||
ps = conn.prepareStatement("INSERT INTO " + HuskHomes.getSettings().getPlayerDataTable() + " (user_uuid,username,home_slots,rtp_cooldown,is_teleporting) VALUES(?,?,?,?,?);");
|
||||
ps.setString(1, playerUUID.toString());
|
||||
ps.setString(2, playerName);
|
||||
ps.setInt(3, HuskHomes.getSettings().getFreeHomeSlots());
|
||||
ps.setInt(4, 0);
|
||||
ps.setBoolean(5, false);
|
||||
|
||||
ps.executeUpdate();
|
||||
} catch (SQLException ex) {
|
||||
plugin.getLogger().log(Level.SEVERE, Error.SQL_CONNECTION_EXECUTE, ex);
|
||||
}
|
||||
}
|
||||
|
||||
// Insert a player into the database
|
||||
public void addPlayer(UUID playerUUID, String playerName, int homeSlots) {
|
||||
Connection conn;
|
||||
PreparedStatement ps;
|
||||
|
||||
try {
|
||||
conn = getSQLConnection();
|
||||
|
||||
ps = conn.prepareStatement("INSERT INTO " + HuskHomes.getSettings().getPlayerDataTable() + " (user_uuid,username,home_slots,rtp_cooldown,is_teleporting) VALUES(?,?,?,?,?);");
|
||||
ps.setString(1, playerUUID.toString());
|
||||
ps.setString(2, playerName);
|
||||
ps.setInt(3, homeSlots);
|
||||
ps.setInt(4, 0);
|
||||
ps.setBoolean(5, false);
|
||||
|
||||
ps.executeUpdate();
|
||||
} catch (SQLException ex) {
|
||||
plugin.getLogger().log(Level.SEVERE, Error.SQL_CONNECTION_EXECUTE, ex);
|
||||
}
|
||||
}
|
||||
|
||||
public void updatePlayerUsername(UUID uuid, String newName) {
|
||||
Connection conn;
|
||||
PreparedStatement ps;
|
||||
|
||||
try {
|
||||
conn = getSQLConnection();
|
||||
ps = conn.prepareStatement("UPDATE " + HuskHomes.getSettings().getPlayerDataTable() + " SET `username`=? WHERE `user_uuid`=?;");
|
||||
ps.setString(1, newName);
|
||||
ps.setString(2, uuid.toString());
|
||||
ps.executeUpdate();
|
||||
} catch (SQLException ex) {
|
||||
plugin.getLogger().log(Level.SEVERE, Error.SQL_CONNECTION_EXECUTE, ex);
|
||||
}
|
||||
}
|
||||
|
||||
public void setPlayerTeleporting(UUID uuid, boolean value) {
|
||||
Connection conn;
|
||||
PreparedStatement ps;
|
||||
|
||||
try {
|
||||
conn = getSQLConnection();
|
||||
ps = conn.prepareStatement("UPDATE " + HuskHomes.getSettings().getPlayerDataTable() + " SET `is_teleporting`=? WHERE `user_uuid`=?;");
|
||||
ps.setBoolean(1, value);
|
||||
ps.setString(2, uuid.toString());
|
||||
ps.executeUpdate();
|
||||
} catch (SQLException ex) {
|
||||
plugin.getLogger().log(Level.SEVERE, Error.SQL_CONNECTION_EXECUTE, ex);
|
||||
}
|
||||
}
|
||||
|
||||
public void setPlayerHomeSlots(UUID uuid, int newValue) {
|
||||
Connection conn;
|
||||
PreparedStatement ps;
|
||||
|
||||
try {
|
||||
conn = getSQLConnection();
|
||||
ps = conn.prepareStatement("UPDATE " + HuskHomes.getSettings().getPlayerDataTable() + " SET `home_slots`=? WHERE `user_uuid`=?;");
|
||||
ps.setInt(1, newValue);
|
||||
ps.setString(2, uuid.toString());
|
||||
ps.executeUpdate();
|
||||
} catch (SQLException ex) {
|
||||
plugin.getLogger().log(Level.SEVERE, Error.SQL_CONNECTION_EXECUTE, ex);
|
||||
}
|
||||
}
|
||||
|
||||
public void setRtpCooldown(UUID uuid, int newTime) {
|
||||
Connection conn;
|
||||
PreparedStatement ps;
|
||||
|
||||
try {
|
||||
conn = getSQLConnection();
|
||||
ps = conn.prepareStatement("UPDATE " + HuskHomes.getSettings().getPlayerDataTable() + " SET `rtp_cooldown`=? WHERE `user_uuid`=?;");
|
||||
ps.setInt(1, newTime);
|
||||
ps.setString(2, uuid.toString());
|
||||
ps.executeUpdate();
|
||||
} catch (SQLException ex) {
|
||||
plugin.getLogger().log(Level.SEVERE, Error.SQL_CONNECTION_EXECUTE, ex);
|
||||
}
|
||||
}
|
||||
|
||||
// Delete a teleportation point from SQL
|
||||
public void deleteTeleportationPoint(int locationID) {
|
||||
Connection conn;
|
||||
PreparedStatement ps;
|
||||
|
||||
try {
|
||||
conn = getSQLConnection();
|
||||
|
||||
ps = conn.prepareStatement("DELETE FROM " + HuskHomes.getSettings().getLocationsDataTable() + " WHERE `location_id`=?;");
|
||||
ps.setInt(1, locationID);
|
||||
|
||||
ps.executeUpdate();
|
||||
} catch (SQLException ex) {
|
||||
plugin.getLogger().log(Level.SEVERE, Error.SQL_CONNECTION_EXECUTE, ex);
|
||||
}
|
||||
}
|
||||
|
||||
// Update the location of a home
|
||||
public void setHomePrivacy(String homeName, String ownerName, boolean isPublic) {
|
||||
Connection conn;
|
||||
PreparedStatement ps;
|
||||
|
||||
try {
|
||||
conn = getSQLConnection();
|
||||
|
||||
// Set the home location ID to the new teleport point for the given home
|
||||
ps = conn.prepareStatement("UPDATE " + HuskHomes.getSettings().getHomesDataTable() + " SET `public`=? WHERE `name`=? AND `player_id`=(SELECT `player_id` FROM " + HuskHomes.getSettings().getPlayerDataTable() + " WHERE `username`=?);");
|
||||
ps.setBoolean(1, isPublic);
|
||||
ps.setString(2, homeName);
|
||||
ps.setString(3, ownerName);
|
||||
ps.executeUpdate();
|
||||
} catch (SQLException ex) {
|
||||
plugin.getLogger().log(Level.SEVERE, Error.SQL_CONNECTION_EXECUTE, ex);
|
||||
}
|
||||
}
|
||||
|
||||
// Update the location of a home
|
||||
public void setHomeTeleportPoint(String homeName, int ownerID, TeleportationPoint point) {
|
||||
Connection conn;
|
||||
PreparedStatement ps;
|
||||
|
||||
try {
|
||||
conn = getSQLConnection();
|
||||
|
||||
// Add the teleportation point
|
||||
Integer locationID = addTeleportationPoint(point, conn);
|
||||
|
||||
// Set the home location ID to the new teleport point for the given home
|
||||
ps = conn.prepareStatement("UPDATE " + HuskHomes.getSettings().getHomesDataTable() + " SET `location_id`=? WHERE `name`=? AND `player_id`=?;");
|
||||
ps.setInt(1, locationID);
|
||||
ps.setString(2, homeName);
|
||||
ps.setInt(3, ownerID);
|
||||
ps.executeUpdate();
|
||||
} catch (SQLException ex) {
|
||||
plugin.getLogger().log(Level.SEVERE, Error.SQL_CONNECTION_EXECUTE, ex);
|
||||
}
|
||||
}
|
||||
|
||||
// Update the location of a home
|
||||
public void setWarpTeleportPoint(String warpName, TeleportationPoint point) {
|
||||
Connection conn;
|
||||
PreparedStatement ps;
|
||||
|
||||
try {
|
||||
conn = getSQLConnection();
|
||||
|
||||
// Add the teleportation point
|
||||
Integer locationID = addTeleportationPoint(point, conn);
|
||||
|
||||
// Set the warp location ID to the new teleport point
|
||||
ps = conn.prepareStatement("UPDATE " + HuskHomes.getSettings().getWarpsDataTable() + " SET `location_id`=? WHERE `name`=?;");
|
||||
ps.setInt(1, locationID);
|
||||
ps.setString(2, warpName);
|
||||
ps.executeUpdate();
|
||||
} catch (SQLException ex) {
|
||||
plugin.getLogger().log(Level.SEVERE, Error.SQL_CONNECTION_EXECUTE, ex);
|
||||
}
|
||||
}
|
||||
// Update the description of a home
|
||||
public void setHomeDescription(String homeName, String ownerName, String newDescription) {
|
||||
Connection conn;
|
||||
PreparedStatement ps;
|
||||
|
||||
try {
|
||||
conn = getSQLConnection();
|
||||
|
||||
// Update the home description
|
||||
ps = conn.prepareStatement("UPDATE " + HuskHomes.getSettings().getHomesDataTable() + " SET `description`=? WHERE `name`=? AND `player_id`=(SELECT `player_id` FROM " + HuskHomes.getSettings().getPlayerDataTable() + " WHERE `username`=?);");
|
||||
ps.setString(1, newDescription);
|
||||
ps.setString(2, homeName);
|
||||
ps.setString(3, ownerName);
|
||||
ps.executeUpdate();
|
||||
} catch (SQLException ex) {
|
||||
plugin.getLogger().log(Level.SEVERE, Error.SQL_CONNECTION_EXECUTE, ex);
|
||||
}
|
||||
}
|
||||
|
||||
// Update the description of a warp
|
||||
public void setWarpDescription(String warpName, String newDescription) {
|
||||
Connection conn;
|
||||
PreparedStatement ps;
|
||||
|
||||
try {
|
||||
conn = getSQLConnection();
|
||||
|
||||
// Update the warp description
|
||||
ps = conn.prepareStatement("UPDATE " + HuskHomes.getSettings().getWarpsDataTable() + " SET `description`=? WHERE `name`=?;");
|
||||
ps.setString(1, newDescription);
|
||||
ps.setString(2, warpName);
|
||||
ps.executeUpdate();
|
||||
} catch (SQLException ex) {
|
||||
plugin.getLogger().log(Level.SEVERE, Error.SQL_CONNECTION_EXECUTE, ex);
|
||||
}
|
||||
}
|
||||
|
||||
// Update the name of a home
|
||||
public void setHomeName(String oldHomeName, String ownerName, String newHomeName) {
|
||||
Connection conn;
|
||||
PreparedStatement ps;
|
||||
|
||||
try {
|
||||
conn = getSQLConnection();
|
||||
|
||||
// Update the home name
|
||||
ps = conn.prepareStatement("UPDATE " + HuskHomes.getSettings().getHomesDataTable() + " SET `name`=? WHERE `name`=? AND `player_id`=(SELECT `player_id` FROM " + HuskHomes.getSettings().getPlayerDataTable() + " WHERE `username`=?);");
|
||||
ps.setString(1, newHomeName);
|
||||
ps.setString(2, oldHomeName);
|
||||
ps.setString(3, ownerName);
|
||||
|
||||
ps.executeUpdate();
|
||||
} catch (SQLException ex) {
|
||||
plugin.getLogger().log(Level.SEVERE, Error.SQL_CONNECTION_EXECUTE, ex);
|
||||
}
|
||||
}
|
||||
|
||||
// Update the name of a warp
|
||||
public void setWarpName(String oldWarpName, String newWarpName) {
|
||||
Connection conn;
|
||||
PreparedStatement ps;
|
||||
|
||||
try {
|
||||
conn = getSQLConnection();
|
||||
|
||||
// Update the warp name
|
||||
ps = conn.prepareStatement("UPDATE " + HuskHomes.getSettings().getWarpsDataTable() + " SET `name`=? WHERE `name`=?;");
|
||||
ps.setString(1, newWarpName);
|
||||
ps.setString(2, oldWarpName);
|
||||
|
||||
ps.executeUpdate();
|
||||
} catch (SQLException ex) {
|
||||
plugin.getLogger().log(Level.SEVERE, Error.SQL_CONNECTION_EXECUTE, ex);
|
||||
}
|
||||
}
|
||||
|
||||
// Update a player's last position teleport point
|
||||
public void setTeleportationLastPosition(UUID uuid, TeleportationPoint point) {
|
||||
Connection conn;
|
||||
PreparedStatement ps;
|
||||
|
||||
try {
|
||||
conn = getSQLConnection();
|
||||
|
||||
// Add the teleportation point
|
||||
Integer locationID = addTeleportationPoint(point, conn);
|
||||
|
||||
// Set the destination location with the location_id of the last inserted teleport point
|
||||
ps = conn.prepareStatement("UPDATE " + HuskHomes.getSettings().getPlayerDataTable() + " SET `last_location_id`=? WHERE `user_uuid`=?;");
|
||||
ps.setInt(1, locationID);
|
||||
ps.setString(2, uuid.toString());
|
||||
ps.executeUpdate();
|
||||
} catch (SQLException ex) {
|
||||
plugin.getLogger().log(Level.SEVERE, Error.SQL_CONNECTION_EXECUTE, ex);
|
||||
}
|
||||
}
|
||||
|
||||
// Update a player's destination teleport point
|
||||
public void setTeleportationDestination(String username, TeleportationPoint point) {
|
||||
Connection conn;
|
||||
PreparedStatement ps;
|
||||
|
||||
try {
|
||||
conn = getSQLConnection();
|
||||
|
||||
// Add the teleportation point
|
||||
Integer locationID = addTeleportationPoint(point, conn);
|
||||
|
||||
// Set the destination location with the location_id of the last inserted teleport point
|
||||
ps = conn.prepareStatement("UPDATE " + HuskHomes.getSettings().getPlayerDataTable() + " SET `dest_location_id`=? WHERE `username`=?;");
|
||||
ps.setInt(1, locationID);
|
||||
ps.setString(2, username);
|
||||
ps.executeUpdate();
|
||||
|
||||
} catch (SQLException ex) {
|
||||
plugin.getLogger().log(Level.SEVERE, Error.SQL_CONNECTION_EXECUTE, ex);
|
||||
}
|
||||
}
|
||||
|
||||
// Clear a player's destination
|
||||
public void clearPlayerDestination(String playerName) {
|
||||
Connection conn;
|
||||
PreparedStatement ps;
|
||||
|
||||
try {
|
||||
conn = getSQLConnection();
|
||||
ps = conn.prepareStatement("UPDATE " + HuskHomes.getSettings().getPlayerDataTable() + " SET `dest_location_id`=NULL WHERE `username`=?;");
|
||||
ps.setString(1, playerName);
|
||||
ps.executeUpdate();
|
||||
} catch (SQLException ex) {
|
||||
plugin.getLogger().log(Level.SEVERE, Error.SQL_CONNECTION_EXECUTE, ex);
|
||||
}
|
||||
}
|
||||
|
||||
// Clear a player's last position
|
||||
public void clearPlayerLastPosition(UUID uuid) {
|
||||
Connection conn;
|
||||
PreparedStatement ps;
|
||||
|
||||
try {
|
||||
conn = getSQLConnection();
|
||||
ps = conn.prepareStatement("UPDATE " + HuskHomes.getSettings().getPlayerDataTable() + " SET `last_location_id`=NULL WHERE `user_uuid`=?;");
|
||||
ps.setString(1, uuid.toString());
|
||||
ps.executeUpdate();
|
||||
} catch (SQLException ex) {
|
||||
plugin.getLogger().log(Level.SEVERE, Error.SQL_CONNECTION_EXECUTE, ex);
|
||||
}
|
||||
}
|
||||
|
||||
public void close(PreparedStatement ps, ResultSet rs) {
|
||||
try {
|
||||
if (ps != null)
|
||||
ps.close();
|
||||
if (rs != null)
|
||||
rs.close();
|
||||
} catch (SQLException ex) {
|
||||
Error.close(plugin, ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,114 +0,0 @@
|
||||
package me.william278.huskhomes2.data;
|
||||
|
||||
import me.william278.huskhomes2.HuskHomes;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.logging.Level;
|
||||
|
||||
/*
|
||||
TODO Split into SQLite, MySQL and abstract SQL classes
|
||||
Even tho both are using pretty much the same syntax and stuff, their initializations are too different and should
|
||||
not co-exist in the same class.
|
||||
*/
|
||||
public class SQL extends Database {
|
||||
|
||||
private static final String SQLITE_DATABASE_NAME = "HuskHomesData";
|
||||
|
||||
private Connection connection;
|
||||
|
||||
private void setConnection(Connection connection) {
|
||||
this.connection = connection;
|
||||
}
|
||||
|
||||
public SQL(HuskHomes instance) {
|
||||
super(instance);
|
||||
}
|
||||
|
||||
// Initialise connection via mySQL
|
||||
private void getMySQLConnection() {
|
||||
String host = HuskHomes.getSettings().getMySQLhost();
|
||||
int port = HuskHomes.getSettings().getMySQLport();
|
||||
String database = HuskHomes.getSettings().getMySQLdatabase();
|
||||
String username = HuskHomes.getSettings().getMySQLusername();
|
||||
String password = HuskHomes.getSettings().getMySQLpassword();
|
||||
|
||||
try {
|
||||
synchronized (HuskHomes.getInstance()) {
|
||||
Class.forName("com.mysql.jdbc.Driver");
|
||||
setConnection(DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database + "?autoReconnect=true&useSSL=false", username, password));
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
HuskHomes.disablePlugin("[!] Could not connect to the mySQL Database with the credentials provided!\n[!] Check that your host IP address and port are valid and that your username and password are valid and that the user has the correct access permissions on the database.");
|
||||
} catch (ClassNotFoundException e) {
|
||||
HuskHomes.disablePlugin("[!] A critical exception occurred when attempting to establish a connection to the mySQL database!");
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
// Initialise connection via SQLite
|
||||
private void getSQLiteConnection() {
|
||||
File dataFolder = new File(plugin.getDataFolder(), SQLITE_DATABASE_NAME + ".db");
|
||||
if (!dataFolder.exists()) {
|
||||
try {
|
||||
if (!dataFolder.createNewFile()) {
|
||||
plugin.getLogger().log(Level.SEVERE, "Failed to write new file: " + SQLITE_DATABASE_NAME + ".db (file already exists)");
|
||||
}
|
||||
} catch (IOException e) {
|
||||
plugin.getLogger().log(Level.SEVERE, "An error occurred writing a file: " + SQLITE_DATABASE_NAME + ".db (" + e.getCause() + ")");
|
||||
}
|
||||
}
|
||||
try {
|
||||
Class.forName("org.sqlite.JDBC");
|
||||
setConnection(DriverManager.getConnection("jdbc:sqlite:" + plugin.getDataFolder().getAbsolutePath() + "/" + SQLITE_DATABASE_NAME + ".db"));
|
||||
} catch (SQLException ex) {
|
||||
plugin.getLogger().log(Level.SEVERE, "An exception occurred initialising the SQLite database", ex);
|
||||
} catch (ClassNotFoundException ex) {
|
||||
plugin.getLogger().log(Level.SEVERE, "You need the SQLite JBDC library; please download and place this in the /lib folder.");
|
||||
}
|
||||
}
|
||||
|
||||
public Connection getSQLConnection() {
|
||||
try {
|
||||
if (connection == null || connection.isClosed()) {
|
||||
String dataStorageType = HuskHomes.getSettings().getStorageType().toLowerCase();
|
||||
switch (dataStorageType) {
|
||||
case "mysql":
|
||||
getMySQLConnection();
|
||||
break;
|
||||
case "sqlite":
|
||||
getSQLiteConnection();
|
||||
break;
|
||||
default:
|
||||
plugin.getLogger().log(Level.WARNING, "An invalid data storage type was specified in config.yml; defaulting to SQLite");
|
||||
getSQLiteConnection();
|
||||
break;
|
||||
}
|
||||
}
|
||||
} catch (SQLException ex) {
|
||||
plugin.getLogger().log(Level.WARNING, "An error occurred checking the status of the SQL connection");
|
||||
}
|
||||
return connection;
|
||||
}
|
||||
|
||||
public void load() {
|
||||
connection = getSQLConnection();
|
||||
|
||||
try {
|
||||
Statement s = connection.createStatement();
|
||||
s.execute(DataManager.createLocationsTable);
|
||||
s.execute(DataManager.createPlayerTable);
|
||||
s.execute(DataManager.createHomesTable);
|
||||
s.execute(DataManager.createWarpsTable);
|
||||
s.close();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
initialize();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package me.william278.huskhomes2.data.SQL;
|
||||
|
||||
import me.william278.huskhomes2.HuskHomes;
|
||||
import me.william278.huskhomes2.data.Error;
|
||||
import me.william278.huskhomes2.teleport.points.Home;
|
||||
import me.william278.huskhomes2.teleport.points.TeleportationPoint;
|
||||
import me.william278.huskhomes2.teleport.points.Warp;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.UUID;
|
||||
import java.util.logging.Level;
|
||||
|
||||
/*
|
||||
TODO Should not relay on SQL
|
||||
Instead should contain abstract getters and setters etc, so some alternative way of storing data may exist
|
||||
Current method should possibly go into the SQL class
|
||||
*/
|
||||
public abstract class Database {
|
||||
protected HuskHomes plugin;
|
||||
private Connection connection;
|
||||
|
||||
public Database(HuskHomes instance) {
|
||||
plugin = instance;
|
||||
}
|
||||
|
||||
public abstract Connection getConnection();
|
||||
|
||||
public abstract void load();
|
||||
|
||||
public void initialize() {
|
||||
connection = getConnection();
|
||||
|
||||
// Test the retrieved connection; throw an error if it fails
|
||||
try {
|
||||
PreparedStatement ps = connection.prepareStatement("SELECT * FROM " + HuskHomes.getSettings().getPlayerDataTable() + ";");
|
||||
ResultSet rs = ps.executeQuery();
|
||||
close(ps, rs);
|
||||
} catch (SQLException ex) {
|
||||
plugin.getLogger().log(Level.SEVERE, "Failed to retrieve Database connection: ", ex);
|
||||
}
|
||||
}
|
||||
|
||||
// Close the mySQL connection
|
||||
public void close(PreparedStatement ps, ResultSet rs) {
|
||||
try {
|
||||
if (ps != null)
|
||||
ps.close();
|
||||
if (rs != null)
|
||||
rs.close();
|
||||
} catch (SQLException ex) {
|
||||
plugin.getLogger().log(Level.SEVERE, "Failed to close the Database connection: ", ex);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
package me.william278.huskhomes2.data.SQL;
|
||||
|
||||
import me.william278.huskhomes2.HuskHomes;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.logging.Level;
|
||||
|
||||
public class MySQL extends Database {
|
||||
|
||||
final static String[] SQL_SETUP_STATEMENTS = {
|
||||
"CREATE TABLE IF NOT EXISTS " + HuskHomes.getSettings().getPlayerDataTable() + " (" +
|
||||
"`player_id` integer AUTO_INCREMENT," +
|
||||
"`user_uuid` char(36) NOT NULL UNIQUE," +
|
||||
"`username` varchar(16) NOT NULL," +
|
||||
"`home_slots` integer NOT NULL," +
|
||||
"`rtp_cooldown` integer NOT NULL DEFAULT 0," +
|
||||
"`is_teleporting` boolean NOT NULL DEFAULT 0," +
|
||||
"`dest_location_id` integer NULL," +
|
||||
"`last_location_id` integer NULL," +
|
||||
|
||||
"PRIMARY KEY (`player_id`)," +
|
||||
"FOREIGN KEY (`dest_location_id`) REFERENCES " + HuskHomes.getSettings().getLocationsDataTable() + " (`location_id`) ON DELETE SET NULL ON UPDATE NO ACTION," +
|
||||
"FOREIGN KEY (`last_location_id`) REFERENCES " + HuskHomes.getSettings().getLocationsDataTable() + " (`location_id`) ON DELETE SET NULL ON UPDATE NO ACTION" +
|
||||
");",
|
||||
|
||||
"CREATE TABLE IF NOT EXISTS " + HuskHomes.getSettings().getLocationsDataTable() + " (" +
|
||||
"`location_id` integer AUTO_INCREMENT," +
|
||||
"`server` text NOT NULL," +
|
||||
"`world` text NOT NULL," +
|
||||
"`x` double NOT NULL," +
|
||||
"`y` double NOT NULL," +
|
||||
"`z` double NOT NULL," +
|
||||
"`yaw` float NOT NULL," +
|
||||
"`pitch` float NOT NULL," +
|
||||
|
||||
"PRIMARY KEY (`location_id`)" +
|
||||
");",
|
||||
|
||||
"CREATE TABLE IF NOT EXISTS " + HuskHomes.getSettings().getHomesDataTable() + " (" +
|
||||
"`player_id` integer NOT NULL," +
|
||||
"`location_id` integer NOT NULL," +
|
||||
"`name` varchar(16) NOT NULL," +
|
||||
"`description` varchar(255) NOT NULL," +
|
||||
"`public` boolean NOT NULL," +
|
||||
"PRIMARY KEY (`player_id`, `name`)," +
|
||||
"FOREIGN KEY (`player_id`) REFERENCES " + HuskHomes.getSettings().getPlayerDataTable() + " (`player_id`) ON DELETE CASCADE ON UPDATE NO ACTION," +
|
||||
"FOREIGN KEY (`location_id`) REFERENCES " + HuskHomes.getSettings().getLocationsDataTable() + " (`location_id`) ON DELETE CASCADE ON UPDATE NO ACTION" +
|
||||
");",
|
||||
|
||||
"CREATE TABLE IF NOT EXISTS " + HuskHomes.getSettings().getWarpsDataTable() + " (" +
|
||||
"`location_id` integer NOT NULL," +
|
||||
"`name` varchar(16) NOT NULL UNIQUE," +
|
||||
"`description` varchar(255) NOT NULL," +
|
||||
"PRIMARY KEY (`location_id`)," +
|
||||
"FOREIGN KEY (`location_id`) REFERENCES " + HuskHomes.getSettings().getLocationsDataTable() + " (`location_id`) ON DELETE CASCADE ON UPDATE NO ACTION" +
|
||||
");"
|
||||
};
|
||||
|
||||
final String host = HuskHomes.getSettings().getMySQLhost();
|
||||
final int port = HuskHomes.getSettings().getMySQLport();
|
||||
final String database = HuskHomes.getSettings().getMySQLdatabase();
|
||||
final String username = HuskHomes.getSettings().getMySQLusername();
|
||||
final String password = HuskHomes.getSettings().getMySQLpassword();
|
||||
final String params = HuskHomes.getSettings().getMySQLparams();
|
||||
|
||||
private Connection connection;
|
||||
|
||||
public MySQL(HuskHomes instance) {
|
||||
super(instance);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Connection getConnection() {
|
||||
try {
|
||||
if (connection == null || connection.isClosed()) {
|
||||
try {
|
||||
synchronized (HuskHomes.getInstance()) {
|
||||
Class.forName("com.mysql.cj.jdbc.Driver");
|
||||
connection = (DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database + params, username, password));
|
||||
}
|
||||
} catch (SQLException ex) {
|
||||
plugin.getLogger().log(Level.SEVERE, "An exception occurred initialising the mySQL database: ", ex);
|
||||
} catch (ClassNotFoundException ex) {
|
||||
plugin.getLogger().log(Level.SEVERE, "The mySQL JBDC library is missing! Please download and place this in the /lib folder.");
|
||||
}
|
||||
}
|
||||
} catch (SQLException exception) {
|
||||
plugin.getLogger().log(Level.WARNING, "An error occurred checking the status of the SQL connection: ", exception);
|
||||
}
|
||||
return connection;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void load() {
|
||||
connection = getConnection();
|
||||
try {
|
||||
Statement statement = connection.createStatement();
|
||||
for (String tableCreationStatement : SQL_SETUP_STATEMENTS) {
|
||||
statement.execute(tableCreationStatement);
|
||||
}
|
||||
statement.close();
|
||||
} catch (SQLException exception) {
|
||||
plugin.getLogger().log(Level.SEVERE, "An error occurred creating tables: ", exception);
|
||||
exception.printStackTrace();
|
||||
}
|
||||
|
||||
initialize();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
package me.william278.huskhomes2.data.SQL;
|
||||
|
||||
import me.william278.huskhomes2.HuskHomes;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.logging.Level;
|
||||
|
||||
public class SQLite extends Database {
|
||||
|
||||
final static String[] SQL_SETUP_STATEMENTS = {
|
||||
"PRAGMA foreign_keys = ON;",
|
||||
|
||||
"CREATE TABLE IF NOT EXISTS " + HuskHomes.getSettings().getPlayerDataTable() + " (" +
|
||||
"`player_id` integer NOT NULL," +
|
||||
"`user_uuid` char(36) NOT NULL UNIQUE," +
|
||||
"`username` varchar(16) NOT NULL," +
|
||||
"`home_slots` integer NOT NULL," +
|
||||
"`rtp_cooldown` integer NOT NULL DEFAULT 0," +
|
||||
"`is_teleporting` boolean NOT NULL DEFAULT 0," +
|
||||
"`dest_location_id` integer NULL," +
|
||||
"`last_location_id` integer NULL," +
|
||||
"PRIMARY KEY (`player_id`)," +
|
||||
"FOREIGN KEY (`dest_location_id`) REFERENCES " + HuskHomes.getSettings().getLocationsDataTable() + " (`location_id`) ON DELETE SET NULL ON UPDATE NO ACTION," +
|
||||
"FOREIGN KEY (`last_location_id`) REFERENCES " + HuskHomes.getSettings().getLocationsDataTable() + " (`location_id`) ON DELETE SET NULL ON UPDATE NO ACTION" +
|
||||
");",
|
||||
|
||||
"CREATE TABLE IF NOT EXISTS " + HuskHomes.getSettings().getLocationsDataTable() + " (" +
|
||||
"`location_id` integer PRIMARY KEY," +
|
||||
"`server` text NOT NULL," +
|
||||
"`world` text NOT NULL," +
|
||||
"`x` double NOT NULL," +
|
||||
"`y` double NOT NULL," +
|
||||
"`z` double NOT NULL," +
|
||||
"`yaw` float NOT NULL," +
|
||||
"`pitch` float NOT NULL" +
|
||||
");",
|
||||
|
||||
"CREATE TABLE IF NOT EXISTS " + HuskHomes.getSettings().getHomesDataTable() + " (" +
|
||||
"`player_id` integer NOT NULL," +
|
||||
"`location_id` integer NOT NULL," +
|
||||
"`name` varchar(16) NOT NULL," +
|
||||
"`description` varchar(255) NOT NULL," +
|
||||
"`public` boolean NOT NULL," +
|
||||
"PRIMARY KEY (`player_id`, `name`)," +
|
||||
"FOREIGN KEY (`player_id`) REFERENCES " + HuskHomes.getSettings().getPlayerDataTable() + " (`player_id`) ON DELETE CASCADE ON UPDATE NO ACTION," +
|
||||
"FOREIGN KEY (`location_id`) REFERENCES " + HuskHomes.getSettings().getLocationsDataTable() + " (`location_id`) ON DELETE CASCADE ON UPDATE NO ACTION" +
|
||||
");",
|
||||
|
||||
"CREATE TABLE IF NOT EXISTS " + HuskHomes.getSettings().getWarpsDataTable() + " (" +
|
||||
"`location_id` integer NOT NULL," +
|
||||
"`name` varchar(16) NOT NULL UNIQUE," +
|
||||
"`description` varchar(255) NOT NULL," +
|
||||
"PRIMARY KEY (`location_id`)," +
|
||||
"FOREIGN KEY (`location_id`) REFERENCES " + HuskHomes.getSettings().getLocationsDataTable() + " (`location_id`) ON DELETE CASCADE ON UPDATE NO ACTION" +
|
||||
");"
|
||||
};
|
||||
|
||||
private static final String DATABASE_NAME = "HuskHomesData";
|
||||
|
||||
private Connection connection;
|
||||
|
||||
public SQLite(HuskHomes instance) {
|
||||
super(instance);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Connection getConnection() {
|
||||
try {
|
||||
if (connection == null || connection.isClosed()) {
|
||||
File databaseFile = new File(plugin.getDataFolder(), DATABASE_NAME + ".db");
|
||||
if (!databaseFile.exists()) {
|
||||
try {
|
||||
if (!databaseFile.createNewFile()) {
|
||||
plugin.getLogger().log(Level.SEVERE, "Failed to write new file: " + DATABASE_NAME + ".db (file already exists)");
|
||||
}
|
||||
} catch (IOException e) {
|
||||
plugin.getLogger().log(Level.SEVERE, "An error occurred writing a file: " + DATABASE_NAME + ".db (" + e.getCause() + ")");
|
||||
}
|
||||
}
|
||||
try {
|
||||
Class.forName("org.sqlite.JDBC");
|
||||
connection = (DriverManager.getConnection("jdbc:sqlite:" + plugin.getDataFolder().getAbsolutePath() + "/" + DATABASE_NAME + ".db"));
|
||||
} catch (SQLException ex) {
|
||||
plugin.getLogger().log(Level.SEVERE, "An exception occurred initialising the SQLite database", ex);
|
||||
} catch (ClassNotFoundException ex) {
|
||||
plugin.getLogger().log(Level.SEVERE, "The SQLite JBDC library is missing! Please download and place this in the /lib folder.");
|
||||
}
|
||||
}
|
||||
} catch (SQLException exception) {
|
||||
plugin.getLogger().log(Level.WARNING, "An error occurred checking the status of the SQL connection: ", exception);
|
||||
}
|
||||
return connection;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void load() {
|
||||
connection = getConnection();
|
||||
try {
|
||||
Statement statement = connection.createStatement();
|
||||
for (String tableCreationStatement : SQL_SETUP_STATEMENTS) {
|
||||
statement.execute(tableCreationStatement);
|
||||
}
|
||||
statement.close();
|
||||
} catch (SQLException exception) {
|
||||
plugin.getLogger().log(Level.SEVERE, "An error occurred creating tables: ", exception);
|
||||
exception.printStackTrace();
|
||||
}
|
||||
|
||||
initialize();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,154 @@
|
||||
package me.william278.huskhomes2.data.pluginmessage;
|
||||
|
||||
import com.google.common.io.ByteArrayDataOutput;
|
||||
import com.google.common.io.ByteStreams;
|
||||
import me.william278.huskhomes2.HuskHomes;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.Locale;
|
||||
|
||||
public class PluginMessage {
|
||||
|
||||
private static final HuskHomes plugin = HuskHomes.getInstance();
|
||||
|
||||
// Move a player to a different server in the bungee network
|
||||
public static void sendPlayer(Player p, String targetServer) {
|
||||
ByteArrayDataOutput out = ByteStreams.newDataOutput();
|
||||
out.writeUTF("Connect");
|
||||
out.writeUTF(targetServer);
|
||||
p.sendPluginMessage(plugin, "BungeeCord", out.toByteArray());
|
||||
}
|
||||
|
||||
private static final String MESSAGE_DATA_SEPARATOR = "$";
|
||||
private final int clusterID;
|
||||
private final PluginMessageType messageType;
|
||||
private final String targetPlayerName;
|
||||
private final String messageData;
|
||||
|
||||
/**
|
||||
* Creates a Plugin Message ready to be sent
|
||||
* @param targetPlayerName Name of the player the message will be sent to over Bungee
|
||||
* @param pluginMessageType Type of the plugin message
|
||||
* @param messageData Associated message data
|
||||
*/
|
||||
public PluginMessage(String targetPlayerName, PluginMessageType pluginMessageType, String... messageData) {
|
||||
this.clusterID = HuskHomes.getSettings().getClusterID();
|
||||
this.messageType = pluginMessageType;
|
||||
StringBuilder newMessageData = new StringBuilder();
|
||||
for (String s : messageData) {
|
||||
newMessageData.append(s).append(MESSAGE_DATA_SEPARATOR);
|
||||
}
|
||||
this.messageData = newMessageData.substring(0, newMessageData.toString().length()-1);
|
||||
this.targetPlayerName = targetPlayerName;
|
||||
}
|
||||
|
||||
public PluginMessage(PluginMessageType pluginMessageType, String... messageData) {
|
||||
this.clusterID = HuskHomes.getSettings().getClusterID();
|
||||
this.messageType = pluginMessageType;
|
||||
StringBuilder newMessageData = new StringBuilder();
|
||||
for (String s : messageData) {
|
||||
newMessageData.append(s).append(MESSAGE_DATA_SEPARATOR);
|
||||
}
|
||||
this.messageData = newMessageData.substring(0, newMessageData.toString().length()-1);
|
||||
this.targetPlayerName = null;
|
||||
}
|
||||
|
||||
public PluginMessage(int clusterID, String targetPlayerName, String pluginMessageType, String... messageData) {
|
||||
this.clusterID = clusterID;
|
||||
this.messageType = PluginMessageType.valueOf(pluginMessageType.toUpperCase(Locale.ENGLISH));
|
||||
StringBuilder newMessageData = new StringBuilder();
|
||||
for (String s : messageData) {
|
||||
newMessageData.append(s).append(MESSAGE_DATA_SEPARATOR);
|
||||
}
|
||||
this.messageData = newMessageData.substring(0, newMessageData.toString().length()-1);
|
||||
this.targetPlayerName = targetPlayerName;
|
||||
}
|
||||
|
||||
// Get the string version of the plugin message type
|
||||
private String getPluginMessageString(PluginMessageType type) {
|
||||
return type.name().toLowerCase(Locale.ENGLISH);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send the plugin message
|
||||
* @param sender The player to send the message
|
||||
*/
|
||||
public void send(Player sender) {
|
||||
ByteArrayDataOutput out = ByteStreams.newDataOutput();
|
||||
|
||||
// Send a plugin message to the specified player name
|
||||
out.writeUTF("ForwardToPlayer");
|
||||
out.writeUTF(targetPlayerName);
|
||||
|
||||
// Send the HuskHomes message with a specific type
|
||||
out.writeUTF("HuskHomes:" + clusterID + ":" + getPluginMessageString(messageType));
|
||||
ByteArrayOutputStream messageBytes = new ByteArrayOutputStream();
|
||||
DataOutputStream messageOut = new DataOutputStream(messageBytes);
|
||||
|
||||
// Send the message data; output an exception if there's an error
|
||||
try {
|
||||
messageOut.writeUTF(messageData);
|
||||
} catch (IOException e) {
|
||||
plugin.getLogger().warning("An error occurred trying to send a plugin message (" + e.getCause() + ")");
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
// Write the messages to the output packet
|
||||
out.writeShort(messageBytes.toByteArray().length);
|
||||
out.write(messageBytes.toByteArray());
|
||||
|
||||
// Send the constructed plugin message packet
|
||||
sender.sendPluginMessage(plugin, "BungeeCord", out.toByteArray());
|
||||
}
|
||||
|
||||
public void sendToAll(Player sender) {
|
||||
ByteArrayDataOutput out = ByteStreams.newDataOutput();
|
||||
|
||||
// Send a plugin message to all players
|
||||
out.writeUTF("Forward");
|
||||
out.writeUTF("ALL");
|
||||
|
||||
// Send the HuskHomes message with a specific type
|
||||
out.writeUTF("HuskHomes:" + clusterID + ":" + getPluginMessageString(messageType));
|
||||
ByteArrayOutputStream messageBytes = new ByteArrayOutputStream();
|
||||
DataOutputStream messageOut = new DataOutputStream(messageBytes);
|
||||
|
||||
// Send the message data; output an exception if there's an error
|
||||
try {
|
||||
messageOut.writeUTF(messageData);
|
||||
} catch (IOException e) {
|
||||
plugin.getLogger().warning("An error occurred trying to send a plugin message (" + e.getCause() + ")");
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
// Write the messages to the output packet
|
||||
out.writeShort(messageBytes.toByteArray().length);
|
||||
out.write(messageBytes.toByteArray());
|
||||
|
||||
// Send the constructed plugin message packet
|
||||
sender.sendPluginMessage(plugin, "BungeeCord", out.toByteArray());
|
||||
}
|
||||
|
||||
public int getClusterID() {
|
||||
return clusterID;
|
||||
}
|
||||
|
||||
public PluginMessageType getMessageType() {
|
||||
return messageType;
|
||||
}
|
||||
|
||||
public String getTargetPlayerName() {
|
||||
return targetPlayerName;
|
||||
}
|
||||
|
||||
public String getMessageData() {
|
||||
return messageData;
|
||||
}
|
||||
|
||||
public String[] getMessageDataItems() {
|
||||
return messageData.split("\\" + MESSAGE_DATA_SEPARATOR);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package me.william278.huskhomes2.data.pluginmessage;
|
||||
|
||||
public enum PluginMessageType {
|
||||
SET_TP_DESTINATION,
|
||||
CONFIRM_DESTINATION_SET,
|
||||
TPA_REQUEST,
|
||||
TPAHERE_REQUEST,
|
||||
TPA_REQUEST_REPLY,
|
||||
TPAHERE_REQUEST_REPLY,
|
||||
TELEPORT_TO_ME,
|
||||
GET_ONLINE_PLAYERS,
|
||||
RETURN_ONLINE_PLAYERS,
|
||||
PLAYER_CHANGE
|
||||
}
|
||||
Reference in new issue
Block a user