Untitled

mail@pastecode.io avatar
unknown
java
a year ago
2.1 kB
9
Indexable
Never
    /**
     * Checks if a block is fortified when broken by a player.
     * @param event fires when a player breaks a block.
     */
    @EventHandler (priority = EventPriority.HIGHEST)
    public void onBlockBreak(BlockBreakEvent event) {
        // Get bottom half if block is a door
        Block block = event.getBlock();
        if (BlockUtils.isDoor(block)) {
            block = BlockUtils.getBottomPartOfDoor(block);
        }

        // Check if block is fortified
        FortifiedBlock fortifiedBlock = CKGlobal.getFortifiedBlock(block.getLocation());
        if (fortifiedBlock == null) {
            if (BiomeData.isCrop(block.getType())) {
                FarmingHandler.removeCrop(block.getLocation());
            }
            // Check if snitch is nearby
            SnitchHandler.handleBlockBreak(event);
            return;
        }

        // Check if fortified block is from your group
        Resident res = CKGlobal.getResident(event.getPlayer());
        if (res.getGroups().contains(fortifiedBlock.getGroup())) {
            if (res.hasPermission(fortifiedBlock.getGroup(), Permissions.BLOCKS)) {
                removeNametag(block.getLocation());
                fortifiedBlock.delete();
                return;
            }
        }

        // Remove one reinforcement from block
        fortifiedBlock.decrement();
        if (fortifiedBlock.getReinforcements() <= 0) {
            if (BiomeData.isCrop(block.getType())) {
                FarmingHandler.removeCrop(block.getLocation());
            }
            removeNametag(block.getLocation());
            // Check if snitch is nearby
            SnitchHandler.handleBlockBreak(event);
            return;
        }
        event.setCancelled(true);

        // Update block nametag
        BlockFace face = getPlayerFacing(event.getPlayer());
        Location adjustedLocation = adjustLocationForFace(block.getLocation(), face);
        FortifiedBlock fb = CKGlobal.getFortifiedBlock(block.getLocation());
        if (fb != null) {
            updateNametag(adjustedLocation, fb);
        }
    }