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 ;
47
59
48
60
private final String VERSION = getClass ().getPackage ().getImplementationVersion ();
@@ -54,6 +66,12 @@ public boolean canRegister() {
54
66
55
67
@ Override
56
68
public boolean register () {
69
+ k = getString ("formatting.thousands" , "k" );
70
+ m = getString ("formatting.millions" , "m" );
71
+ b = getString ("formatting.billions" , "b" );
72
+ t = getString ("formatting.trillions" , "t" );
73
+ q = getString ("formatting.quadrillions" , "q" );
74
+
57
75
essentials = (Essentials ) Bukkit .getPluginManager ().getPlugin ("Essentials" );
58
76
if (essentials != null ) {
59
77
return super .register ();
@@ -169,6 +187,89 @@ public String onRequest(OfflinePlayer player, @NotNull String identifier) {
169
187
return oPlayer .hasPermission ("essentials.kits." + kit ) ? papiTrue : papiFalse ;
170
188
}
171
189
190
+ if (identifier .startsWith ("baltop_" )) {
191
+ BalanceTop baltop = essentials .getBalanceTop ();
192
+ Map <UUID , BalanceTop .Entry > baltopCache = baltop .getBalanceTopCache ();
193
+ identifier = identifier .substring (7 );
194
+
195
+ if (identifier .startsWith ("balance_" )) {
196
+ identifier = identifier .substring (8 );
197
+
198
+ if (identifier .startsWith ("fixed_" )) {
199
+ identifier = identifier .substring (6 );
200
+
201
+ Integer id = Ints .tryParse (identifier );
202
+ if (id == null ) {
203
+ return "Invalid ID" ;
204
+ }
205
+
206
+ BalanceTop .Entry [] entries = baltopCache .values ().toArray (new BalanceTop .Entry [0 ]);
207
+ if (id >= entries .length ) {
208
+ return "0" ;
209
+ }
210
+ return String .valueOf (entries [id ].getBalance ().longValue ());
211
+ }
212
+
213
+ if (identifier .startsWith ("formatted_" )) {
214
+ identifier = identifier .substring (10 );
215
+
216
+ Integer id = Ints .tryParse (identifier );
217
+ if (id == null ) {
218
+ return "Invalid ID" ;
219
+ }
220
+
221
+ BalanceTop .Entry [] entries = baltopCache .values ().toArray (new BalanceTop .Entry [0 ]);
222
+ if (id >= entries .length ) {
223
+ return "0" ;
224
+ }
225
+ return fixMoney (entries [id ].getBalance ().doubleValue ());
226
+ }
227
+
228
+ if (identifier .startsWith ("commas_" )) {
229
+ identifier = identifier .substring (7 );
230
+
231
+ Integer id = Ints .tryParse (identifier );
232
+ if (id == null ) {
233
+ return "Invalid ID" ;
234
+ }
235
+
236
+ BalanceTop .Entry [] entries = baltopCache .values ().toArray (new BalanceTop .Entry [0 ]);
237
+ if (id >= entries .length ) {
238
+ return "0" ;
239
+ }
240
+ return format .format (entries [id ].getBalance ().doubleValue ());
241
+ }
242
+
243
+ Integer id = Ints .tryParse (identifier );
244
+ if (id == null ) {
245
+ return "Invalid ID" ;
246
+ }
247
+
248
+ BalanceTop .Entry [] entries = baltopCache .values ().toArray (new BalanceTop .Entry [0 ]);
249
+ if (id >= entries .length ) {
250
+ return "0" ;
251
+ }
252
+ return String .valueOf (entries [id ].getBalance ().doubleValue ());
253
+ }
254
+
255
+ if (identifier .startsWith ("player_" )) {
256
+ identifier = identifier .substring (7 );
257
+
258
+ Integer id = Ints .tryParse (identifier );
259
+ if (id == null ) {
260
+ return "Invalid ID" ;
261
+ }
262
+
263
+ BalanceTop .Entry [] entries = baltopCache .values ().toArray (new BalanceTop .Entry [0 ]);
264
+ if (id >= entries .length ) {
265
+ return "0" ;
266
+ }
267
+ return entries [id ].getDisplayName ();
268
+ }
269
+
270
+ return null ;
271
+ }
272
+
172
273
if (identifier .startsWith ("home_" )) {
173
274
Integer homeNumber ;
174
275
final User user = essentials .getUser (player .getUniqueId ());
@@ -291,4 +392,35 @@ public String onRequest(OfflinePlayer player, @NotNull String identifier) {
291
392
}
292
393
return null ;
293
394
}
395
+
396
+ private String format (double d ) {
397
+ NumberFormat format = NumberFormat .getInstance (Locale .ENGLISH );
398
+ format .setMaximumFractionDigits (2 );
399
+ format .setMinimumFractionDigits (0 );
400
+ return format .format (d );
401
+ }
402
+
403
+ private String fixMoney (double d ) {
404
+
405
+ if (d < 1000L ) {
406
+ return format (d );
407
+ }
408
+ if (d < 1000000L ) {
409
+ return format (d / 1000L ) + k ;
410
+ }
411
+ if (d < 1000000000L ) {
412
+ return format (d / 1000000L ) + m ;
413
+ }
414
+ if (d < 1000000000000L ) {
415
+ return format (d / 1000000000L ) + b ;
416
+ }
417
+ if (d < 1000000000000000L ) {
418
+ return format (d / 1000000000000L ) + t ;
419
+ }
420
+ if (d < 1000000000000000000L ) {
421
+ return format (d / 1000000000000000L ) + q ;
422
+ }
423
+
424
+ return String .valueOf (d );
425
+ }
294
426
}
0 commit comments