Skip to content

Commit 4dc60fe

Browse files
authored
Merge pull request #40 from PlaceholderAPI/baltop
2 parents 3e80bd0 + 9ed6801 commit 4dc60fe

3 files changed

Lines changed: 157 additions & 7 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
### Eclipse ###
22

33
.metadata
4+
.idea/
45
bin/
56
tmp/
67
*.tmp

pom.xml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<modelVersion>4.0.0</modelVersion>
33
<groupId>com.extendedclip.papi.expansion.essentials</groupId>
44
<artifactId>essentials-expansion</artifactId>
5-
<version>1.4.0</version>
5+
<version>1.5.0</version>
66
<name>PAPI-Expansion-Essentials</name>
77
<description>PlaceholderAPI expansion for Essentials placeholders</description>
88

@@ -17,27 +17,27 @@
1717
</repository>
1818
<repository>
1919
<id>ess-repo</id>
20-
<url>https://repo.essentialsx.net/snapshots/</url>
20+
<url>https://repo.essentialsx.net/releases/</url>
2121
</repository>
2222
</repositories>
2323

2424
<dependencies>
2525
<dependency>
2626
<groupId>org.spigotmc</groupId>
2727
<artifactId>spigot-api</artifactId>
28-
<version>1.17-R0.1-SNAPSHOT</version>
28+
<version>1.18.2-R0.1-SNAPSHOT</version>
2929
<scope>provided</scope>
3030
</dependency>
3131
<dependency>
3232
<groupId>me.clip</groupId>
3333
<artifactId>placeholderapi</artifactId>
34-
<version>2.10.9</version>
34+
<version>2.11.1</version>
3535
<scope>provided</scope>
3636
</dependency>
3737
<dependency>
3838
<groupId>net.essentialsx</groupId>
3939
<artifactId>EssentialsX</artifactId>
40-
<version>2.19.0-SNAPSHOT</version>
40+
<version>2.19.3</version>
4141
<exclusions>
4242
<exclusion>
4343
<groupId>org.bstats</groupId>

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

Lines changed: 151 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,34 +28,55 @@
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;
59+
private BalanceTop baltop;
4760

4861
private final String VERSION = getClass().getPackage().getImplementationVersion();
4962

5063
@Override
5164
public boolean canRegister() {
52-
return Bukkit.getPluginManager().getPlugin("Essentials") != null;
65+
return Bukkit.getPluginManager().getPlugin("Essentials") != null && Bukkit.getPluginManager().getPlugin("Essentials").isEnabled();
5366
}
5467

5568
@Override
5669
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+
5776
essentials = (Essentials) Bukkit.getPluginManager().getPlugin("Essentials");
58-
if (essentials != null) {
77+
if (essentials != null && essentials.isEnabled()) {
78+
baltop = essentials.getBalanceTop();
79+
baltop.calculateBalanceTopMapAsync();
5980
return super.register();
6081
}
6182
return false;
@@ -169,6 +190,103 @@ public String onRequest(OfflinePlayer player, @NotNull String identifier) {
169190
return oPlayer.hasPermission("essentials.kits." + kit) ? papiTrue : papiFalse;
170191
}
171192

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+
172290
if (identifier.startsWith("home_")) {
173291
Integer homeNumber;
174292
final User user = essentials.getUser(player.getUniqueId());
@@ -291,4 +409,35 @@ public String onRequest(OfflinePlayer player, @NotNull String identifier) {
291409
}
292410
return null;
293411
}
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+
}
294443
}

0 commit comments

Comments
 (0)