-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathEconomyHook.java
More file actions
136 lines (110 loc) · 5.15 KB
/
Copy pathEconomyHook.java
File metadata and controls
136 lines (110 loc) · 5.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package at.helpch.placeholderapi.expansion.vault;
import com.google.common.primitives.Ints;
import net.milkbowl.vault.economy.Economy;
import org.bukkit.OfflinePlayer;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.NavigableMap;
import java.util.TreeMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class EconomyHook extends VaultHook {
private static final Pattern BALANCE_DECIMAL_POINTS_PATTERN = Pattern.compile("balance_(?<points>\\d+)dp");
private static final DecimalFormat COMMAS_FORMAT = new DecimalFormat("#,###");
private static final DecimalFormat FIXED_FORMAT = new DecimalFormat("#");
private static final Map<Integer, DecimalFormat> DECIMAL_FORMATS_CACHE = new HashMap<>();
private final NavigableMap<BigInteger, String> suffixes = new TreeMap<>();
private Economy economy;
public EconomyHook(VaultExpansion expansion) {
super(expansion);
suffixes.put(IntegerUtil.getBigInteger("1_000"), expansion.getString("formatting.thousands", "K"));
suffixes.put(IntegerUtil.getBigInteger("1_000_000"), expansion.getString("formatting.millions", "M"));
suffixes.put(IntegerUtil.getBigInteger("1_000_000_000"), expansion.getString("formatting.billions", "B"));
suffixes.put(IntegerUtil.getBigInteger("1_000_000_000_000"), expansion.getString("formatting.trillions", "T"));
suffixes.put(IntegerUtil.getBigInteger("1_000_000_000_000_000"), expansion.getString("formatting.quadrillions", "Q"));
suffixes.put(IntegerUtil.getBigInteger("1_000_000_000_000_000_000"), expansion.getString("formatting.quintillion", "Qi"));
suffixes.put(IntegerUtil.getBigInteger("1_000_000_000_000_000_000_000"), expansion.getString("formatting.sextillion", "Sx"));
suffixes.put(IntegerUtil.getBigInteger("1_000_000_000_000_000_000_000_000"), expansion.getString("formatting.septillion", "Sp"));
setup();
}
private BigDecimal getBalance(@NotNull final OfflinePlayer player) {
return IntegerUtil.getBigDecimal(economy.getBalance(player));
}
private @NotNull String setDecimalPoints(double balance, int points) {
final DecimalFormat cachedFormat = DECIMAL_FORMATS_CACHE.get(points);
if (cachedFormat != null) {
return cachedFormat.format(balance);
}
final DecimalFormat decimalFormat = (DecimalFormat) DecimalFormat.getIntegerInstance();
decimalFormat.setMaximumFractionDigits(points);
decimalFormat.setGroupingUsed(false);
DECIMAL_FORMATS_CACHE.put(points, decimalFormat);
return decimalFormat.format(balance);
}
/**
* Format player's balance, 1200 -> 1.2K
*
* @param balance balance to format
* @return balance formatted
* @author <a href="https://stackoverflow.com/users/829571/assylias">assylias</a> (<a href="https://stackoverflow.com/a/30661479/11496439">source</a>)
*/
private @NotNull String formatBalance(BigDecimal balance) {
if (balance.compareTo(BigDecimal.ZERO) == 0) {
return "0";
}
BigInteger intPart = new BigInteger(balance.toPlainString().split("\\.")[0]);
Map.Entry<BigInteger, String> e = suffixes.floorEntry(intPart);
if (e == null) {
return balance.stripTrailingZeros().toPlainString();
}
BigDecimal divideBy = new BigDecimal(e.getKey());
String suffix = e.getValue();
BigDecimal result = balance.divide(divideBy, 3, RoundingMode.DOWN).stripTrailingZeros();
return result.toPlainString() + suffix;
}
@Override
public void setup() {
economy = getService(Economy.class);
}
@Override
public boolean isReady() {
return economy != null;
}
@SuppressWarnings("UnstableApiUsage")
@Override
public @Nullable String onRequest(@Nullable OfflinePlayer offlinePlayer, @NotNull String params) {
if (offlinePlayer == null) {
return "";
}
final BigDecimal balance = getBalance(offlinePlayer);
if (params.startsWith("balance_")) {
final Matcher matcher = BALANCE_DECIMAL_POINTS_PATTERN.matcher(params);
if (matcher.find()) {
final Integer points = Ints.tryParse(matcher.group("points"));
if (points == null) {
return matcher.group("points") + " is not a valid number";
}
return setDecimalPoints(balance.doubleValue(), points);
}
}
switch (params) {
case "balance":
return setDecimalPoints(balance.doubleValue(), Math.max(2, economy.fractionalDigits()));
case "balance_fixed":
return FIXED_FORMAT.format(balance);
case "balance_formatted": {
return formatBalance(balance);
}
case "balance_commas":
return COMMAS_FORMAT.format(balance);
default:
return null;
}
}
}