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