Skip to content

Commit 7d5f73a

Browse files
committed
better number formatting
1 parent b581d9d commit 7d5f73a

File tree

6 files changed

+27
-29
lines changed

6 files changed

+27
-29
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333

3434
import java.io.File;
3535
import java.io.IOException;
36+
import java.text.DecimalFormat;
3637
import java.text.SimpleDateFormat;
3738
import java.util.Locale;
3839
import java.util.logging.Logger;
@@ -59,6 +60,8 @@ public final class ToolStats extends JavaPlugin {
5960
public final NamespacedKey newElytra = new NamespacedKey(this, "new");
6061

6162
public final SimpleDateFormat dateFormat = new SimpleDateFormat("M/dd/yyyy", Locale.ENGLISH);
63+
public final DecimalFormat decimalFormat = new DecimalFormat("#,###.00");
64+
public final DecimalFormat commaFormat = new DecimalFormat("#,###");
6265

6366
public BlocksMined blocksMined;
6467
public ChunkPopulate chunkPopulate;

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -207,47 +207,47 @@ private void fixItemLore(ItemStack original, Player player) {
207207
if (container.has(toolStats.swordPlayerKills, PersistentDataType.INTEGER)) {
208208
Integer kills = container.get(toolStats.swordPlayerKills, PersistentDataType.INTEGER);
209209
if (kills != null) {
210-
lore.add(toolStats.getLoreFromConfig("kills.player", true).replace("{kills}", Integer.toString(kills)));
210+
lore.add(toolStats.getLoreFromConfig("kills.player", true).replace("{kills}", toolStats.commaFormat.format(kills)));
211211
}
212212
}
213213
}
214214
if (toolStats.checkConfig(original, "mob-kills")) {
215215
if (container.has(toolStats.swordMobKills, PersistentDataType.INTEGER)) {
216216
Integer kills = container.get(toolStats.swordMobKills, PersistentDataType.INTEGER);
217217
if (kills != null) {
218-
lore.add(toolStats.getLoreFromConfig("kills.mob", true).replace("{kills}", Integer.toString(kills)));
218+
lore.add(toolStats.getLoreFromConfig("kills.mob", true).replace("{kills}", toolStats.commaFormat.format(kills)));
219219
}
220220
}
221221
}
222222
if (toolStats.checkConfig(original, "blocks-mined")) {
223223
if (container.has(toolStats.genericMined, PersistentDataType.INTEGER)) {
224224
Integer blocksMined = container.get(toolStats.genericMined, PersistentDataType.INTEGER);
225225
if (blocksMined != null) {
226-
lore.add(toolStats.getLoreFromConfig("blocks-mined", true).replace("{blocks}", Integer.toString(blocksMined)));
226+
lore.add(toolStats.getLoreFromConfig("blocks-mined", true).replace("{blocks}", toolStats.commaFormat.format(blocksMined)));
227227
}
228228
}
229229
}
230230
if (toolStats.config.getBoolean("enabled.fish-caught")) {
231231
if (container.has(toolStats.fishingRodCaught, PersistentDataType.INTEGER)) {
232232
Integer fish = container.get(toolStats.fishingRodCaught, PersistentDataType.INTEGER);
233233
if (fish != null) {
234-
lore.add(toolStats.getLoreFromConfig("fished.fish-caught", true).replace("{fish}", Integer.toString(fish)));
234+
lore.add(toolStats.getLoreFromConfig("fished.fish-caught", true).replace("{fish}", toolStats.commaFormat.format(fish)));
235235
}
236236
}
237237
}
238238
if (toolStats.config.getBoolean("enabled.sheep-sheared")) {
239239
if (container.has(toolStats.shearsSheared, PersistentDataType.INTEGER)) {
240240
Integer sheep = container.get(toolStats.shearsSheared, PersistentDataType.INTEGER);
241241
if (sheep != null) {
242-
lore.add(toolStats.getLoreFromConfig("sheep-sheared", true).replace("{sheep}", Integer.toString(sheep)));
242+
lore.add(toolStats.getLoreFromConfig("sheep-sheared", true).replace("{sheep}", toolStats.commaFormat.format(sheep)));
243243
}
244244
}
245245
}
246246
if (toolStats.config.getBoolean("enabled.armor-damage")) {
247247
if (container.has(toolStats.armorDamage, PersistentDataType.INTEGER)) {
248248
Integer damage = container.get(toolStats.armorDamage, PersistentDataType.INTEGER);
249249
if (damage != null) {
250-
lore.add(toolStats.getLoreFromConfig("damage-taken", true).replace("{damage}", Integer.toString(damage)));
250+
lore.add(toolStats.getLoreFromConfig("damage-taken", true).replace("{damage}", toolStats.commaFormat.format(damage)));
251251
}
252252
}
253253
}

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ private void updateBlocksMined(ItemStack itemStack) {
8181
blocksMined++;
8282
}
8383
container.set(toolStats.genericMined, PersistentDataType.INTEGER, blocksMined);
84-
8584
String configLore = toolStats.getLoreFromConfig("blocks-mined", false);
8685
String configLoreRaw = toolStats.getLoreFromConfig("blocks-mined", true);
8786

@@ -100,18 +99,18 @@ private void updateBlocksMined(ItemStack itemStack) {
10099
for (int x = 0; x < lore.size(); x++) {
101100
if (lore.get(x).contains(configLore)) {
102101
hasLore = true;
103-
lore.set(x, configLoreRaw.replace("{blocks}", Integer.toString(blocksMined)));
102+
lore.set(x, configLoreRaw.replace("{blocks}", toolStats.commaFormat.format(blocksMined)));
104103
break;
105104
}
106105
}
107106
// if the item has lore but doesn't have the tag, add it
108107
if (!hasLore) {
109-
lore.add(configLoreRaw.replace("{blocks}", Integer.toString(blocksMined)));
108+
lore.add(configLoreRaw.replace("{blocks}", toolStats.commaFormat.format(blocksMined)));
110109
}
111110
} else {
112111
// if the item has no lore, create a new list and add the string
113112
lore = new ArrayList<>();
114-
lore.add(configLoreRaw.replace("{blocks}", Integer.toString(blocksMined)));
113+
lore.add(configLoreRaw.replace("{blocks}", toolStats.commaFormat.format(blocksMined)));
115114
}
116115
// do we add the lore based on the config?
117116
if (toolStats.checkConfig(itemStack, "blocks-mined")) {

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

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,11 @@
3333
import org.bukkit.persistence.PersistentDataContainer;
3434
import org.bukkit.persistence.PersistentDataType;
3535

36-
import java.math.RoundingMode;
37-
import java.text.DecimalFormat;
3836
import java.util.*;
3937

4038
public class EntityDamage implements Listener {
4139

4240
private final ToolStats toolStats;
43-
private final DecimalFormat decimalFormat = new DecimalFormat("0.00");
4441
public final Set<UUID> trackedMobs = new HashSet<>();
4542

4643
public EntityDamage(ToolStats toolStats) {
@@ -227,18 +224,18 @@ private ItemStack updatePlayerKills(ItemStack itemStack) {
227224
for (int x = 0; x < lore.size(); x++) {
228225
if (lore.get(x).contains(playerKillsLore)) {
229226
hasLore = true;
230-
lore.set(x, playerKillsLoreRaw.replace("{kills}", Integer.toString(playerKills)));
227+
lore.set(x, playerKillsLoreRaw.replace("{kills}", toolStats.commaFormat.format(playerKills)));
231228
break;
232229
}
233230
}
234231
// if the item has lore but doesn't have the tag, add it
235232
if (!hasLore) {
236-
lore.add(playerKillsLoreRaw.replace("{kills}", Integer.toString(playerKills)));
233+
lore.add(playerKillsLoreRaw.replace("{kills}", toolStats.commaFormat.format(playerKills)));
237234
}
238235
} else {
239236
// if the item has no lore, create a new list and add the string
240237
lore = new ArrayList<>();
241-
lore.add(playerKillsLoreRaw.replace("{kills}", Integer.toString(playerKills)));
238+
lore.add(playerKillsLoreRaw.replace("{kills}", toolStats.commaFormat.format(playerKills)));
242239
}
243240
// do we add the lore based on the config?
244241
if (toolStats.checkConfig(itemStack, "player-kills")) {
@@ -290,18 +287,18 @@ private ItemStack updateMobKills(ItemStack itemStack) {
290287
for (int x = 0; x < lore.size(); x++) {
291288
if (lore.get(x).contains(mobKillsLore)) {
292289
hasLore = true;
293-
lore.set(x, mobKillsLoreRaw.replace("{kills}", Integer.toString(mobKills)));
290+
lore.set(x, mobKillsLoreRaw.replace("{kills}", toolStats.commaFormat.format(mobKills)));
294291
break;
295292
}
296293
}
297294
// if the item has lore but doesn't have the tag, add it
298295
if (!hasLore) {
299-
lore.add(mobKillsLoreRaw.replace("{kills}", Integer.toString(mobKills)));
296+
lore.add(mobKillsLoreRaw.replace("{kills}", toolStats.commaFormat.format(mobKills)));
300297
}
301298
} else {
302299
// if the item has no lore, create a new list and add the string
303300
lore = new ArrayList<>();
304-
lore.add(mobKillsLoreRaw.replace("{kills}", Integer.toString(mobKills)));
301+
lore.add(mobKillsLoreRaw.replace("{kills}", toolStats.commaFormat.format(mobKills)));
305302
}
306303
// do we add the lore based on the config?
307304
if (toolStats.checkConfig(itemStack, "mob-kills")) {
@@ -332,7 +329,6 @@ private void updateArmorDamage(ItemStack itemStack, double damage) {
332329
} else {
333330
damageTaken = damageTaken + damage;
334331
}
335-
decimalFormat.setRoundingMode(RoundingMode.DOWN);
336332
container.set(toolStats.armorDamage, PersistentDataType.DOUBLE, damageTaken);
337333

338334
String damageTakenLore = toolStats.getLoreFromConfig("damage-taken", false);
@@ -353,18 +349,18 @@ private void updateArmorDamage(ItemStack itemStack, double damage) {
353349
for (int x = 0; x < lore.size(); x++) {
354350
if (lore.get(x).contains(damageTakenLore)) {
355351
hasLore = true;
356-
lore.set(x, damageTakenLoreRaw.replace("{damage}", decimalFormat.format(damageTaken)));
352+
lore.set(x, damageTakenLoreRaw.replace("{damage}", toolStats.decimalFormat.format(damageTaken)));
357353
break;
358354
}
359355
}
360356
// if the item has lore but doesn't have the tag, add it
361357
if (!hasLore) {
362-
lore.add(damageTakenLoreRaw.replace("{damage}", decimalFormat.format(damageTaken)));
358+
lore.add(damageTakenLoreRaw.replace("{damage}", toolStats.decimalFormat.format(damageTaken)));
363359
}
364360
} else {
365361
// if the item has no lore, create a new list and add the string
366362
lore = new ArrayList<>();
367-
lore.add(damageTakenLoreRaw.replace("{damage}", decimalFormat.format(damageTaken)));
363+
lore.add(damageTakenLoreRaw.replace("{damage}", toolStats.decimalFormat.format(damageTaken)));
368364
}
369365
if (toolStats.config.getBoolean("enabled.armor-damage")) {
370366
meta.setLore(lore);

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,18 +112,18 @@ private void updateFishCount(ItemStack itemStack) {
112112
for (int x = 0; x < lore.size(); x++) {
113113
if (lore.get(x).contains(fishCaughtLore)) {
114114
hasLore = true;
115-
lore.set(x, fishCaughtLoreRaw.replace("{fish}", Integer.toString(fishCaught)));
115+
lore.set(x, fishCaughtLoreRaw.replace("{fish}", toolStats.commaFormat.format(fishCaught)));
116116
break;
117117
}
118118
}
119119
// if the item has lore but doesn't have the tag, add it
120120
if (!hasLore) {
121-
lore.add(fishCaughtLoreRaw.replace("{fish}", Integer.toString(fishCaught)));
121+
lore.add(fishCaughtLoreRaw.replace("{fish}", toolStats.commaFormat.format(fishCaught)));
122122
}
123123
} else {
124124
// if the item has no lore, create a new list and add the string
125125
lore = new ArrayList<>();
126-
lore.add(fishCaughtLoreRaw.replace("{fish}", Integer.toString(fishCaught)));
126+
lore.add(fishCaughtLoreRaw.replace("{fish}", toolStats.commaFormat.format(fishCaught)));
127127
}
128128
if (toolStats.config.getBoolean("enabled.fish-caught")) {
129129
meta.setLore(lore);

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,18 +108,18 @@ private void addLore(ItemStack itemStack) {
108108
for (int x = 0; x < lore.size(); x++) {
109109
if (lore.get(x).contains(sheepShearedLore)) {
110110
hasLore = true;
111-
lore.set(x, sheepShearedLoreRaw.replace("{sheep}", Integer.toString(sheepSheared)));
111+
lore.set(x, sheepShearedLoreRaw.replace("{sheep}", toolStats.commaFormat.format(sheepSheared)));
112112
break;
113113
}
114114
}
115115
// if the item has lore but doesn't have the tag, add it
116116
if (!hasLore) {
117-
lore.add(sheepShearedLoreRaw.replace("{sheep}", Integer.toString(sheepSheared)));
117+
lore.add(sheepShearedLoreRaw.replace("{sheep}", toolStats.commaFormat.format(sheepSheared)));
118118
}
119119
} else {
120120
// if the item has no lore, create a new list and add the string
121121
lore = new ArrayList<>();
122-
lore.add(sheepShearedLoreRaw.replace("{sheep}", Integer.toString(sheepSheared)));
122+
lore.add(sheepShearedLoreRaw.replace("{sheep}", toolStats.commaFormat.format(sheepSheared)));
123123
}
124124
if (toolStats.config.getBoolean("enabled.sheep-sheared")) {
125125
meta.setLore(lore);

0 commit comments

Comments
 (0)