Java Code Examples for com.griefcraft.lwc.LWC#getProtectionCache()

The following examples show how to use com.griefcraft.lwc.LWC#getProtectionCache() . 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: AdminCache.java    From Modern-LWC with MIT License 5 votes vote down vote up
@Override
public void onCommand(LWCCommandEvent event) {
    if (event.isCancelled()) {
        return;
    }

    if (!event.hasFlag("a", "admin")) {
        return;
    }

    LWC lwc = event.getLWC();
    CommandSender sender = event.getSender();
    String[] args = event.getArgs();

    if (!args[0].equals("cache")) {
        return;
    }

    // we have the right command
    event.setCancelled(true);
    ProtectionCache cache = lwc.getProtectionCache();

    if (args.length > 1) {
        String cmd = args[1].toLowerCase();

        if (cmd.equals("clear")) {
            cache.clear();
            lwc.sendLocale(sender, "lwc.admin.caches.cleared");
        }
    } else {
        int size = cache.size();
        int capacity = cache.capacity();

        sender.sendMessage(Colors.Dark_Green + size + Colors.Yellow + "/" + Colors.Dark_Green + capacity);
    }
}
 
Example 2
Source File: PhysDB.java    From Modern-LWC with MIT License 5 votes vote down vote up
/**
 * Fill the protection cache as much as possible with protections Caches the
 * most recent protections
 */
public void precache() {
    LWC lwc = LWC.getInstance();
    ProtectionCache cache = lwc.getProtectionCache();

    // clear the cache incase we're working on a dirty cache
    cache.clear();

    int precacheSize = lwc.getConfiguration().getInt("core.precache", -1);

    if (precacheSize == -1) {
        precacheSize = lwc.getConfiguration().getInt("core.cacheSize", 10000);
    }
    try {
        statement = prepare(
                "SELECT id, owner, type, x, y, z, data, blockId, world, password, date, last_accessed FROM "
                        + prefix + "protections ORDER BY id DESC LIMIT ?");
        statement.setInt(1, precacheSize);
        statement.setFetchSize(10);

        // scrape the protections from the result set now
        List<Protection> protections = resolveProtections(statement);

        // throw all of the protections in
        for (Protection protection : protections) {
            cache.addProtection(protection);
        }

    } catch (SQLException e) {
        printException(e);
    }
}
 
Example 3
Source File: LWCBlockListener.java    From Modern-LWC with MIT License 5 votes vote down vote up
@SuppressWarnings("deprecation")
@EventHandler(ignoreCancelled = true)
public void onBlockPlace(BlockPlaceEvent event) {
    if (!LWC.ENABLED) {
        return;
    }

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

    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);
    }

    // check if the block is blacklisted
    if (blacklistedBlocks.contains(block.getType())) {
        // it's blacklisted, check for a protected chest
        for (Protection protection : lwc.findAdjacentProtectionsOnAllSides(block)) {
            if (protection != null) {
                if (!lwc.canAccessProtection(player, protection)
                        || (protection.getType() == Protection.Type.DONATION
                        && !lwc.canAdminProtection(player, protection))) {
                    // they can't access the protection ..
                    event.setCancelled(true);
                    return;
                }
            }
        }
    }
}
 
Example 4
Source File: Statistics.java    From Modern-LWC with MIT License 4 votes vote down vote up
/**
 * Send a performance report to a Console Sender
 *
 * @param sender
 */
