Compare commits

...

14 Commits
0.3 ... main

3 changed files with 707 additions and 460 deletions

@ -1,13 +1,14 @@
name: GriefAlert
main: net.ardakaz.griefalert.GriefAlert
version: 0.3
version: 0.4
api-version: 1.21
depends: [CoreProtect]
softdepend: [DiscordSRV]
description: A simple grief alert plugin using the CoreProtect API.
authors: [Ardakaz, kleedje30]
commands:
griefalert:
description: GriefAlert staff commands
usage: /griefalert <ignore|unignore|list|clear>
usage: /griefalert <ignore|unignore|check>
permission: griefalert.staff
permission-message: You do not have permission to use this command.

@ -6,21 +6,28 @@ import net.coreprotect.CoreProtectAPI.ParseResult;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabCompleter;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.hanging.HangingBreakByEntityEvent;
import org.bukkit.event.inventory.InventoryAction;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.event.player.PlayerInteractAtEntityEvent;
import org.bukkit.event.player.PlayerInteractEntityEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.entity.ArmorStand;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
@ -31,8 +38,12 @@ import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import org.bukkit.inventory.EntityEquipment;
import org.bukkit.entity.ItemFrame;
public class GriefAlert extends JavaPlugin implements Listener {
public class GriefAlert extends JavaPlugin implements Listener, TabCompleter {
private static CoreProtectAPI coreProtectAPI;
private Integer identicalAlerts = 1;
@ -144,21 +155,6 @@ public class GriefAlert extends JavaPlugin implements Listener {
}
}
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)
// Block break alerts
public void onBlockBreak(BlockBreakEvent event) {
@ -240,6 +236,182 @@ public class GriefAlert extends JavaPlugin implements Listener {
}
}
// Armor Stand break alerts
@EventHandler(ignoreCancelled = true)
public void onArmorStandBreak(EntityDamageByEntityEvent event) {
if (!(event.getEntity() instanceof ArmorStand)) return;
if (!(event.getDamager() instanceof Player)) return;
Player player = (Player) event.getDamager();
ArmorStand armorStand = (ArmorStand) event.getEntity();
int x = armorStand.getLocation().getBlockX();
int y = armorStand.getLocation().getBlockY();
int z = armorStand.getLocation().getBlockZ();
String worldName = armorStand.getWorld().getName();
if (isLocationIgnored(x, y, z, worldName)) {
event.setCancelled(true);
return;
}
String playerName = player.getName();
String target = inspectBlock(armorStand.getLocation().getBlock(), player);
if (target != null) {
String message = ChatColor.GRAY + playerName + " broke an armor stand 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, x, y, z, worldName);
}
}
// Armor Stand item interaction alerts
@EventHandler(ignoreCancelled = true)
public void onArmorStandInteract(PlayerInteractAtEntityEvent event) {
if (!(event.getRightClicked() instanceof ArmorStand)) return;
Player player = event.getPlayer();
ArmorStand armorStand = (ArmorStand) event.getRightClicked();
int x = armorStand.getLocation().getBlockX();
int y = armorStand.getLocation().getBlockY();
int z = armorStand.getLocation().getBlockZ();
String worldName = armorStand.getWorld().getName();
if (isLocationIgnored(x, y, z, worldName)) {
event.setCancelled(true);
return;
}
String target = inspectBlock(armorStand.getLocation().getBlock(), player);
if (target != null) {
String playerName = player.getName();
ItemStack handItem = player.getInventory().getItemInMainHand();
// Determine which slot is being interacted with
//org.bukkit.inventory.EquipmentSlot slot = event.getHand();
EntityEquipment equipment = armorStand.getEquipment();
ItemStack armorItem = null;
if (event.getClickedPosition() != null) {
// Approximate slot by Y position (not perfect, but Bukkit API is limited so it is what it is :3)
double yPos = event.getClickedPosition().getY();
if (yPos > 1.6) armorItem = equipment.getHelmet();
else if (yPos > 1.2) armorItem = equipment.getChestplate();
else if (yPos > 0.8) armorItem = equipment.getLeggings();
else if (yPos > 0.1) armorItem = equipment.getBoots();
else armorItem = equipment.getItemInMainHand();
} else {
armorItem = equipment.getItemInMainHand();
}
boolean handEmpty = handItem == null || handItem.getType() == Material.AIR;
boolean armorEmpty = armorItem == null || armorItem.getType() == Material.AIR;
String action;
//this works at the instance of interaction before items get moved around
if (handEmpty && !armorEmpty) {
action = "took " + armorItem.getType().toString();
} else if (!handEmpty && armorEmpty) {
action = "added " + handItem.getType().toString();
} else if (!handEmpty && !armorEmpty) {
action = "swapped " + armorItem.getType().toString() + " with " + handItem.getType().toString();
} else {
action = "interacted with";
}
String message = ChatColor.GRAY + playerName + " " + action + " on an armor stand owned 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, x, y, z, worldName);
}
}
//event handler for item frames
@EventHandler(ignoreCancelled = true)
public void onPlayerInteractEntity(PlayerInteractEntityEvent event){
if(!(event.getRightClicked() instanceof ItemFrame)) return;
Player player = event.getPlayer();
ItemFrame frame = (ItemFrame) event.getRightClicked();
int x = frame.getLocation().getBlockX();
int y = frame.getLocation().getBlockY();
int z = frame.getLocation().getBlockZ();
String worldName = frame.getWorld().getName();
if (isLocationIgnored(x, y, z, worldName)) {
event.setCancelled(true);
return;
}
String target = inspectBlock(frame.getLocation().getBlock(), player);
if(target !=null){
ItemStack itemInFrame = frame.getItem();
ItemStack handItem = player.getInventory().getItemInMainHand();
boolean handEmpty = handItem ==null || handItem.getType() == Material.AIR;
boolean frameEmpty = itemInFrame ==null || itemInFrame.getType() == Material.AIR;
String playerName = player.getName();
String action;
if (!handEmpty && frameEmpty) {
action = "added " + handItem.getType();
}
else {
return;
}
String message = ChatColor.GRAY + playerName + " " + action + " an item to an item frame owned 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, x, y, z, worldName);
}
}
//breaking item frame item (uses a different event then placing an item)
@EventHandler (ignoreCancelled = true)
public void onItemFrameDamage(EntityDamageByEntityEvent event){
if (!(event.getEntity() instanceof ItemFrame)) return;
if (!(event.getDamager() instanceof Player)) return;
ItemFrame frame = (ItemFrame) event.getEntity();
Player player = (Player) event.getDamager();
ItemStack item = frame.getItem();
String playerName = player.getName();
int x = frame.getLocation().getBlockX();
int y = frame.getLocation().getBlockY();
int z = frame.getLocation().getBlockZ();
String worldName = frame.getWorld().getName();
if (isLocationIgnored(x, y, z, worldName)) {
event.setCancelled(true);
return;
}
String target = inspectBlock(frame.getLocation().getBlock(), player);
if (target == null || target.equals(player.getName())) return;
if (item == null || item.getType() == Material.AIR) return;
// At this point, the player is removing the item
String action = "took " + item.getType();
String message = ChatColor.GRAY + playerName + " " + action + " from item frame owned 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, x, y, z, worldName);
}
@EventHandler(ignoreCancelled = true)
public void onItemFrameBreak(HangingBreakByEntityEvent event){
if (!(event.getEntity() instanceof ItemFrame)) return;
if (!(event.getRemover() instanceof Player)) return;
ItemFrame frame = (ItemFrame) event.getEntity();
Player player = (Player) event.getRemover();
String playerName = player.getName();
Location loc = frame.getLocation();
int x = loc.getBlockX();
int y = loc.getBlockY();
int z = loc.getBlockZ();
String worldName = loc.getWorld().getName();
if (isLocationIgnored(x, y, z, worldName)) {
event.setCancelled(true);
return;
}
String target = inspectBlock(loc.getBlock(), player);
if (target ==null || target.equals(player.getName())) return;
String action = "broke an item frame";
String message = ChatColor.GRAY + playerName + " " + action + " owned 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, x, y, z, worldName);
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (!command.getName().equalsIgnoreCase("griefalert")) return false;
@ -250,27 +422,43 @@ public class GriefAlert extends JavaPlugin implements Listener {
sender.sendMessage(ChatColor.RED + "You do not have permission.");
return true;
}
if (args.length != 4) {
sender.sendMessage(ChatColor.RED + "Usage: /griefalert ignore <x> <y> <z>");
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.");
String world;
int x, y, z;
if (args.length == 4) {
// /griefalert ignore x y z (use sender's world)
if (!(sender instanceof Player)) {
sender.sendMessage(ChatColor.RED + "Only players can use this command without specifying a world.");
return true;
}
} catch (NumberFormatException e) {
sender.sendMessage(ChatColor.RED + "Coordinates must be numbers.");
world = ((Player)sender).getWorld().getName();
try {
x = Integer.parseInt(args[1]);
y = Integer.parseInt(args[2]);
z = Integer.parseInt(args[3]);
} catch (NumberFormatException e) {
sender.sendMessage(ChatColor.RED + "Coordinates must be numbers.");
return true;
}
} else if (args.length == 5) {
// /griefalert ignore world x y z
world = args[1];
try {
x = Integer.parseInt(args[2]);
y = Integer.parseInt(args[3]);
z = Integer.parseInt(args[4]);
} catch (NumberFormatException e) {
sender.sendMessage(ChatColor.RED + "Coordinates must be numbers.");
return true;
}
} else {
sender.sendMessage(ChatColor.RED + "Usage: /griefalert ignore <world> <x> <y> <z> or /griefalert ignore <x> <y> <z>");
return true;
}
boolean result = addIgnoredLocation(x, y, z, world);
if (result) {
sender.sendMessage(ChatColor.GREEN + "Location ignored.");
} else {
sender.sendMessage(ChatColor.YELLOW + "Location was already ignored.");
}
return true;
} else if (sub.equals("unignore")) {
@ -278,38 +466,88 @@ public class GriefAlert extends JavaPlugin implements Listener {
sender.sendMessage(ChatColor.RED + "You do not have permission.");
return true;
}
if (args.length != 4) {
sender.sendMessage(ChatColor.RED + "Usage: /griefalert unignore <x> <y> <z>");
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.");
String world;
int x, y, z;
if (args.length == 4) {
// /griefalert unignore x y z (use sender's world)
if (!(sender instanceof Player)) {
sender.sendMessage(ChatColor.RED + "Only players can use this command without specifying a world.");
return true;
}
} catch (NumberFormatException e) {
sender.sendMessage(ChatColor.RED + "Coordinates must be numbers.");
world = ((Player)sender).getWorld().getName();
try {
x = Integer.parseInt(args[1]);
y = Integer.parseInt(args[2]);
z = Integer.parseInt(args[3]);
} catch (NumberFormatException e) {
sender.sendMessage(ChatColor.RED + "Coordinates must be valid.");
return true;
}
} else if (args.length == 5) {
// /griefalert unignore world x y z
world = args[1];
try {
x = Integer.parseInt(args[2]);
y = Integer.parseInt(args[3]);
z = Integer.parseInt(args[4]);
} catch (NumberFormatException e) {
sender.sendMessage(ChatColor.RED + "Coordinates must be numbers.");
return true;
}
} else {
sender.sendMessage(ChatColor.RED + "Usage: /griefalert unignore <world> <x> <y> <z> or /griefalert unignore <x> <y> <z>");
return true;
}
boolean result = removeIgnoredLocation(x, y, z, world);
if (result) {
sender.sendMessage(ChatColor.GREEN + "Location unignored.");
} else {
sender.sendMessage(ChatColor.YELLOW + "Location was not ignored.");
}
return true;
} else if (sub.equals("list")) {
if (!sender.hasPermission("griefalert.staff.list")) {
sender.sendMessage(ChatColor.RED + "You do not have permission.");
} else if (sub.equals("check")) {
// /griefalert check <world> <x> <y> <z> or /griefalert check <x> <y> <z>
String world;
int x, y, z;
if (args.length == 4) {
// /griefalert check x y z (use sender's world)
if (!(sender instanceof Player)) {
sender.sendMessage(ChatColor.RED + "Only players can use this command without specifying a world.");
return true;
}
world = ((Player)sender).getWorld().getName();
try {
x = Integer.parseInt(args[1]);
y = Integer.parseInt(args[2]);
z = Integer.parseInt(args[3]);
} catch (NumberFormatException e) {
sender.sendMessage(ChatColor.RED + "Coordinates must be numbers.");
return true;
}
} else if (args.length == 5) {
// /griefalert check world x y z
world = args[1];
try {
x = Integer.parseInt(args[2]);
y = Integer.parseInt(args[3]);
z = Integer.parseInt(args[4]);
} catch (NumberFormatException e) {
sender.sendMessage(ChatColor.RED + "Coordinates must be numbers.");
return true;
}
} else {
sender.sendMessage(ChatColor.RED + "Usage: /griefalert check <world>(optional) <x> <y> <z> ");
return true;
}
listIgnoredLocations(sender);
boolean ignored = isLocationIgnored(x, y, z, world);
if (ignored) {
sender.sendMessage(ChatColor.YELLOW + "GriefAlert is DISABLED at " + x + ", " + y + ", " + z + " in " + world + ".");
} else {
sender.sendMessage(ChatColor.GREEN + "GriefAlert is ENABLED at " + x + ", " + y + ", " + z + " in " + world + ".");
}
return true;
}
sender.sendMessage(ChatColor.RED + "Unknown subcommand. Use: ignore, unignore, list");
sender.sendMessage(ChatColor.RED + "Unknown subcommand. Use: ignore, unignore, check");
return false;
}
@ -318,13 +556,20 @@ public class GriefAlert extends JavaPlugin implements Listener {
if (!command.getName().equalsIgnoreCase("griefalert")) return null;
java.util.List<String> completions = new java.util.ArrayList<>();
if (args.length == 1) {
java.util.List<String> subs = java.util.Arrays.asList("ignore", "unignore", "list");
java.util.List<String> subs = java.util.Arrays.asList("ignore", "unignore", "check");
for (String s : subs) {
if (s.startsWith(args[0].toLowerCase())) completions.add(s);
}
return completions;
}
// No world argument needed anymore
if (args.length == 2 && (args[0].equalsIgnoreCase("ignore") || args[0].equalsIgnoreCase("unignore") || args[0].equalsIgnoreCase("check"))) {
// Suggest world names for the 2nd argument
for (org.bukkit.World world : org.bukkit.Bukkit.getWorlds()) {
completions.add(world.getName());
}
return completions;
}
// Optionally, you could add number tab completion for coordinates, but it's not necessary
return null;
}
@ -364,7 +609,7 @@ public class GriefAlert extends JavaPlugin implements Listener {
}
// Send an event for external hooks
GriefAlertEvent griefalert_event;
if (mapLink != null && !mapLink.isEmpty()) {
if (MAP_LINK != null && !MAP_LINK.isEmpty() && mapLink != null) {
griefalert_event = new GriefAlertEvent(message + " (" + mapLink + ")");
} else {
griefalert_event = new GriefAlertEvent(message);

1
src/net/ardakaz/griefalert/GriefAlertEvent.java Executable file → Normal file

@ -2,6 +2,7 @@
package net.ardakaz.griefalert;
import org.bukkit.Bukkit;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;