Compare commits

...

2 Commits

Author SHA1 Message Date
Doc
434918a178 Coordinates saving validation 2024-01-16 09:42:48 +01:00
Doc
8b481340b1 Check if coords already exist if player try's to save new 2024-01-16 09:19:56 +01:00

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.*;
@@ -51,29 +54,62 @@ public class CoordSave extends JavaPlugin {
}
public void doSaveSubcommand(Player player, String coordname) throws SQLException {
Location location = player.getLocation();
String playerUUID = player.getUniqueId().toString();
// 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());
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();
// Executing the query
pstmt.executeUpdate();
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());
// Building the message component
final TextComponent conformationMessage = text()
.content("The coordinates for ")
.append(text(coordname).color(TextColor.color(BLUE)))
.append(text(" where saved!")).build();
// Executing the query
pstmt.executeUpdate();
// Sending the message component to player
player.sendMessage(conformationMessage);
String validateQuery = "SELECT * FROM coords WHERE uuid = ? and coordname = ?";
PreparedStatement validatePstmt = connection.prepareStatement(validateQuery);
checkPstmt.setString(1, playerUUID);
checkPstmt.setString(2, coordname);
ResultSet validateResult = validatePstmt.executeQuery();
if (Objects.equals(validateResult.getString("coordname"), coordname)) {
// 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);
} else {
// Building the message component
final TextComponent errorSavingMessage = text()
.content("Error saving the ").color(TextColor.color(RED))
.append(text(coordname).color(TextColor.color(RED)))
.append(text(" coordinates!")).color(TextColor.color(RED)).build();
// Sending the message component to player
player.sendMessage(errorSavingMessage);
}
}
}
public void doShowSubcommand(Player player, String subarg) throws SQLException {