Search in sources :

Example 1 with ChallengeCompleteEvent

use of com.wasteofplastic.acidisland.events.ChallengeCompleteEvent in project acidisland by tastybento.

the class Challenges method giveReward.

/**
 * Gives the reward for completing the challenge
 *
 * @param player
 * @param challenge
 * @return ture if reward given successfully
 */
private boolean giveReward(final Player player, final String challenge) {
    // Grab the rewards from the config.yml file
    String[] permList;
    String[] itemRewards;
    double moneyReward = 0;
    int expReward = 0;
    String rewardText = "";
    // If the friendly name is available use it
    String challengeName = ChatColor.translateAlternateColorCodes('&', getChallengeConfig().getString("challenges.challengeList." + challenge + ".friendlyname", challenge.substring(0, 1).toUpperCase() + challenge.substring(1)));
    // If player has done a challenge already, the rewards are different
    if (!plugin.getPlayers().checkChallenge(player.getUniqueId(), challenge)) {
        // First time
        Util.sendMessage(player, ChatColor.GREEN + plugin.myLocale(player.getUniqueId()).challengesyouHaveCompleted.replace("[challenge]", challengeName));
        if (Settings.broadcastMessages) {
            for (Player p : plugin.getServer().getOnlinePlayers()) {
                Util.sendMessage(p, ChatColor.GOLD + plugin.myLocale(p.getUniqueId()).challengesnameHasCompleted.replace("[name]", player.getName()).replace("[challenge]", challengeName));
            }
        }
        plugin.getMessages().tellOfflineTeam(player.getUniqueId(), ChatColor.GOLD + plugin.myLocale(player.getUniqueId()).challengesnameHasCompleted.replace("[name]", player.getName()).replace("[challenge]", challengeName));
        itemRewards = getChallengeConfig().getString("challenges.challengeList." + challenge.toLowerCase() + ".itemReward", "").split(" ");
        moneyReward = getChallengeConfig().getDouble("challenges.challengeList." + challenge.toLowerCase() + ".moneyReward", 0D);
        rewardText = ChatColor.translateAlternateColorCodes('&', getChallengeConfig().getString("challenges.challengeList." + challenge.toLowerCase() + ".rewardText", "Goodies!"));
        expReward = getChallengeConfig().getInt("challenges.challengeList." + challenge + ".expReward", 0);
    } else {
        // Repeat challenge
        Util.sendMessage(player, ChatColor.GREEN + plugin.myLocale(player.getUniqueId()).challengesyouRepeated.replace("[challenge]", challengeName));
        itemRewards = getChallengeConfig().getString("challenges.challengeList." + challenge.toLowerCase() + ".repeatItemReward", "").split(" ");
        moneyReward = getChallengeConfig().getDouble("challenges.challengeList." + challenge.toLowerCase() + ".repeatMoneyReward", 0);
        rewardText = ChatColor.translateAlternateColorCodes('&', getChallengeConfig().getString("challenges.challengeList." + challenge.toLowerCase() + ".repeatRewardText", "Goodies!"));
        expReward = getChallengeConfig().getInt("challenges.challengeList." + challenge + ".repeatExpReward", 0);
    }
    // Report the rewards and give out exp, money and permissions if
    // appropriate
    Util.sendMessage(player, ChatColor.GOLD + plugin.myLocale(player.getUniqueId()).challengesrewards + ": " + ChatColor.WHITE + rewardText);
    if (expReward > 0) {
        Util.sendMessage(player, ChatColor.GOLD + plugin.myLocale(player.getUniqueId()).challengesexpReward + ": " + ChatColor.WHITE + expReward);
        player.giveExp(expReward);
    }
    if (Settings.useEconomy && moneyReward > 0 && (VaultHelper.econ != null)) {
        EconomyResponse e = VaultHelper.econ.depositPlayer(player, Settings.worldName, moneyReward);
        if (e.transactionSuccess()) {
            Util.sendMessage(player, ChatColor.GOLD + plugin.myLocale(player.getUniqueId()).challengesmoneyReward + ": " + ChatColor.WHITE + VaultHelper.econ.format(moneyReward));
        } else {
            plugin.getLogger().severe("Error giving player " + player.getUniqueId() + " challenge money:" + e.errorMessage);
            plugin.getLogger().severe("Reward was $" + moneyReward);
        }
    }
    // Dole out permissions
    // plugin.getLogger().info("DEBUG: dole out permissions");
    permList = getChallengeConfig().getString("challenges.challengeList." + challenge.toLowerCase() + ".permissionReward", "").split(" ");
    for (final String s : permList) {
        if (!s.isEmpty()) {
            VaultHelper.addPerm(player, s);
            plugin.getLogger().info("Added permission " + s + " to " + player.getName() + "");
        }
    }
    // Give items
    List<ItemStack> rewardedItems = giveItems(player, itemRewards);
    if (rewardedItems == null) {
        return false;
    }
    // Run reward commands
    if (!plugin.getPlayers().checkChallenge(player.getUniqueId(), challenge)) {
        // First time
        List<String> commands = getChallengeConfig().getStringList("challenges.challengeList." + challenge.toLowerCase() + ".rewardcommands");
        runCommands(player, commands);
    } else {
        // Repeat challenge
        List<String> commands = getChallengeConfig().getStringList("challenges.challengeList." + challenge.toLowerCase() + ".repeatrewardcommands");
        runCommands(player, commands);
    }
    // Mark the challenge as complete
    // if (!plugin.getPlayers().checkChallenge(player.getUniqueId(),challenge)) {
    plugin.getPlayers().completeChallenge(player.getUniqueId(), challenge);
    // }
    // Call the Challenge Complete Event
    final ChallengeCompleteEvent event = new ChallengeCompleteEvent(player, challenge, permList, itemRewards, moneyReward, expReward, rewardText, rewardedItems);
    plugin.getServer().getPluginManager().callEvent(event);
    return true;
}
Also used : Player(org.bukkit.entity.Player) EconomyResponse(net.milkbowl.vault.economy.EconomyResponse) ChallengeCompleteEvent(com.wasteofplastic.acidisland.events.ChallengeCompleteEvent) ItemStack(org.bukkit.inventory.ItemStack)

Aggregations

ChallengeCompleteEvent (com.wasteofplastic.acidisland.events.ChallengeCompleteEvent)1 EconomyResponse (net.milkbowl.vault.economy.EconomyResponse)1 Player (org.bukkit.entity.Player)1 ItemStack (org.bukkit.inventory.ItemStack)1