|
28 | 28 | import com.google.common.primitives.Ints; |
29 | 29 | import me.clip.placeholderapi.PlaceholderAPIPlugin; |
30 | 30 | import me.clip.placeholderapi.expansion.PlaceholderExpansion; |
| 31 | +import net.essentialsx.api.v2.services.BalanceTop; |
31 | 32 | import org.bukkit.*; |
32 | 33 | import org.bukkit.entity.Player; |
33 | 34 | import org.bukkit.inventory.ItemStack; |
34 | 35 | import org.jetbrains.annotations.NotNull; |
35 | 36 |
|
36 | 37 | import java.math.BigDecimal; |
37 | 38 | import java.text.DateFormat; |
| 39 | +import java.text.DecimalFormat; |
38 | 40 | import java.text.NumberFormat; |
39 | 41 | import java.time.Instant; |
40 | 42 | import java.time.temporal.ChronoUnit; |
41 | 43 | import java.util.Date; |
| 44 | +import java.util.Locale; |
| 45 | +import java.util.Map; |
| 46 | +import java.util.UUID; |
42 | 47 | import java.util.stream.StreamSupport; |
43 | 48 |
|
44 | 49 | public class EssentialsExpansion extends PlaceholderExpansion { |
45 | 50 |
|
| 51 | + private String k; |
| 52 | + private String m; |
| 53 | + private String b; |
| 54 | + private String t; |
| 55 | + private String q; |
| 56 | + private final DecimalFormat format = new DecimalFormat("#,###"); |
| 57 | + |
46 | 58 | private Essentials essentials; |
| 59 | + private BalanceTop baltop; |
47 | 60 |
|
48 | 61 | private final String VERSION = getClass().getPackage().getImplementationVersion(); |
49 | 62 |
|
50 | 63 | @Override |
51 | 64 | public boolean canRegister() { |
52 | | - return Bukkit.getPluginManager().getPlugin("Essentials") != null; |
| 65 | + return Bukkit.getPluginManager().getPlugin("Essentials") != null && Bukkit.getPluginManager().getPlugin("Essentials").isEnabled(); |
53 | 66 | } |
54 | 67 |
|
55 | 68 | @Override |
56 | 69 | public boolean register() { |
| 70 | + k = getString("formatting.thousands", "k"); |
| 71 | + m = getString("formatting.millions", "m"); |
| 72 | + b = getString("formatting.billions", "b"); |
| 73 | + t = getString("formatting.trillions", "t"); |
| 74 | + q = getString("formatting.quadrillions", "q"); |
| 75 | + |
57 | 76 | essentials = (Essentials) Bukkit.getPluginManager().getPlugin("Essentials"); |
58 | | - if (essentials != null) { |
| 77 | + if (essentials != null && essentials.isEnabled()) { |
| 78 | + baltop = essentials.getBalanceTop(); |
| 79 | + baltop.calculateBalanceTopMapAsync(); |
59 | 80 | return super.register(); |
60 | 81 | } |
61 | 82 | return false; |
@@ -169,6 +190,103 @@ public String onRequest(OfflinePlayer player, @NotNull String identifier) { |
169 | 190 | return oPlayer.hasPermission("essentials.kits." + kit) ? papiTrue : papiFalse; |
170 | 191 | } |
171 | 192 |
|
| 193 | + if (identifier.startsWith("baltop_")) { |
| 194 | + Map<UUID, BalanceTop.Entry> baltopCache = baltop.getBalanceTopCache(); |
| 195 | + identifier = identifier.substring(7); |
| 196 | + |
| 197 | + if (identifier.startsWith("balance_")) { |
| 198 | + identifier = identifier.substring(8); |
| 199 | + |
| 200 | + if (identifier.startsWith("fixed_")) { |
| 201 | + identifier = identifier.substring(6); |
| 202 | + |
| 203 | + Integer id = Ints.tryParse(identifier); |
| 204 | + if (id == null) { |
| 205 | + return "Invalid ID"; |
| 206 | + } |
| 207 | + |
| 208 | + BalanceTop.Entry[] entries = baltopCache.values().toArray(new BalanceTop.Entry[0]); |
| 209 | + if (id >= entries.length) { |
| 210 | + return "0"; |
| 211 | + } |
| 212 | + return String.valueOf(entries[id].getBalance().longValue()); |
| 213 | + } |
| 214 | + |
| 215 | + if (identifier.startsWith("formatted_")) { |
| 216 | + identifier = identifier.substring(10); |
| 217 | + |
| 218 | + Integer id = Ints.tryParse(identifier); |
| 219 | + if (id == null) { |
| 220 | + return "Invalid ID"; |
| 221 | + } |
| 222 | + |
| 223 | + BalanceTop.Entry[] entries = baltopCache.values().toArray(new BalanceTop.Entry[0]); |
| 224 | + if (id >= entries.length) { |
| 225 | + return "0"; |
| 226 | + } |
| 227 | + return fixMoney(entries[id].getBalance().doubleValue()); |
| 228 | + } |
| 229 | + |
| 230 | + if (identifier.startsWith("commas_")) { |
| 231 | + identifier = identifier.substring(7); |
| 232 | + |
| 233 | + Integer id = Ints.tryParse(identifier); |
| 234 | + if (id == null) { |
| 235 | + return "Invalid ID"; |
| 236 | + } |
| 237 | + |
| 238 | + BalanceTop.Entry[] entries = baltopCache.values().toArray(new BalanceTop.Entry[0]); |
| 239 | + if (id >= entries.length) { |
| 240 | + return "0"; |
| 241 | + } |
| 242 | + return format.format(entries[id].getBalance().doubleValue()); |
| 243 | + } |
| 244 | + |
| 245 | + Integer id = Ints.tryParse(identifier); |
| 246 | + if (id == null) { |
| 247 | + return "Invalid ID"; |
| 248 | + } |
| 249 | + |
| 250 | + BalanceTop.Entry[] entries = baltopCache.values().toArray(new BalanceTop.Entry[0]); |
| 251 | + if (id >= entries.length) { |
| 252 | + return "0"; |
| 253 | + } |
| 254 | + return String.valueOf(entries[id].getBalance().doubleValue()); |
| 255 | + } |
| 256 | + |
| 257 | + if (identifier.startsWith("player_")) { |
| 258 | + identifier = identifier.substring(7); |
| 259 | + |
| 260 | + Integer id = Ints.tryParse(identifier); |
| 261 | + if (id == null) { |
| 262 | + return "Invalid ID"; |
| 263 | + } |
| 264 | + |
| 265 | + BalanceTop.Entry[] entries = baltopCache.values().toArray(new BalanceTop.Entry[0]); |
| 266 | + if (id >= entries.length) { |
| 267 | + return "0"; |
| 268 | + } |
| 269 | + return entries[id].getDisplayName(); |
| 270 | + } |
| 271 | + |
| 272 | + if (identifier.equals("rank")) { |
| 273 | + if (!baltopCache.containsKey(player.getUniqueId())) { |
| 274 | + return ""; |
| 275 | + } |
| 276 | + |
| 277 | + int index = 1; |
| 278 | + for (Map.Entry<UUID, BalanceTop.Entry> entry : baltopCache.entrySet()) { |
| 279 | + if (entry.getKey() == player.getUniqueId()) { |
| 280 | + return String.valueOf(index); |
| 281 | + } |
| 282 | + |
| 283 | + index++; |
| 284 | + } |
| 285 | + } |
| 286 | + |
| 287 | + return null; |
| 288 | + } |
| 289 | + |
172 | 290 | if (identifier.startsWith("home_")) { |
173 | 291 | Integer homeNumber; |
174 | 292 | final User user = essentials.getUser(player.getUniqueId()); |
@@ -291,4 +409,35 @@ public String onRequest(OfflinePlayer player, @NotNull String identifier) { |
291 | 409 | } |
292 | 410 | return null; |
293 | 411 | } |
| 412 | + |
| 413 | + private String format(double d) { |
| 414 | + NumberFormat format = NumberFormat.getInstance(Locale.ENGLISH); |
| 415 | + format.setMaximumFractionDigits(2); |
| 416 | + format.setMinimumFractionDigits(0); |
| 417 | + return format.format(d); |
| 418 | + } |
| 419 | + |
| 420 | + private String fixMoney(double d) { |
| 421 | + |
| 422 | + if (d < 1000L) { |
| 423 | + return format(d); |
| 424 | + } |
| 425 | + if (d < 1000000L) { |
| 426 | + return format(d / 1000L) + k; |
| 427 | + } |
| 428 | + if (d < 1000000000L) { |
| 429 | + return format(d / 1000000L) + m; |
| 430 | + } |
| 431 | + if (d < 1000000000000L) { |
| 432 | + return format(d / 1000000000L) + b; |
| 433 | + } |
| 434 | + if (d < 1000000000000000L) { |
| 435 | + return format(d / 1000000000000L) + t; |
| 436 | + } |
| 437 | + if (d < 1000000000000000000L) { |
| 438 | + return format(d / 1000000000000000L) + q; |
| 439 | + } |
| 440 | + |
| 441 | + return String.valueOf(d); |
| 442 | + } |
294 | 443 | } |
0 commit comments