Search in sources :

Example 1 with PlayerRewardEvent

use of com.bencodez.advancedcore.listeners.PlayerRewardEvent in project AdvancedCore by BenCodez.

the class Reward method giveReward.

public void giveReward(AdvancedCoreUser user, RewardOptions rewardOptions) {
    if (!AdvancedCorePlugin.getInstance().getOptions().isProcessRewards()) {
        AdvancedCorePlugin.getInstance().getLogger().warning("Processing rewards is disabled");
        return;
    }
    if (rewardOptions == null) {
        rewardOptions = new RewardOptions();
    }
    if (!rewardOptions.getPlaceholders().containsKey("ExecDate")) {
        rewardOptions.addPlaceholder("ExecDate", "" + System.currentTimeMillis());
    }
    if (!rewardOptions.getPlaceholders().containsKey("date")) {
        try {
            LocalDateTime ldt = LocalDateTime.now();
            Date date = Date.from(ldt.atZone(ZoneId.systemDefault()).toInstant());
            rewardOptions.addPlaceholder("Date", "" + new SimpleDateFormat(plugin.getOptions().getFormatRewardTimeFormat()).format(date));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    PlayerRewardEvent event = new PlayerRewardEvent(this, user, rewardOptions);
    Bukkit.getPluginManager().callEvent(event);
    if (event.isCancelled()) {
        plugin.debug("Reward " + name + " was cancelled for " + user.getPlayerName());
        return;
    }
    if (rewardOptions.isCheckTimed()) {
        if (checkDelayed(user, rewardOptions.getPlaceholders()) || checkTimed(user, rewardOptions.getPlaceholders())) {
            return;
        }
    }
    if (!rewardOptions.isOnlineSet()) {
        rewardOptions.setOnline(user.isOnline());
    }
    for (RewardPlaceholderHandle handle : plugin.getRewardHandler().getPlaceholders()) {
        if (handle.isPreProcess()) {
            rewardOptions.addPlaceholder(handle.getKey(), handle.getValue(this, user));
        }
    }
    // Check requirements
    boolean allowOffline = false;
    boolean canGive = true;
    if (!rewardOptions.isIgnoreRequirements()) {
        for (RequirementInject inject : plugin.getRewardHandler().getInjectedRequirements()) {
            try {
                plugin.extraDebug(getRewardName() + ": Checking requirement " + inject.getPath() + ":" + inject.getPriority());
                if (!inject.onRequirementRequest(this, user, getConfig().getConfigData(), rewardOptions)) {
                    plugin.debug(getRewardName() + ": Requirement failed " + inject.getPath() + ":" + inject.isAllowReattempt());
                    canGive = false;
                    if (inject.isAllowReattempt()) {
                        allowOffline = true;
                    } else {
                        return;
                    }
                }
            } catch (Exception e) {
                plugin.debug("Failed to check requirement " + inject.getPath());
                e.printStackTrace();
                canGive = false;
            }
        }
    }
    if ((plugin.getOptions().isTreatVanishAsOffline() && user.isVanished())) {
        checkRewardFile();
        user.addOfflineRewards(this, rewardOptions.getPlaceholders());
        plugin.getLogger().info(getRewardName() + ": " + user.getPlayerName() + " is vanished, saving reward offline");
        return;
    }
    // save reward for offline
    if (((((!rewardOptions.isOnline() || rewardOptions.getServer() != null) && !user.isOnline()) || allowOffline) && (!isForceOffline() && !rewardOptions.isForceOffline()))) {
        if (rewardOptions.isGiveOffline()) {
            checkRewardFile();
            user.addOfflineRewards(this, rewardOptions.getPlaceholders());
            plugin.debug("Saving offline reward " + getRewardName() + " for " + user.getPlayerName());
        }
        return;
    }
    // give reward
    if (canGive || isForceOffline() || rewardOptions.isForceOffline()) {
        plugin.debug(name + ": Passed requirements, attempting to give to " + user.getPlayerName() + "/" + user.getUUID());
        giveRewardUser(user, rewardOptions.getPlaceholders(), rewardOptions);
    }
}
Also used : LocalDateTime(java.time.LocalDateTime) PlayerRewardEvent(com.bencodez.advancedcore.listeners.PlayerRewardEvent) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) RequirementInject(com.bencodez.advancedcore.api.rewards.injectedrequirement.RequirementInject)

Aggregations

RequirementInject (com.bencodez.advancedcore.api.rewards.injectedrequirement.RequirementInject)1 PlayerRewardEvent (com.bencodez.advancedcore.listeners.PlayerRewardEvent)1 SimpleDateFormat (java.text.SimpleDateFormat)1 LocalDateTime (java.time.LocalDateTime)1 Date (java.util.Date)1