mirror of
https://github.com/darksaid98/HuskHomes.git
synced 2026-12-02 14:38:52 +00:00
Updated SQL handling
This commit is contained in:
1 parent
9d902a8453
commit
b95629a986
8 files changed
+454
-39
No files matched your search
@@ -1,21 +1,24 @@
|
||||
package me.william278.huskhomes2.Data.SQLite;
|
||||
|
||||
import me.william278.huskhomes2.Data.SQLite.Errors.Error;
|
||||
import me.william278.huskhomes2.Data.SQLite.Errors.Errors;
|
||||
import me.william278.huskhomes2.Main;
|
||||
import me.william278.huskhomes2.Objects.Home;
|
||||
import me.william278.huskhomes2.Objects.TeleportationPoint;
|
||||
import me.william278.huskhomes2.Objects.Warp;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.logging.Level;
|
||||
|
||||
import me.william278.huskhomes2.Main;
|
||||
import me.william278.huskhomes2.Data.SQLite.Errors.Error;
|
||||
import me.william278.huskhomes2.Data.SQLite.Errors.Errors;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public abstract class Database {
|
||||
Main plugin;
|
||||
Connection connection;
|
||||
|
||||
public Database(Main instance){
|
||||
public Database(Main instance) {
|
||||
plugin = instance;
|
||||
}
|
||||
|
||||
@@ -23,12 +26,12 @@ public abstract class Database {
|
||||
|
||||
public abstract void load();
|
||||
|
||||
public void initialize(){
|
||||
public void initialize() {
|
||||
connection = getSQLConnection();
|
||||
try{
|
||||
try {
|
||||
PreparedStatement ps = connection.prepareStatement("SELECT * FROM " + Main.settings.getPlayerTable() + " WHERE uuid = ?");
|
||||
ResultSet rs = ps.executeQuery();
|
||||
close(ps,rs);
|
||||
close(ps, rs);
|
||||
|
||||
} catch (SQLException ex) {
|
||||
plugin.getLogger().log(Level.SEVERE, "Unable to retrieve connection", ex);
|
||||
@@ -59,29 +62,39 @@ public abstract class Database {
|
||||
return null;
|
||||
}
|
||||
|
||||
// Insert a teleportation point
|
||||
public void addTeleportationPoint(TeleportationPoint point, Connection conn, PreparedStatement ps) {
|
||||
try {
|
||||
ps = conn.prepareStatement("INSERT INTO " + Main.settings.getLocationsTable() + " (world,server,x,y,z,yaw,pitch) VALUES(?,?,?,?,?,?,?);");
|
||||
|
||||
ps.setString(1, point.getServer());
|
||||
ps.setString(2, point.getWorldName());
|
||||
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();
|
||||
} catch (SQLException ex) {
|
||||
plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Now we need methods to save things to the database
|
||||
public void setTokens(Player player, Integer tokens, Integer total) {
|
||||
// Insert a home into the database
|
||||
public void addHome(Home home, int playerID) {
|
||||
Connection conn = null;
|
||||
PreparedStatement ps = null;
|
||||
|
||||
try {
|
||||
conn = getSQLConnection();
|
||||
ps = conn.prepareStatement("REPLACE INTO " + table + " (player,kills,total) VALUES(?,?,?)"); // IMPORTANT. In SQLite class, We made 3 colums. player, Kills, Total.
|
||||
ps.setString(1, player.getName().toLowerCase()); // YOU MUST put these into this line!! And depending on how many
|
||||
// colums you put (say you made 5) All 5 need to be in the brackets
|
||||
// Seperated with comma's (,) AND there needs to be the same amount of
|
||||
// question marks in the VALUES brackets. Right now i only have 3 colums
|
||||
// So VALUES (?,?,?) If you had 5 colums VALUES(?,?,?,?,?)
|
||||
addTeleportationPoint(home, conn, ps);
|
||||
|
||||
ps.setInt(2, tokens); // This sets the value in the database. The colums go in order. Player is ID 1, kills is ID 2, Total would be 3 and so on. you can use
|
||||
// setInt, setString and so on. tokens and total are just variables sent in, You can manually send values in as well. p.setInt(2, 10) <-
|
||||
// This would set the players kills instantly to 10. Sorry about the variable names, It sets their kills to 10 i just have the variable called
|
||||
// Tokens from another plugin :/
|
||||
ps.setInt(3, total);
|
||||
ps = conn.prepareStatement("INSERT INTO " + Main.settings.getHomesTable() + " (player_id,location_id,name,description,public) VALUES(?,(SELECT last_insert_rowid()),?,?,?);");
|
||||
ps.setInt(1, playerID);
|
||||
ps.setString(2, home.getName());
|
||||
ps.setString(3, home.getDescription());
|
||||
ps.setBoolean(4, home.isPublic());
|
||||
ps.executeUpdate();
|
||||
return;
|
||||
} catch (SQLException ex) {
|
||||
plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex);
|
||||
} finally {
|
||||
@@ -94,11 +107,125 @@ public abstract class Database {
|
||||
plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionClose(), ex);
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Insert a warp into the database
|
||||
public void addWarp(Warp warp) {
|
||||
Connection conn = null;
|
||||
PreparedStatement ps = null;
|
||||
|
||||
public void close(PreparedStatement ps, ResultSet rs){
|
||||
try {
|
||||
conn = getSQLConnection();
|
||||
|
||||
// Add the teleportation point
|
||||
addTeleportationPoint(warp, conn, ps);
|
||||
|
||||
// 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.setString(1, warp.getName());
|
||||
ps.setString(2, warp.getDescription());
|
||||
ps.executeUpdate();
|
||||
} catch (SQLException ex) {
|
||||
plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex);
|
||||
} finally {
|
||||
try {
|
||||
if (ps != null)
|
||||
ps.close();
|
||||
if (conn != null)
|
||||
conn.close();
|
||||
} catch (SQLException ex) {
|
||||
plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionClose(), ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Insert a player into the database
|
||||
public void addPlayer(Player p) {
|
||||
Connection conn = null;
|
||||
PreparedStatement ps = null;
|
||||
|
||||
try {
|
||||
conn = getSQLConnection();
|
||||
|
||||
ps = conn.prepareStatement("INSERT INTO " + Main.settings.getPlayerTable() + " (uuid,username,home_count,home_slots,rtp_cooldown,is_teleporting) VALUES(?,?,?,?,?,?);");
|
||||
ps.setString(1, p.getUniqueId().toString());
|
||||
ps.setString(2, p.getName());
|
||||
ps.setInt(3, 0);
|
||||
ps.setInt(4, 5); //TODO: Update default home slots to be based on permission & config
|
||||
ps.setInt(5, 0);
|
||||
ps.setBoolean(6, false);
|
||||
|
||||
ps.executeUpdate();
|
||||
} catch (SQLException ex) {
|
||||
plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex);
|
||||
} finally {
|
||||
try {
|
||||
if (ps != null)
|
||||
ps.close();
|
||||
if (conn != null)
|
||||
conn.close();
|
||||
} catch (SQLException ex) {
|
||||
plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionClose(), ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Delete a teleportation point from SQL
|
||||
public void deleteTeleportationPoint(int locationID) {
|
||||
Connection conn = null;
|
||||
PreparedStatement ps = null;
|
||||
|
||||
try {
|
||||
conn = getSQLConnection();
|
||||
|
||||
ps = conn.prepareStatement("DELETE FROM " + Main.settings.getPlayerTable() + " WHERE `location_id`=?;");
|
||||
ps.setInt(1, locationID);
|
||||
|
||||
ps.executeUpdate();
|
||||
} catch (SQLException ex) {
|
||||
plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionExecute(), ex);
|
||||
} finally {
|
||||
try {
|
||||
if (ps != null)
|
||||
ps.close();
|
||||
if (conn != null)
|
||||
conn.close();
|
||||
} catch (SQLException ex) {
|
||||
plugin.getLogger().log(Level.SEVERE, Errors.sqlConnectionClose(), ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Update a player's destination teleport point
|
||||
public void setTeleportationLastOrDest(int playerID, TeleportationPoint point, String type) {
|
||||
Connection conn = null;
|
||||
PreparedStatement ps = null;
|
||||
|
||||
try {
|
||||
conn = getSQLConnection();
|
||||
|
||||
// Add the teleportation point
|
||||
addTeleportationPoint(point, conn, ps);
|
||||
|
||||
// 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.setInt(1, 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void close(PreparedStatement ps, ResultSet rs) {
|
||||
try {
|
||||
if (ps != null)
|
||||
ps.close();
|
||||
|
||||
@@ -14,6 +14,11 @@ public final class Main extends JavaPlugin {
|
||||
}
|
||||
public static Settings settings;
|
||||
|
||||
private static void setupBungeeChannels(Main plugin) {
|
||||
plugin.getServer().getMessenger().registerOutgoingPluginChannel(plugin, "BungeeCord");
|
||||
plugin.getServer().getMessenger().registerIncomingPluginChannel(plugin, "BungeeCord", new pluginMessageHandler());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
// Plugin startup logic
|
||||
@@ -27,6 +32,11 @@ public final class Main extends JavaPlugin {
|
||||
|
||||
// Set up data storage
|
||||
dataManager.setupStorage(settings.getStorageType());
|
||||
|
||||
// Set up bungee channels if bungee mode is enabled
|
||||
if (settings.doBungee()) {
|
||||
setupBungeeChannels(this);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -29,6 +29,7 @@ public class Home extends TeleportationPoint {
|
||||
this.ownerUsername = ownerUsername;
|
||||
this.ownerUUID = UUID.fromString(ownerUUID);
|
||||
this.name = name;
|
||||
this.description = description;
|
||||
this.isPublic = isPublic;
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,8 @@ package me.william278.huskhomes2.Objects;
|
||||
|
||||
import org.bukkit.Location;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public class Warp extends TeleportationPoint {
|
||||
|
||||
String name;
|
||||
@@ -13,6 +15,12 @@ public class Warp extends TeleportationPoint {
|
||||
this.description = "A publicly accessible warp";
|
||||
}
|
||||
|
||||
public Warp(TeleportationPoint teleportationPoint, String name, String description) {
|
||||
super(teleportationPoint.worldName, teleportationPoint.x, teleportationPoint.y, teleportationPoint.z, teleportationPoint.yaw, teleportationPoint.pitch, teleportationPoint.server);
|
||||
this.name = name;
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ public class configManager {
|
||||
"| HuskHomes Config |\n" +
|
||||
"| Developed by William278 |\n" +
|
||||
" ------------------------------ \n" +
|
||||
"Configuration guide: https://github.com/WiIIiam278/HuskHomesDocs/wiki/Config-File/ \n" +
|
||||
"Configuration guide: [tbd] \n" +
|
||||
"To quickly setup the plugin:\n" +
|
||||
"1. Fill in the details of your mySQL Database below\n" +
|
||||
"2. (Optional) Change the appropriate details in the configuration\n" +
|
||||
|
||||
@@ -4,28 +4,39 @@ import me.william278.huskhomes2.Data.SQLite.Database;
|
||||
import me.william278.huskhomes2.Data.SQLite.SQLite;
|
||||
import me.william278.huskhomes2.Objects.Home;
|
||||
import me.william278.huskhomes2.Objects.TeleportationPoint;
|
||||
import me.william278.huskhomes2.Objects.Warp;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
|
||||
// This class handles the saving of data; whether that be through SQLite or SQL
|
||||
public class dataManager {
|
||||
|
||||
public static String createPlayerTable = "CREATE TABLE IF NOT EXISTS " + Main.settings.getPlayerTable() + " (" +
|
||||
"`player_id` integer PRIMARY KEY," +
|
||||
"`player_id` integer NOT NULL," +
|
||||
"`uuid` text NOT NULL UNIQUE," +
|
||||
"`username` text NOT NULL," +
|
||||
"`home_count` integer NOT NULL," +
|
||||
"`home_slots` integer NOT NULL," +
|
||||
"`rtp_cooldown` integer NOT NULL," +
|
||||
"`home_count` integer 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," +
|
||||
"`last_location_id` integer," +
|
||||
|
||||
"PRIMARY KEY (`player_id`)," +
|
||||
"FOREIGN KEY (`dest_location_id`) REFERENCES " + Main.settings.getLocationsTable() + " (`location_id`) ON DELETE CASCADE ON UPDATE NO ACTION," +
|
||||
"FOREIGN KEY (`last_location_id`) REFERENCES " + Main.settings.getLocationsTable() + " (`location_id`) ON DELETE CASCADE ON UPDATE NO ACTION" +
|
||||
");";
|
||||
|
||||
public static String createLocationsTable = "CREATE TABLE IF NOT EXISTS " + Main.settings.getLocationsTable() + " (" +
|
||||
"`location_id` integer PRIMARY KEY," +
|
||||
"`world` text NOT NULL," +
|
||||
"`server` text NOT NULL," +
|
||||
"`world` text NOT NULL," +
|
||||
"`x` double NOT NULL," +
|
||||
"`y` double NOT NULL," +
|
||||
"`z` double NOT NULL," +
|
||||
@@ -42,16 +53,16 @@ public class dataManager {
|
||||
|
||||
"PRIMARY KEY (`player_id`, `location_id`)," +
|
||||
"FOREIGN KEY (`player_id`) REFERENCES " + Main.settings.getPlayerTable() + " (`player_id`) ON DELETE CASCADE ON UPDATE NO ACTION," +
|
||||
"FOREIGN KEY (`location_id`) REFERENCES " + Main.settings.getLocationsTable() + " (`location_id`) ON DELETE CASCADE ON UPDATE NO ACTION," +
|
||||
"FOREIGN KEY (`location_id`) REFERENCES " + Main.settings.getLocationsTable() + " (`location_id`) ON DELETE CASCADE ON UPDATE NO ACTION" +
|
||||
");";
|
||||
|
||||
public static String createWarpsTable = "CREATE TABLE IF NOT EXISTS " + Main.settings.getWarpsTable() + " (" +
|
||||
"`location_id` integer NOT NULL," +
|
||||
"`name` text NOT NULL," +
|
||||
"`name` text NOT NULL UNIQUE," +
|
||||
"`description` double NOT NULL," +
|
||||
|
||||
"PRIMARY KEY (`location_id`)," +
|
||||
"FOREIGN KEY (`location_id`) REFERENCES " + Main.settings.getLocationsTable() + " (`location_id`) ON DELETE CASCADE ON UPDATE NO ACTION," +
|
||||
"FOREIGN KEY (`location_id`) REFERENCES " + Main.settings.getLocationsTable() + " (`location_id`) ON DELETE CASCADE ON UPDATE NO ACTION" +
|
||||
");";
|
||||
|
||||
private static Database sqliteDatabase;
|
||||
@@ -73,12 +84,80 @@ public class dataManager {
|
||||
}
|
||||
}
|
||||
|
||||
// Return an array of a player's homes
|
||||
public ArrayList<Home> getPlayerHomes(String playerName) {
|
||||
try {
|
||||
Integer playerID = getPlayerId(playerName);
|
||||
ResultSet rs = queryDatabase("SELECT * FROM " + Main.settings.getHomesTable() + " WHERE `player_id`=" + playerID + ";");
|
||||
ArrayList<Home> publicHomes = new ArrayList<>();
|
||||
while (rs.next()) {
|
||||
int locationID = rs.getInt("location_id");
|
||||
publicHomes.add(new Home(getTeleportationPoint(locationID), playerName, getPlayerUUID(playerID), rs.getString("name"), rs.getString("description"), rs.getBoolean("public")));
|
||||
}
|
||||
return publicHomes;
|
||||
} catch (SQLException e) {
|
||||
Bukkit.getLogger().severe("An SQL exception occurred returning a home!");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// Return an array of all the public homes
|
||||
public ArrayList<Home> getPublicHomes() {
|
||||
try {
|
||||
ResultSet rs = queryDatabase("SELECT * FROM " + Main.settings.getHomesTable() + " WHERE `public`;");
|
||||
ArrayList<Home> publicHomes = new ArrayList<>();
|
||||
while (rs.next()) {
|
||||
int playerID = rs.getInt("player_id");
|
||||
int locationID = rs.getInt("location_id");
|
||||
publicHomes.add(new Home(getTeleportationPoint(locationID), getPlayerUsername(playerID), getPlayerUUID(playerID), rs.getString("name"), rs.getString("description"), true));
|
||||
}
|
||||
return publicHomes;
|
||||
} catch (SQLException e) {
|
||||
Bukkit.getLogger().severe("An SQL exception occurred returning a home!");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// Return an array of all the warps
|
||||
public ArrayList<Warp> getWarps() {
|
||||
try {
|
||||
ResultSet rs = queryDatabase("SELECT * FROM " + Main.settings.getWarpsTable() + ";");
|
||||
ArrayList<Warp> warps = new ArrayList<>();
|
||||
while (rs.next()) {
|
||||
int locationID = rs.getInt("location_id");
|
||||
warps.add(new Warp(getTeleportationPoint(locationID), rs.getString("name"), rs.getString("description")));
|
||||
}
|
||||
return warps;
|
||||
} catch (SQLException e) {
|
||||
Bukkit.getLogger().severe("An SQL exception occurred returning a list of warps!");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// Return a warp with a given name (warp names are unique)
|
||||
public Warp getWarp(String name) {
|
||||
try {
|
||||
ResultSet rs = queryDatabase("SELECT * FROM " + Main.settings.getWarpsTable() + " WHERE `name`=" + name + ";");
|
||||
if (rs.next()) {
|
||||
int locationID = rs.getInt("location_id");
|
||||
return new Warp(getTeleportationPoint(locationID), rs.getString("name"), rs.getString("description"));
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
Bukkit.getLogger().severe("An SQL exception occurred returning a list of warps!");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// Return a home with a given owner username and home name
|
||||
public Home getHome(String ownerUsername, String homeName) {
|
||||
try {
|
||||
Integer playerID = getPlayerId(ownerUsername);
|
||||
ResultSet rs = queryDatabase("SELECT * FROM " + Main.settings.getPlayerTable() + " 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()) {
|
||||
return new Home(getTransportationPoint(rs.getInt("location_id")), ownerUsername, getPlayerUUID(playerID), rs.getString("name"), rs.getString("description"), rs.getBoolean("public"));
|
||||
int locationID = rs.getInt("location_id");
|
||||
return new Home(getTeleportationPoint(locationID), ownerUsername, getPlayerUUID(playerID), rs.getString("name"), rs.getString("description"), rs.getBoolean("public"));
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
@@ -88,6 +167,17 @@ public class dataManager {
|
||||
}
|
||||
}
|
||||
|
||||
public boolean playerExists(Player p) {
|
||||
try {
|
||||
ResultSet rs = queryDatabase("SELECT * FROM " + Main.settings.getPlayerTable() + " WHERE `uuid`=" + p.getUniqueId().toString() + ";");
|
||||
return rs.next();
|
||||
} catch (SQLException e) {
|
||||
Bukkit.getLogger().severe("An SQL exception occurred in determining if a player exists in the player table.");
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private Integer getPlayerId(String playerUsername) {
|
||||
try {
|
||||
ResultSet rs = queryDatabase("SELECT * FROM " + Main.settings.getPlayerTable() + " WHERE `username`=" + (playerUsername) + ";");
|
||||
@@ -104,7 +194,7 @@ public class dataManager {
|
||||
|
||||
public String getPlayerUUID(int playerID) {
|
||||
try {
|
||||
ResultSet rs = queryDatabase("SELECT * FROM " + Main.settings.getLocationsTable() + " WHERE `player_id`=" + (playerID) + ";");
|
||||
ResultSet rs = queryDatabase("SELECT * FROM " + Main.settings.getPlayerTable() + " WHERE `player_id`=" + (playerID) + ";");
|
||||
if (rs.next()) {
|
||||
return rs.getString("uuid");
|
||||
} else {
|
||||
@@ -116,7 +206,95 @@ public class dataManager {
|
||||
}
|
||||
}
|
||||
|
||||
public TeleportationPoint getTransportationPoint(int locationID) {
|
||||
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);
|
||||
}
|
||||
|
||||
public TeleportationPoint getPlayerLastPosition(Player p) {
|
||||
int locationID = getPlayerPositionID(p, "last");
|
||||
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
|
||||
public void setPlayerLastPosition(Player p, TeleportationPoint point) {
|
||||
Integer playerID = getPlayerId(p.getName());
|
||||
if (playerID != null) {
|
||||
deletePlayerLastPosition(p);
|
||||
if (Main.settings.getStorageType().equalsIgnoreCase("mySQL")) {
|
||||
|
||||
} else {
|
||||
sqliteDatabase.setTeleportationLastOrDest(playerID, point, "last");
|
||||
}
|
||||
} else {
|
||||
Bukkit.getLogger().warning("Failed to update player last position records for " + p.getName());
|
||||
}
|
||||
}
|
||||
|
||||
// Update a player's destination location on SQL
|
||||
public void setPlayerDestination(Player p, TeleportationPoint point) {
|
||||
Integer playerID = getPlayerId(p.getName());
|
||||
if (playerID != null) {
|
||||
deletePlayerDestination(p);
|
||||
if (Main.settings.getStorageType().equalsIgnoreCase("mySQL")) {
|
||||
|
||||
} else {
|
||||
sqliteDatabase.setTeleportationLastOrDest(playerID, point, "dest");
|
||||
}
|
||||
} else {
|
||||
Bukkit.getLogger().warning("Failed to update player destination records for " + p.getName());
|
||||
}
|
||||
}
|
||||
|
||||
public String getPlayerUsername(int playerID) {
|
||||
try {
|
||||
ResultSet rs = queryDatabase("SELECT * FROM " + Main.settings.getPlayerTable() + " WHERE `player_id`=" + (playerID) + ";");
|
||||
if (rs.next()) {
|
||||
return rs.getString("username");
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
Bukkit.getLogger().severe("An SQL exception occurred returning a player username!");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public TeleportationPoint getTeleportationPoint(int locationID) {
|
||||
try {
|
||||
ResultSet rs = queryDatabase("SELECT * FROM " + Main.settings.getLocationsTable() + " WHERE `location_id`=" + (locationID) + ";");
|
||||
if (rs.next()) {
|
||||
|
||||
@@ -1,5 +1,90 @@
|
||||
package me.william278.huskhomes2;
|
||||
|
||||
public class pluginMessageHandler {
|
||||
import com.google.common.io.ByteArrayDataInput;
|
||||
import com.google.common.io.ByteArrayDataOutput;
|
||||
import com.google.common.io.ByteStreams;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.messaging.PluginMessageListener;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
public class pluginMessageHandler implements PluginMessageListener {
|
||||
|
||||
private static final Main plugin = Main.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());
|
||||
}
|
||||
|
||||
// Send a plugin message
|
||||
public static void sendPluginMessage(Player sender, String targetPlayerName, String messageType, String messageData) {
|
||||
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:" + 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) {
|
||||
Bukkit.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());
|
||||
}
|
||||
|
||||
// When a plugin message is received
|
||||
@Override
|
||||
public void onPluginMessageReceived(String channel, Player p, byte[] message) {
|
||||
// Return if the message is not a Bungee message
|
||||
if (!channel.equals("BungeeCord")) {
|
||||
return;
|
||||
}
|
||||
ByteArrayDataInput input = ByteStreams.newDataInput(message);
|
||||
String messageType = input.readUTF();
|
||||
|
||||
// Return if the message was not sent by HuskHomes
|
||||
if (!messageType.contains("HuskHomes:")) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Get the HuskHomes message type
|
||||
messageType = messageType.split(":")[1];
|
||||
|
||||
// Get the message data packets
|
||||
String messageData;
|
||||
short messageLength = input.readShort();
|
||||
byte[] messageBytes = new byte[messageLength];
|
||||
input.readFully(messageBytes);
|
||||
DataInputStream messageIn = new DataInputStream(new ByteArrayInputStream(messageBytes));
|
||||
|
||||
// Get the message data string from the packets received
|
||||
try {
|
||||
messageData = messageIn.readUTF();
|
||||
} catch (IOException e) {
|
||||
Bukkit.getLogger().warning("An error occurred trying to read a plugin message (" + e.getCause() + ")");
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
/*switch (messageType) {
|
||||
|
||||
}*/
|
||||
}
|
||||
}
|
||||
@@ -1,14 +1,20 @@
|
||||
package me.william278.huskhomes2;
|
||||
|
||||
import me.william278.huskhomes2.Objects.TeleportationPoint;
|
||||
import org.bukkit.Server;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public class teleportManager {
|
||||
|
||||
private static void teleportPlayer(Player p, TeleportationPoint teleportationPoint) {
|
||||
if (teleportationPoint.getServer().equals(Main.settings.getServerID())) {
|
||||
String server = teleportationPoint.getServer();
|
||||
if (!Main.settings.doBungee() || server.equals(Main.settings.getServerID())) {
|
||||
p.teleport(teleportationPoint.getLocation());
|
||||
messageManager.sendMessage(p, "teleport_success");
|
||||
} else if (Main.settings.doBungee()) {
|
||||
pluginMessageHandler.sendPlayer(p, server);
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user