diff --git a/config.yml b/config.yml old mode 100755 new mode 100644 diff --git a/plugin.yml b/plugin.yml old mode 100755 new mode 100644 index c819c8b..2024b00 --- a/plugin.yml +++ b/plugin.yml @@ -1,7 +1,13 @@ -name: GriefAlert -main: net.ardakaz.griefalert.GriefAlert -version: 0.2.1 -api-version: 1.21 -depends: [CoreProtect] -description: A simple grief alert plugin using CoreProtect API. -author: Ardakaz +name: GriefAlert +main: net.ardakaz.griefalert.GriefAlert +version: 0.3 +api-version: 1.21 +depends: [CoreProtect] +description: A simple grief alert plugin using the CoreProtect API. +authors: [Ardakaz, kleedje30] +commands: + griefalert: + description: GriefAlert staff commands + usage: /griefalert + permission: griefalert.staff + permission-message: You do not have permission to use this command. diff --git a/src/net/ardakaz/griefalert/GriefAlert.java b/src/net/ardakaz/griefalert/GriefAlert.java old mode 100755 new mode 100644 index 961c312..d9e7c13 --- a/src/net/ardakaz/griefalert/GriefAlert.java +++ b/src/net/ardakaz/griefalert/GriefAlert.java @@ -8,6 +8,8 @@ import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.Material; import org.bukkit.block.Block; +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; @@ -19,7 +21,12 @@ import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; import org.bukkit.plugin.Plugin; import org.bukkit.plugin.java.JavaPlugin; - +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Statement; import java.util.Arrays; import java.util.List; import java.util.Set; @@ -36,6 +43,9 @@ public class GriefAlert extends JavaPlugin implements Listener { private String MAP_LINK; private Boolean ALLOW_STEALING; + private Connection connection; + private final String DB_FILE = "ignored_locations.db"; + // Init GriefAlert @Override public void onEnable() { @@ -58,12 +68,95 @@ public class GriefAlert extends JavaPlugin implements Listener { MAP_LINK = getConfig().getString("map-link"); ALLOW_STEALING = getConfig().getBoolean("allow-stealing"); + setupDatabase(); + + getCommand("griefalert").setTabCompleter(this); + getLogger().info("GriefAlert has been enabled."); } @Override public void onDisable() { getLogger().info("GriefAlert has been disabled."); + if (connection != null) { + try { connection.close(); } catch (SQLException ignored) {} + } + } + + private void setupDatabase() { + try { + connection = DriverManager.getConnection("jdbc:sqlite:" + getDataFolder().getAbsolutePath() + "/" + DB_FILE); + Statement stmt = connection.createStatement(); + stmt.executeUpdate("CREATE TABLE IF NOT EXISTS ignored_locations (x INTEGER, y INTEGER, z INTEGER, world TEXT, PRIMARY KEY (x, y, z, world))"); + stmt.close(); + } catch (SQLException e) { + getLogger().severe("Could not set up SQLite database: " + e.getMessage()); + } + } + + private boolean isLocationIgnored(int x, int y, int z, String world) { + try { + PreparedStatement ps = connection.prepareStatement("SELECT 1 FROM ignored_locations WHERE x=? AND y=? AND z=? AND world=?"); + ps.setInt(1, x); + ps.setInt(2, y); + ps.setInt(3, z); + ps.setString(4, world); + ResultSet rs = ps.executeQuery(); + boolean exists = rs.next(); + rs.close(); + ps.close(); + return exists; + } catch (SQLException e) { + getLogger().warning("DB error: " + e.getMessage()); + return false; + } + } + + private boolean addIgnoredLocation(int x, int y, int z, String world) { + try { + PreparedStatement ps = connection.prepareStatement("INSERT OR IGNORE INTO ignored_locations (x, y, z, world) VALUES (?, ?, ?, ?)"); + ps.setInt(1, x); + ps.setInt(2, y); + ps.setInt(3, z); + ps.setString(4, world); + int updated = ps.executeUpdate(); + ps.close(); + return updated > 0; + } catch (SQLException e) { + getLogger().warning("DB error: " + e.getMessage()); + return false; + } + } + + private boolean removeIgnoredLocation(int x, int y, int z, String world) { + try { + PreparedStatement ps = connection.prepareStatement("DELETE FROM ignored_locations WHERE x=? AND y=? AND z=? AND world=?"); + ps.setInt(1, x); + ps.setInt(2, y); + ps.setInt(3, z); + ps.setString(4, world); + int updated = ps.executeUpdate(); + ps.close(); + return updated > 0; + } catch (SQLException e) { + getLogger().warning("DB error: " + e.getMessage()); + return false; + } + } + + private void listIgnoredLocations(CommandSender sender) { + try { + Statement stmt = connection.createStatement(); + ResultSet rs = stmt.executeQuery("SELECT x, y, z, world FROM ignored_locations"); + sender.sendMessage(ChatColor.YELLOW + "Ignored Locations:"); + while (rs.next()) { + sender.sendMessage(ChatColor.GRAY + "- " + rs.getInt("x") + ", " + rs.getInt("y") + ", " + rs.getInt("z") + " in " + rs.getString("world")); + } + rs.close(); + stmt.close(); + } catch (SQLException e) { + sender.sendMessage(ChatColor.RED + "DB error: " + e.getMessage()); + } } @EventHandler (ignoreCancelled = true) @@ -85,9 +178,8 @@ public class GriefAlert extends JavaPlugin implements Listener { // Check if grief String target = inspectBlock(event.getBlock(), event.getPlayer()); if (target != null) { - // Alert String message = ChatColor.GRAY + playerName + " broke " + blockType + " placed by " + target + " at " + x + " " + y + " " + z + getHumanWorldName(worldName); - alert(message, playerName, "[Map Link](" + MAP_LINK + "/?worldname=" + worldName + "&zoom=7&x=" + x + "&y=" + y + "&z=" + z + ")", target); + alert(message, playerName, "[Map Link](" + MAP_LINK + "/?worldname=" + worldName + "&zoom=7&x=" + x + "&y=" + y + "&z=" + z + ")", target, x, y, z, worldName); } } @@ -138,97 +230,181 @@ public class GriefAlert extends JavaPlugin implements Listener { int y = inventory.getLocation().getBlockY(); int z = inventory.getLocation().getBlockZ(); String worldName = inventory.getLocation().getWorld().getName(); - if (stealing) { - // Stealing - String message = ChatColor.GRAY + playerName + " took " + amount + " " + itemName + " from " + target + "'s container at " + x + " " + y + " " + z + getHumanWorldName(worldName); - alert(message, playerName, "[Map Link](" + MAP_LINK + "/?worldname=" + worldName + "&zoom=7&x=" + x + "&y=" + y + "&z=" + z + ")", target); + String message = ChatColor.GRAY + playerName + " took " + amount + " " + itemName + " from " + target + "'s container at " + x + " " + y + " " + z + getHumanWorldName(worldName); + alert(message, playerName, "[Map Link](" + MAP_LINK + "/?worldname=" + worldName + "&zoom=7&x=" + x + "&y=" + y + "&z=" + z + ")", target, x, y, z, worldName); } else { - // Putting back - String message = ChatColor.GRAY + playerName + " put " + amount + " " + itemName + " into " + target + "'s container at " + x + " " + y + " " + z + getHumanWorldName(worldName); - alert(message, playerName, "[Map Link](" + MAP_LINK + "/?worldname=" + worldName + "&zoom=7&x=" + x + "&y=" + y + "&z=" + z + ")", target); + String message = ChatColor.GRAY + playerName + " put " + amount + " " + itemName + " into " + target + "'s container at " + x + " " + y + " " + z + getHumanWorldName(worldName); + alert(message, playerName, "[Map Link](" + MAP_LINK + "/?worldname=" + worldName + "&zoom=7&x=" + x + "&y=" + y + "&z=" + z + ")", target, x, y, z, worldName); } } } + @Override + public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { + if (!command.getName().equalsIgnoreCase("griefalert")) return false; + if (args.length == 0) return false; + String sub = args[0].toLowerCase(); + if (sub.equals("ignore")) { + if (!sender.hasPermission("griefalert.staff.ignore")) { + sender.sendMessage(ChatColor.RED + "You do not have permission."); + return true; + } + if (args.length != 4) { + sender.sendMessage(ChatColor.RED + "Usage: /griefalert ignore "); + return true; + } + if (!(sender instanceof Player)) { + sender.sendMessage(ChatColor.RED + "Only players can use this command without specifying a world."); + return true; + } + try { + int x = Integer.parseInt(args[1]); + int y = Integer.parseInt(args[2]); + int z = Integer.parseInt(args[3]); + String world = ((Player)sender).getWorld().getName(); + boolean result = addIgnoredLocation(x, y, z, world); + if (result) { + sender.sendMessage(ChatColor.GREEN + "Location ignored."); + } else { + sender.sendMessage(ChatColor.YELLOW + "Location was already ignored."); + } + } catch (NumberFormatException e) { + sender.sendMessage(ChatColor.RED + "Coordinates must be numbers."); + } + return true; + } else if (sub.equals("unignore")) { + if (!sender.hasPermission("griefalert.staff.UnIgnore")) { + sender.sendMessage(ChatColor.RED + "You do not have permission."); + return true; + } + if (args.length != 4) { + sender.sendMessage(ChatColor.RED + "Usage: /griefalert unignore "); + return true; + } + if (!(sender instanceof Player)) { + sender.sendMessage(ChatColor.RED + "Only players can use this command without specifying a world."); + return true; + } + try { + int x = Integer.parseInt(args[1]); + int y = Integer.parseInt(args[2]); + int z = Integer.parseInt(args[3]); + String world = ((Player)sender).getWorld().getName(); + boolean result = removeIgnoredLocation(x, y, z, world); + if (result) { + sender.sendMessage(ChatColor.GREEN + "Location unignored."); + } else { + sender.sendMessage(ChatColor.YELLOW + "Location was not ignored."); + } + } catch (NumberFormatException e) { + sender.sendMessage(ChatColor.RED + "Coordinates must be numbers."); + } + return true; + } else if (sub.equals("list")) { + if (!sender.hasPermission("griefalert.staff.list")) { + sender.sendMessage(ChatColor.RED + "You do not have permission."); + return true; + } + listIgnoredLocations(sender); + return true; + } + sender.sendMessage(ChatColor.RED + "Unknown subcommand. Use: ignore, unignore, list"); + return false; + } + + @Override + public java.util.List onTabComplete(CommandSender sender, Command command, String alias, String[] args) { + if (!command.getName().equalsIgnoreCase("griefalert")) return null; + java.util.List completions = new java.util.ArrayList<>(); + if (args.length == 1) { + java.util.List subs = java.util.Arrays.asList("ignore", "unignore", "list"); + for (String s : subs) { + if (s.startsWith(args[0].toLowerCase())) completions.add(s); + } + return completions; + } + // No world argument needed anymore + return null; + } + // Sends the alert (or cancels it) - private void alert(String message, String playerName, String mapLink, String target) { - // Exclude trusted people - Player griefer = Bukkit.getPlayer(playerName); - if (griefer.hasPermission("griefalert.exclude") || griefer.hasPermission("griefalert.exclude." + target)) { - return; - } - - // Spam limiter - String realAlertMessage = message; - String[] alert1 = null; - if (lastAlert != null) { - alert1 = lastAlert.split(" "); - } - String[] alert2 = message.split(" "); - - if (alert1 != null) { - if (alert1[2].equals(alert2[2]) && alert1[5].equals(alert2[5]) && alert1[1].equals("broke") && alert2[1].equals("broke")) { - identicalAlerts += 1; - } - else if (Arrays.equals(alert1, alert2)) { - identicalAlerts += 1; - } - else { - identicalAlerts = 1; - } - } - - if (identicalAlerts == 4) { - message = ChatColor.GRAY + "Same behavior continues."; - mapLink = null; - } - - if (identicalAlerts > 4) { - return; - } - - // Send an event for external hooks - GriefAlertEvent griefalert_event; - if (mapLink != null && !mapLink.isEmpty()) { - griefalert_event = new GriefAlertEvent(message + " (" + mapLink + ")"); - } - else { - griefalert_event = new GriefAlertEvent(message); - } - getServer().getPluginManager().callEvent(griefalert_event); - - // Notify staff ingame - for (Player player : Bukkit.getOnlinePlayers()) { - if (player.hasPermission("griefalert.notify")) { - player.sendMessage(message); + private void alert(String message, String playerName, String mapLink, String target, int x, int y, int z, String world) { + // Exclude trusted people + Player griefer = Bukkit.getPlayer(playerName); + if (griefer.hasPermission("griefalert.exclude") || griefer.hasPermission("griefalert.exclude." + target)) { + return; + } + // Spam limiter + String realAlertMessage = message; + String[] alert1 = null; + if (lastAlert != null) { + alert1 = lastAlert.split(" "); + } + String[] alert2 = message.split(" "); + if (alert1 != null) { + if (alert1[2].equals(alert2[2]) && alert1[5].equals(alert2[5]) && alert1[1].equals("broke") && alert2[1].equals("broke")) { + identicalAlerts += 1; + } else if (Arrays.equals(alert1, alert2)) { + identicalAlerts += 1; + } else { + identicalAlerts = 1; + } + } + if (identicalAlerts == 4) { + message = ChatColor.GRAY + "Same behavior continues."; + mapLink = null; + } + if (identicalAlerts > 4) { + return; + } + // Use direct location check + if (world != null && isLocationIgnored(x, y, z, world)) { + return; // Do not alert if location is ignored + } + // Send an event for external hooks + GriefAlertEvent griefalert_event; + if (mapLink != null && !mapLink.isEmpty()) { + griefalert_event = new GriefAlertEvent(message + " (" + mapLink + ")"); + } else { + griefalert_event = new GriefAlertEvent(message); + } + getServer().getPluginManager().callEvent(griefalert_event); + // Notify staff ingame + for (Player player : Bukkit.getOnlinePlayers()) { + if (player.hasPermission("griefalert.notify")) { + player.sendMessage(message); } } - lastAlert = realAlertMessage; } - // Block inspector: if the block was placed by another player, returns their name. + // Block inspector: only the most recent placement counts for ownership. private static String inspectBlock(Block block, Player player) { List lookup = coreProtectAPI.blockLookup(block, 50000000); - if (lookup == null || lookup.size() <= 0) { - // Natural block - return null; - } - - String[] result = lookup.get(0); - ParseResult parseResult = coreProtectAPI.parseResult(result); - if (parseResult.isRolledBack() && lookup.size() != 1) { - result = lookup.get(1); - parseResult = coreProtectAPI.parseResult(result); - } - - if (result == null || parseResult == null || parseResult.getPlayer().startsWith("#") || parseResult.getPlayer().equals(player.getName())) { - // Placed by breaker or natural event + if (lookup == null || lookup.size() == 0) { + // Natural block return null; } - return parseResult.getPlayer(); - + // Find the most recent placement event only + for (String[] result : lookup) { + ParseResult parseResult = coreProtectAPI.parseResult(result); + if (parseResult == null) continue; + if (parseResult.getActionId() == 1 && !parseResult.isRolledBack() && !parseResult.getPlayer().startsWith("#")) { + // If the current player placed it, it's theirs (no alert) + if (parseResult.getPlayer().equals(player.getName())) { + return null; + } else { + return parseResult.getPlayer(); + } + } + // If we see a break before a placement, stop (block is gone) + if (parseResult.getActionId() == 0) { + break; + } + } + // No valid placement found + return null; } private static String getHumanWorldName(String worldName) {