Skip to content

Commit 08012e0

Browse files
authored
Merge pull request #47 from PlaceholderAPI/baltop-up
fix(baltop): Moved baltop above player null check
2 parents 704e07d + 33878de commit 08012e0

1 file changed

Lines changed: 95 additions & 99 deletions

File tree

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

Lines changed: 95 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import com.earth2me.essentials.User;
2626
import com.earth2me.essentials.utils.DateUtil;
2727
import com.earth2me.essentials.utils.DescParseTickFormat;
28+
import com.google.common.collect.Streams;
2829
import com.google.common.primitives.Ints;
2930
import me.clip.placeholderapi.PlaceholderAPIPlugin;
3031
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
@@ -40,10 +41,8 @@
4041
import java.text.NumberFormat;
4142
import java.time.Instant;
4243
import java.time.temporal.ChronoUnit;
43-
import java.util.Date;
44-
import java.util.Locale;
45-
import java.util.Map;
46-
import java.util.UUID;
44+
import java.util.*;
45+
import java.util.stream.IntStream;
4746
import java.util.stream.StreamSupport;
4847

4948
public class EssentialsExpansion extends PlaceholderExpansion {
@@ -102,94 +101,7 @@ public String onRequest(OfflinePlayer player, @NotNull String identifier) {
102101
final String papiTrue = PlaceholderAPIPlugin.booleanTrue();
103102
final String papiFalse = PlaceholderAPIPlugin.booleanFalse();
104103

105-
if (player == null) return "";
106-
107-
if (identifier.startsWith("kit_last_use_")) {
108-
String kitName = identifier.split("kit_last_use_")[1].toLowerCase();
109-
Kit kit;
110-
111-
try {
112-
kit = new Kit(kitName, essentials);
113-
} catch (Exception e) {
114-
return "Invalid kit name";
115-
}
116-
117-
long time = essentials.getUser(player.getUniqueId()).getKitTimestamp(kit.getName());
118-
119-
if (time == 1 || time <= 0) {
120-
return "1";
121-
}
122-
return PlaceholderAPIPlugin.getDateFormat().format(new Date(time));
123-
}
124-
125-
if (identifier.startsWith("kit_is_available_")) {
126-
String kitName = identifier.split("kit_is_available_")[1].toLowerCase();
127-
Kit kit;
128-
User user = essentials.getUser(player.getUniqueId());
129-
long time;
130-
131-
try {
132-
kit = new Kit(kitName, essentials);
133-
} catch (Exception e) {
134-
return "Invalid kit name";
135-
}
136-
137-
try {
138-
time = kit.getNextUse(user);
139-
} catch (Exception e) {
140-
return papiFalse;
141-
}
142-
143-
return time == 0 ? papiTrue : papiFalse;
144-
}
145-
146-
if (identifier.startsWith("kit_time_until_available_")) {
147-
String kitName = identifier.split("kit_time_until_available_")[1].toLowerCase();
148-
boolean raw = false;
149-
User user = essentials.getUser(player.getUniqueId());
150-
Kit kit;
151-
long time;
152-
153-
if (kitName.startsWith("raw_")) {
154-
raw = true;
155-
kitName = kitName.substring(4);
156-
157-
if (kitName.isEmpty()) {
158-
return "Invalid kit name";
159-
}
160-
}
161-
162-
try {
163-
kit = new Kit(kitName, essentials);
164-
} catch (Exception e) {
165-
return "Invalid kit name";
166-
}
167-
168-
try {
169-
time = kit.getNextUse(user);
170-
} catch (Exception e) {
171-
return "-1";
172-
}
173-
174-
if (time <= System.currentTimeMillis()) {
175-
return raw ? "0" : DateUtil.formatDateDiff(System.currentTimeMillis());
176-
}
177-
178-
if (raw) {
179-
return String.valueOf(Instant.now().until(Instant.ofEpochMilli(time), ChronoUnit.MILLIS));
180-
} else {
181-
return DateUtil.formatDateDiff(time);
182-
}
183-
}
184-
185-
if (identifier.startsWith("has_kit_")) {
186-
Player oPlayer = player.getPlayer();
187-
if (oPlayer == null) return papiFalse;
188-
189-
String kit = identifier.split("has_kit_")[1];
190-
return oPlayer.hasPermission("essentials.kits." + kit) ? papiTrue : papiFalse;
191-
}
192-
104+
// Put this before the null check as most of it is not required
193105
if (identifier.startsWith("baltop_")) {
194106
Map<UUID, BalanceTop.Entry> baltopCache = baltop.getBalanceTopCache();
195107
identifier = identifier.substring(7);
@@ -270,21 +182,105 @@ public String onRequest(OfflinePlayer player, @NotNull String identifier) {
270182
}
271183

272184
if (identifier.equals("rank")) {
185+
// Another null check because it is above the normal one
186+
if (player == null) return "";
187+
273188
if (!baltopCache.containsKey(player.getUniqueId())) {
274189
return "";
275190
}
276191

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-
}
192+
return String.valueOf(new ArrayList<>(baltopCache.keySet()).indexOf(player.getUniqueId()) + 1);
193+
}
194+
195+
return null;
196+
}
197+
198+
if (player == null) return "";
199+
200+
if (identifier.startsWith("kit_last_use_")) {
201+
String kitName = identifier.split("kit_last_use_")[1].toLowerCase();
202+
Kit kit;
282203

283-
index++;
204+
try {
205+
kit = new Kit(kitName, essentials);
206+
} catch (Exception e) {
207+
return "Invalid kit name";
208+
}
209+
210+
long time = essentials.getUser(player.getUniqueId()).getKitTimestamp(kit.getName());
211+
212+
if (time == 1 || time <= 0) {
213+
return "1";
214+
}
215+
return PlaceholderAPIPlugin.getDateFormat().format(new Date(time));
216+
}
217+
218+
if (identifier.startsWith("kit_is_available_")) {
219+
String kitName = identifier.split("kit_is_available_")[1].toLowerCase();
220+
Kit kit;
221+
User user = essentials.getUser(player.getUniqueId());
222+
long time;
223+
224+
try {
225+
kit = new Kit(kitName, essentials);
226+
} catch (Exception e) {
227+
return "Invalid kit name";
228+
}
229+
230+
try {
231+
time = kit.getNextUse(user);
232+
} catch (Exception e) {
233+
return papiFalse;
234+
}
235+
236+
return time == 0 ? papiTrue : papiFalse;
237+
}
238+
239+
if (identifier.startsWith("kit_time_until_available_")) {
240+
String kitName = identifier.split("kit_time_until_available_")[1].toLowerCase();
241+
boolean raw = false;
242+
User user = essentials.getUser(player.getUniqueId());
243+
Kit kit;
244+
long time;
245+
246+
if (kitName.startsWith("raw_")) {
247+
raw = true;
248+
kitName = kitName.substring(4);
249+
250+
if (kitName.isEmpty()) {
251+
return "Invalid kit name";
284252
}
285253
}
286254

287-
return null;
255+
try {
256+
kit = new Kit(kitName, essentials);
257+
} catch (Exception e) {
258+
return "Invalid kit name";
259+
}
260+
261+
try {
262+
time = kit.getNextUse(user);
263+
} catch (Exception e) {
264+
return "-1";
265+
}
266+
267+
if (time <= System.currentTimeMillis()) {
268+
return raw ? "0" : DateUtil.formatDateDiff(System.currentTimeMillis());
269+
}
270+
271+
if (raw) {
272+
return String.valueOf(Instant.now().until(Instant.ofEpochMilli(time), ChronoUnit.MILLIS));
273+
} else {
274+
return DateUtil.formatDateDiff(time);
275+
}
276+
}
277+
278+
if (identifier.startsWith("has_kit_")) {
279+
Player oPlayer = player.getPlayer();
280+
if (oPlayer == null) return papiFalse;
281+
282+
String kit = identifier.split("has_kit_")[1];
283+
return oPlayer.hasPermission("essentials.kits." + kit) ? papiTrue : papiFalse;
288284
}
289285

290286
if (identifier.startsWith("home_")) {

0 commit comments

Comments
 (0)