Skip to content

Commit 3944cf9

Browse files
committed
Migrate baltop from Vault to here
1 parent 8ca0cd6 commit 3944cf9

File tree

1 file changed

+132
-0
lines changed

1 file changed

+132
-0
lines changed

src/main/java/com/extendedclip/papi/expansion/essentials/EssentialsExpansion.java

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,33 @@
2828
import com.google.common.primitives.Ints;
2929
import me.clip.placeholderapi.PlaceholderAPIPlugin;
3030
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
31+
import net.essentialsx.api.v2.services.BalanceTop;
3132
import org.bukkit.*;
3233
import org.bukkit.entity.Player;
3334
import org.bukkit.inventory.ItemStack;
3435
import org.jetbrains.annotations.NotNull;
3536

3637
import java.math.BigDecimal;
3738
import java.text.DateFormat;
39+
import java.text.DecimalFormat;
3840
import java.text.NumberFormat;
3941
import java.time.Instant;
4042
import java.time.temporal.ChronoUnit;
4143
import java.util.Date;
44+
import java.util.Locale;
45+
import java.util.Map;
46+
import java.util.UUID;
4247
import java.util.stream.StreamSupport;
4348

4449
public class EssentialsExpansion extends PlaceholderExpansion {
4550

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+
4658
private Essentials essentials;
4759

4860
private final String VERSION = getClass().getPackage().getImplementationVersion();
@@ -54,6 +66,12 @@ public boolean canRegister() {
5466

5567
@Override
5668
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+
5775
essentials = (Essentials) Bukkit.getPluginManager().getPlugin("Essentials");
5876
if (essentials != null) {
5977
return super.register();
@@ -169,6 +187,89 @@ public String onRequest(OfflinePlayer player, @NotNull String identifier) {
169187
return oPlayer.hasPermission("essentials.kits." + kit) ? papiTrue : papiFalse;
170188
}
171189

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+
172273
if (identifier.startsWith("home_")) {
173274
Integer homeNumber;
174275
final User user = essentials.getUser(player.getUniqueId());
@@ -291,4 +392,35 @@ public String onRequest(OfflinePlayer player, @NotNull String identifier) {
291392
}
292393
return null;
293394
}
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+
}
294426
}

0 commit comments

Comments
 (0)