Cleaning up

This commit is contained in:
Ardakaz
2026-05-19 15:49:03 +03:00
parent f59c484d6b
commit 8e28ba0b87
3 changed files with 167 additions and 185 deletions

View File

@@ -1,6 +1,6 @@
name: GriefAlert
main: net.ardakaz.griefalert.GriefAlert
version: 0.5
version: 0.6
api-version: 1.21
depends: [CoreProtect]
softdepend: [DiscordSRV]

View File

@@ -3,8 +3,6 @@ package net.ardakaz.griefalert;
import net.coreprotect.CoreProtect;
import net.coreprotect.CoreProtectAPI;
import net.coreprotect.CoreProtectAPI.ParseResult;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
@@ -12,52 +10,85 @@ 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.*;
import org.bukkit.event.*;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.entity.EntityDeathEvent;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityDeathEvent;
import org.bukkit.event.hanging.HangingBreakByEntityEvent;
import org.bukkit.inventory.ItemStack;
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.event.player.PlayerInteractEvent;
import org.bukkit.inventory.EntityEquipment;
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.sql.*;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import org.bukkit.inventory.EntityEquipment;
public class GriefAlert extends JavaPlugin implements Listener {
private static CoreProtectAPI coreProtectAPI;
private final String DB_FILE = "ignored_locations.db";
private Integer identicalAlerts = 1;
private String lastAlert;
private Set<Material> EXCLUDED_BLOCKS;
private Set<InventoryType> VALID_CONTAINERS;
private String MAP_LINK;
private Boolean ALLOW_STEALING;
private Connection connection;
private final String DB_FILE = "ignored_locations.db";
// 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.isEmpty()) {
// 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;
}
// Init GriefAlert
@Override
@@ -92,7 +123,10 @@ public class GriefAlert extends JavaPlugin implements Listener {
public void onDisable() {
getLogger().info("GriefAlert has been disabled.");
if (connection != null) {
try { connection.close(); } catch (SQLException ignored) {}
try {
connection.close();
} catch (SQLException ignored) {
}
}
}
@@ -190,8 +224,7 @@ public class GriefAlert extends JavaPlugin implements Listener {
boolean stealing;
// Event parser for inv
if (!(event.getWhoClicked() instanceof Player)) return;
Player player = (Player) event.getWhoClicked();
if (!(event.getWhoClicked() instanceof Player player)) return;
Inventory inventory = event.getInventory();
Inventory clickedInventory = event.getClickedInventory();
ItemStack item = event.getCurrentItem();
@@ -291,11 +324,11 @@ public class GriefAlert extends JavaPlugin implements Listener {
String action;
//this works at the instance of interaction before items get moved around
if (handEmpty && !armorEmpty) {
action = "took " + armorItem.getType().toString();
action = "took " + armorItem.getType();
} else if (!handEmpty && armorEmpty) {
action = "added " + handItem.getType().toString();
action = "added " + handItem.getType();
} else if (!handEmpty) {
action = "swapped " + armorItem.getType().toString() + " with " + handItem.getType().toString();
action = "swapped " + armorItem.getType() + " with " + handItem.getType();
} else {
action = "interacted with";
}
@@ -303,6 +336,7 @@ public class GriefAlert extends JavaPlugin implements Listener {
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) {
@@ -328,19 +362,16 @@ public class GriefAlert extends JavaPlugin implements Listener {
String action;
if (!handEmpty && frameEmpty) {
action = "added " + handItem.getType();
}
else {
} else {
return;
}
String message = ChatColor.GRAY + playerName + " " + action + " an item to an item frame owned by " + target + " at " + x + " " + y + " " + z + getHumanWorldName(worldName);
String message = ChatColor.GRAY + playerName + " " + action + " an item to an item frame 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);
}
}
// Breaking item frame item (uses a different event then placing an item)
@EventHandler(ignoreCancelled = true)
public void onItemFrameDamage(EntityDamageByEntityEvent event) {
@@ -366,11 +397,10 @@ public class GriefAlert extends JavaPlugin implements Listener {
if (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);
String message = ChatColor.GRAY + playerName + " " + action + " from an item frame 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);
}
@EventHandler(ignoreCancelled = true)
public void onItemFrameBreak(HangingBreakByEntityEvent event) {
if (!(event.getEntity() instanceof ItemFrame frame)) return;
@@ -397,9 +427,6 @@ public class GriefAlert extends JavaPlugin implements Listener {
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (!command.getName().equalsIgnoreCase("griefalert")) return false;
@@ -612,34 +639,6 @@ public class GriefAlert extends JavaPlugin implements Listener {
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.isEmpty()) {
// 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;
}
//pet alert(dog and cat only)
@EventHandler
public void onPetKill(EntityDeathEvent event) {
@@ -666,6 +665,7 @@ public class GriefAlert extends JavaPlugin implements Listener {
alert(message, killer.getName(), "[Map Link](" + MAP_LINK + "/?worldname=" + worldName + "&zoom=7&x=" + x + "&y=" + y + "&z=" + z + ")", Owner.getName(), x, y, z, worldName);
}
//farm animal handler
@EventHandler
public void onFarmAnimalDeath(EntityDeathEvent event) {
@@ -696,6 +696,7 @@ public class GriefAlert extends JavaPlugin implements Listener {
}
/*code i hope for copper waxing/unwaxing theres no proper onwax() or onUnWax()
so im kinda gonna try to just check a situation like, if ur holding an axe, left clicking a copper block
and its waxed, ill assume ur unwaxing, and therefor sending an alert
@@ -734,7 +735,7 @@ public class GriefAlert extends JavaPlugin implements Listener {
//boolean isAxe = item.getType().toString().endsWith("_AXE");
String target = inspectBlock(block, player);
if(target == player.getName()) return;
if (Objects.equals(target, player.getName())) return;
if (target == null) return;
Bukkit.getScheduler().runTaskLater(this, () -> {
@@ -760,12 +761,7 @@ public class GriefAlert extends JavaPlugin implements Listener {
String message = ChatColor.GRAY + player.getName() + action + " Copper " + " at " + x + " " + y + " " + z + getHumanWorldName(world);
alert(message, player.getName(), "[Map Link](" + MAP_LINK + "/?worldname=" + world + "&zoom=7&x=" + x + "&y=" + y + "&z=" + z + ")", target, x, y, z, world);
}, 1L);
}
//stripping logs
@@ -798,7 +794,7 @@ public class GriefAlert extends JavaPlugin implements Listener {
String target = inspectBlock(block, player);
if (target == null) return;
if(target == player.getName()) return;
if (target.equals(player.getName())) return;
Bukkit.getScheduler().runTaskLater(this, () -> {
String action;
@@ -807,32 +803,18 @@ public class GriefAlert extends JavaPlugin implements Listener {
if (!before.name().contains("STRIPPED") && after.name().contains("STRIPPED")) {
action = "Stripped";
} else{return;}
} else {
return;
}
String message = ChatColor.GRAY + player.getName() + action + " a(n) " + before + " at " + x + " " + y + " " + z + getHumanWorldName(world);
alert(message, player.getName(), "[Map Link](" + MAP_LINK + "/?worldname=" + world + "&zoom=7&x=" + x + "&y=" + y + "&z=" + z + ")", target, x, y, z, world);
}, 1L);
}
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");