UW-PlattevilleMC/uwp-utils/commands/SetHome.java

67 lines
2.4 KiB
Java

package me.arcodeskel.uwpmcutils.commands;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.Listener;
import java.sql.*;
public class SetHome implements Listener, CommandExecutor {
@Override
public boolean onCommand(CommandSender sender, Command cmd, String s, String[] strings) {
if (sender instanceof Player) {
Player p = (Player) sender;
Connection connection = null;
try {
connection = DatabaseConnection.getConnection();
String name = p.getName();
double x = p.getLocation().getX();
double y = p.getLocation().getY();
double z = p.getLocation().getZ();
float pitch = p.getLocation().getPitch();
float yaw = p.getLocation().getYaw();
String insertUsername = "INSERT OR IGNORE INTO homes (username) VALUES (?)";
try (PreparedStatement psInsertUsername = connection.prepareStatement(insertUsername)) {
psInsertUsername.setString(1, name);
psInsertUsername.executeUpdate();
}
String updateHomes = "UPDATE homes SET x = ?, y = ?, z = ?, pitch = ?, yaw = ? WHERE username = ?";
try (PreparedStatement psUpdateHomes = connection.prepareStatement(updateHomes)) {
psUpdateHomes.setDouble(1, x);
psUpdateHomes.setDouble(2, y);
psUpdateHomes.setDouble(3, z);
psUpdateHomes.setFloat(4, pitch);
psUpdateHomes.setFloat(5, yaw);
psUpdateHomes.setString(6, name);
psUpdateHomes.executeUpdate();
psUpdateHomes.close();
}
p.sendMessage("Home set to: " + x + ", " + y + ", " + z);
} catch (SQLException e) {
e.printStackTrace(); // Handle exception
} finally {
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace(); // Handle exception
}
}
}
}
return true;
}
}