Skip to content

Commit

Permalink
Merge pull request #35 from BlitzOffline/breaking/baltop-removal
Browse files Browse the repository at this point in the history
  • Loading branch information
darbyjack authored Jul 20, 2022
2 parents 63ddf69 + 2be80b5 commit 8814230
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 257 deletions.

This file was deleted.

48 changes: 0 additions & 48 deletions src/main/java/com/extendedclip/papi/expansion/vault/TopPlayer.java

This file was deleted.

141 changes: 3 additions & 138 deletions src/main/java/com/extendedclip/papi/expansion/vault/VaultEcoHook.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,11 @@
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class VaultEcoHook implements VaultHook {

private static final Pattern TOP_BALANCE_FIXED_PATTERN = Pattern.compile("top_balance_fixed_");
private static final Pattern TOP_BALANCE_FORMATTED_PATTERN = Pattern.compile("top_balance_formatted_");
private static final Pattern TOP_BALANCE_COMMAS_PATTERN = Pattern.compile("top_balance_commas_");
private static final Pattern TOP_BALANCE_PATTERN = Pattern.compile("top_balance_");
private static final Pattern TOP_PLAYER_PATTERN = Pattern.compile("top_player_");

private static final Pattern BALANCE_DECIMAL_POINTS_PATTERN = Pattern.compile("balance_(?<points>\\d+)dp");

private final Map<Integer, DecimalFormat> decimalFormats = new HashMap<>();
Expand All @@ -52,24 +44,14 @@ public class VaultEcoHook implements VaultHook {
private final String t;
private final String q;
private final DecimalFormat format = new DecimalFormat("#,###");
private final boolean baltopEnabled;
private final int taskDelay;
private final int topSize;
private final Map<Integer, TopPlayer> balTop = new TreeMap<>();

private final VaultExpansion expansion;
private final VaultPermsHook perms;

private Economy eco;
private BalTopTask balTopTask;

VaultEcoHook(VaultExpansion expansion, VaultPermsHook perms) {
VaultEcoHook(VaultExpansion expansion) {
this.expansion = expansion;
this.perms = perms;

baltopEnabled = (Boolean) expansion.get("baltop.enabled", false);
topSize = expansion.getInt("baltop.cache_size", 100);
taskDelay = expansion.getInt("baltop.check_delay", 30);
k = expansion.getString("formatting.thousands", "k");
m = expansion.getString("formatting.millions", "m");
b = expansion.getString("formatting.billions", "b");
Expand All @@ -79,29 +61,14 @@ public class VaultEcoHook implements VaultHook {

@Override
public boolean setup() {
RegisteredServiceProvider<Economy> rsp = Bukkit.getServer().getServicesManager()
.getRegistration(Economy.class);
RegisteredServiceProvider<Economy> rsp = Bukkit.getServer().getServicesManager().getRegistration(Economy.class);

if (rsp == null) {
return false;
}

eco = rsp.getProvider();

if (baltopEnabled) {
this.balTopTask = new BalTopTask(this, perms);
balTopTask.runTaskTimerAsynchronously(expansion.getPlaceholderAPI(), 20, 20 * taskDelay);
}

return eco != null;
}

public void clear() {
balTop.clear();
if (this.balTopTask != null) {
this.balTopTask.cancel();
this.balTopTask = null;
}
return true;
}

protected Economy getEco() {
Expand All @@ -112,78 +79,8 @@ protected VaultExpansion getExpansion() {
return this.expansion;
}

protected Map<Integer, TopPlayer> getBalTop() {
return balTop;
}

void setBalTop(Map<String, Double> map) {
this.balTop.clear();
int count = 1;
for (Entry<String, Double> entry : map.entrySet()) {
if (count >= topSize) {
break;
}
balTop.put(count, new TopPlayer(entry.getKey(), entry.getValue()));
count++;
}
}

@Override
public String onPlaceholderRequest(OfflinePlayer p, String identifier) {
if (!baltopEnabled && identifier.startsWith("top_")) {
return (identifier.startsWith("top_balance")) ? "0" : "";
}

if (identifier.startsWith("top_balance_fixed_")) {
String[] args = TOP_BALANCE_FIXED_PATTERN.split(identifier);

if (args.length > 1) {
return toLong(getTopBalance(getInt(args[1])));
}

return "0";
}

if (identifier.startsWith("top_balance_formatted_")) {
String[] args = TOP_BALANCE_FORMATTED_PATTERN.split(identifier);

if (args.length > 1) {
return fixMoney(getTopBalance(getInt(args[1])));
}

return "0";
}

if (identifier.startsWith("top_balance_commas_")) {
String[] args = TOP_BALANCE_COMMAS_PATTERN.split(identifier);

if (args.length > 1) {
return format.format(getTopBalance(getInt(args[1])));
}

return "0";
}

if (identifier.startsWith("top_balance_")) {
String[] args = TOP_BALANCE_PATTERN.split(identifier);

if (args.length > 1) {
return String.valueOf(getTopBalance(getInt(args[1])));
}

return "0";
}

if (identifier.startsWith("top_player_")) {
String[] args = TOP_PLAYER_PATTERN.split(identifier);

if (args.length > 1) {
return getTopPlayer(getInt(args[1]));
}

return "";
}

if (p == null) {
return "";
}
Expand All @@ -205,8 +102,6 @@ public String onPlaceholderRequest(OfflinePlayer p, String identifier) {
return fixMoney(getBalance(p));
case "balance_commas":
return format.format(getBalance(p));
case "top_rank":
return getRank(p.getName());
}
return null;
}
Expand Down Expand Up @@ -262,36 +157,6 @@ private String setDecimalPoints(double d, int points) {
return eco == null ? 0 : eco.getBalance(p);
}

private String getTopPlayer(int rank) {
if (!baltopEnabled) {
return "";
}

final TopPlayer topPlayer = balTop.get(rank);
return topPlayer.getName();
}

private double getTopBalance(int rank) {
if (!baltopEnabled) {
return 0;
}

final TopPlayer topPlayer = balTop.get(rank);
return topPlayer == null ? 0 : topPlayer.getBal();
}

private String getRank(String player) {
if (!baltopEnabled) {
return null;
}

return balTop.entrySet().stream()
.filter(e -> e.getValue().getName().equals(player))
.findFirst()
.map(e -> e.getKey().toString())
.orElse("");
}

private int getInt(String string) {
final Integer integer = Ints.tryParse(string);
return integer == null ? 0 : integer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,18 @@ public class VaultExpansion extends PlaceholderExpansion implements Cacheable, C

public VaultExpansion() {
perms = new VaultPermsHook();
eco = new VaultEcoHook(this, perms);
eco = new VaultEcoHook(this);
}

@Override
public void clear() {
if (eco != null) {
eco.clear();
}
eco = null;
perms = null;
}

@Override
public Map<String, Object> getDefaults() {
final Map<String, Object> defaults = new HashMap<>();
defaults.put("baltop.enabled", false);
defaults.put("baltop.cache_size", 100);
defaults.put("baltop.check_delay", 30);
defaults.put("formatting.thousands", "k");
defaults.put("formatting.millions", "M");
defaults.put("formatting.billions", "B");
Expand Down

0 comments on commit 8814230

Please sign in to comment.