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

The following examples show how to use com.griefcraft.model.Protection#getRelatedHistory() . 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: EconomyModule.java    From Modern-LWC with MIT License 4 votes vote down vote up
@Override
public void onDestroyProtection(LWCProtectionDestroyEvent event) {
    if (event.isCancelled())
        return;

    if (!configuration.getBoolean("Economy.enabled", true))
        return;

    // is refunding enabled?
    if (!configuration.getBoolean("Economy.refunds", true))
        return;

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

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

    // first, do we still have a currency processor?
    if (!lwc.getCurrency().isActive())
        return;

    // Does it support the server bank feature
    if (!lwc.getCurrency().usingCentralBank())
        return;

    // load the transactions so we can check the server bank
    List<History> transactions = protection.getRelatedHistory(History.Type.TRANSACTION);

    for (History history : transactions) {
        if (history.getStatus() == History.Status.INACTIVE)
            continue;

        // obtain the charge
        double charge = history.getDouble("charge");

        // No need to refund if it's negative or 0
        if (charge <= 0)
            continue;

        // check the server bank
        if (!lwc.getCurrency().canCentralBankAfford(charge)) {
            // TODO: Extract string to string.yml
            player.sendMessage(Colors.Dark_Red + "The Server's Bank does not contain enough funds to remove that protection!");
            event.setCancelled(true);
            return;
        }
    }
}
 
Example 2
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 3
Source File: EconomyModule.java    From Modern-LWC with MIT License 4 votes vote down vote up
@Override
public void onPostRemoval(LWCProtectionRemovePostEvent event) {
    if (!configuration.getBoolean("Economy.enabled", true))
        return;

    // is refunding enabled?
    if (!configuration.getBoolean("Economy.refunds", true))
        return;

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

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

    // first, do we still have a currency processor?
    if (!lwc.getCurrency().isActive())
        return;

    // we need to refund them, load up transactions
    List<History> transactions = protection.getRelatedHistory(History.Type.TRANSACTION);

    for (History history : transactions) {
        if (history.getStatus() == History.Status.INACTIVE)
            continue;

        // obtain the charge
        double charge = history.getDouble("charge");

        // No need to refund if it's negative or 0
        if (charge <= 0)
            continue;

        // refund them :)
        Player owner = Bukkit.getServer().getPlayer(history.getPlayer());

        // we can't pay them ..
        if (owner == null)
            continue;

        // the currency to use
        ICurrency currency = lwc.getCurrency();

        currency.addMoney(owner, charge);
        // TODO: Extract string to string.yml
        owner.sendMessage(Colors.Dark_Green + "You have been refunded " + currency.format(charge) + " because an LWC protection of yours was removed!");
    }
}
 
Example 4
Source File: DestroyModule.java    From Modern-LWC with MIT License 4 votes vote down vote up
@Override
public void onDestroyProtection(LWCProtectionDestroyEvent event) {

    if (event.isCancelled()) {
        return;
    }

    if (event.getMethod() != LWCProtectionDestroyEvent.Method.BLOCK_DESTRUCTION &&
            event.getMethod() != LWCProtectionDestroyEvent.Method.ENTITY_DESTRUCTION) {
        return;
    }

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

    boolean isOwner = protection.isOwner(player);

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

        // 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 (!Boolean.parseBoolean(lwc.resolveProtectionConfiguration(protection.getBlock(), "quiet"))) {
            BlockCache blockCache = BlockCache.getInstance();
            lwc.sendLocaleToActionBar(player, "protection.unregistered", "block",
                    LWC.materialToString(blockCache.getBlockType(protection.getBlockId())));
        }
        return;
    }

    event.setCancelled(true);
}
 
Example 5
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);
    }
}
 
Example 6
Source File: FreeModule.java    From Modern-LWC with MIT License 4 votes vote down vote up
@Override
public void onEntityInteractProtection(LWCProtectionInteractEntityEvent 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);

    if (protection.getBlock().getType() != Material.AIR) {
        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();
            lwc.sendLocaleToActionBar(player, "protection.interact.remove.finalize", "block",
                    event.getEvent().getRightClicked().getType().name());
        }

        lwc.removeModes(player);
    } else {
        lwc.sendLocale(player, "protection.interact.error.notowner", "block",
                event.getEvent().getRightClicked().getType().name());
        lwc.removeModes(player);
    }
}