Java Code Examples for com.griefcraft.model.Protection#getProtectionFinder()

The following examples show how to use com.griefcraft.model.Protection#getProtectionFinder() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RedstoneModule.java    From Modern-LWC with MIT License 5 votes vote down vote up
@Override
public void onRedstone(LWCRedstoneEvent event) {
    if (event.isCancelled()) {
        return;
    }

    LWC lwc = event.getLWC();
    Protection protection = event.getProtection();

    // check for a player using it
    ProtectionFinder finder = protection.getProtectionFinder();

    if (finder != null) {
        for (BlockState found : finder.getBlocks()) {
            if (DoorMatcher.PRESSURE_PLATES.contains(found.getType())) {
                // find a player that is using it
                int x = found.getX();
                int y = found.getY();
                int z = found.getZ();
                Player player = lwc.findPlayer(x - 1, x + 1, y, y + 1, z - 1, z + 1);

                if (player != null) {
                    if (!lwc.canAccessProtection(player, protection)) {
                        event.setCancelled(true);
                    } else {
                        // bypass the denyRedstone/REDSTONE flag check
                        return;
                    }
                }
            }
        }
    }

    boolean hasFlag = protection.hasFlag(Flag.Type.REDSTONE);
    boolean denyRedstone = lwc.getConfiguration().getBoolean("protections.denyRedstone", false);

    if ((!hasFlag && denyRedstone) || (hasFlag && !denyRedstone)) {
        event.setCancelled(true);
    }
}
 
Example 2
Source File: ProtectionCache.java    From Modern-LWC with MIT License 5 votes vote down vote up
/**
 * Cache a protection
 *
 * @param protection
 */
public void addProtection(Protection protection) {
    if (protection == null) {
        return;
    }

    counter.increment("addProtection");

    // Add the hard reference
    references.put(protection, null);

    // Add weak references which are used to lookup protections
    byCacheKey.put(protection.getCacheKey(), protection);
    byId.put(protection.getId(), protection);

    // get the protection's finder if it was found via that
    if (protection.getProtectionFinder() != null) {
        Block protectedBlock = protection.getBlock();

        for (BlockState state : protection.getProtectionFinder()
                .getBlocks()) {
            if (!protectedBlock.equals(state.getBlock())) {
                String cacheKey = cacheKey(state.getLocation());
                byKnownBlock.put(cacheKey, protection);
            }
        }
    }
}
 
Example 3
Source File: ProtectionCache.java    From Modern-LWC with MIT License 5 votes vote down vote up
/**
 * Remove the protection from the cache
 *
 * @param protection
 */
public void removeProtection(Protection protection) {
    counter.increment("removeProtection");

    references.remove(protection);
    byId.remove(protection.getId());

    if (protection.getProtectionFinder() != null) {
        for (BlockState state : protection.getProtectionFinder()
                .getBlocks()) {
            remove(cacheKey(state.getLocation()));
        }
    }
}
 
Example 4
Source File: ProtectionFinder.java    From Modern-LWC with MIT License 4 votes vote down vote up
/**
 * Try and load a protection for a given block. If succeded, cache it locally
 *
 * @param block
 * @param noAutoCache if a match is found, don't cache it to be the protection we use
 * @return
 */
