|
| 1 | +package net.leaderos.plugin.modules.communitygoal; |
| 2 | + |
| 3 | +import me.clip.placeholderapi.expansion.PlaceholderExpansion; |
| 4 | +import net.leaderos.plugin.Bukkit; |
| 5 | +import net.leaderos.plugin.configuration.Modules; |
| 6 | +import net.leaderos.plugin.modules.communitygoal.managers.CommunityGoalManager; |
| 7 | +import net.leaderos.plugin.modules.communitygoal.model.CommunityGoal; |
| 8 | +import net.leaderos.plugin.helpers.ChatUtil; |
| 9 | +import org.bukkit.entity.Player; |
| 10 | + |
| 11 | +import java.text.DecimalFormat; |
| 12 | +import java.text.DecimalFormatSymbols; |
| 13 | +import java.util.Locale; |
| 14 | + |
| 15 | +/** |
| 16 | + * Community Goal module placeholder system |
| 17 | + * @author leaderos |
| 18 | + * @since 1.0 |
| 19 | + */ |
| 20 | +public class Placeholders extends PlaceholderExpansion { |
| 21 | + |
| 22 | + /** |
| 23 | + * identifier of placeholder |
| 24 | + * @return identifier |
| 25 | + */ |
| 26 | + public String getIdentifier() { |
| 27 | + return "leaderos-communitygoal"; |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * The author of the Placeholder |
| 32 | + * This cannot be null |
| 33 | + * @return author name |
| 34 | + */ |
| 35 | + public String getAuthor() { |
| 36 | + return "leaderos"; |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Same with #getAuthor() but for versioon |
| 41 | + * This cannot be null |
| 42 | + * @return version of plugin |
| 43 | + */ |
| 44 | + public String getVersion() { |
| 45 | + return "1.0"; |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * PlaceholderRequest method |
| 50 | + * @param p player |
| 51 | + * @param identifier placeholder identifier |
| 52 | + * @return placeholder value |
| 53 | + */ |
| 54 | + @Override |
| 55 | + public String onPlaceholderRequest(Player p, String identifier) { |
| 56 | + CommunityGoal communityGoal = CommunityGoalManager.getCommunityGoal(); |
| 57 | + if (communityGoal == null) |
| 58 | + return null; |
| 59 | + |
| 60 | + if (identifier.equals("is_active")) { |
| 61 | + return String.valueOf(communityGoal.isActive()); |
| 62 | + } |
| 63 | + else if (identifier.equals("currency")) { |
| 64 | + return communityGoal.getCurrency(); |
| 65 | + } |
| 66 | + else if (identifier.equals("total_raised")) { |
| 67 | + return formatDouble(communityGoal.getTotalRaised()); |
| 68 | + } |
| 69 | + else if (identifier.equals("goal_amount")) { |
| 70 | + return formatDouble(communityGoal.getGoalAmount()); |
| 71 | + } |
| 72 | + else if (identifier.equals("percentage_achieved")) { |
| 73 | + return formatDouble(communityGoal.getPercentageAchieved()); |
| 74 | + } |
| 75 | + else if (identifier.equals("progress_bar")) { |
| 76 | + return getProgressBar(communityGoal.getPercentageAchieved()); |
| 77 | + } |
| 78 | + |
| 79 | + return null; |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Format double value |
| 84 | + * @param value double value |
| 85 | + * @return formatted string |
| 86 | + */ |
| 87 | + private String formatDouble(double value) { |
| 88 | + DecimalFormatSymbols symbols = new DecimalFormatSymbols(Locale.ENGLISH); |
| 89 | + DecimalFormat decimalFormat = new DecimalFormat("0.00", symbols); |
| 90 | + String formatted = decimalFormat.format(value); |
| 91 | + if (formatted.endsWith(".00")) { |
| 92 | + return formatted.substring(0, formatted.length() - 3); |
| 93 | + } |
| 94 | + return formatted; |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Get progress bar |
| 99 | + * @param percentage percentage |
| 100 | + * @return progress bar |
| 101 | + */ |
| 102 | + private String getProgressBar(double percentage) { |
| 103 | + Modules.CommunityGoal.ProgressBar progressBar = Bukkit.getInstance().getModulesFile().getCommunityGoal().getProgressBar(); |
| 104 | + int length = progressBar.getLength(); |
| 105 | + String symbol = progressBar.getSymbol(); |
| 106 | + String filledColor = progressBar.getFilledColor(); |
| 107 | + String emptyColor = progressBar.getEmptyColor(); |
| 108 | + |
| 109 | + int filledAmount = (int) (percentage / 100 * length); |
| 110 | + if (filledAmount > length) |
| 111 | + filledAmount = length; |
| 112 | + |
| 113 | + StringBuilder sb = new StringBuilder(); |
| 114 | + sb.append(filledColor); |
| 115 | + for (int i = 0; i < filledAmount; i++) { |
| 116 | + sb.append(symbol); |
| 117 | + } |
| 118 | + sb.append(emptyColor); |
| 119 | + for (int i = 0; i < length - filledAmount; i++) { |
| 120 | + sb.append(symbol); |
| 121 | + } |
| 122 | + |
| 123 | + return ChatUtil.color(sb.toString()); |
| 124 | + } |
| 125 | +} |
0 commit comments