Update src/net/ardakaz/griefalert/GriefAlert.java

This commit is contained in:
Kleedje30 2025-05-23 21:38:41 +00:00
parent 3bc045e799
commit 594523fb38

View File

@ -1,256 +1,298 @@
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.Material; import org.bukkit.Material;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent; import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.inventory.InventoryAction; import org.bukkit.event.inventory.InventoryAction;
import org.bukkit.event.inventory.InventoryClickEvent; import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryType; import org.bukkit.event.inventory.InventoryType;
import org.bukkit.inventory.Inventory; import org.bukkit.command.Command;
import org.bukkit.inventory.ItemStack; import org.bukkit.command.CommandSender;
import org.bukkit.plugin.Plugin; import org.bukkit.inventory.Inventory;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.Plugin;
import java.util.Arrays; import org.bukkit.plugin.java.JavaPlugin;
import java.util.List;
import java.util.Set; import java.util.Arrays;
import java.util.stream.Collectors; import java.util.List;
import java.util.Set;
public class GriefAlert extends JavaPlugin implements Listener { import java.util.stream.Collectors;
private static CoreProtectAPI coreProtectAPI; public class GriefAlert extends JavaPlugin implements Listener {
private Integer identicalAlerts = 1;
private String lastAlert; private static CoreProtectAPI coreProtectAPI;
private Integer identicalAlerts = 1;
private Set<Material> EXCLUDED_BLOCKS; private String lastAlert;
private Set<InventoryType> VALID_CONTAINERS;
private String MAP_LINK; private Set<Material> EXCLUDED_BLOCKS;
private Boolean ALLOW_STEALING; private Set<InventoryType> VALID_CONTAINERS;
private String MAP_LINK;
// Init GriefAlert private Boolean ALLOW_STEALING;
@Override
public void onEnable() { private AlertsLogic alertsLogic;
coreProtectAPI = getCoreProtect(); // Init GriefAlert
if (coreProtectAPI == null) { @Override
getLogger().severe("CoreProtect not found! Disabling plugin."); public void onEnable() {
getServer().getPluginManager().disablePlugin(this);
return; coreProtectAPI = getCoreProtect();
} if (coreProtectAPI == null) {
getLogger().severe("CoreProtect not found! Disabling plugin.");
getServer().getPluginManager().registerEvents(this, this); getServer().getPluginManager().disablePlugin(this);
return;
// Config }
saveDefaultConfig();
List<String> excludedBlocks = getConfig().getStringList("excluded-blocks"); getServer().getPluginManager().registerEvents(this, this);
EXCLUDED_BLOCKS = excludedBlocks.stream().map(Material::valueOf).collect(Collectors.toSet());
List<String> validContainers = getConfig().getStringList("valid-containers"); // Config
VALID_CONTAINERS = validContainers.stream().map(InventoryType::valueOf).collect(Collectors.toSet()); saveDefaultConfig();
MAP_LINK = getConfig().getString("map-link"); List<String> excludedBlocks = getConfig().getStringList("excluded-blocks");
ALLOW_STEALING = getConfig().getBoolean("allow-stealing"); EXCLUDED_BLOCKS = excludedBlocks.stream().map(Material::valueOf).collect(Collectors.toSet());
List<String> validContainers = getConfig().getStringList("valid-containers");
getLogger().info("GriefAlert has been enabled."); VALID_CONTAINERS = validContainers.stream().map(InventoryType::valueOf).collect(Collectors.toSet());
} MAP_LINK = getConfig().getString("map-link");
ALLOW_STEALING = getConfig().getBoolean("allow-stealing");
@Override
public void onDisable() { getLogger().info("GriefAlert has been enabled.");
getLogger().info("GriefAlert has been disabled.");
} // Initialize AlertsLogic and register commands
alertsLogic = new AlertsLogic(this);
@EventHandler (ignoreCancelled = true) getCommand("disablelocation").setExecutor(this);
// Block break alerts getCommand("enablelocation").setExecutor(this);
public void onBlockBreak(BlockBreakEvent event) { getCommand("clearlocations").setExecutor(this);
// Exclusion list getCommand("checklocation").setExecutor(this);
if (EXCLUDED_BLOCKS.contains(event.getBlock().getType())) { }
return;
} @Override
public void onDisable() {
// Event parser if (alertsLogic != null) {
String playerName = event.getPlayer().getName(); alertsLogic.close();
String blockType = event.getBlock().getType().toString(); }
int x = event.getBlock().getX(); getLogger().info("GriefAlert has been disabled.");
int y = event.getBlock().getY(); }
int z = event.getBlock().getZ();
String worldName = event.getBlock().getWorld().getName(); @Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
// Check if grief String cmd = command.getName().toLowerCase();
String target = inspectBlock(event.getBlock(), event.getPlayer()); switch (cmd) {
if (target != null) { case "disablelocation":
// Alert return alertsLogic.handleDisableLocation(sender, args);
String message = ChatColor.GRAY + playerName + " broke " + blockType + " placed by " + target + " at " + x + " " + y + " " + z + getHumanWorldName(worldName); case "enablelocation":
alert(message, playerName, "[Map Link](" + MAP_LINK + "/?worldname=" + worldName + "&zoom=7&x=" + x + "&y=" + y + "&z=" + z + ")", target); return alertsLogic.handleEnableLocation(sender, args);
} case "clearlocations":
} return alertsLogic.handleClearLocations(sender, args);
case "checklocation":
// Stealing alerts return alertsLogic.handleCheckLocation(sender, args);
@EventHandler (ignoreCancelled = true) default:
public void onInventoryClick(InventoryClickEvent event) { return false;
if (ALLOW_STEALING) { }
return; }
}
boolean stealing; @EventHandler (ignoreCancelled = true)
// Block break alerts
// Event parser for inv public void onBlockBreak(BlockBreakEvent event) {
if (!(event.getWhoClicked() instanceof Player)) return; // Exclusion list
Player player = (Player) event.getWhoClicked(); if (EXCLUDED_BLOCKS.contains(event.getBlock().getType())) {
Inventory inventory = event.getInventory(); return;
Inventory clickedInventory = event.getClickedInventory(); }
ItemStack item = event.getCurrentItem();
// Event parser
if (item == null || inventory.getLocation() == null || item.getType() == Material.AIR) { String playerName = event.getPlayer().getName();
return; String blockType = event.getBlock().getType().toString();
} int x = event.getBlock().getX();
int y = event.getBlock().getY();
// Exclusion list int z = event.getBlock().getZ();
if (!VALID_CONTAINERS.contains(inventory.getType())) { String worldName = event.getBlock().getWorld().getName();
return;
} // Check if alerts are disabled for this location
if (alertsLogic != null && alertsLogic.isLocationSaved(x, y, z, worldName)) {
// Inv actions (needs fixing) return;
InventoryAction action = event.getAction(); }
if ((action == InventoryAction.PICKUP_ALL || action == InventoryAction.PICKUP_HALF ||
action == InventoryAction.PICKUP_ONE || action == InventoryAction.PICKUP_SOME || // Check if grief
action == InventoryAction.MOVE_TO_OTHER_INVENTORY) && clickedInventory == inventory) { String target = inspectBlock(event.getBlock(), event.getPlayer());
stealing = true; if (target != null) {
} else if (action == InventoryAction.PLACE_ALL || action == InventoryAction.PLACE_SOME || // Alert
action == InventoryAction.PLACE_ONE || (action == InventoryAction.MOVE_TO_OTHER_INVENTORY && clickedInventory != inventory)) { String message = ChatColor.GRAY + playerName + " broke " + blockType + " placed by " + target + " at " + x + " " + y + " " + z + getHumanWorldName(worldName);
stealing = false; alert(message, playerName, "[Map Link](" + MAP_LINK + "/?worldname=" + worldName + "&zoom=7&x=" + x + "&y=" + y + "&z=" + z + ")", target);
} else { }
return; }
}
// Stealing alerts
// Event parser for container + check if grief @EventHandler (ignoreCancelled = true)
String target = inspectBlock(inventory.getLocation().getBlock(), player); public void onInventoryClick(InventoryClickEvent event) {
if (target != null) { if (ALLOW_STEALING) {
String playerName = player.getName(); return;
String itemName = item.getType().toString(); }
int amount = item.getAmount(); boolean stealing;
int x = inventory.getLocation().getBlockX();
int y = inventory.getLocation().getBlockY(); // Event parser for inv
int z = inventory.getLocation().getBlockZ(); if (!(event.getWhoClicked() instanceof Player)) return;
String worldName = inventory.getLocation().getWorld().getName(); Player player = (Player) event.getWhoClicked();
Inventory inventory = event.getInventory();
if (stealing) { Inventory clickedInventory = event.getClickedInventory();
// Stealing ItemStack item = event.getCurrentItem();
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); if (item == null || inventory.getLocation() == null || item.getType() == Material.AIR) {
} else { return;
// 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); // Exclusion list
} if (!VALID_CONTAINERS.contains(inventory.getType())) {
} return;
} }
// Sends the alert (or cancels it) // Check if alerts are disabled for this location
private void alert(String message, String playerName, String mapLink, String target) { int x = inventory.getLocation().getBlockX();
// Exclude trusted people int y = inventory.getLocation().getBlockY();
Player griefer = Bukkit.getPlayer(playerName); int z = inventory.getLocation().getBlockZ();
if (griefer.hasPermission("griefalert.exclude") || griefer.hasPermission("griefalert.exclude." + target)) { String worldName = inventory.getLocation().getWorld().getName();
return; if (alertsLogic != null && alertsLogic.isLocationSaved(x, y, z, worldName)) {
} return;
}
// Spam limiter
String realAlertMessage = message; // Inv actions (needs fixing)
String[] alert1 = null; InventoryAction action = event.getAction();
if (lastAlert != null) { if ((action == InventoryAction.PICKUP_ALL || action == InventoryAction.PICKUP_HALF ||
alert1 = lastAlert.split(" "); action == InventoryAction.PICKUP_ONE || action == InventoryAction.PICKUP_SOME ||
} action == InventoryAction.MOVE_TO_OTHER_INVENTORY) && clickedInventory == inventory) {
String[] alert2 = message.split(" "); stealing = true;
} else if (action == InventoryAction.PLACE_ALL || action == InventoryAction.PLACE_SOME ||
if (alert1 != null) { action == InventoryAction.PLACE_ONE || (action == InventoryAction.MOVE_TO_OTHER_INVENTORY && clickedInventory != inventory)) {
if (alert1[2].equals(alert2[2]) && alert1[5].equals(alert2[5]) && alert1[1].equals("broke") && alert2[1].equals("broke")) { stealing = false;
identicalAlerts += 1; } else {
} return;
else if (Arrays.equals(alert1, alert2)) { }
identicalAlerts += 1;
} // Event parser for container + check if grief
else { String target = inspectBlock(inventory.getLocation().getBlock(), player);
identicalAlerts = 1; if (target != null) {
} String playerName = player.getName();
} String itemName = item.getType().toString();
int amount = item.getAmount();
if (identicalAlerts == 4) { // x, y, z, worldName already defined above
message = ChatColor.GRAY + "Same behavior continues.";
mapLink = null; if (stealing) {
} // Stealing
String message = ChatColor.GRAY + playerName + " took " + amount + " " + itemName + " from " + target + "'s container at " + x + " " + y + " " + z + getHumanWorldName(worldName);
if (identicalAlerts > 4) { alert(message, playerName, "[Map Link](" + MAP_LINK + "/?worldname=" + worldName + "&zoom=7&x=" + x + "&y=" + y + "&z=" + z + ")", target);
return; } else {
} // Putting back
String message = ChatColor.GRAY + playerName + " put " + amount + " " + itemName + " into " + target + "'s container at " + x + " " + y + " " + z + getHumanWorldName(worldName);
// Send an event for external hooks alert(message, playerName, "[Map Link](" + MAP_LINK + "/?worldname=" + worldName + "&zoom=7&x=" + x + "&y=" + y + "&z=" + z + ")", target);
GriefAlertEvent griefalert_event; }
if (mapLink != null && !mapLink.isEmpty()) { }
griefalert_event = new GriefAlertEvent(message + " (" + mapLink + ")"); }
}
else { // Sends the alert (or cancels it)
griefalert_event = new GriefAlertEvent(message); private void alert(String message, String playerName, String mapLink, String target) {
} // Exclude trusted people
getServer().getPluginManager().callEvent(griefalert_event); Player griefer = Bukkit.getPlayer(playerName);
if (griefer.hasPermission("griefalert.exclude") || griefer.hasPermission("griefalert.exclude." + target)) {
// Notify staff ingame return;
for (Player player : Bukkit.getOnlinePlayers()) { }
if (player.hasPermission("griefalert.notify")) {
player.sendMessage(message); // Spam limiter
} String realAlertMessage = message;
} String[] alert1 = null;
if (lastAlert != null) {
lastAlert = realAlertMessage; alert1 = lastAlert.split(" ");
} }
String[] alert2 = message.split(" ");
// Block inspector: if the block was placed by another player, returns their name.
private static String inspectBlock(Block block, Player player) { if (alert1 != null) {
List<String[]> lookup = coreProtectAPI.blockLookup(block, 50000000); if (alert1[2].equals(alert2[2]) && alert1[5].equals(alert2[5]) && alert1[1].equals("broke") && alert2[1].equals("broke")) {
if (lookup == null || lookup.size() <= 0) { identicalAlerts += 1;
// Natural block }
return null; else if (Arrays.equals(alert1, alert2)) {
} identicalAlerts += 1;
}
String[] result = lookup.get(0); else {
ParseResult parseResult = coreProtectAPI.parseResult(result); identicalAlerts = 1;
if (parseResult.isRolledBack() && lookup.size() != 1) { }
result = lookup.get(1); }
parseResult = coreProtectAPI.parseResult(result);
} if (identicalAlerts == 4) {
message = ChatColor.GRAY + "Same behavior continues.";
if (result == null || parseResult == null || parseResult.getPlayer().startsWith("#") || parseResult.getPlayer().equals(player.getName())) { mapLink = null;
// Placed by breaker or natural event }
return null;
} if (identicalAlerts > 4) {
return parseResult.getPlayer(); return;
}
}
// Send an event for external hooks
private static String getHumanWorldName(String worldName) { GriefAlertEvent griefalert_event;
String world = ""; if (mapLink != null && !mapLink.isEmpty()) {
griefalert_event = new GriefAlertEvent(message + " (" + mapLink + ")");
if (worldName.endsWith("_nether")) { }
world = " in the Nether"; else {
} griefalert_event = new GriefAlertEvent(message);
else if (worldName.endsWith("_the_end")) { }
world = " in the End"; getServer().getPluginManager().callEvent(griefalert_event);
}
// Notify staff ingame
return world; for (Player player : Bukkit.getOnlinePlayers()) {
} if (player.hasPermission("griefalert.notify")) {
player.sendMessage(message);
private CoreProtectAPI getCoreProtect() { }
Plugin plugin = getServer().getPluginManager().getPlugin("CoreProtect"); }
if (plugin == null || !(plugin instanceof CoreProtect)) { lastAlert = realAlertMessage;
return null; }
}
// Block inspector: if the block was placed by another player, returns their name.
return ((CoreProtect) plugin).getAPI(); 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;
}
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
return null;
}
return parseResult.getPlayer();
}
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();
}
} }