fixed /back problem

This commit is contained in:
Louis Vallat 2020-04-06 22:26:46 +02:00
parent 3bceda991a
commit 762e8befbe

View File

@ -86,15 +86,16 @@ public class BackHome extends JavaPlugin implements Listener, TabCompleter {
@EventHandler
public void onPlayerDeath(PlayerDeathEvent event) {
Player player = event.getEntity().getPlayer();
try {
removeBack(player);
addBack(player);
} catch (SQLException e) {
assert player != null;
player.sendMessage(ChatColor.RED + "Your back hasn't been set due to an error, " +
"but your death coordinates were X:" + player.getLocation().getX()
+ " Y:" + player.getLocation().getY() + " Z:" + player.getLocation().getZ());
getLogger().warning(e.getMessage());
if (player != null) {
try {
removeBack(player);
addBack(player, player.getLocation());
} catch (SQLException e) {
player.sendMessage(ChatColor.RED + "Your back hasn't been set due to an error, " +
"but your death coordinates were X:" + player.getLocation().getX()
+ " Y:" + player.getLocation().getY() + " Z:" + player.getLocation().getZ());
getLogger().warning(e.getMessage());
}
}
}
@ -124,9 +125,7 @@ public class BackHome extends JavaPlugin implements Listener, TabCompleter {
if (setBackCommand(sender, cmd)) return true;
if (setHomeCommand(sender, cmd, args)) return true;
return delHomeCommand(sender, cmd, args);
} else {
sender.sendMessage(ChatColor.RED + "This command can't be issued by a non-player.");
}
} else sender.sendMessage(ChatColor.RED + "This command can't be issued by a non-player.");
return false;
}
@ -175,9 +174,17 @@ public class BackHome extends JavaPlugin implements Listener, TabCompleter {
try (PreparedStatement recherche = this.connexion.prepareStatement(
"SELECT x, y, z, yaw, pitch, world FROM " + BACK_TABLE + " WHERE uuid = ?;")) {
recherche.setString(1, ((Player) sender).getUniqueId().toString());
Location beforeTeleportLocation = new Location(
((Player) sender).getLocation().getWorld(),
((Player) sender).getLocation().getX(),
((Player) sender).getLocation().getY(),
((Player) sender).getLocation().getZ(),
((Player) sender).getLocation().getYaw(),
((Player) sender).getLocation().getPitch()
);
teleportPlayerFromPreparedStatement(sender, recherche);
removeBack(((Player) sender));
addBack(((Player) sender));
addBack(((Player) sender), beforeTeleportLocation);
}
this.connexion.close();
} catch (SQLException e) {
@ -209,7 +216,7 @@ public class BackHome extends JavaPlugin implements Listener, TabCompleter {
recherche.setString(1, ((Player) sender).getUniqueId().toString());
recherche.setString(2, name);
removeBack(((Player) sender));
addBack(((Player) sender));
addBack(((Player) sender), ((Player) sender).getLocation());
teleportPlayerFromPreparedStatement(sender, recherche);
}
this.connexion.close();
@ -304,10 +311,10 @@ public class BackHome extends JavaPlugin implements Listener, TabCompleter {
if (cmd.getName().equalsIgnoreCase("setback")) {
try {
removeBack(((Player) sender));
addBack(((Player) sender));
addBack(((Player) sender), ((Player) sender).getLocation());
sender.sendMessage(ChatColor.GREEN + "Your back has been set successfully.");
} catch (SQLException e) {
sender.sendMessage(ChatColor.RED + "An error occured while adding your home. Try again later.");
sender.sendMessage(ChatColor.RED + "An error occured while setting your back location. Try again later.");
getLogger().warning(e.getMessage());
}
return true;
@ -373,11 +380,12 @@ public class BackHome extends JavaPlugin implements Listener, TabCompleter {
/**
* Add a back for a player.
*
* @param player the player that needs a back.
* @param player the player that needs a back.
* @param location the location to add the back.
* @throws SQLException if an SQL error occured.
*/
private void addBack(Player player) throws SQLException {
if (player != null && player.getLocation().getWorld() != null) {
private void addBack(Player player, Location location) throws SQLException {
if (player != null && location.getWorld() != null) {
this.connexion = getConnection();
List<PreparedStatement> stmts = new ArrayList<>();
stmts.add(this.connexion.prepareStatement(
@ -389,12 +397,12 @@ public class BackHome extends JavaPlugin implements Listener, TabCompleter {
for (PreparedStatement stmt : stmts) {
stmt.setString(1, player.getUniqueId().toString());
stmt.setString(2, new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
stmt.setDouble(3, player.getLocation().getX());
stmt.setDouble(4, player.getLocation().getY());
stmt.setDouble(5, player.getLocation().getZ());
stmt.setFloat(6, player.getLocation().getYaw());
stmt.setFloat(7, player.getLocation().getPitch());
stmt.setString(8, player.getLocation().getWorld().getName());
stmt.setDouble(3, location.getX());
stmt.setDouble(4, location.getY());
stmt.setDouble(5, location.getZ());
stmt.setFloat(6, location.getYaw());
stmt.setFloat(7, location.getPitch());
stmt.setString(8, location.getWorld().getName());
stmt.execute();
}
connexion.close();