Skip to content

Commit 1b1b45e

Browse files
committed
even BETTER number formatting?!?!
closes #19
1 parent 2de2ae8 commit 1b1b45e

File tree

7 files changed

+73
-16
lines changed

7 files changed

+73
-16
lines changed

src/main/java/lol/hyper/toolstats/ToolStats.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,6 @@ public final class ToolStats extends JavaPlugin {
6161
public final NamespacedKey newElytra = new NamespacedKey(this, "new");
6262

6363
public SimpleDateFormat dateFormat;
64-
public final DecimalFormat decimalFormat = new DecimalFormat("#,###.00", new DecimalFormatSymbols(Locale.getDefault()));
65-
public final DecimalFormat commaFormat = new DecimalFormat("#,###", new DecimalFormatSymbols(Locale.getDefault()));
6664
public BlocksMined blocksMined;
6765
public ChunkPopulate chunkPopulate;
6866
public CraftItem craftItem;
@@ -121,8 +119,6 @@ public void onEnable() {
121119
new Metrics(this, 14110);
122120

123121
Bukkit.getScheduler().runTaskAsynchronously(this, this::checkForUpdates);
124-
125-
logger.info("Locale: " + Locale.getDefault());
126122
}
127123

128124
public void loadConfig() {

src/main/java/lol/hyper/toolstats/commands/CommandToolStats.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package lol.hyper.toolstats.commands;
1919

2020
import lol.hyper.toolstats.ToolStats;
21+
import lol.hyper.toolstats.tools.NumberFormat;
2122
import lol.hyper.toolstats.tools.UUIDDataType;
2223
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
2324
import net.kyori.adventure.text.Component;
@@ -215,47 +216,47 @@ private void fixItemLore(ItemStack original, Player player) {
215216
if (container.has(toolStats.swordPlayerKills, PersistentDataType.INTEGER)) {
216217
Integer kills = container.get(toolStats.swordPlayerKills, PersistentDataType.INTEGER);
217218
if (kills != null) {
218-
lore.add(toolStats.getLoreFromConfig("kills.player", true).replace("{kills}", toolStats.commaFormat.format(kills)));
219+
lore.add(toolStats.getLoreFromConfig("kills.player", true).replace("{kills}", NumberFormat.formatInt(kills)));
219220
}
220221
}
221222
}
222223
if (toolStats.checkConfig(original, "mob-kills")) {
223224
if (container.has(toolStats.swordMobKills, PersistentDataType.INTEGER)) {
224225
Integer kills = container.get(toolStats.swordMobKills, PersistentDataType.INTEGER);
225226
if (kills != null) {
226-
lore.add(toolStats.getLoreFromConfig("kills.mob", true).replace("{kills}", toolStats.commaFormat.format(kills)));
227+
lore.add(toolStats.getLoreFromConfig("kills.mob", true).replace("{kills}", NumberFormat.formatInt(kills)));
227228
}
228229
}
229230
}
230231
if (toolStats.checkConfig(original, "blocks-mined")) {
231232
if (container.has(toolStats.genericMined, PersistentDataType.INTEGER)) {
232233
Integer blocksMined = container.get(toolStats.genericMined, PersistentDataType.INTEGER);
233234
if (blocksMined != null) {
234-
lore.add(toolStats.getLoreFromConfig("blocks-mined", true).replace("{blocks}", toolStats.commaFormat.format(blocksMined)));
235+
lore.add(toolStats.getLoreFromConfig("blocks-mined", true).replace("{blocks}", NumberFormat.formatInt(blocksMined)));
235236
}
236237
}
237238
}
238239
if (toolStats.config.getBoolean("enabled.fish-caught")) {
239240
if (container.has(toolStats.fishingRodCaught, PersistentDataType.INTEGER)) {
240241
Integer fish = container.get(toolStats.fishingRodCaught, PersistentDataType.INTEGER);
241242
if (fish != null) {
242-
lore.add(toolStats.getLoreFromConfig("fished.fish-caught", true).replace("{fish}", toolStats.commaFormat.format(fish)));
243+
lore.add(toolStats.getLoreFromConfig("fished.fish-caught", true).replace("{fish}", NumberFormat.formatInt(fish)));
243244
}
244245
}
245246
}
246247
if (toolStats.config.getBoolean("enabled.sheep-sheared")) {
247248
if (container.has(toolStats.shearsSheared, PersistentDataType.INTEGER)) {
248249
Integer sheep = container.get(toolStats.shearsSheared, PersistentDataType.INTEGER);
249250
if (sheep != null) {
250-
lore.add(toolStats.getLoreFromConfig("sheep-sheared", true).replace("{sheep}", toolStats.commaFormat.format(sheep)));
251+
lore.add(toolStats.getLoreFromConfig("sheep-sheared", true).replace("{sheep}", NumberFormat.formatInt(sheep)));
251252
}
252253
}
253254
}
254255
if (toolStats.config.getBoolean("enabled.armor-damage")) {
255256
if (container.has(toolStats.armorDamage, PersistentDataType.DOUBLE)) {
256257
Double damage = container.get(toolStats.armorDamage, PersistentDataType.DOUBLE);
257258
if (damage != null) {
258-
lore.add(toolStats.getLoreFromConfig("damage-taken", true).replace("{damage}", toolStats.commaFormat.format(damage)));
259+
lore.add(toolStats.getLoreFromConfig("damage-taken", true).replace("{damage}", NumberFormat.formatDouble(damage)));
259260
}
260261
}
261262
}

src/main/java/lol/hyper/toolstats/events/BlocksMined.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import lol.hyper.toolstats.ToolStats;
2121
import lol.hyper.toolstats.tools.ItemChecker;
22+
import lol.hyper.toolstats.tools.NumberFormat;
2223
import org.bukkit.GameMode;
2324
import org.bukkit.Material;
2425
import org.bukkit.entity.Player;
@@ -98,7 +99,7 @@ private void updateBlocksMined(ItemStack playerTool) {
9899
}
99100

100101
List<String> lore;
101-
String newLine = configLoreRaw.replace("{blocks}", toolStats.thousandsFormat.format(blocksMined));
102+
String newLine = configLoreRaw.replace("{blocks}", NumberFormat.formatInt(blocksMined));
102103
if (meta.hasLore()) {
103104
lore = meta.getLore();
104105
boolean hasLore = false;

src/main/java/lol/hyper/toolstats/events/EntityDamage.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import lol.hyper.toolstats.ToolStats;
2121
import lol.hyper.toolstats.tools.ItemChecker;
22+
import lol.hyper.toolstats.tools.NumberFormat;
2223
import org.bukkit.GameMode;
2324
import org.bukkit.Material;
2425
import org.bukkit.entity.*;
@@ -243,7 +244,7 @@ private ItemStack updatePlayerKills(ItemStack itemStack) {
243244
}
244245

245246
List<String> lore;
246-
String newLine = playerKillsLoreRaw.replace("{kills}", toolStats.commaFormat.format(playerKills));
247+
String newLine = playerKillsLoreRaw.replace("{kills}", NumberFormat.formatInt(playerKills));
247248
if (meta.hasLore()) {
248249
lore = meta.getLore();
249250
boolean hasLore = false;
@@ -311,7 +312,7 @@ private ItemStack updateMobKills(ItemStack itemStack) {
311312
}
312313

313314
List<String> lore;
314-
String newLine = mobKillsLoreRaw.replace("{kills}", toolStats.commaFormat.format(mobKills));
315+
String newLine = mobKillsLoreRaw.replace("{kills}", NumberFormat.formatInt(mobKills));
315316
if (meta.hasLore()) {
316317
lore = meta.getLore();
317318
boolean hasLore = false;
@@ -378,7 +379,7 @@ private void updateArmorDamage(ItemStack itemStack, double damage) {
378379
}
379380

380381
List<String> lore;
381-
String newLine = damageTakenLoreRaw.replace("{damage}", toolStats.decimalFormat.format(damageTaken));
382+
String newLine = damageTakenLoreRaw.replace("{damage}", NumberFormat.formatDouble(damageTaken));
382383
if (meta.hasLore()) {
383384
lore = meta.getLore();
384385
boolean hasLore = false;

src/main/java/lol/hyper/toolstats/events/PlayerFish.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import lol.hyper.toolstats.ToolStats;
2121
import lol.hyper.toolstats.tools.ItemChecker;
22+
import lol.hyper.toolstats.tools.NumberFormat;
2223
import lol.hyper.toolstats.tools.UUIDDataType;
2324
import org.bukkit.GameMode;
2425
import org.bukkit.Material;
@@ -115,7 +116,7 @@ private void updateFishCount(ItemStack fishingRod) {
115116
}
116117

117118
List<String> lore;
118-
String newLine = fishCaughtLoreRaw.replace("{fish}", toolStats.commaFormat.format(fishCaught));
119+
String newLine = fishCaughtLoreRaw.replace("{fish}", NumberFormat.formatInt(fishCaught));
119120
if (meta.hasLore()) {
120121
lore = meta.getLore();
121122
boolean hasLore = false;

src/main/java/lol/hyper/toolstats/events/SheepShear.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package lol.hyper.toolstats.events;
1919

2020
import lol.hyper.toolstats.ToolStats;
21+
import lol.hyper.toolstats.tools.NumberFormat;
2122
import org.bukkit.GameMode;
2223
import org.bukkit.Material;
2324
import org.bukkit.entity.Entity;
@@ -110,7 +111,7 @@ private ItemStack addLore(ItemStack oldShears) {
110111
}
111112

112113
List<String> lore;
113-
String newLine = sheepShearedLoreRaw.replace("{sheep}", toolStats.commaFormat.format(sheepSheared));
114+
String newLine = sheepShearedLoreRaw.replace("{sheep}", NumberFormat.formatInt(sheepSheared));
114115
if (meta.hasLore()) {
115116
lore = meta.getLore();
116117
boolean hasLore = false;
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* This file is part of ToolStats.
3+
*
4+
* ToolStats is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* ToolStats is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with ToolStats. If not, see <https://www.gnu.org/licenses/>.
16+
*/
17+
18+
package lol.hyper.toolstats.tools;
19+
20+
import java.text.DecimalFormat;
21+
import java.text.DecimalFormatSymbols;
22+
import java.util.Locale;
23+
24+
public class NumberFormat {
25+
26+
private static final DecimalFormat DECIMAL_FORMAT = new DecimalFormat("#,###.00", new DecimalFormatSymbols(Locale.getDefault()));
27+
private static final DecimalFormat COMMA_FORMAT = new DecimalFormat("#,###", new DecimalFormatSymbols(Locale.getDefault()));
28+
29+
/**
30+
* Formats a number to make it pretty. Example: 4322 to 4,322
31+
* @param number The number to format.
32+
* @return The formatted number.
33+
*/
34+
public static String formatInt(int number) {
35+
String finalNumber = COMMA_FORMAT.format(number);
36+
// hardcode French system because Minecraft bad
37+
if (Locale.getDefault() == Locale.FRANCE || Locale.getDefault() == Locale.FRENCH) {
38+
finalNumber = finalNumber.replaceAll("[\\x{202f}\\x{00A0}]", " ");
39+
}
40+
return finalNumber;
41+
}
42+
43+
/**
44+
* Formats a number to make it pretty. Example: 4322.33 to 4,322.33
45+
* @param number The number to format.
46+
* @return The formatted number.
47+
*/
48+
public static String formatDouble(double number) {
49+
String finalNumber = DECIMAL_FORMAT.format(number);
50+
// hardcode French system because Minecraft bad
51+
if (Locale.getDefault() == Locale.FRANCE || Locale.getDefault() == Locale.FRENCH) {
52+
finalNumber = finalNumber.replaceAll("[\\x{202f}\\x{00A0}]", " ");
53+
}
54+
return finalNumber;
55+
}
56+
}

0 commit comments

Comments
 (0)