Compare commits

..

No commits in common. "main" and "0.2.1" have entirely different histories.
main ... 0.2.1

4 changed files with 289 additions and 718 deletions

0
config.yml Normal file → Executable file
View File

21
plugin.yml Normal file → Executable file
View File

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

931
src/net/ardakaz/griefalert/GriefAlert.java Normal file → Executable file
View File

@ -1,677 +1,256 @@
package net.ardakaz.griefalert; package net.ardakaz.griefalert;
import net.coreprotect.CoreProtect; import net.coreprotect.CoreProtect;
import net.coreprotect.CoreProtectAPI; import net.coreprotect.CoreProtectAPI;
import net.coreprotect.CoreProtectAPI.ParseResult; import net.coreprotect.CoreProtectAPI.ParseResult;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.Location; import org.bukkit.Material;
import org.bukkit.Material; import org.bukkit.block.Block;
import org.bukkit.block.Block; import org.bukkit.entity.Player;
import org.bukkit.command.Command; import org.bukkit.event.EventHandler;
import org.bukkit.command.CommandSender; import org.bukkit.event.Listener;
import org.bukkit.command.TabCompleter; import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.entity.Player; import org.bukkit.event.inventory.InventoryAction;
import org.bukkit.event.EventHandler; import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.Listener; import org.bukkit.event.inventory.InventoryType;
import org.bukkit.event.block.BlockBreakEvent; import org.bukkit.inventory.Inventory;
import org.bukkit.event.entity.EntityDamageByEntityEvent; import org.bukkit.inventory.ItemStack;
import org.bukkit.event.hanging.HangingBreakByEntityEvent; import org.bukkit.plugin.Plugin;
import org.bukkit.event.inventory.InventoryAction; import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryType; import java.util.Arrays;
import org.bukkit.event.player.PlayerInteractAtEntityEvent; import java.util.List;
import org.bukkit.event.player.PlayerInteractEntityEvent; import java.util.Set;
import org.bukkit.inventory.Inventory; import java.util.stream.Collectors;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.Plugin; public class GriefAlert extends JavaPlugin implements Listener {
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.entity.ArmorStand; private static CoreProtectAPI coreProtectAPI;
import java.sql.Connection; private Integer identicalAlerts = 1;
import java.sql.DriverManager; private String lastAlert;
import java.sql.PreparedStatement;
import java.sql.ResultSet; private Set<Material> EXCLUDED_BLOCKS;
import java.sql.SQLException; private Set<InventoryType> VALID_CONTAINERS;
import java.sql.Statement; private String MAP_LINK;
import java.util.Arrays; private Boolean ALLOW_STEALING;
import java.util.List;
import java.util.Set; // Init GriefAlert
import java.util.stream.Collectors; @Override
import org.bukkit.inventory.EntityEquipment; public void onEnable() {
import org.bukkit.entity.ItemFrame;
coreProtectAPI = getCoreProtect();
if (coreProtectAPI == null) {
getLogger().severe("CoreProtect not found! Disabling plugin.");
public class GriefAlert extends JavaPlugin implements Listener, TabCompleter { getServer().getPluginManager().disablePlugin(this);
return;
private static CoreProtectAPI coreProtectAPI; }
private Integer identicalAlerts = 1;
private String lastAlert; getServer().getPluginManager().registerEvents(this, this);
private Set<Material> EXCLUDED_BLOCKS; // Config
private Set<InventoryType> VALID_CONTAINERS; saveDefaultConfig();
private String MAP_LINK; List<String> excludedBlocks = getConfig().getStringList("excluded-blocks");
private Boolean ALLOW_STEALING; EXCLUDED_BLOCKS = excludedBlocks.stream().map(Material::valueOf).collect(Collectors.toSet());
List<String> validContainers = getConfig().getStringList("valid-containers");
private Connection connection; VALID_CONTAINERS = validContainers.stream().map(InventoryType::valueOf).collect(Collectors.toSet());
private final String DB_FILE = "ignored_locations.db"; MAP_LINK = getConfig().getString("map-link");
ALLOW_STEALING = getConfig().getBoolean("allow-stealing");
// Init GriefAlert
@Override getLogger().info("GriefAlert has been enabled.");
public void onEnable() { }
coreProtectAPI = getCoreProtect(); @Override
if (coreProtectAPI == null) { public void onDisable() {
getLogger().severe("CoreProtect not found! Disabling plugin."); getLogger().info("GriefAlert has been disabled.");
getServer().getPluginManager().disablePlugin(this); }
return;
} @EventHandler (ignoreCancelled = true)
// Block break alerts
getServer().getPluginManager().registerEvents(this, this); public void onBlockBreak(BlockBreakEvent event) {
// Exclusion list
// Config if (EXCLUDED_BLOCKS.contains(event.getBlock().getType())) {
saveDefaultConfig(); return;
List<String> excludedBlocks = getConfig().getStringList("excluded-blocks"); }
EXCLUDED_BLOCKS = excludedBlocks.stream().map(Material::valueOf).collect(Collectors.toSet());
List<String> validContainers = getConfig().getStringList("valid-containers"); // Event parser
VALID_CONTAINERS = validContainers.stream().map(InventoryType::valueOf).collect(Collectors.toSet()); String playerName = event.getPlayer().getName();
MAP_LINK = getConfig().getString("map-link"); String blockType = event.getBlock().getType().toString();
ALLOW_STEALING = getConfig().getBoolean("allow-stealing"); int x = event.getBlock().getX();
int y = event.getBlock().getY();
setupDatabase(); int z = event.getBlock().getZ();
String worldName = event.getBlock().getWorld().getName();
getCommand("griefalert").setTabCompleter(this);
// Check if grief
getLogger().info("GriefAlert has been enabled."); String target = inspectBlock(event.getBlock(), event.getPlayer());
} if (target != null) {
// Alert
@Override String message = ChatColor.GRAY + playerName + " broke " + blockType + " placed by " + target + " at " + x + " " + y + " " + z + getHumanWorldName(worldName);
public void onDisable() { alert(message, playerName, "[Map Link](" + MAP_LINK + "/?worldname=" + worldName + "&zoom=7&x=" + x + "&y=" + y + "&z=" + z + ")", target);
getLogger().info("GriefAlert has been disabled."); }
if (connection != null) { }
try { connection.close(); } catch (SQLException ignored) {}
} // Stealing alerts
} @EventHandler (ignoreCancelled = true)
public void onInventoryClick(InventoryClickEvent event) {
private void setupDatabase() { if (ALLOW_STEALING) {
try { return;
connection = DriverManager.getConnection("jdbc:sqlite:" + getDataFolder().getAbsolutePath() + "/" + DB_FILE); }
Statement stmt = connection.createStatement(); boolean stealing;
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(); // Event parser for inv
} catch (SQLException e) { if (!(event.getWhoClicked() instanceof Player)) return;
getLogger().severe("Could not set up SQLite database: " + e.getMessage()); Player player = (Player) event.getWhoClicked();
} Inventory inventory = event.getInventory();
} Inventory clickedInventory = event.getClickedInventory();
ItemStack item = event.getCurrentItem();
private boolean isLocationIgnored(int x, int y, int z, String world) {
try { if (item == null || inventory.getLocation() == null || item.getType() == Material.AIR) {
PreparedStatement ps = connection.prepareStatement("SELECT 1 FROM ignored_locations WHERE x=? AND y=? AND z=? AND world=?"); return;
ps.setInt(1, x); }
ps.setInt(2, y);
ps.setInt(3, z); // Exclusion list
ps.setString(4, world); if (!VALID_CONTAINERS.contains(inventory.getType())) {
ResultSet rs = ps.executeQuery(); return;
boolean exists = rs.next(); }
rs.close();
ps.close(); // Inv actions (needs fixing)
return exists; InventoryAction action = event.getAction();
} catch (SQLException e) { if ((action == InventoryAction.PICKUP_ALL || action == InventoryAction.PICKUP_HALF ||
getLogger().warning("DB error: " + e.getMessage()); action == InventoryAction.PICKUP_ONE || action == InventoryAction.PICKUP_SOME ||
return false; action == InventoryAction.MOVE_TO_OTHER_INVENTORY) && clickedInventory == inventory) {
} stealing = true;
} } else if (action == InventoryAction.PLACE_ALL || action == InventoryAction.PLACE_SOME ||
action == InventoryAction.PLACE_ONE || (action == InventoryAction.MOVE_TO_OTHER_INVENTORY && clickedInventory != inventory)) {
private boolean addIgnoredLocation(int x, int y, int z, String world) { stealing = false;
try { } else {
PreparedStatement ps = connection.prepareStatement("INSERT OR IGNORE INTO ignored_locations (x, y, z, world) VALUES (?, ?, ?, ?)"); return;
ps.setInt(1, x); }
ps.setInt(2, y);
ps.setInt(3, z); // Event parser for container + check if grief
ps.setString(4, world); String target = inspectBlock(inventory.getLocation().getBlock(), player);
int updated = ps.executeUpdate(); if (target != null) {
ps.close(); String playerName = player.getName();
return updated > 0; String itemName = item.getType().toString();
} catch (SQLException e) { int amount = item.getAmount();
getLogger().warning("DB error: " + e.getMessage()); int x = inventory.getLocation().getBlockX();
return false; int y = inventory.getLocation().getBlockY();
} int z = inventory.getLocation().getBlockZ();
} String worldName = inventory.getLocation().getWorld().getName();
private boolean removeIgnoredLocation(int x, int y, int z, String world) { if (stealing) {
try { // Stealing
PreparedStatement ps = connection.prepareStatement("DELETE FROM ignored_locations WHERE x=? AND y=? AND z=? AND world=?"); String message = ChatColor.GRAY + playerName + " took " + amount + " " + itemName + " from " + target + "'s container at " + x + " " + y + " " + z + getHumanWorldName(worldName);
ps.setInt(1, x); alert(message, playerName, "[Map Link](" + MAP_LINK + "/?worldname=" + worldName + "&zoom=7&x=" + x + "&y=" + y + "&z=" + z + ")", target);
ps.setInt(2, y); } else {
ps.setInt(3, z); // Putting back
ps.setString(4, world); String message = ChatColor.GRAY + playerName + " put " + amount + " " + itemName + " into " + target + "'s container at " + x + " " + y + " " + z + getHumanWorldName(worldName);
int updated = ps.executeUpdate(); alert(message, playerName, "[Map Link](" + MAP_LINK + "/?worldname=" + worldName + "&zoom=7&x=" + x + "&y=" + y + "&z=" + z + ")", target);
ps.close(); }
return updated > 0; }
} catch (SQLException e) { }
getLogger().warning("DB error: " + e.getMessage());
return false; // 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);
@EventHandler (ignoreCancelled = true) if (griefer.hasPermission("griefalert.exclude") || griefer.hasPermission("griefalert.exclude." + target)) {
// Block break alerts return;
public void onBlockBreak(BlockBreakEvent event) { }
// Exclusion list
if (EXCLUDED_BLOCKS.contains(event.getBlock().getType())) { // Spam limiter
return; String realAlertMessage = message;
} String[] alert1 = null;
if (lastAlert != null) {
// Event parser alert1 = lastAlert.split(" ");
String playerName = event.getPlayer().getName(); }
String blockType = event.getBlock().getType().toString(); String[] alert2 = message.split(" ");
int x = event.getBlock().getX();
int y = event.getBlock().getY(); if (alert1 != null) {
int z = event.getBlock().getZ(); if (alert1[2].equals(alert2[2]) && alert1[5].equals(alert2[5]) && alert1[1].equals("broke") && alert2[1].equals("broke")) {
String worldName = event.getBlock().getWorld().getName(); identicalAlerts += 1;
}
// Check if grief else if (Arrays.equals(alert1, alert2)) {
String target = inspectBlock(event.getBlock(), event.getPlayer()); identicalAlerts += 1;
if (target != null) { }
String message = ChatColor.GRAY + playerName + " broke " + blockType + " placed by " + target + " at " + x + " " + y + " " + z + getHumanWorldName(worldName); else {
alert(message, playerName, "[Map Link](" + MAP_LINK + "/?worldname=" + worldName + "&zoom=7&x=" + x + "&y=" + y + "&z=" + z + ")", target, x, y, z, worldName); identicalAlerts = 1;
} }
} }
// Stealing alerts if (identicalAlerts == 4) {
@EventHandler (ignoreCancelled = true) message = ChatColor.GRAY + "Same behavior continues.";
public void onInventoryClick(InventoryClickEvent event) { mapLink = null;
if (ALLOW_STEALING) { }
return;
} if (identicalAlerts > 4) {
boolean stealing; return;
}
// Event parser for inv
if (!(event.getWhoClicked() instanceof Player)) return; // Send an event for external hooks
Player player = (Player) event.getWhoClicked(); GriefAlertEvent griefalert_event;
Inventory inventory = event.getInventory(); if (mapLink != null && !mapLink.isEmpty()) {
Inventory clickedInventory = event.getClickedInventory(); griefalert_event = new GriefAlertEvent(message + " (" + mapLink + ")");
ItemStack item = event.getCurrentItem(); }
else {
if (item == null || inventory.getLocation() == null || item.getType() == Material.AIR) { griefalert_event = new GriefAlertEvent(message);
return; }
} getServer().getPluginManager().callEvent(griefalert_event);
// Exclusion list // Notify staff ingame
if (!VALID_CONTAINERS.contains(inventory.getType())) { for (Player player : Bukkit.getOnlinePlayers()) {
return; if (player.hasPermission("griefalert.notify")) {
} player.sendMessage(message);
}
// Inv actions (needs fixing) }
InventoryAction action = event.getAction();
if ((action == InventoryAction.PICKUP_ALL || action == InventoryAction.PICKUP_HALF || lastAlert = realAlertMessage;
action == InventoryAction.PICKUP_ONE || action == InventoryAction.PICKUP_SOME || }
action == InventoryAction.MOVE_TO_OTHER_INVENTORY) && clickedInventory == inventory) {
stealing = true; // Block inspector: if the block was placed by another player, returns their name.
} else if (action == InventoryAction.PLACE_ALL || action == InventoryAction.PLACE_SOME || private static String inspectBlock(Block block, Player player) {
action == InventoryAction.PLACE_ONE || (action == InventoryAction.MOVE_TO_OTHER_INVENTORY && clickedInventory != inventory)) { List<String[]> lookup = coreProtectAPI.blockLookup(block, 50000000);
stealing = false; if (lookup == null || lookup.size() <= 0) {
} else { // Natural block
return; return null;
} }
// Event parser for container + check if grief String[] result = lookup.get(0);
String target = inspectBlock(inventory.getLocation().getBlock(), player); ParseResult parseResult = coreProtectAPI.parseResult(result);
if (target != null) { if (parseResult.isRolledBack() && lookup.size() != 1) {
String playerName = player.getName(); result = lookup.get(1);
String itemName = item.getType().toString(); parseResult = coreProtectAPI.parseResult(result);
int amount = item.getAmount(); }
int x = inventory.getLocation().getBlockX();
int y = inventory.getLocation().getBlockY(); if (result == null || parseResult == null || parseResult.getPlayer().startsWith("#") || parseResult.getPlayer().equals(player.getName())) {
int z = inventory.getLocation().getBlockZ(); // Placed by breaker or natural event
String worldName = inventory.getLocation().getWorld().getName(); return null;
if (stealing) { }
String message = ChatColor.GRAY + playerName + " took " + amount + " " + itemName + " from " + target + "'s container at " + x + " " + y + " " + z + getHumanWorldName(worldName); return parseResult.getPlayer();
alert(message, playerName, "[Map Link](" + MAP_LINK + "/?worldname=" + worldName + "&zoom=7&x=" + x + "&y=" + y + "&z=" + z + ")", target, x, y, z, worldName);
} else { }
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); private static String getHumanWorldName(String worldName) {
} String world = "";
}
} if (worldName.endsWith("_nether")) {
world = " in the Nether";
// Armor Stand break alerts }
@EventHandler(ignoreCancelled = true) else if (worldName.endsWith("_the_end")) {
public void onArmorStandBreak(EntityDamageByEntityEvent event) { world = " in the End";
if (!(event.getEntity() instanceof ArmorStand)) return; }
if (!(event.getDamager() instanceof Player)) return;
Player player = (Player) event.getDamager(); return world;
ArmorStand armorStand = (ArmorStand) event.getEntity(); }
int x = armorStand.getLocation().getBlockX();
int y = armorStand.getLocation().getBlockY(); private CoreProtectAPI getCoreProtect() {
int z = armorStand.getLocation().getBlockZ(); Plugin plugin = getServer().getPluginManager().getPlugin("CoreProtect");
String worldName = armorStand.getWorld().getName();
if (isLocationIgnored(x, y, z, worldName)) { if (plugin == null || !(plugin instanceof CoreProtect)) {
event.setCancelled(true); return null;
return; }
}
String playerName = player.getName(); return ((CoreProtect) plugin).getAPI();
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;
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;
}
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;
}
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")) {
if (!sender.hasPermission("griefalert.staff.UnIgnore")) {
sender.sendMessage(ChatColor.RED + "You do not have permission.");
return true;
}
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;
}
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("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;
}
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, check");
return false;
}
@Override
public java.util.List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) {
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", "check");
for (String s : subs) {
if (s.startsWith(args[0].toLowerCase())) completions.add(s);
}
return completions;
}
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;
}
// Sends the alert (or cancels it)
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 (MAP_LINK != null && !MAP_LINK.isEmpty() && mapLink != null) {
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: only the most recent placement counts for ownership.
private static String inspectBlock(Block block, Player player) {
List<String[]> lookup = coreProtectAPI.blockLookup(block, 50000000);
if (lookup == null || lookup.size() == 0) {
// Natural block
return null;
}
// 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) {
String world = "";
if (worldName.endsWith("_nether")) {
world = " in the Nether";
}
else if (worldName.endsWith("_the_end")) {
world = " in the End";
}
return world;
}
private CoreProtectAPI getCoreProtect() {
Plugin plugin = getServer().getPluginManager().getPlugin("CoreProtect");
if (plugin == null || !(plugin instanceof CoreProtect)) {
return null;
}
return ((CoreProtect) plugin).getAPI();
}
} }

55
src/net/ardakaz/griefalert/GriefAlertEvent.java Normal file → Executable file
View File

@ -1,29 +1,28 @@
// "API" for other plugins // "API" for other plugins
package net.ardakaz.griefalert; package net.ardakaz.griefalert;
import org.bukkit.Bukkit; import org.bukkit.event.Event;
import org.bukkit.event.Event; import org.bukkit.event.HandlerList;
import org.bukkit.event.HandlerList;
public class GriefAlertEvent extends Event {
public class GriefAlertEvent extends Event { private static final HandlerList HANDLERS = new HandlerList();
private static final HandlerList HANDLERS = new HandlerList(); private String alert = "";
private String alert = "";
public GriefAlertEvent(String alert) {
public GriefAlertEvent(String alert) { this.alert = alert;
this.alert = alert; }
}
public static HandlerList getHandlerList() {
public static HandlerList getHandlerList() { return HANDLERS;
return HANDLERS; }
}
@Override
@Override public HandlerList getHandlers() {
public HandlerList getHandlers() { return HANDLERS;
return HANDLERS; }
}
public String getAlert() {
public String getAlert() { return this.alert;
return this.alert; }
}
} }