Question ItemFrame Protection help

  • After careful consideration and due to limited usage, we’ve made the decision to discontinue the PaperMC forums. Moving forward, we recommend using Hangar for plugin uploads, and for all other community discussions and support, please join us on Discord.

BlackPoison357

New member
Mar 5, 2023
4
0
1
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;
                    }
                }
            }
        }
    }
}
 

BlackPoison357

New member
Mar 5, 2023
4
0
1
Figured it out. Tried another way without relying on blockfaces to do it, for some reason i couldn't get that way to work, if anybody needs it, below code prevents the block that a protected itemframe is attached to. If anybody is feeling generous and know why my original way didn't work, I'd still like to learn. Thanks!
Java:
    public void onBlockBreak(BlockBreakEvent event) {
        Block brokenBlock = event.getBlock();

        // Check for item frames within a radius of 2 blocks from the broken block
        for (Entity entity : brokenBlock.getWorld().getNearbyEntities(brokenBlock.getLocation(), 2, 2, 2)) {
            if (entity instanceof ItemFrame) {
                ItemFrame itemFrame = (ItemFrame) entity;

                // Check if the item frame is attached to the broken block
                if (itemFrame.getLocation().getBlock().getRelative(itemFrame.getAttachedFace()).equals(brokenBlock)) {
                    Player player = event.getPlayer();
                    if (!player.hasPermission("frame.remove")) {
                        player.sendMessage(ChatColor.RED + "You do not have permission to remove item frames.");
                        event.setCancelled(true);
                        return;
                    }
                    if (!canRemoveItemFrame(player, itemFrame)) {
                        player.sendMessage(ChatColor.RED + "You are not the owner of this item frame and cannot remove it.");
                        event.setCancelled(true);
                    } else {
                        removeItemFrameFromConfig(player, itemFrame);
                        player.sendMessage(ChatColor.GREEN + "Item frame removed successfully.");
                    }
                    return;
                }
            }
        }
    }