Upload files to "src/net/ardakaz/griefalert"

should fix armor stands and item frames
This commit is contained in:
Kleedje30 2025-06-20 19:53:39 +00:00
parent ed05bf48ef
commit 4d9ba74c42
2 changed files with 651 additions and 523 deletions

View File

@ -15,13 +15,17 @@ 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.entity.EntityDamageByEntityEvent;
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.event.player.PlayerInteractAtEntityEvent;
import org.bukkit.event.player.PlayerInteractEntityEvent;
import org.bukkit.inventory.Inventory; import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.entity.ArmorStand;
import java.sql.Connection; import java.sql.Connection;
import java.sql.DriverManager; import java.sql.DriverManager;
import java.sql.PreparedStatement; import java.sql.PreparedStatement;
@ -32,6 +36,10 @@ import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import org.bukkit.inventory.EntityEquipment;
import org.bukkit.entity.ItemFrame;
public class GriefAlert extends JavaPlugin implements Listener, TabCompleter { public class GriefAlert extends JavaPlugin implements Listener, TabCompleter {
@ -226,6 +234,126 @@ public class GriefAlert extends JavaPlugin implements Listener, TabCompleter {
} }
} }
// 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 ArmorStand 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 ArmorStand 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 action;
if (handEmpty && !frameEmpty) {
action = "took " + itemInFrame.getType();
} else if (!handEmpty && frameEmpty) {
action = "added " + handItem.getType();
} else {
return;
}
String message = ChatColor.GRAY + player.getName() + " " + action + " ItemFrame owned by " + target +
" at " + x + " " + y + " " + z + " (" + worldName + ")";
alert(message, player.getName(),
"[Map Link](" + MAP_LINK + "/?worldname=" + worldName + "&zoom=7&x=" + x + "&y=" + y + "&z=" + z + ")",
target, x, y, z, worldName);
}
}
@Override @Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (!command.getName().equalsIgnoreCase("griefalert")) return false; if (!command.getName().equalsIgnoreCase("griefalert")) return false;