Skip to content

Commit

Permalink
Merge pull request #47 from PlaceholderAPI/baltop-up
Browse files Browse the repository at this point in the history
fix(baltop): Moved baltop above player null check
  • Loading branch information
Starmism authored Apr 7, 2022
2 parents 704e07d + 33878de commit 08012e0
Showing 1 changed file with 95 additions and 99 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.earth2me.essentials.User;
import com.earth2me.essentials.utils.DateUtil;
import com.earth2me.essentials.utils.DescParseTickFormat;
import com.google.common.collect.Streams;
import com.google.common.primitives.Ints;
import me.clip.placeholderapi.PlaceholderAPIPlugin;
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
Expand All @@ -40,10 +41,8 @@
import java.text.NumberFormat;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.Date;
import java.util.Locale;
import java.util.Map;
import java.util.UUID;
import java.util.*;
import java.util.stream.IntStream;
import java.util.stream.StreamSupport;

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

if (player == null) return "";

if (identifier.startsWith("kit_last_use_")) {
String kitName = identifier.split("kit_last_use_")[1].toLowerCase();
Kit kit;

try {
kit = new Kit(kitName, essentials);
} catch (Exception e) {
return "Invalid kit name";
}

long time = essentials.getUser(player.getUniqueId()).getKitTimestamp(kit.getName());

if (time == 1 || time <= 0) {
return "1";
}
return PlaceholderAPIPlugin.getDateFormat().format(new Date(time));
}

if (identifier.startsWith("kit_is_available_")) {
String kitName = identifier.split("kit_is_available_")[1].toLowerCase();
Kit kit;
User user = essentials.getUser(player.getUniqueId());
long time;

try {
kit = new Kit(kitName, essentials);
} catch (Exception e) {
return "Invalid kit name";
}

try {
time = kit.getNextUse(user);
} catch (Exception e) {
return papiFalse;
}

return time == 0 ? papiTrue : papiFalse;
}

if (identifier.startsWith("kit_time_until_available_")) {
String kitName = identifier.split("kit_time_until_available_")[1].toLowerCase();
boolean raw = false;
User user = essentials.getUser(player.getUniqueId());
Kit kit;
long time;

if (kitName.startsWith("raw_")) {
raw = true;
kitName = kitName.substring(4);

if (kitName.isEmpty()) {
return "Invalid kit name";
}
}

try {
kit = new Kit(kitName, essentials);
} catch (Exception e) {
return "Invalid kit name";
}

try {
time = kit.getNextUse(user);
} catch (Exception e) {
return "-1";
}

if (time <= System.currentTimeMillis()) {
return raw ? "0" : DateUtil.formatDateDiff(System.currentTimeMillis());
}

if (raw) {
return String.valueOf(Instant.now().until(Instant.ofEpochMilli(time), ChronoUnit.MILLIS));
} else {
return DateUtil.formatDateDiff(time);
}
}

if (identifier.startsWith("has_kit_")) {
Player oPlayer = player.getPlayer();
if (oPlayer == null) return papiFalse;

String kit = identifier.split("has_kit_")[1];
return oPlayer.hasPermission("essentials.kits." + kit) ? papiTrue : papiFalse;
}

// Put this before the null check as most of it is not required
if (identifier.startsWith("baltop_")) {
Map<UUID, BalanceTop.Entry> baltopCache = baltop.getBalanceTopCache();
identifier = identifier.substring(7);
Expand Down Expand Up @@ -270,21 +182,105 @@ public String onRequest(OfflinePlayer player, @NotNull String identifier) {
}

if (identifier.equals("rank")) {
// Another null check because it is above the normal one
if (player == null) return "";

if (!baltopCache.containsKey(player.getUniqueId())) {
return "";
}

int index = 1;
for (Map.Entry<UUID, BalanceTop.Entry> entry : baltopCache.entrySet()) {
if (entry.getKey() == player.getUniqueId()) {
return String.valueOf(index);
}
return String.valueOf(new ArrayList<>(baltopCache.keySet()).indexOf(player.getUniqueId()) + 1);
}

return null;
}

if (player == null) return "";

if (identifier.startsWith("kit_last_use_")) {
String kitName = identifier.split("kit_last_use_")[1].toLowerCase();
Kit kit;

index++;
try {
kit = new Kit(kitName, essentials);
} catch (Exception e) {
return "Invalid kit name";
}

long time = essentials.getUser(player.getUniqueId()).getKitTimestamp(kit.getName());

if (time == 1 || time <= 0) {
return "1";
}
return PlaceholderAPIPlugin.getDateFormat().format(new Date(time));
}

if (identifier.startsWith("kit_is_available_")) {
String kitName = identifier.split("kit_is_available_")[1].toLowerCase();
Kit kit;
User user = essentials.getUser(player.getUniqueId());
long time;

try {
kit = new Kit(kitName, essentials);
} catch (Exception e) {
return "Invalid kit name";
}

try {
time = kit.getNextUse(user);
} catch (Exception e) {
return papiFalse;
}

return time == 0 ? papiTrue : papiFalse;
}

if (identifier.startsWith("kit_time_until_available_")) {
String kitName = identifier.split("kit_time_until_available_")[1].toLowerCase();
boolean raw = false;
User user = essentials.getUser(player.getUniqueId());
Kit kit;
long time;

if (kitName.startsWith("raw_")) {
raw = true;
kitName = kitName.substring(4);

if (kitName.isEmpty()) {
return "Invalid kit name";
}
}

return null;
try {
kit = new Kit(kitName, essentials);
} catch (Exception e) {
return "Invalid kit name";
}

try {
time = kit.getNextUse(user);
} catch (Exception e) {
return "-1";
}

if (time <= System.currentTimeMillis()) {
return raw ? "0" : DateUtil.formatDateDiff(System.currentTimeMillis());
}

if (raw) {
return String.valueOf(Instant.now().until(Instant.ofEpochMilli(time), ChronoUnit.MILLIS));
} else {
return DateUtil.formatDateDiff(time);
}
}

if (identifier.startsWith("has_kit_")) {
Player oPlayer = player.getPlayer();
if (oPlayer == null) return papiFalse;

String kit = identifier.split("has_kit_")[1];
return oPlayer.hasPermission("essentials.kits." + kit) ? papiTrue : papiFalse;
}

if (identifier.startsWith("home_")) {
Expand Down

0 comments on commit 08012e0

Please sign in to comment.