public static void sendReport(CommandSender sender) {
    LWC lwc = LWC.getInstance();

    sender.sendMessage(" ");
    sender.sendMessage(Colors.Dark_Red + "LWC Report");
    sender.sendMessage("  Version: " + Colors.Dark_Green + LWCInfo.FULL_VERSION);
    sender.sendMessage("  Running time: " + Colors.Dark_Green + TimeUtil.timeToString(getTimeRunningSeconds()));
    sender.sendMessage("  Players: " + Colors.Dark_Green + Bukkit.getServer().getOnlinePlayers().size() + "/" + Bukkit.getServer().getMaxPlayers());
    sender.sendMessage("  Item entities: " + Colors.Dark_Green + getEntityCount(Item.class) + "/" + getEntityCount(null));
    sender.sendMessage("  Permissions API: " + Colors.Dark_Green + lwc.getPermissions().getClass().getSimpleName());
    sender.sendMessage("  Currency API: " + Colors.Dark_Green + lwc.getCurrency().getClass().getSimpleName());
    sender.sendMessage(" ");
    sender.sendMessage(Colors.Dark_Red + " ==== Modules ====");

    for (Map.Entry<Plugin, List<MetaData>> entry : lwc.getModuleLoader().getRegisteredModules().entrySet()) {
        Plugin plugin = entry.getKey();
        List<MetaData> modules = entry.getValue();

        // Why?
        if (plugin == null) {
            continue;
        }

        sender.sendMessage("  " + Colors.Dark_Green + plugin.getDescription().getName() + " v" + plugin.getDescription().getVersion() + Colors.Yellow + " -> " + Colors.Dark_Green + modules.size() + Colors.Yellow + " registered modules");
    }
    sender.sendMessage(" ");

    sender.sendMessage(Colors.Dark_Red + " ==== Database ====");
    sender.sendMessage("  Engine: " + Colors.Dark_Green + Database.DefaultType);
    sender.sendMessage("  Protections: " + Colors.Dark_Green + formatNumber(lwc.getPhysicalDatabase().getProtectionCount()));
    sender.sendMessage("  Queries: " + Colors.Dark_Green + formatNumber(queries) + " | " + String.format("%.2f", getAverage(queries)) + " / second");
    sender.sendMessage(" ");

    sender.sendMessage(Colors.Dark_Red + " ==== Cache ==== ");

    ProtectionCache cache = lwc.getProtectionCache();

    double cachePercentFilled = ((double) cache.size() / cache.totalCapacity()) * 100;

    String cacheColour = Colors.Dark_Green;
    if (cachePercentFilled > 75 && cachePercentFilled < 85) {
        cacheColour = Colors.Yellow;
    } else if (cachePercentFilled > 85 && cachePercentFilled < 95) {
        cacheColour = Colors.Red;
    } else if (cachePercentFilled > 95) {
        cacheColour = Colors.Dark_Red;
    }

    sender.sendMessage("  Usage: " + cacheColour + String.format("%.2f", cachePercentFilled) + "% " + Colors.White + " ( " + cache.size() + "/" + cache.totalCapacity() + " [" + cache.capacity() + "+" + cache.adaptiveCapacity() + "] )");
    sender.sendMessage("  Profile: ");
    sendMethodCounter(sender, cache.getMethodCounter());
    // sender.sendMessage("  Reads: " + formatNumber(cache.getReads()) + " | " + String.format("%.2f", getAverage(cache.getReads())) + " / second");
    // sender.sendMessage("  Writes: " + formatNumber(cache.getWrites()) + " | " + String.format("%.2f", getAverage(cache.getWrites())) + " / second");
}
 
Example 5
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 6
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();
    }
}
 
Example 7
Source File: LWCEntityListener.java    From Modern-LWC with MIT License 4 votes vote down vote up
@EventHandler(ignoreCancelled = true)
public void onEntityDamageByEntity(EntityDamageByEntityEvent event) {
    if (!LWC.ENABLED || event.isCancelled()) {
        return;
    }

    LWC lwc = plugin.getLWC();

    if (event.getDamager() instanceof Player) {
        Player player = (Player) event.getDamager();
        Entity entity = event.getEntity();
        EntityBlock entityBlock = new EntityBlock(entity);

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

        if (ignoreBlockDestruction) {
            return;
        }

        if (event.getEntityType().equals(EntityType.ARMOR_STAND)) {
            if (event.getDamage() < 1.0 ||
                    ((Player) event.getDamager()).getGameMode().equals(GameMode.CREATIVE)) { // Armor Stand Broke
                ProtectionCache cache = lwc.getProtectionCache();
                String cacheKey = cache.cacheKey(entityBlock.getLocation());

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

                Protection protection = lwc.findProtection(entityBlock);

                if (protection == null) {
                    return;
                }

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

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

                    protection.remove();
                    protection.removeAllPermissions();
                    protection.removeCache();

                    if (evt.isCancelled() || !canAccess) {
                        event.setCancelled(true);
                    }
                } catch (Exception e) {
                    event.setCancelled(true);
                    lwc.sendLocale(player, "protection.internalerror", "id", "BLOCK_BREAK");
                    e.printStackTrace();
                }
            }
            /*else { // Armor Stand Punched
                LWC.getInstance().log("Armor Stand Punched");
                if(plugin.getLWC().isProtectable(entity.getType())){
                    int A = 50000 + entity.getUniqueId().hashCode();
                    Protection protection = lwc.getPhysicalDatabase().loadProtection(entity.getWorld().getName(), A, A, A);
                    boolean canAccess = lwc.canAccessProtection(player, protection);
                    boolean canAdmin = lwc.canAdminProtection(player, protection);
                    Set<String> actions = lwc.wrapPlayer(player).getActionNames();
                    Module.Result result = Module.Result.CANCEL;

                    // TODO: Finish this implementation
                    if (protection != null) {
                        LWCEntityDamageByEntityEvent evt =
                                new LWCEntityDamageByEntityEvent(event, protection, actions, canAccess, canAdmin);
                        lwc.getModuleLoader().dispatchEvent(evt);

                        result = evt.getResult();
                    } else {

                    }
                    if (result == Module.Result.ALLOW) {
                        return;
                    }
                    if (player.hasPermission("lwc.lockentity." + entity.getType()) || player.hasPermission("lwc.lockentity.all")) {
                        if (onPlayerEntityInteract(p, entity, e.isCancelled())) {
                            chunkUnload(entity.getWorld().getName(), A);
                            e.setCancelled(true);
                        }
                    }
                    if (protection != null) {
                        if (canAccess)
                            return;
                        e.setCancelled(true);
                    }
                }
            }*/
        }
    }


}