Updated number formatting

This commit is contained in:
BuildTools
2020-11-13 12:07:48 +00:00
parent 6de4663c57
commit 0beaa8c684
5 changed files with 33 additions and 12 deletions

View File

@@ -8,6 +8,7 @@ import com.willfp.ecoenchants.enchantments.meta.EnchantmentRarity;
import com.willfp.ecoenchants.enchantments.util.Watcher;
import com.willfp.ecoenchants.integrations.placeholder.PlaceholderEntry;
import com.willfp.ecoenchants.integrations.placeholder.PlaceholderManager;
import com.willfp.ecoenchants.util.NumberUtils;
import com.willfp.ecoenchants.util.StringUtils;
import com.willfp.ecoenchants.util.interfaces.Registerable;
import com.willfp.ecoenchants.util.optional.Prerequisite;
@@ -151,12 +152,12 @@ public abstract class EcoEnchant extends Enchantment implements Listener, Regist
if(this.getConfig().config.get(EcoEnchants.CONFIG_LOCATION + "chance-per-level") != null) {
PlaceholderManager.registerPlaceholder(
new PlaceholderEntry(this.getPermissionName() + "_" + "chance_per_level", (player) -> {
return String.valueOf(this.getConfig().getDouble(EcoEnchants.CONFIG_LOCATION + "chance-per-level"));
return NumberUtils.format(this.getConfig().getDouble(EcoEnchants.CONFIG_LOCATION + "chance-per-level"));
})
);
PlaceholderManager.registerPlaceholder(
new PlaceholderEntry(this.getPermissionName() + "_" + "chance_per_level_percentage", (player) -> {
return String.valueOf(this.getConfig().getDouble(EcoEnchants.CONFIG_LOCATION + "chance-per-level") * 100);
return NumberUtils.format(this.getConfig().getDouble(EcoEnchants.CONFIG_LOCATION + "chance-per-level") * 100);
})
);
}
@@ -164,12 +165,12 @@ public abstract class EcoEnchant extends Enchantment implements Listener, Regist
if(this.getConfig().config.get(EcoEnchants.CONFIG_LOCATION + "multiplier") != null) {
PlaceholderManager.registerPlaceholder(
new PlaceholderEntry(this.getPermissionName() + "_" + "multiplier", (player) -> {
return String.valueOf(this.getConfig().getDouble(EcoEnchants.CONFIG_LOCATION + "multiplier"));
return NumberUtils.format(this.getConfig().getDouble(EcoEnchants.CONFIG_LOCATION + "multiplier"));
})
);
PlaceholderManager.registerPlaceholder(
new PlaceholderEntry(this.getPermissionName() + "_" + "multiplier_percentage", (player) -> {
return String.valueOf(this.getConfig().getDouble(EcoEnchants.CONFIG_LOCATION + "multiplier") * 100);
return NumberUtils.format(this.getConfig().getDouble(EcoEnchants.CONFIG_LOCATION + "multiplier") * 100);
})
);
}
@@ -177,12 +178,12 @@ public abstract class EcoEnchant extends Enchantment implements Listener, Regist
if(this instanceof Spell) {
PlaceholderManager.registerPlaceholder(
new PlaceholderEntry(this.getPermissionName() + "_" + "cooldown", (player) -> {
return String.valueOf(Spell.getCooldown((Spell) this, player));
return NumberUtils.format(Spell.getCooldown((Spell) this, player));
}, true)
);
PlaceholderManager.registerPlaceholder(
new PlaceholderEntry(this.getPermissionName() + "_" + "cooldown_total", (player) -> {
return String.valueOf(((Spell) this).getCooldownTime());
return NumberUtils.format(((Spell) this).getCooldownTime());
})
);
}

View File

@@ -25,4 +25,8 @@ public class PlaceholderEntry {
public String getResult(Player player) {
return this.function.call(player);
}
public boolean requiresPlayer() {
return requiresPlayer;
}
}

View File

@@ -1,7 +1,10 @@
package com.willfp.ecoenchants.integrations.placeholder;
import com.willfp.ecoenchants.util.Logger;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import javax.print.DocFlavor;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
@@ -23,7 +26,12 @@ public class PlaceholderManager {
public static String getResult(Player player, String identifier) {
Optional<PlaceholderEntry> matching = placeholders.stream().filter(expansion -> expansion.getIdentifier().equalsIgnoreCase(identifier)).findFirst();
return matching.map(placeholderEntry -> placeholderEntry.getResult(player)).orElse(null);
if(!matching.isPresent())
return null;
PlaceholderEntry entry = matching.get();
if(player == null && entry.requiresPlayer())
return "";
return entry.getResult(player);
}
public static String translatePlaceholders(String text, Player player) {

View File

@@ -7,8 +7,6 @@ import me.clip.placeholderapi.PlaceholderAPI;
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
import org.bukkit.entity.Player;
import javax.annotation.PostConstruct;
public class PlaceholderIntegrationPAPI extends PlaceholderExpansion implements PlaceholderIntegration {
@Override
public boolean persist() {
@@ -37,9 +35,6 @@ public class PlaceholderIntegrationPAPI extends PlaceholderExpansion implements
@Override
public String onPlaceholderRequest(Player player, String identifier) {
if(player == null)
return "";
return PlaceholderManager.getResult(player, identifier);
}

View File

@@ -1,5 +1,6 @@
package com.willfp.ecoenchants.util;
import java.text.DecimalFormat;
import java.util.TreeMap;
public class NumberUtils {
@@ -123,4 +124,16 @@ public class NumberUtils {
public static int log2(int N) {
return (int)(Math.log(N) / Math.log(2));
}
/**
* Format double to string
* @param toFormat The number to format
* @return Formatted
*/
public static String format(double toFormat) {
DecimalFormat df = new DecimalFormat("0.00");
String formatted = df.format(toFormat);
return formatted.endsWith("00") ? String.valueOf((int) toFormat) : formatted;
}
}