diff --git a/src/net/ardakaz/griefalert/AlertsLogic.java b/src/net/ardakaz/griefalert/AlertsLogic.java index e69de29..c0aea39 100644 --- a/src/net/ardakaz/griefalert/AlertsLogic.java +++ b/src/net/ardakaz/griefalert/AlertsLogic.java @@ -0,0 +1,211 @@ +/*this SHOULD handle checking the alert for example: +if a block is broken at world 10 10 10 and the location has been disabled +then it should return null and cancle actualy giving an alert +(if location is enabled the alert will just alert) +and it SHOULD handle the logic and processing of /disable or /enable +and the SQL database logic +*/ +package net.ardakaz.griefalert; + +import java.sql.*; +import org.bukkit.plugin.java.JavaPlugin; +import org.bukkit.command.CommandSender; +import org.bukkit.ChatColor; + +public class AlertsLogic { + private final String DB_URL = "jdbc:sqlite:plugins/GriefAlert/locations.db"; + private Connection connection; + private final JavaPlugin plugin; + + public AlertsLogic(JavaPlugin plugin) { + this.plugin = plugin; + try { + connectDatabase(); + createLocationsTable(); + } catch (SQLException e) { + plugin.getLogger().severe("Could not initialize SQLite database: " + e.getMessage()); + } + } + + private void connectDatabase() throws SQLException { + connection = DriverManager.getConnection(DB_URL); + } + + private void createLocationsTable() throws SQLException { + String sql = "CREATE TABLE IF NOT EXISTS locations (id INTEGER PRIMARY KEY AUTOINCREMENT, a INTEGER, b INTEGER, c INTEGER, world TEXT)"; + try (Statement stmt = connection.createStatement()) { + stmt.execute(sql); + } + } + + public void saveLocation(int a, int b, int c, String world) { + String sql = "INSERT INTO locations (a, b, c, world) VALUES (?, ?, ?, ?)"; + try (PreparedStatement pstmt = connection.prepareStatement(sql)) { + pstmt.setInt(1, a); + pstmt.setInt(2, b); + pstmt.setInt(3, c); + pstmt.setString(4, world); + pstmt.executeUpdate(); + } catch (SQLException e) { + plugin.getLogger().severe("Error saving location: " + e.getMessage()); + } + } + + public boolean isLocationSaved(int a, int b, int c, String world) { + String sql = "SELECT id FROM locations WHERE a = ? AND b = ? AND c = ? AND world = ?"; + try (PreparedStatement pstmt = connection.prepareStatement(sql)) { + pstmt.setInt(1, a); + pstmt.setInt(2, b); + pstmt.setInt(3, c); + pstmt.setString(4, world); + ResultSet rs = pstmt.executeQuery(); + return rs.next(); + } catch (SQLException e) { + plugin.getLogger().severe("Error querying location: " + e.getMessage()); + return false; + } + } + + public boolean removeLocation(int a, int b, int c, String world) { + String sql = "DELETE FROM locations WHERE a = ? AND b = ? AND c = ? AND world = ?"; + try (PreparedStatement pstmt = connection.prepareStatement(sql)) { + pstmt.setInt(1, a); + pstmt.setInt(2, b); + pstmt.setInt(3, c); + pstmt.setString(4, world); + int affected = pstmt.executeUpdate(); + return affected > 0; + } catch (SQLException e) { + plugin.getLogger().severe("Error removing location: " + e.getMessage()); + return false; + } + } + + public boolean clearAllLocations() { + String sql = "DELETE FROM locations"; + try (Statement stmt = connection.createStatement()) { + stmt.executeUpdate(sql); + return true; + } catch (SQLException e) { + plugin.getLogger().severe("Error clearing locations: " + e.getMessage()); + return false; + } + } + + public void close() { + if (connection != null) { + try { + connection.close(); + } catch (SQLException e) { + plugin.getLogger().severe("Error closing SQLite connection: " + e.getMessage()); + } + } + } + + public boolean handleDisableLocation(CommandSender sender, String[] args) { + if (!sender.hasPermission("griefalert.staff.disable")) { + sender.sendMessage(ChatColor.RED + "You do not have permission to use this command."); + return true; + } + if (args.length != 4) { + sender.sendMessage(ChatColor.RED + "Usage: /disablelocation "); + return true; + } + String world = args[0]; + if (!world.equals("world_world") && !world.equals("world_nether") && !world.equals("world_end")) { + sender.sendMessage(ChatColor.RED + "Invalid world. Use one of: world_world, world_nether, world_end"); + return true; + } + int a, b, c; + try { + a = Integer.parseInt(args[1]); + b = Integer.parseInt(args[2]); + c = Integer.parseInt(args[3]); + } catch (NumberFormatException e) { + sender.sendMessage(ChatColor.RED + "Coordinates must be whole numbers."); + return true; + } + saveLocation(a, b, c, world); + sender.sendMessage(ChatColor.GREEN + "Location disabled for alerts at " + world + " X: " + a + ", Y: " + b + ", Z: " + c + "."); + return true; + } + + public boolean handleEnableLocation(CommandSender sender, String[] args) { + if (!sender.hasPermission("griefalert.staff.enable")) { + sender.sendMessage(ChatColor.RED + "You do not have permission to use this command."); + return true; + } + if (args.length != 4) { + sender.sendMessage(ChatColor.RED + "Usage: /enablelocation "); + return true; + } + String world = args[0]; + if (!world.equals("world_world") && !world.equals("world_nether") && !world.equals("world_end")) { + sender.sendMessage(ChatColor.RED + "Invalid world. Use one of: world_world, world_nether, world_end"); + return true; + } + int a, b, c; + try { + a = Integer.parseInt(args[1]); + b = Integer.parseInt(args[2]); + c = Integer.parseInt(args[3]); + } catch (NumberFormatException e) { + sender.sendMessage(ChatColor.RED + "Coordinates must be whole numbers."); + return true; + } + if (removeLocation(a, b, c, world)) { + sender.sendMessage(ChatColor.GREEN + "Location enabled for alerts at " + world + " X: " + a + ", Y: " + b + ", Z: " + c + "."); + } else { + sender.sendMessage(ChatColor.RED + "Location was not disabled or could not be found."); + } + return true; + } + + public boolean handleClearLocations(CommandSender sender, String[] args) { + if (!sender.hasPermission("griefalert.staff.clear")) { + sender.sendMessage(ChatColor.RED + "You do not have permission to use this command."); + return true; + } + if (args.length != 0) { + sender.sendMessage(ChatColor.RED + "Usage: /clearlocations"); + return true; + } + if (clearAllLocations()) { + sender.sendMessage(ChatColor.GREEN + "All disabled alert locations (a, b, c) have been cleared."); + } else { + sender.sendMessage(ChatColor.RED + "Failed to clear locations. Check console for errors."); + } + return true; + } + + public boolean handleCheckLocation(CommandSender sender, String[] args) { + if (!sender.hasPermission("griefalert.staff.check")) { + sender.sendMessage(ChatColor.RED + "You do not have permission to use this command."); + return true; + } + if (args.length != 4) { + sender.sendMessage(ChatColor.RED + "Usage: /checklocation "); + return true; + } + String world = args[0]; + if (!world.equals("world_world") && !world.equals("world_nether") && !world.equals("world_end")) { + sender.sendMessage(ChatColor.RED + "Invalid world. Use one of: world_world, world_nether, world_end"); + return true; + } + int a, b, c; + try { + a = Integer.parseInt(args[1]); + b = Integer.parseInt(args[2]); + c = Integer.parseInt(args[3]); + } catch (NumberFormatException e) { + sender.sendMessage(ChatColor.RED + "Coordinates must be whole numbers."); + return true; + } + if (isLocationSaved(a, b, c, world)) { + sender.sendMessage(ChatColor.YELLOW + "This block (X: " + a + ", Y: " + b + ", Z: " + c + ") has been disabled for alerts."); + } else { + sender.sendMessage(ChatColor.GREEN + "This block (X: " + a + ", Y: " + b + ", Z: " + c + ") is still enabled for alerts."); + } + return true; + } +}