I'm attempting to check if a block that is broke has an itemframe that is protected on it. I just can't seem to get it to work. Currently only the Event Triggered logger, so I know the issue is somewhere with detected the itemframe. The 2nd code block is the protection method it is checking for reference.
Java:
public void onBlockBreak(BlockBreakEvent e) {
plugin.getLogger().info("Event Triggered");
Block b = e.getBlock();
BlockFace[] SIDES = new BlockFace[]{BlockFace.UP, BlockFace.DOWN, BlockFace.NORTH, BlockFace.SOUTH, BlockFace.WEST, BlockFace.EAST};
for (BlockFace side : SIDES) {
final Block relative = b.getRelative(side);
if (relative.getState().getData() instanceof ItemFrame) {
plugin.getLogger().info("Instance of ItemFrame");
ItemFrame itemframe = (ItemFrame) relative.getState().getData();
if (relative.getRelative(itemframe.getAttachedFace()).equals(b)) {
plugin.getLogger().info("itemframe equals b");
Player player = e.getPlayer();
if (!canRemoveItemFrame(player, itemframe)) {
player.sendMessage(ChatColor.RED + "You are not the owner of this item frame and cannot remove it.");
e.setCancelled(true);
} else {
removeItemFrameFromConfig(player, itemframe);
player.sendMessage(ChatColor.GREEN + "Item frame removed successfully.");
}
return;
}
}
}
}
Java:
public boolean canRemoveItemFrame(Player player, ItemFrame itemFrame) {
if (player.isOp() || player.hasPermission("frame.bypass")) {
return true; // allow OP to bypass all checks
}
String uuid = player.getUniqueId().toString();
ConfigurationSection itemFrames = plugin.getFrameProtectorConfig().getConfigurationSection("ItemFrames");
if (itemFrames != null) {
ConfigurationSection playerSection = itemFrames.getConfigurationSection(uuid);
if (playerSection != null) {
List<Map<?, ?>> framesList = playerSection.getMapList("frames");
for (Map<?, ?> element : framesList) {
@SuppressWarnings("unchecked")
Map<String, Object> frameMap = (Map<String, Object>) element;
if (frameMap != null && frameMap.get("x").equals(itemFrame.getLocation().getBlockX())
&& frameMap.get("y").equals(itemFrame.getLocation().getBlockY())
&& frameMap.get("z").equals(itemFrame.getLocation().getBlockZ())
&& frameMap.get("world").equals(itemFrame.getWorld().getUID().toString())) {
String frameOwnerUUID = playerSection.getName();
// Remove the configuration entry if the player is the owner
if (uuid.equals(frameOwnerUUID)) {
return true;
} else {
return false;
}
}
}
}
}
return false;
}
//removes the itemframe from the save file.
public void removeItemFrameFromConfig(Player player, ItemFrame itemFrame) {
String uuid = player.getUniqueId().toString();
ConfigurationSection itemFrames = plugin.getFrameProtectorConfig().getConfigurationSection("ItemFrames");
if (itemFrames != null) {
ConfigurationSection playerSection = itemFrames.getConfigurationSection(uuid);
if (playerSection != null) {
List<Map<?, ?>> framesList = playerSection.getMapList("frames");
for (int i = 0; i < framesList.size(); i++) {
@SuppressWarnings("unchecked")
Map<String, Object> frameMap = (Map<String, Object>) framesList.get(i);
if (frameMap != null && frameMap.get("x").equals(itemFrame.getLocation().getBlockX())
&& frameMap.get("y").equals(itemFrame.getLocation().getBlockY())
&& frameMap.get("z").equals(itemFrame.getLocation().getBlockZ())
&& frameMap.get("world").equals(itemFrame.getWorld().getUID().toString())) {
String frameOwnerUUID = playerSection.getName();
// Remove the configuration entry if the player is the owner
if (uuid.equals(frameOwnerUUID)) {
framesList.remove(i);
playerSection.set("frames", framesList);
try {
plugin.getFrameProtectorConfig().save(plugin.frameprotectorf);
} catch (IOException e) {
e.printStackTrace();
}
}
break;
}
}
}
}
}
}