changed decimalFormat api

This commit is contained in:
Indyuce 2019-09-01 18:41:15 +02:00
parent a3a7a05fea
commit 19dc1161c2
13 changed files with 75 additions and 41 deletions

View File

@ -1,9 +1,6 @@
package net.Indyuce.mmocore;
import java.io.File;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.SimpleDateFormat;
import java.util.logging.Level;
import org.bukkit.Bukkit;
@ -128,9 +125,6 @@ public class MMOCore extends JavaPlugin {
public final MMOLoadManager loadManager = new MMOLoadManager();
public RPGUtilHandler rpgUtilHandler = new DefaultRPGUtilHandler();
public static final DecimalFormat digit = new DecimalFormat("0.#"), digit2 = new DecimalFormat("0.##"), digit3 = new DecimalFormat("0.###");
public static final SimpleDateFormat smoothDateFormat = new SimpleDateFormat("");
public void onLoad() {
plugin = this;
version = new ServerVersion(Bukkit.getServer().getClass());
@ -153,15 +147,6 @@ public class MMOCore extends JavaPlugin {
public void onEnable() {
/*
* decimal format
*/
DecimalFormatSymbols symbols = digit.getDecimalFormatSymbols();
symbols.setDecimalSeparator('.');
digit.setDecimalFormatSymbols(symbols);
digit2.setDecimalFormatSymbols(symbols);
digit3.setDecimalFormatSymbols(symbols);
try {
getLogger().log(Level.INFO, "Detected Bukkit Version: " + version.toString());
nms = (NMSHandler) Class.forName("net.Indyuce.mmocore.version.nms.NMSHandler_" + version.toString().substring(1)).newInstance();

View File

@ -0,0 +1,15 @@
package net.Indyuce.mmocore.api.math.format;
import java.text.DecimalFormat;
import net.Indyuce.mmocore.MMOCore;
public class MMOCoreDecimalFormat extends DecimalFormat {
private static final long serialVersionUID = -2794789471349113852L;
public MMOCoreDecimalFormat(String str) {
super(str);
setDecimalFormatSymbols(MMOCore.plugin.configManager.formatSymbols);
}
}

View File

@ -1,5 +1,6 @@
package net.Indyuce.mmocore.api.player;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;
@ -14,12 +15,15 @@ import net.Indyuce.mmocore.MMOCore;
import net.Indyuce.mmocore.api.ConfigMessage;
import net.Indyuce.mmocore.api.event.PlayerLevelUpEvent;
import net.Indyuce.mmocore.api.experience.Profession;
import net.Indyuce.mmocore.api.math.format.MMOCoreDecimalFormat;
import net.Indyuce.mmocore.api.math.particle.SmallParticleEffect;
public class Professions {
private final Map<String, Integer> exp = new HashMap<>();
private final Map<String, Integer> level = new HashMap<>();
private final PlayerData playerData;
private static final DecimalFormat decimal = new MMOCoreDecimalFormat("0.#");
public Professions(PlayerData playerData) {
this.playerData = playerData;
@ -108,6 +112,6 @@ public class Professions {
int chars = (int) ((double) exp / needed * 20);
for (int j = 0; j < 20; j++)
bar += (j == chars ? "" + ChatColor.WHITE + ChatColor.BOLD : "") + "|";
playerData.displayActionBar(MMOCore.plugin.configManager.getSimpleMessage("exp-notification", "profession", profession.getName(), "progress", bar, "ratio", MMOCore.digit.format((double) exp / needed * 100)));
playerData.displayActionBar(MMOCore.plugin.configManager.getSimpleMessage("exp-notification", "profession", profession.getName(), "progress", bar, "ratio", decimal.format((double) exp / needed * 100)));
}
}

View File

@ -9,7 +9,7 @@ import org.bukkit.ChatColor;
import org.bukkit.attribute.Attribute;
import org.bukkit.attribute.AttributeInstance;
import net.Indyuce.mmocore.MMOCore;
import net.Indyuce.mmocore.api.math.format.MMOCoreDecimalFormat;
import net.Indyuce.mmocore.api.player.PlayerData;
import net.Indyuce.mmocore.api.player.stats.stat.modifier.StatModifier;
@ -22,6 +22,8 @@ public class PlayerStats {
*/
private final Map<String, StatInstance> extra = new HashMap<>();
private static final DecimalFormat decimal = new MMOCoreDecimalFormat("0.#");
public PlayerStats(PlayerData data) {
this.data = data;
}
@ -41,11 +43,11 @@ public class PlayerStats {
public String format(ChatColor color, Attribute attribute) {
AttributeInstance instance = data.getPlayer().getAttribute(attribute);
return format(color, instance.getBaseValue(), instance.getValue() - instance.getBaseValue(), MMOCore.digit);
return format(color, instance.getBaseValue(), instance.getValue() - instance.getBaseValue(), decimal);
}
public String format(ChatColor color, StatType stat) {
return format(color, data.getProfess().calculateStat(stat, data.getLevel()), getExtraStat(stat), MMOCore.digit);
return format(color, data.getProfess().calculateStat(stat, data.getLevel()), getExtraStat(stat), decimal);
}
public String format(ChatColor color, StatType stat, DecimalFormat format) {
@ -53,7 +55,7 @@ public class PlayerStats {
}
public String format(ChatColor color, double base, double extra, DecimalFormat format) {
return "" + color + format.format(base + extra) + ChatColor.GRAY + " (" + color + MMOCore.digit.format(base) + ChatColor.GRAY + "+" + color + MMOCore.digit.format(extra) + ChatColor.GRAY + ")";
return "" + color + format.format(base + extra) + ChatColor.GRAY + " (" + color + decimal.format(base) + ChatColor.GRAY + "+" + color + decimal.format(extra) + ChatColor.GRAY + ")";
}
/*

View File

@ -1,13 +1,17 @@
package net.Indyuce.mmocore.api.player.stats.stat.modifier;
import java.text.DecimalFormat;
import org.apache.commons.lang.Validate;
import net.Indyuce.mmocore.MMOCore;
import net.Indyuce.mmocore.api.math.format.MMOCoreDecimalFormat;
public class StatModifier {
private final double d;
private final boolean relative;
private static final DecimalFormat digit = new MMOCoreDecimalFormat("0.#");
public StatModifier(double d) {
this(d, false);
}
@ -42,6 +46,6 @@ public class StatModifier {
@Override
public String toString() {
return MMOCore.digit.format(d) + (relative ? "%" : "");
return digit.format(d) + (relative ? "%" : "");
}
}

View File

@ -1,5 +1,7 @@
package net.Indyuce.mmocore.command.rpg.booster;
import java.text.DecimalFormat;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
@ -7,10 +9,13 @@ import org.bukkit.entity.Player;
import net.Indyuce.mmocore.MMOCore;
import net.Indyuce.mmocore.api.experience.Booster;
import net.Indyuce.mmocore.api.math.format.DelayFormat;
import net.Indyuce.mmocore.api.math.format.MMOCoreDecimalFormat;
import net.Indyuce.mmocore.command.api.CommandEnd;
import net.Indyuce.mmocore.command.api.CommandMap;
public class ListCommandMap extends CommandEnd {
private static final DecimalFormat decimal = new MMOCoreDecimalFormat("0.##");
public ListCommandMap(CommandMap parent) {
super(parent, "list");
}
@ -23,7 +28,7 @@ public class ListCommandMap extends CommandEnd {
sender.sendMessage(ChatColor.YELLOW + "----------------------------------------------------");
for (Booster booster : MMOCore.plugin.boosterManager.getBoosters())
if (!booster.isTimedOut())
MMOCore.plugin.nms.sendJson((Player) sender, "{\"text\":\"" + ChatColor.YELLOW + "- " + ChatColor.GOLD + MMOCore.digit2.format((1 + booster.getExtra())) + "x" + ChatColor.YELLOW + " Booster - " + ChatColor.GOLD + (!booster.hasProfession() ? "Main" : booster.getProfession().getName()) + ChatColor.YELLOW + " - " + ChatColor.GOLD + new DelayFormat().format(booster.getCreationDate() + booster.getLength() - System.currentTimeMillis()) + "\",\"clickEvent\":{\"action\":\"suggest_command\",\"value\":\"/mmocore booster remove " + booster.getUniqueId().toString() + "\"},\"hoverEvent\":{\"action\":\"show_text\",\"value\":{\"text\":\"Click to remove.\"}}}");
MMOCore.plugin.nms.sendJson((Player) sender, "{\"text\":\"" + ChatColor.YELLOW + "- " + ChatColor.GOLD + decimal.format((1 + booster.getExtra())) + "x" + ChatColor.YELLOW + " Booster - " + ChatColor.GOLD + (!booster.hasProfession() ? "Main" : booster.getProfession().getName()) + ChatColor.YELLOW + " - " + ChatColor.GOLD + new DelayFormat().format(booster.getCreationDate() + booster.getLength() - System.currentTimeMillis()) + "\",\"clickEvent\":{\"action\":\"suggest_command\",\"value\":\"/mmocore booster remove " + booster.getUniqueId().toString() + "\"},\"hoverEvent\":{\"action\":\"show_text\",\"value\":{\"text\":\"Click to remove.\"}}}");
sender.sendMessage(ChatColor.YELLOW + "----------------------------------------------------");
return CommandResult.SUCCESS;

View File

@ -1,5 +1,7 @@
package net.Indyuce.mmocore.comp.placeholder;
import java.text.DecimalFormat;
import org.bukkit.ChatColor;
import org.bukkit.attribute.Attribute;
import org.bukkit.entity.Player;
@ -7,13 +9,14 @@ import org.bukkit.entity.Player;
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
import net.Indyuce.mmocore.MMOCore;
import net.Indyuce.mmocore.api.AltChar;
import net.Indyuce.mmocore.api.math.format.MMOCoreDecimalFormat;
import net.Indyuce.mmocore.api.player.PlayerData;
import net.Indyuce.mmocore.api.player.PlayerQuests;
import net.Indyuce.mmocore.api.player.Professions;
import net.Indyuce.mmocore.api.player.stats.StatType;
public class RPGPlaceholders extends PlaceholderExpansion {
// private static final DelayFormat delayFormat = new DelayFormat();
private static final DecimalFormat decimal = new MMOCoreDecimalFormat("0.#"), decimal2 = new MMOCoreDecimalFormat("0.##");
@Override
public String getAuthor() {
@ -39,14 +42,14 @@ public class RPGPlaceholders extends PlaceholderExpansion {
else if (identifier.equals("level_percent")) {
PlayerData playerData = PlayerData.get(player);
double current = playerData.getExperience(), next = MMOCore.plugin.configManager.getNeededExperience(playerData.getLevel() + 1);
return MMOCore.digit.format(current / next * 100);
return decimal.format(current / next * 100);
}
else if (identifier.equals("combat"))
return String.valueOf(PlayerData.get(player).isInCombat());
else if (identifier.equals("health"))
return MMOCore.digit2.format(player.getHealth());
return decimal2.format(player.getHealth());
else if (identifier.startsWith("attribute_"))
return String.valueOf(PlayerData.get(player).getAttributes().getAttribute(MMOCore.plugin.attributeManager.get(identifier.substring(10).toLowerCase().replace("_", "-"))));
@ -58,14 +61,14 @@ public class RPGPlaceholders extends PlaceholderExpansion {
Professions professions = PlayerData.get(player).getCollectionSkills();
String profession = identifier.substring(19).replace(" ", "-").replace("_", "-").toLowerCase();
double current = professions.getExperience(profession), next = MMOCore.plugin.configManager.getNeededExperience(professions.getLevel(profession) + 1);
return MMOCore.digit.format(current / next * 100);
return decimal.format(current / next * 100);
}
else if (identifier.startsWith("profession_"))
return "" + PlayerData.get(player).getCollectionSkills().getLevel(identifier.substring(11).replace(" ", "-").replace("_", "-").toLowerCase());
else if (identifier.equals("max_health"))
return MMOCore.digit2.format(player.getAttribute(Attribute.GENERIC_MAX_HEALTH).getValue());
return decimal2.format(player.getAttribute(Attribute.GENERIC_MAX_HEALTH).getValue());
else if (identifier.equals("experience"))
return "" + PlayerData.get(player).getExperience();
@ -86,10 +89,10 @@ public class RPGPlaceholders extends PlaceholderExpansion {
return "" + PlayerData.get(player).getAttributeReallocationPoints();
else if (identifier.equals("mana"))
return "" + MMOCore.digit.format(PlayerData.get(player).getMana());
return "" + decimal.format(PlayerData.get(player).getMana());
else if (identifier.equals("max_mana"))
return "" + MMOCore.digit.format(PlayerData.get(player).getStats().getStat(StatType.MAX_MANA));
return "" + decimal.format(PlayerData.get(player).getStats().getStat(StatType.MAX_MANA));
else if (identifier.equals("mana_bar")) {
String format = "";
@ -101,10 +104,10 @@ public class RPGPlaceholders extends PlaceholderExpansion {
}
else if (identifier.equals("stamina"))
return "" + MMOCore.digit.format(PlayerData.get(player).getStamina());
return "" + decimal.format(PlayerData.get(player).getStamina());
else if (identifier.equals("max_stamina"))
return "" + MMOCore.digit.format(PlayerData.get(player).getStats().getStat(StatType.MAX_STAMINA));
return "" + decimal.format(PlayerData.get(player).getStats().getStat(StatType.MAX_STAMINA));
else if (identifier.equals("stamina_bar")) {
String format = "";
@ -116,10 +119,10 @@ public class RPGPlaceholders extends PlaceholderExpansion {
}
else if (identifier.equals("stellium"))
return "" + MMOCore.digit.format(PlayerData.get(player).getStellium());
return "" + decimal.format(PlayerData.get(player).getStellium());
else if (identifier.equals("max_stellium"))
return "" + MMOCore.digit.format(PlayerData.get(player).getStats().getStat(StatType.MAX_STELLIUM));
return "" + decimal.format(PlayerData.get(player).getStats().getStat(StatType.MAX_STELLIUM));
else if (identifier.equals("stellium_bar")) {
String format = "";
@ -137,7 +140,7 @@ public class RPGPlaceholders extends PlaceholderExpansion {
else if (identifier.equals("quest_progress")) {
PlayerQuests data = PlayerData.get(player).getQuestData();
return data.hasCurrent() ? MMOCore.digit.format((int) (double) data.getCurrent().getObjectiveNumber() / data.getCurrent().getQuest().getObjectives().size() * 100) : "0";
return data.hasCurrent() ? decimal.format((int) (double) data.getCurrent().getObjectiveNumber() / data.getCurrent().getQuest().getObjectives().size() * 100) : "0";
}
else if (identifier.equals("quest_objective")) {

View File

@ -55,7 +55,7 @@ public class ClassConfirmation extends EditableInventory {
for (int j = 0; j < 20; j++)
bar += (j == chars ? "" + ChatColor.WHITE + ChatColor.BOLD : "") + "|";
holders.register("percent", MMOCore.digit.format(ratio * 100));
holders.register("percent", decimal.format(ratio * 100));
holders.register("progress", bar);
holders.register("class", profess.getName());
holders.register("unlocked_skills", info.getSkillKeys().size());

View File

@ -89,7 +89,7 @@ public class PlayerStats extends EditableInventory {
// holders.register("profession", type.getName());
holders.register("progress", bar);
holders.register("level", "" + inv.getPlayerData().getCollectionSkills().getLevel(profession));
holders.register("percent", MMOCore.digit.format(ratio * 100));
holders.register("percent", decimal.format(ratio * 100));
for (StatType stat : StatType.values())
if (stat.matches(profession))
holders.register(stat.name().toLowerCase(), stat.format(stats.getStat(stat)));
@ -284,7 +284,7 @@ public class PlayerStats extends EditableInventory {
for (int j = 0; j < 20; j++)
bar += (j == chars ? "" + ChatColor.WHITE + ChatColor.BOLD : "") + "|";
holders.register("percent", MMOCore.digit.format(ratio * 100));
holders.register("percent", decimal.format(ratio * 100));
holders.register("exp", "" + data.getExperience());
holders.register("level", "" + data.getLevel());
holders.register("class_points", "" + data.getClassPoints());

View File

@ -97,7 +97,7 @@ public class WaypointViewer extends EditableInventory {
Waypoint waypoint = generated.waypoints.get(generated.page * 27 + n);
holders.register("name", waypoint.getName());
holders.register("stellium", "" + MMOCore.digit.format(waypoint.getStelliumCost()));
holders.register("stellium", decimal.format(waypoint.getStelliumCost()));
return holders;
}
@ -195,13 +195,13 @@ public class WaypointViewer extends EditableInventory {
double left = waypoint.getStelliumCost() - playerData.getStellium();
if (left > 0) {
player.sendMessage(MMOCore.plugin.configManager.getSimpleMessage("not-enough-stellium", "more", MMOCore.digit.format(left)));
player.sendMessage(MMOCore.plugin.configManager.getSimpleMessage("not-enough-stellium", "more", decimal.format(left)));
return;
}
double next = (double) playerData.getNextWaypointMillis() / 1000;
if (next < 0) {
player.sendMessage(MMOCore.plugin.configManager.getSimpleMessage("not-enough-stellium", "cooldown", MMOCore.digit.format(next)));
player.sendMessage(MMOCore.plugin.configManager.getSimpleMessage("not-enough-stellium", "cooldown", decimal.format(next)));
return;
}

View File

@ -1,5 +1,6 @@
package net.Indyuce.mmocore.gui.api;
import java.text.DecimalFormat;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.logging.Level;
@ -9,6 +10,7 @@ import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import net.Indyuce.mmocore.MMOCore;
import net.Indyuce.mmocore.api.math.format.MMOCoreDecimalFormat;
import net.Indyuce.mmocore.gui.api.item.InventoryItem;
import net.Indyuce.mmocore.gui.api.item.TriggerItem;
@ -24,6 +26,8 @@ public abstract class EditableInventory {
*/
private final Set<InventoryItem> items = new LinkedHashSet<>();
protected static final DecimalFormat decimal = new MMOCoreDecimalFormat("0.#");
public EditableInventory(String id) {
this.id = id;
Validate.notNull(id, "ID must not be null");

View File

@ -5,6 +5,7 @@ import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Files;
import java.text.DecimalFormatSymbols;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
@ -28,6 +29,8 @@ public class ConfigManager {
public double expPartyBuff, regenPartyBuff;
public String partyChatPrefix;
public final DecimalFormatSymbols formatSymbols = new DecimalFormatSymbols();
private List<Integer> neededExp = new ArrayList<>();
private FileConfiguration messages;
private boolean chatInput;
@ -92,6 +95,7 @@ public class ConfigManager {
expPartyBuff = MMOCore.plugin.getConfig().getDouble("party.buff.experience");
regenPartyBuff = MMOCore.plugin.getConfig().getDouble("party.buff.health-regen");
partyChatPrefix = MMOCore.plugin.getConfig().getString("party.chat-prefix");
formatSymbols.setDecimalSeparator(getFirstChar(MMOCore.plugin.getConfig().getString("number-format.decimal-separator"), ','));
neededExp.clear();
int line = 0;
@ -109,6 +113,10 @@ public class ConfigManager {
}
}
private char getFirstChar(String str, char defaultChar) {
return str == null || str.isEmpty() ? defaultChar : str.charAt(0);
}
public PlayerInput newPlayerInput(Player player, InputType type, Consumer<String> output) {
return chatInput ? new ChatInput(player, type, output) : new AnvilGUI(player, type, output);
}

View File

@ -60,6 +60,10 @@ death-exp-loss:
# Percentage of current EXP you lose when dying.
percent: 30
# Edit general plugin number formatting here.
number-format:
decimal-separator: .
# Allows to scale health up/down to a specific
# amount so extra health does not fill up the screen.
# Requires a SERVER reload when changed.