Merge pull request 'Moved command methods into own class' (#1) from detached into More-validation

Reviewed-on: #1
This commit is contained in:
Doc
2024-03-01 12:59:34 +01:00
4 changed files with 174 additions and 154 deletions

View File

@@ -1,5 +1,6 @@
plugins {
id("java")
id("io.papermc.paperweight.userdev") version "1.5.10" // Check for new versions at https://plugins.gradle.org/plugin/io.papermc.paperweight.userdev
}
group = "dev.protron"
@@ -14,6 +15,7 @@ dependencies {
testImplementation(platform("org.junit:junit-bom:5.9.1"))
testImplementation("org.junit.jupiter:junit-jupiter")
compileOnly("io.papermc.paper:paper-api:1.20.1-R0.1-SNAPSHOT")
paperweight.paperDevBundle("1.20.4-R0.1-SNAPSHOT")
implementation("org.xerial:sqlite-jdbc:3.42.0.0")
}

View File

@@ -0,0 +1,166 @@
package dev.protron.coordsave;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.format.TextColor;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Objects;
import static net.kyori.adventure.text.Component.text;
import static net.kyori.adventure.text.format.NamedTextColor.*;
import static net.kyori.adventure.text.format.TextColor.color;
public class Commands {
public static void doSaveSubcommand(Player player, String coordname, Connection connection) 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();
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();
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 static void doShowSubcommand(Player player, String subarg, Connection connection) throws SQLException {
// Prepare SQL query
String query = "SELECT X, Y, Z FROM coords WHERE coordname = ? AND uuid = ?";
PreparedStatement pstmt = connection.prepareStatement(query);
pstmt.setString(1, subarg);
pstmt.setString(2, player.getUniqueId().toString());
// Execute SQL query
ResultSet result = pstmt.executeQuery();
// Check if query returned something
if (result.getString("X") == null){
// Building error message
final TextComponent errorMessage = text()
.content("No entry with that name").color(color(RED))
.build();
//Sending error message
player.sendMessage(errorMessage);
}else {
// Building return message
final TextComponent returnMessage = text()
.content("The coordinates for ")
.append(text(subarg))
.append(text(" are "))
.append(text(" X: " + result.getString("X")).color(color(LIGHT_PURPLE)))
.append(text(" Y: " + result.getString("Y")).color(color(LIGHT_PURPLE)))
.append(text(" Z: " + result.getString("Z")).color(color(LIGHT_PURPLE)))
.build();
//sending return message to player
player.sendMessage(returnMessage);
}
}
public static void doListSubcommand(Player player, Connection connection) throws SQLException {
String query = "SELECT * FROM coords WHERE uuid = ?";
PreparedStatement pstmt = connection.prepareStatement(query);
pstmt.setString(1, player.getUniqueId().toString());
ResultSet results= pstmt.executeQuery();
if (results.getString("coordname") == null){
final TextComponent errorMessage = text()
.content("No entry's").color(color(RED))
.build();
player.sendMessage(errorMessage);
}else {
while (results.next()){
// Building return message
final TextComponent returnMessage = text()
.content(results.getString("coordname")).color(color(BLUE))
.build();
//sending return message to player
player.sendMessage(returnMessage);
}
}
}
static void doRemoveSubcommand(Player player, String arg, Connection connection) throws SQLException {
String query = "SELECT X FROM coords WHERE uuid = ? AND coordname = ?";
PreparedStatement pstmt = connection.prepareStatement(query);
pstmt.setString(1, player.getUniqueId().toString());
pstmt.setString(2, arg);
ResultSet result = pstmt.executeQuery();
if (result.getString("X") == null){
//Building error message
final TextComponent errorMessage = text()
.content("No entry with the name ").color(color(RED))
.append(text(arg).color(color(RED)))
.append(text(" exists").color(color(RED)))
.build();
//Sending the text component
player.sendMessage(errorMessage);
}else {
String deleteQuery = "DELETE FROM coords WHERE uuid = ? AND coordname = ?";
PreparedStatement pstmtEX = connection.prepareStatement(deleteQuery);
pstmtEX.setString(1, player.getUniqueId().toString());
pstmtEX.setString(2, arg);
pstmtEX.executeUpdate();
final TextComponent returnMessage = text()
.content("The entry ")
.append(text(arg).color(color(BLUE)))
.append(text(" was deleted!"))
.build();
//Sending return message
player.sendMessage(returnMessage);
}
}
}

View File

@@ -1,8 +1,6 @@
package dev.protron.coordsave;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.format.TextColor;
import org.bukkit.Location;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
@@ -15,7 +13,6 @@ 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;
@@ -53,153 +50,7 @@ public class CoordSave extends JavaPlugin {
getLogger().info("CoordSave has been enabled");
}
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();
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();
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 {
// Prepare SQL query
String query = "SELECT X, Y, Z FROM coords WHERE coordname = ? AND uuid = ?";
PreparedStatement pstmt = connection.prepareStatement(query);
pstmt.setString(1, subarg);
pstmt.setString(2, player.getUniqueId().toString());
// Execute SQL query
ResultSet result = pstmt.executeQuery();
// Check if query returned something
if (result.getString("X") == null){
// Building error message
final TextComponent errorMessage = text()
.content("No entry with that name").color(color(RED))
.build();
//Sending error message
player.sendMessage(errorMessage);
}else {
// Building return message
final TextComponent returnMessage = text()
.content("The coordinates for ")
.append(text(subarg))
.append(text(" are "))
.append(text(" X: " + result.getString("X")).color(color(LIGHT_PURPLE)))
.append(text(" Y: " + result.getString("Y")).color(color(LIGHT_PURPLE)))
.append(text(" Z: " + result.getString("Z")).color(color(LIGHT_PURPLE)))
.build();
//sending return message to player
player.sendMessage(returnMessage);
}
}
public void doListSubcommand(Player player) throws SQLException {
String query = "SELECT * FROM coords WHERE uuid = ?";
PreparedStatement pstmt = connection.prepareStatement(query);
pstmt.setString(1, player.getUniqueId().toString());
ResultSet results= pstmt.executeQuery();
if (results.getString("coordname") == null){
final TextComponent errorMessage = text()
.content("No entry's").color(color(RED))
.build();
player.sendMessage(errorMessage);
}else {
while (results.next()){
// Building return message
final TextComponent returnMessage = text()
.content(results.getString("coordname")).color(color(BLUE))
.build();
//sending return message to player
player.sendMessage(returnMessage);
}
}
}
private void doRemoveSubcommand(Player player, String arg) throws SQLException {
String query = "SELECT X FROM coords WHERE uuid = ? AND coordname = ?";
PreparedStatement pstmt = connection.prepareStatement(query);
pstmt.setString(1, player.getUniqueId().toString());
pstmt.setString(2, arg);
ResultSet result = pstmt.executeQuery();
if (result.getString("X") == null){
//Building error message
final TextComponent errorMessage = text()
.content("No entry with the name ").color(color(RED))
.append(text(arg).color(color(RED)))
.append(text(" exists").color(color(RED)))
.build();
//Sending the text component
player.sendMessage(errorMessage);
}else {
String deleteQuery = "DELETE FROM coords WHERE uuid = ? AND coordname = ?";
PreparedStatement pstmtEX = connection.prepareStatement(deleteQuery);
pstmtEX.setString(1, player.getUniqueId().toString());
pstmtEX.setString(2, arg);
pstmtEX.executeUpdate();
final TextComponent returnMessage = text()
.content("The entry ")
.append(text(arg).color(color(BLUE)))
.append(text(" was deleted!"))
.build();
//Sending return message
player.sendMessage(returnMessage);
}
}
@Override
public boolean onCommand(@NotNull CommandSender sender, Command cmd, @NotNull String label, String[] args){
@@ -217,7 +68,7 @@ public class CoordSave extends JavaPlugin {
yield false;
}
try {
doSaveSubcommand(player, args[1]);
Commands.doSaveSubcommand(player, args[1], connection);
} catch (SQLException e) {
getLogger().warning(e.getMessage());
}
@@ -233,7 +84,7 @@ public class CoordSave extends JavaPlugin {
yield false;
}
try {
doShowSubcommand(player, args[1]);
Commands.doShowSubcommand(player, args[1], connection);
} catch (SQLException e) {
getLogger().warning(e.getMessage());
}
@@ -249,7 +100,7 @@ public class CoordSave extends JavaPlugin {
yield false;
}
try {
doListSubcommand(player);
Commands.doListSubcommand(player, connection);
} catch (SQLException e) {
getLogger().warning(e.getMessage());
}
@@ -265,7 +116,7 @@ public class CoordSave extends JavaPlugin {
yield false;
}
try {
doRemoveSubcommand(player, args[1]);
Commands.doRemoveSubcommand(player, args[1], connection);
} catch (SQLException e) {
getLogger().warning(e.getMessage());
}
@@ -291,6 +142,7 @@ public class CoordSave extends JavaPlugin {
return false;
}
sender.sendMessage("Not a Player");
return false;
}

View File

@@ -1,5 +1,5 @@
name: CoordSave
version: 0.0.4
version: 0.0.5-dev
main: dev.protron.coordsave.CoordSave
description: A plugin to simply save your Coordinates
author: Doc