support for complete customization of numbers

closes #24
This commit is contained in:
hyperdefined
2022-11-24 00:44:59 -05:00
parent 1d4faa89a1
commit 6428925e40
12 changed files with 120 additions and 65 deletions

View File

@@ -21,6 +21,7 @@ import lol.hyper.githubreleaseapi.GitHubRelease;
import lol.hyper.githubreleaseapi.GitHubReleaseAPI;
import lol.hyper.toolstats.commands.CommandToolStats;
import lol.hyper.toolstats.events.*;
import lol.hyper.toolstats.tools.NumberFormat;
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
import org.bstats.bukkit.Metrics;
import org.bukkit.Bukkit;
@@ -33,10 +34,6 @@ import org.bukkit.plugin.java.JavaPlugin;
import java.io.File;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.SimpleDateFormat;
import java.util.Locale;
import java.util.logging.Logger;
public final class ToolStats extends JavaPlugin {
@@ -60,7 +57,6 @@ public final class ToolStats extends JavaPlugin {
// used for tracking new elytras
public final NamespacedKey newElytra = new NamespacedKey(this, "new");
public SimpleDateFormat dateFormat;
public BlocksMined blocksMined;
public ChunkPopulate chunkPopulate;
public CraftItem craftItem;
@@ -77,10 +73,12 @@ public final class ToolStats extends JavaPlugin {
public final Logger logger = this.getLogger();
public final File configFile = new File(this.getDataFolder(), "config.yml");
public FileConfiguration config;
public final int CONFIG_VERSION = 3;
public final int CONFIG_VERSION = 4;
private BukkitAudiences adventure;
public NumberFormat numberFormat;
@Override
public void onEnable() {
this.adventure = BukkitAudiences.create(this);
@@ -127,19 +125,7 @@ public final class ToolStats extends JavaPlugin {
logger.warning("Your config file is outdated! Please regenerate the config.");
}
String dateFormatConfig = config.getString("date-format");
if (dateFormatConfig != null) {
try {
dateFormat = new SimpleDateFormat(dateFormatConfig, Locale.getDefault());
} catch (IllegalArgumentException exception) {
logger.severe("date-format is NOT a valid format! Using default American English format.");
exception.printStackTrace();
dateFormat = new SimpleDateFormat("M/dd/yyyy", Locale.ENGLISH);
}
} else {
logger.warning("date-format is missing from your config! Using default American English format.");
dateFormat = new SimpleDateFormat("M/dd/yyyy", Locale.ENGLISH);
}
numberFormat = new NumberFormat(this);
}
public void checkForUpdates() {

View File

@@ -18,7 +18,6 @@
package lol.hyper.toolstats.commands;
import lol.hyper.toolstats.ToolStats;
import lol.hyper.toolstats.tools.NumberFormat;
import lol.hyper.toolstats.tools.UUIDDataType;
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
import net.kyori.adventure.text.Component;
@@ -151,7 +150,7 @@ public class CommandToolStats implements TabExecutor {
if (container.has(toolStats.timeCreated, PersistentDataType.LONG)) {
Long time = container.get(toolStats.timeCreated, PersistentDataType.LONG);
if (time != null) {
lore.add(toolStats.getLoreFromConfig("looted.found-on", true).replace("{date}", toolStats.dateFormat.format(new Date(time))));
lore.add(toolStats.getLoreFromConfig("looted.found-on", true).replace("{date}", toolStats.numberFormat.formatDate(new Date(time))));
}
}
finalMeta.setLore(lore);
@@ -193,19 +192,19 @@ public class CommandToolStats implements TabExecutor {
// show how when the item was created based on the previous lore
switch (type) {
case "DEFAULT": {
lore.add(toolStats.getLoreFromConfig("created.created-on", true).replace("{date}", toolStats.dateFormat.format(new Date(time))));
lore.add(toolStats.getLoreFromConfig("created.created-on", true).replace("{date}", toolStats.numberFormat.formatDate(new Date(time))));
break;
}
case "CAUGHT": {
lore.add(toolStats.getLoreFromConfig("fished.caught-on", true).replace("{date}", toolStats.dateFormat.format(new Date(time))));
lore.add(toolStats.getLoreFromConfig("fished.caught-on", true).replace("{date}", toolStats.numberFormat.formatDate(new Date(time))));
break;
}
case "LOOTED": {
lore.add(toolStats.getLoreFromConfig("looted.found-on", true).replace("{date}", toolStats.dateFormat.format(new Date(time))));
lore.add(toolStats.getLoreFromConfig("looted.found-on", true).replace("{date}", toolStats.numberFormat.formatDate(new Date(time))));
break;
}
case "TRADED": {
lore.add(toolStats.getLoreFromConfig("traded.traded-on", true).replace("{date}", toolStats.dateFormat.format(new Date(time))));
lore.add(toolStats.getLoreFromConfig("traded.traded-on", true).replace("{date}", toolStats.numberFormat.formatDate(new Date(time))));
break;
}
}
@@ -216,7 +215,7 @@ public class CommandToolStats implements TabExecutor {
if (container.has(toolStats.swordPlayerKills, PersistentDataType.INTEGER)) {
Integer kills = container.get(toolStats.swordPlayerKills, PersistentDataType.INTEGER);
if (kills != null) {
lore.add(toolStats.getLoreFromConfig("kills.player", true).replace("{kills}", NumberFormat.formatInt(kills)));
lore.add(toolStats.getLoreFromConfig("kills.player", true).replace("{kills}", toolStats.numberFormat.formatInt(kills)));
}
}
}
@@ -224,7 +223,7 @@ public class CommandToolStats implements TabExecutor {
if (container.has(toolStats.swordMobKills, PersistentDataType.INTEGER)) {
Integer kills = container.get(toolStats.swordMobKills, PersistentDataType.INTEGER);
if (kills != null) {
lore.add(toolStats.getLoreFromConfig("kills.mob", true).replace("{kills}", NumberFormat.formatInt(kills)));
lore.add(toolStats.getLoreFromConfig("kills.mob", true).replace("{kills}", toolStats.numberFormat.formatInt(kills)));
}
}
}
@@ -232,7 +231,7 @@ public class CommandToolStats implements TabExecutor {
if (container.has(toolStats.genericMined, PersistentDataType.INTEGER)) {
Integer blocksMined = container.get(toolStats.genericMined, PersistentDataType.INTEGER);
if (blocksMined != null) {
lore.add(toolStats.getLoreFromConfig("blocks-mined", true).replace("{blocks}", NumberFormat.formatInt(blocksMined)));
lore.add(toolStats.getLoreFromConfig("blocks-mined", true).replace("{blocks}", toolStats.numberFormat.formatInt(blocksMined)));
}
}
}
@@ -240,7 +239,7 @@ public class CommandToolStats implements TabExecutor {
if (container.has(toolStats.fishingRodCaught, PersistentDataType.INTEGER)) {
Integer fish = container.get(toolStats.fishingRodCaught, PersistentDataType.INTEGER);
if (fish != null) {
lore.add(toolStats.getLoreFromConfig("fished.fish-caught", true).replace("{fish}", NumberFormat.formatInt(fish)));
lore.add(toolStats.getLoreFromConfig("fished.fish-caught", true).replace("{fish}", toolStats.numberFormat.formatInt(fish)));
}
}
}
@@ -248,7 +247,7 @@ public class CommandToolStats implements TabExecutor {
if (container.has(toolStats.shearsSheared, PersistentDataType.INTEGER)) {
Integer sheep = container.get(toolStats.shearsSheared, PersistentDataType.INTEGER);
if (sheep != null) {
lore.add(toolStats.getLoreFromConfig("sheep-sheared", true).replace("{sheep}", NumberFormat.formatInt(sheep)));
lore.add(toolStats.getLoreFromConfig("sheep-sheared", true).replace("{sheep}", toolStats.numberFormat.formatInt(sheep)));
}
}
}
@@ -256,7 +255,7 @@ public class CommandToolStats implements TabExecutor {
if (container.has(toolStats.armorDamage, PersistentDataType.DOUBLE)) {
Double damage = container.get(toolStats.armorDamage, PersistentDataType.DOUBLE);
if (damage != null) {
lore.add(toolStats.getLoreFromConfig("damage-taken", true).replace("{damage}", NumberFormat.formatDouble(damage)));
lore.add(toolStats.getLoreFromConfig("damage-taken", true).replace("{damage}", toolStats.numberFormat.formatDouble(damage)));
}
}
}

View File

@@ -19,7 +19,6 @@ package lol.hyper.toolstats.events;
import lol.hyper.toolstats.ToolStats;
import lol.hyper.toolstats.tools.ItemChecker;
import lol.hyper.toolstats.tools.NumberFormat;
import org.bukkit.Bukkit;
import org.bukkit.GameMode;
import org.bukkit.Material;
@@ -98,7 +97,7 @@ public class BlocksMined implements Listener {
}
List<String> lore;
String newLine = configLoreRaw.replace("{blocks}", NumberFormat.formatInt(blocksMined));
String newLine = configLoreRaw.replace("{blocks}", toolStats.numberFormat.formatInt(blocksMined));
if (meta.hasLore()) {
lore = meta.getLore();
boolean hasLore = false;

View File

@@ -128,7 +128,7 @@ public class CraftItem implements Listener {
}
// do we add the lore based on the config?
if (toolStats.checkConfig(itemStack, "created-date")) {
lore.add(createdOnRaw.replace("{date}", toolStats.dateFormat.format(finalDate)));
lore.add(createdOnRaw.replace("{date}", toolStats.numberFormat.formatDate(finalDate)));
}
if (toolStats.checkConfig(itemStack, "created-by")) {
lore.add(createdByRaw.replace("{player}", owner.getName()));

View File

@@ -19,7 +19,6 @@ package lol.hyper.toolstats.events;
import lol.hyper.toolstats.ToolStats;
import lol.hyper.toolstats.tools.ItemChecker;
import lol.hyper.toolstats.tools.NumberFormat;
import org.bukkit.Bukkit;
import org.bukkit.GameMode;
import org.bukkit.Material;
@@ -30,7 +29,6 @@ import org.bukkit.event.Listener;
import org.bukkit.event.entity.EntityDamageByBlockEvent;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import org.bukkit.inventory.meta.ItemMeta;
@@ -249,7 +247,7 @@ public class EntityDamage implements Listener {
}
List<String> lore;
String newLine = playerKillsLoreRaw.replace("{kills}", NumberFormat.formatInt(playerKills));
String newLine = playerKillsLoreRaw.replace("{kills}", toolStats.numberFormat.formatInt(playerKills));
if (meta.hasLore()) {
lore = meta.getLore();
boolean hasLore = false;
@@ -315,7 +313,7 @@ public class EntityDamage implements Listener {
}
List<String> lore;
String newLine = mobKillsLoreRaw.replace("{kills}", NumberFormat.formatInt(mobKills));
String newLine = mobKillsLoreRaw.replace("{kills}", toolStats.numberFormat.formatInt(mobKills));
if (meta.hasLore()) {
lore = meta.getLore();
boolean hasLore = false;
@@ -380,7 +378,7 @@ public class EntityDamage implements Listener {
}
List<String> lore;
String newLine = damageTakenLoreRaw.replace("{damage}", NumberFormat.formatDouble(damageTaken));
String newLine = damageTakenLoreRaw.replace("{damage}", toolStats.numberFormat.formatDouble(damageTaken));
if (meta.hasLore()) {
lore = meta.getLore();
boolean hasLore = false;

View File

@@ -160,7 +160,7 @@ public class GenerateLoot implements Listener {
lore = new ArrayList<>();
}
if (toolStats.checkConfig(newItem, "looted-tag")) {
lore.add(foundOnLoreRaw.replace("{date}", toolStats.dateFormat.format(finalDate)));
lore.add(foundOnLoreRaw.replace("{date}", toolStats.numberFormat.formatDate(finalDate)));
lore.add(foundByLoreRaw.replace("{player}", owner.getName()));
}
meta.setLore(lore);

View File

@@ -109,7 +109,7 @@ public class PickupItem implements Listener {
lore = new ArrayList<>();
}
if (toolStats.config.getBoolean("enabled.elytra-tag")) {
lore.add(foundOnLoreRaw.replace("{date}", toolStats.dateFormat.format(finalDate)));
lore.add(foundOnLoreRaw.replace("{date}", toolStats.numberFormat.formatDate(finalDate)));
lore.add(foundByLoreRaw.replace("{player}", owner.getName()));
}
meta.setLore(lore);

View File

@@ -19,7 +19,6 @@ package lol.hyper.toolstats.events;
import lol.hyper.toolstats.ToolStats;
import lol.hyper.toolstats.tools.ItemChecker;
import lol.hyper.toolstats.tools.NumberFormat;
import lol.hyper.toolstats.tools.UUIDDataType;
import org.bukkit.Bukkit;
import org.bukkit.GameMode;
@@ -118,7 +117,7 @@ public class PlayerFish implements Listener {
}
List<String> lore;
String newLine = fishCaughtLoreRaw.replace("{fish}", NumberFormat.formatInt(fishCaught));
String newLine = fishCaughtLoreRaw.replace("{fish}", toolStats.numberFormat.formatInt(fishCaught));
if (meta.hasLore()) {
lore = meta.getLore();
boolean hasLore = false;
@@ -140,19 +139,6 @@ public class PlayerFish implements Listener {
lore = new ArrayList<>();
lore.add(newLine);
}
/*
if (Bukkit.getPluginManager().isPluginEnabled("EvenMoreFish")) {
ListIterator<String> iterator = lore.listIterator();
while (iterator.hasNext()) {
String line = iterator.next();
toolStats.logger.info(line);
if (line.equalsIgnoreCase("§f")) {
iterator.remove();
}
}
}*/
if (toolStats.config.getBoolean("enabled.fish-caught")) {
meta.setLore(lore);
}
@@ -199,7 +185,7 @@ public class PlayerFish implements Listener {
lore = new ArrayList<>();
}
if (toolStats.checkConfig(newItem, "fished-tag")) {
lore.add(caughtOnLoreRaw.replace("{date}", toolStats.dateFormat.format(finalDate)));
lore.add(caughtOnLoreRaw.replace("{date}", toolStats.numberFormat.formatDate(finalDate)));
lore.add(caughtByLoreRaw.replace("{player}", owner.getName()));
meta.setLore(lore);
}

View File

@@ -18,7 +18,6 @@
package lol.hyper.toolstats.events;
import lol.hyper.toolstats.ToolStats;
import lol.hyper.toolstats.tools.NumberFormat;
import org.bukkit.Bukkit;
import org.bukkit.GameMode;
import org.bukkit.Material;
@@ -110,7 +109,7 @@ public class SheepShear implements Listener {
}
List<String> lore;
String newLine = sheepShearedLoreRaw.replace("{sheep}", NumberFormat.formatInt(sheepSheared));
String newLine = sheepShearedLoreRaw.replace("{sheep}", toolStats.numberFormat.formatInt(sheepSheared));
if (meta.hasLore()) {
lore = meta.getLore();
boolean hasLore = false;

View File

@@ -125,7 +125,7 @@ public class VillagerTrade implements Listener {
lore = new ArrayList<>();
}
if (toolStats.checkConfig(newItem, "traded-tag")) {
lore.add(tradedOnLoreRaw.replace("{date}", toolStats.dateFormat.format(finalDate)));
lore.add(tradedOnLoreRaw.replace("{date}", toolStats.numberFormat.formatDate(finalDate)));
lore.add(tradedByLoreRaw.replace("{player}", owner.getName()));
meta.setLore(lore);
}

View File

@@ -17,21 +17,90 @@
package lol.hyper.toolstats.tools;
import lol.hyper.toolstats.ToolStats;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
public class NumberFormat {
private static final DecimalFormat DECIMAL_FORMAT = new DecimalFormat("#,###.00", new DecimalFormatSymbols(Locale.getDefault()));
private static final DecimalFormat COMMA_FORMAT = new DecimalFormat("#,###", new DecimalFormatSymbols(Locale.getDefault()));
private DecimalFormat DECIMAL_FORMAT;
private DecimalFormat COMMA_FORMAT;
private SimpleDateFormat DATE_FORMAT;
public NumberFormat(ToolStats toolStats) {
String dateFormat = toolStats.config.getString("date-format");
String decimalSeparator = toolStats.config.getString("number-formats.decimal-separator");
String commaSeparator = toolStats.config.getString("number-formats.comma-separator");
String commaFormat = toolStats.config.getString("number-formats.comma-format");
String decimalFormat = toolStats.config.getString("number-formats.decimal-format");
if (dateFormat == null) {
dateFormat = "M/dd/yyyy";
toolStats.logger.warning("date-format is missing! Using default American English format.");
}
if (decimalSeparator == null) {
decimalSeparator = ".";
toolStats.logger.warning("number-formats.decimal-separator is missing! Using default \".\" instead.");
}
if (commaSeparator == null) {
commaSeparator = ",";
toolStats.logger.warning("number-formats.comma-separator is missing! Using default \",\" instead.");
}
if (commaFormat == null) {
commaFormat = "#,###";
toolStats.logger.warning("number-formats.comma-format is missing! Using default #,### instead.");
}
if (decimalFormat == null) {
decimalFormat = "#,###.00";
toolStats.logger.warning("number-formats.comma-separator is missing! Using default #,###.00 instead.");
}
try {
DATE_FORMAT = new SimpleDateFormat(dateFormat, Locale.getDefault());
} catch (NullPointerException | IllegalArgumentException exception) {
toolStats.logger.warning("date-format is NOT a valid format! Using default American English format.");
exception.printStackTrace();
DATE_FORMAT = new SimpleDateFormat("M/dd/yyyy", Locale.ENGLISH);
}
DecimalFormatSymbols formatSymbols = new DecimalFormatSymbols(Locale.getDefault());
formatSymbols.setDecimalSeparator(decimalSeparator.charAt(0));
formatSymbols.setGroupingSeparator(commaSeparator.charAt(0));
try {
COMMA_FORMAT = new DecimalFormat(commaFormat, formatSymbols);
} catch (NullPointerException | IllegalArgumentException exception) {
toolStats.logger.warning("number-formats.comma-format is NOT a valid format! Using default #,### instead.");
exception.printStackTrace();
COMMA_FORMAT = new DecimalFormat("#,###", formatSymbols);
}
try {
DECIMAL_FORMAT = new DecimalFormat(decimalFormat, formatSymbols);
} catch (NullPointerException | IllegalArgumentException exception) {
toolStats.logger.warning("number-formats.decimal-format is NOT a valid format! Using default #,###.00 instead.");
exception.printStackTrace();
DECIMAL_FORMAT = new DecimalFormat("#,###.00", formatSymbols);
}
}
/**
* Formats a number to make it pretty. Example: 4322 to 4,322
*
* @param number The number to format.
* @return The formatted number.
*/
public static String formatInt(int number) {
public String formatInt(int number) {
String finalNumber = COMMA_FORMAT.format(number);
finalNumber = finalNumber.replaceAll("[\\x{202f}\\x{00A0}]", " ");
return finalNumber;
@@ -39,12 +108,23 @@ public class NumberFormat {
/**
* Formats a number to make it pretty. Example: 4322.33 to 4,322.33
*
* @param number The number to format.
* @return The formatted number.
*/
public static String formatDouble(double number) {
public String formatDouble(double number) {
String finalNumber = DECIMAL_FORMAT.format(number);
finalNumber = finalNumber.replaceAll("[\\x{202f}\\x{00A0}]", " ");
return finalNumber;
}
/**
* Formats a date into the readable format.
*
* @param date The date to format.
* @return The date into a readable format.
*/
public String formatDate(Date date) {
return DATE_FORMAT.format(date);
}
}

View File

@@ -103,4 +103,12 @@ messages:
# Example: "dd/MM/yyyy"
date-format: "M/dd/yyyy"
config-version: 3
# Change number formatting.
# You probably do not need to touch this.
number-formats:
comma-separator: ","
decimal-separator: "."
comma-format: "#,###"
decimal-format: "#,###.00"
config-version: 4