protected Result tryLoadProtection(BlockState block, boolean noAutoCache) {
    if (matchedProtection != null) {
        return Result.E_FOUND;
    }

    LWC lwc = LWC.getInstance();
    ProtectionCache cache = lwc.getProtectionCache();

    // Check the cache
    if ((matchedProtection = cache.getProtection(block)) != null) {
        searched = true;
        if (matchedProtection.getProtectionFinder() == null) {
            fullMatchBlocks();
            matchedProtection.setProtectionFinder(this);
            cache.addProtection(matchedProtection);
        }
        return Result.E_FOUND;
    }

    // Manual intervention is required
    if (block.getType() == Material.REDSTONE_WIRE || block.getType() == Material.REDSTONE_WALL_TORCH ||
            block.getType() == Material.REDSTONE_TORCH) {
        return Result.E_ABORT;
    }

    // don't bother trying to load it if it is not protectable
    if (!lwc.isProtectable(block)) {
        return Result.E_NOT_FOUND;
    }

    // Null-check
    if (block.getWorld() == null) {
        return Result.E_NOT_FOUND;
    }

    Protection protection = lwc.getPhysicalDatabase().loadProtection(block.getWorld().getName(), block.getX(), block.getY(), block.getZ());

    if (protection != null) {
        if (protection.getProtectionFinder() == null) {
            protection.setProtectionFinder(this);
            fullMatchBlocks();
            cache.addProtection(matchedProtection);
        }

        // ensure it's the right block
        if (protection.getBlockId() > 0) {
            if (protection.isBlockInWorld()) {
                if (noAutoCache) {
                    return Result.E_FOUND;
                }

                this.matchedProtection = protection;
                searched = true;
            } else {
                // Removing orrupted protection
                protection.remove();
            }
        }
    }

    return this.matchedProtection != null ? Result.E_FOUND : Result.E_NOT_FOUND;
}
 
Example 5
Source File: LWCBlockListener.java    From Modern-LWC with MIT License 4 votes vote down vote up
@EventHandler
public void onBlockBreak(BlockBreakEvent event) {
    if (!LWC.ENABLED || event.isCancelled()) {
        return;
    }

    LWC lwc = plugin.getLWC();
    Player player = event.getPlayer();
    Block block = event.getBlock();

    boolean ignoreBlockDestruction = Boolean
            .parseBoolean(lwc.resolveProtectionConfiguration(block, "ignoreBlockDestruction"));

    if (ignoreBlockDestruction) {
        return;
    }

    ProtectionCache cache = lwc.getProtectionCache();
    String cacheKey = cache.cacheKey(block.getLocation());

    // In the event they place a block, remove any known nulls there
    if (cache.isKnownNull(cacheKey)) {
        cache.remove(cacheKey);
    }

    Protection protection = lwc.findProtection(block.getLocation());

    if (protection == null) {
        return;
    }

    boolean canAccess = lwc.canAccessProtection(player, protection);
    boolean canAdmin = lwc.canAdminProtection(player, protection);

    // when destroying a chest, it's possible they are also destroying a
    // double chest
    // in the event they're trying to destroy a double chest, we should just
    // move
    // the protection to the chest that is not destroyed, if it is not that
    // one already.
    if (protection.isOwner(player) && DoubleChestMatcher.PROTECTABLES_CHESTS.contains(block.getType())) {
        Block doubleChest = lwc.findAdjacentDoubleChest(block);

        if (doubleChest != null) {
            // if they destroyed the protected block we want to move it aye?
            if (lwc.blockEquals(protection.getBlock(), block)) {
                // correct the block
                BlockCache blockCache = BlockCache.getInstance();
                protection.setBlockId(blockCache.getBlockId(doubleChest));
                protection.setX(doubleChest.getX());
                protection.setY(doubleChest.getY());
                protection.setZ(doubleChest.getZ());
                protection.saveNow();
            }

            // Repair the cache
            protection.radiusRemoveCache();

            if (protection.getProtectionFinder() != null) {
                protection.getProtectionFinder().removeBlock(block.getState());
            }

            lwc.getProtectionCache().addProtection(protection);

            return;
        }
    }

    try {
        LWCProtectionDestroyEvent evt = new LWCProtectionDestroyEvent(player, protection,
                LWCProtectionDestroyEvent.Method.BLOCK_DESTRUCTION, canAccess, canAdmin);
        lwc.getModuleLoader().dispatchEvent(evt);

        if (evt.isCancelled() || !canAccess) {
            event.setCancelled(true);
        }
    } catch (Exception e) {
        event.setCancelled(true);
        lwc.sendLocale(player, "protection.internalerror", "id", "BLOCK_BREAK");
        e.printStackTrace();
    }
}