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

The following examples show how to use com.griefcraft.model.Protection#getBlock() . 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: FixModule.java    From Modern-LWC with MIT License 6 votes vote down vote up
@Override
public void onProtectionInteract(LWCProtectionInteractEvent event) {
    if (event.getResult() == Result.CANCEL) {
        return;
    }

    if (!event.hasAction("fix")) {
        return;
    }

    LWC lwc = event.getLWC();
    LWCPlayer player = lwc.wrapPlayer(event.getPlayer());
    Protection protection = event.getProtection();
    Block block = protection.getBlock();

    if (!lwc.canAdminProtection(event.getPlayer(), protection)) {
        return;
    }

    // Should we fix orientation?
    if (DoubleChestMatcher.PROTECTABLES_CHESTS.contains(block.getType()) || block.getType() == Material.FURNACE || block.getType() == Material.DISPENSER) {
        lwc.adjustChestDirection(block, event.getEvent().getBlockFace());
        lwc.sendLocale(player, "lwc.fix.fixed", "block", block.getType().toString().toLowerCase());
        player.removeAction(player.getAction("fix"));
    }
}
 
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: EconomyModule.java    From Modern-LWC with MIT License 4 votes vote down vote up
@Override
public void onPostRegistration(LWCProtectionRegistrationPostEvent event) {
    if (!configuration.getBoolean("Economy.enabled", true))
        return;

    if (!LWC.getInstance().isHistoryEnabled())
        return;

    Protection protection = event.getProtection();

    // we need to inject the iconomy price into the transaction!
    Block block = protection.getBlock();

    // Uh-oh! This REALLY should never happen ... !
    if (block == null || !priceCache.containsKey(block.getLocation()))
        return;

    Location location = block.getLocation();

    // okey, get how much they were charged
    String cachedPrice = priceCache.get(location);

    boolean usedDiscount = cachedPrice.startsWith("d");
    double charge = Double.parseDouble(usedDiscount ? cachedPrice.substring(1) : cachedPrice);

    // get related transactions..
    List<History> transactions = protection.getRelatedHistory(History.Type.TRANSACTION);

    // this really should not happen either (never!)
    if (transactions.size() == 0)
        logger.severe("LWC-Economy POST_REGISTRATION encountered a severe problem!: transactions.size() == 0");

    // get the last entry
    History history = transactions.get(transactions.size() - 1);

    // add the price
    history.addMetaData("charge=" + charge);

    // was it a discount?
    if (usedDiscount) {
        history.addMetaData("discount=true");

        // Was the discount's id non-null?
        String discountId = resolveValue(protection.getBukkitOwner(), "discount.id");

        if (!discountId.isEmpty())
            history.addMetaData("discountId=" + discountId);
    }

    // save it immediately
    history.saveNow();

    // we no longer need the value in the price cache :)
    priceCache.remove(location);
}
 
Example 4
Source File: FreeModule.java    From Modern-LWC with MIT License 4 votes vote down vote up
@Override
public void onProtectionInteract(LWCProtectionInteractEvent event) {
    if (event.getResult() != Result.DEFAULT) {
        return;
    }

    if (!event.hasAction("free")) {
        return;
    }

    LWC lwc = event.getLWC();
    Protection protection = event.getProtection();
    Player player = event.getPlayer();
    event.setResult(Result.CANCEL);

    /* Due to treating entities as blocks some issues are triggered
     *  such as when clicking an armor stand the block is air.
     *  I feel there is something better to be done here but this works */
    if (protection.getBlock().getType() == Material.AIR) {
        if (!protection.toString().contains("ARMOR_STAND")) {
            return;
        }
    }

    if (!lwc.isAdmin(player)
            && Boolean.parseBoolean(lwc.resolveProtectionConfiguration(protection.getBlock(), "readonly-remove"))) {
        lwc.sendLocale(player, "protection.accessdenied");
        return;
    }

    if (lwc.hasAdminPermission(player, "lwc.admin.remove") || protection.isOwner(player)) {
        LWCProtectionDestroyEvent evt = new LWCProtectionDestroyEvent(player, protection,
                LWCProtectionDestroyEvent.Method.COMMAND, true, true);
        lwc.getModuleLoader().dispatchEvent(evt);

        if (!evt.isCancelled()) {
            // bind the player of destroyed the protection
            // We don't need to save the history we modify because it will
            // be saved anyway immediately after this
            for (History history : protection.getRelatedHistory(History.Type.TRANSACTION)) {
                if (history.getStatus() != History.Status.ACTIVE) {
                    continue;
                }

                history.addMetaData("destroyer=" + player.getName());
                history.addMetaData("destroyerTime=" + System.currentTimeMillis() / 1000L);
            }

            protection.remove();
            if (protection.getBlock() instanceof EntityBlock) {
                lwc.sendLocaleToActionBar(player, "protection.interact.remove.finalize", "block",
                        EntityBlock.getEntity().getType().name());
            } else {
                lwc.sendLocaleToActionBar(player, "protection.interact.remove.finalize", "block",
                        !protection.toString().contains("ARMOR_STAND") ?
                                LWC.materialToString(protection.getBlock()) : "ARMOR_STAND");
            }
        }

        lwc.removeModes(player);
    } else {
        if (protection.getBlock() instanceof EntityBlock) {
            lwc.sendLocale(player, "protection.interact.error.notowner", "block",
                    EntityBlock.getEntity().getType().name());
        } else {
            lwc.sendLocale(player, "protection.interact.error.notowner", "block",
                    LWC.materialToString(protection.getBlock()));
        }
        lwc.removeModes(player);
    }
}