Check if coords already exist if player try's to save new

This commit is contained in:
Doc
2024-01-16 09:19:56 +01:00
parent 8427e66ddd
commit 8b481340b1

View File

@@ -10,10 +10,13 @@ import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.util.StringUtil;
import org.jetbrains.annotations.NotNull;
import java.io.File;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import static net.kyori.adventure.text.Component.text;
import static net.kyori.adventure.text.format.NamedTextColor.*;
@@ -53,27 +56,40 @@ public class CoordSave extends JavaPlugin {
public void doSaveSubcommand(Player player, String coordname) throws SQLException {
Location location = player.getLocation();
String playerUUID = player.getUniqueId().toString();
String checkQuery = "SELECT * FROM coords WHERE uuid = ? and coordname = ?";
PreparedStatement checkPstmt = connection.prepareStatement(checkQuery);
checkPstmt.setString(1, playerUUID);
checkPstmt.setString(2, coordname);
ResultSet checkResult = checkPstmt.executeQuery();
// Preparing the query string
String query = "INSERT INTO coords (uuid, coordname, X, Y, Z) VALUES(?, ?, ?, ?, ?)";
PreparedStatement pstmt = connection.prepareStatement(query);
pstmt.setString(1, playerUUID);
pstmt.setString(2, coordname);
pstmt.setFloat(3, location.getBlockX());
pstmt.setFloat(4, location.getBlockY());
pstmt.setFloat(5, location.getBlockZ());
if (Objects.equals(checkResult.getString("coordname"), coordname)) {
TextComponent alreadyExistMessage = text()
.content("A entry with the name ")
.append(text(coordname)).color(TextColor.color(RED))
.append(text("already exists.")).build();
player.sendMessage(alreadyExistMessage);
} else {
// Preparing the query string
String query = "INSERT INTO coords (uuid, coordname, X, Y, Z) VALUES(?, ?, ?, ?, ?)";
PreparedStatement pstmt = connection.prepareStatement(query);
pstmt.setString(1, playerUUID);
pstmt.setString(2, coordname);
pstmt.setFloat(3, location.getBlockX());
pstmt.setFloat(4, location.getBlockY());
pstmt.setFloat(5, location.getBlockZ());
// Executing the query
pstmt.executeUpdate();
// Executing the query
pstmt.executeUpdate();
// Building the message component
final TextComponent conformationMessage = text()
.content("The coordinates for ")
.append(text(coordname).color(TextColor.color(BLUE)))
.append(text(" where saved!")).build();
// Building the message component
final TextComponent conformationMessage = text()
.content("The coordinates for ")
.append(text(coordname).color(TextColor.color(BLUE)))
.append(text(" where saved!")).build();
// Sending the message component to player
player.sendMessage(conformationMessage);
// Sending the message component to player
player.sendMessage(conformationMessage);
}
}
public void doShowSubcommand(Player player, String subarg) throws SQLException {