From c9f6aba915cc3d689702c1709f748147ce56dd86 Mon Sep 17 00:00:00 2001 From: Evan Debenham Date: Fri, 24 Feb 2023 12:17:48 -0500 Subject: [PATCH] v2.0.0: code cleanup on use of DecimalFormat --- .../shatteredpixeldungeon/actors/buffs/Berserk.java | 4 +--- .../shatteredpixeldungeon/actors/buffs/Buff.java | 5 +---- .../shatteredpixeldungeon/actors/buffs/Chill.java | 4 +--- .../items/armor/ClassArmor.java | 3 +-- .../items/rings/RingOfAccuracy.java | 6 ++---- .../items/rings/RingOfArcana.java | 6 ++---- .../items/rings/RingOfElements.java | 5 ++--- .../items/rings/RingOfEnergy.java | 6 ++---- .../items/rings/RingOfEvasion.java | 6 ++---- .../items/rings/RingOfFuror.java | 6 ++---- .../items/rings/RingOfHaste.java | 6 ++---- .../items/rings/RingOfMight.java | 6 ++---- .../items/rings/RingOfSharpshooting.java | 6 ++---- .../items/rings/RingOfTenacity.java | 6 ++---- .../items/rings/RingOfWealth.java | 5 ++--- .../shatteredpixeldungeon/messages/Messages.java | 12 ++++++++++++ 16 files changed, 38 insertions(+), 54 deletions(-) diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/buffs/Berserk.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/buffs/Berserk.java index 9032dfcf7..405dbc552 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/buffs/Berserk.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/buffs/Berserk.java @@ -38,8 +38,6 @@ import com.watabou.noosa.audio.Sample; import com.watabou.utils.Bundle; import com.watabou.utils.GameMath; -import java.text.DecimalFormat; - public class Berserk extends Buff implements ActionIndicator.Action { { @@ -302,7 +300,7 @@ public class Berserk extends Buff implements ActionIndicator.Action { return (int)(power*100) + "%"; case RECOVERING: if (levelRecovery > 0) { - return new DecimalFormat("#.#").format(levelRecovery); + return Messages.decimalFormat("#.##", levelRecovery); } else { return Integer.toString(turnRecovery); } diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/buffs/Buff.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/buffs/Buff.java index 252f79da3..95cc74d8e 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/buffs/Buff.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/buffs/Buff.java @@ -28,10 +28,7 @@ import com.shatteredpixel.shatteredpixeldungeon.ui.BuffIndicator; import com.watabou.noosa.Image; import com.watabou.utils.Reflection; -import java.text.DecimalFormat; -import java.text.DecimalFormatSymbols; import java.util.HashSet; -import java.util.Locale; public class Buff extends Actor { @@ -135,7 +132,7 @@ public class Buff extends Actor { //to handle the common case of showing how many turns are remaining in a buff description. protected String dispTurns(float input){ - return new DecimalFormat("#.##", DecimalFormatSymbols.getInstance(Locale.ENGLISH)).format(input); + return Messages.decimalFormat("#.##", input); } //buffs act after the hero, so it is often useful to use cooldown+1 when display buff time remaining diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/buffs/Chill.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/buffs/Chill.java index 300194fef..77ba3bedc 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/buffs/Chill.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/actors/buffs/Chill.java @@ -26,8 +26,6 @@ import com.shatteredpixel.shatteredpixeldungeon.messages.Messages; import com.shatteredpixel.shatteredpixeldungeon.sprites.CharSprite; import com.shatteredpixel.shatteredpixeldungeon.ui.BuffIndicator; -import java.text.DecimalFormat; - public class Chill extends FlavourBuff { public static final float DURATION = 10f; @@ -67,6 +65,6 @@ public class Chill extends FlavourBuff { @Override public String desc() { - return Messages.get(this, "desc", dispTurns(), new DecimalFormat("#.##").format((1f-speedFactor())*100f)); + return Messages.get(this, "desc", dispTurns(), Messages.decimalFormat("#.##", (1f-speedFactor())*100f)); } } diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/armor/ClassArmor.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/armor/ClassArmor.java index 751d947c7..f7b040440 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/armor/ClassArmor.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/armor/ClassArmor.java @@ -46,7 +46,6 @@ import com.shatteredpixel.shatteredpixeldungeon.windows.WndOptions; import com.watabou.noosa.audio.Sample; import com.watabou.utils.Bundle; -import java.text.DecimalFormat; import java.util.ArrayList; abstract public class ClassArmor extends Armor { @@ -270,7 +269,7 @@ abstract public class ClassArmor extends Armor { if (ability != null) { desc += "\n\n" + ability.shortDesc(); float chargeUse = ability.chargeUse(Dungeon.hero); - desc += " " + Messages.get(this, "charge_use", new DecimalFormat("#.##").format(chargeUse)); + desc += " " + Messages.get(this, "charge_use", Messages.decimalFormat("#.##", chargeUse)); } else { desc += "\n\n" + "_" + Messages.get(this, "no_ability") + "_"; } diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/rings/RingOfAccuracy.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/rings/RingOfAccuracy.java index 56271dd4b..04fe641f4 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/rings/RingOfAccuracy.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/rings/RingOfAccuracy.java @@ -25,8 +25,6 @@ import com.shatteredpixel.shatteredpixeldungeon.actors.Char; import com.shatteredpixel.shatteredpixeldungeon.messages.Messages; import com.shatteredpixel.shatteredpixeldungeon.sprites.ItemSpriteSheet; -import java.text.DecimalFormat; - public class RingOfAccuracy extends Ring { { @@ -35,9 +33,9 @@ public class RingOfAccuracy extends Ring { public String statsInfo() { if (isIdentified()){ - return Messages.get(this, "stats", new DecimalFormat("#.##").format(100f * (Math.pow(1.3f, soloBuffedBonus()) - 1f))); + return Messages.get(this, "stats", Messages.decimalFormat("#.##", 100f * (Math.pow(1.3f, soloBuffedBonus()) - 1f))); } else { - return Messages.get(this, "typical_stats", new DecimalFormat("#.##").format(30f)); + return Messages.get(this, "typical_stats", Messages.decimalFormat("#.##", 30f)); } } diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/rings/RingOfArcana.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/rings/RingOfArcana.java index cf60cf3b0..33052e777 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/rings/RingOfArcana.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/rings/RingOfArcana.java @@ -25,8 +25,6 @@ import com.shatteredpixel.shatteredpixeldungeon.actors.Char; import com.shatteredpixel.shatteredpixeldungeon.messages.Messages; import com.shatteredpixel.shatteredpixeldungeon.sprites.ItemSpriteSheet; -import java.text.DecimalFormat; - public class RingOfArcana extends Ring { { @@ -35,9 +33,9 @@ public class RingOfArcana extends Ring { public String statsInfo() { if (isIdentified()){ - return Messages.get(this, "stats", new DecimalFormat("#.##").format(100f * (Math.pow(1.175f, soloBuffedBonus()) - 1f))); + return Messages.get(this, "stats", Messages.decimalFormat("#.##", 100f * (Math.pow(1.175f, soloBuffedBonus()) - 1f))); } else { - return Messages.get(this, "typical_stats", new DecimalFormat("#.##").format(175f)); + return Messages.get(this, "typical_stats", Messages.decimalFormat("#.##", 175f)); } } diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/rings/RingOfElements.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/rings/RingOfElements.java index 64b723792..0ac86d745 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/rings/RingOfElements.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/rings/RingOfElements.java @@ -35,7 +35,6 @@ import com.shatteredpixel.shatteredpixeldungeon.items.armor.glyphs.AntiMagic; import com.shatteredpixel.shatteredpixeldungeon.messages.Messages; import com.shatteredpixel.shatteredpixeldungeon.sprites.ItemSpriteSheet; -import java.text.DecimalFormat; import java.util.HashSet; public class RingOfElements extends Ring { @@ -46,9 +45,9 @@ public class RingOfElements extends Ring { public String statsInfo() { if (isIdentified()){ - return Messages.get(this, "stats", new DecimalFormat("#.##").format(100f * (1f - Math.pow(0.825f, soloBuffedBonus())))); + return Messages.get(this, "stats", Messages.decimalFormat("#.##", 100f * (1f - Math.pow(0.825f, soloBuffedBonus())))); } else { - return Messages.get(this, "typical_stats", new DecimalFormat("#.##").format(17.5f)); + return Messages.get(this, "typical_stats", Messages.decimalFormat("#.##", 17.5f)); } } diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/rings/RingOfEnergy.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/rings/RingOfEnergy.java index 1b2cf1df5..c4556c115 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/rings/RingOfEnergy.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/rings/RingOfEnergy.java @@ -28,8 +28,6 @@ import com.shatteredpixel.shatteredpixeldungeon.actors.hero.Talent; import com.shatteredpixel.shatteredpixeldungeon.messages.Messages; import com.shatteredpixel.shatteredpixeldungeon.sprites.ItemSpriteSheet; -import java.text.DecimalFormat; - public class RingOfEnergy extends Ring { { @@ -39,10 +37,10 @@ public class RingOfEnergy extends Ring { public String statsInfo() { if (isIdentified()){ return Messages.get(this, "stats", - new DecimalFormat("#.##").format(100f * (Math.pow(1.15f, soloBuffedBonus()) - 1f))); + Messages.decimalFormat("#.##", 100f * (Math.pow(1.15f, soloBuffedBonus()) - 1f))); } else { return Messages.get(this, "typical_stats", - new DecimalFormat("#.##").format(15f)); + Messages.decimalFormat("#.##", 15f)); } } diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/rings/RingOfEvasion.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/rings/RingOfEvasion.java index 16423f1a9..84d22d068 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/rings/RingOfEvasion.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/rings/RingOfEvasion.java @@ -25,8 +25,6 @@ import com.shatteredpixel.shatteredpixeldungeon.actors.Char; import com.shatteredpixel.shatteredpixeldungeon.messages.Messages; import com.shatteredpixel.shatteredpixeldungeon.sprites.ItemSpriteSheet; -import java.text.DecimalFormat; - public class RingOfEvasion extends Ring { { @@ -35,9 +33,9 @@ public class RingOfEvasion extends Ring { public String statsInfo() { if (isIdentified()){ - return Messages.get(this, "stats", new DecimalFormat("#.##").format(100f * (Math.pow(1.125f, soloBuffedBonus()) - 1f))); + return Messages.get(this, "stats", Messages.decimalFormat("#.##", 100f * (Math.pow(1.125f, soloBuffedBonus()) - 1f))); } else { - return Messages.get(this, "typical_stats", new DecimalFormat("#.##").format(12.5f)); + return Messages.get(this, "typical_stats", Messages.decimalFormat("#.##", 12.5f)); } } diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/rings/RingOfFuror.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/rings/RingOfFuror.java index 631ea672e..aaf54803b 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/rings/RingOfFuror.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/rings/RingOfFuror.java @@ -25,8 +25,6 @@ import com.shatteredpixel.shatteredpixeldungeon.actors.Char; import com.shatteredpixel.shatteredpixeldungeon.messages.Messages; import com.shatteredpixel.shatteredpixeldungeon.sprites.ItemSpriteSheet; -import java.text.DecimalFormat; - public class RingOfFuror extends Ring { { @@ -35,9 +33,9 @@ public class RingOfFuror extends Ring { public String statsInfo() { if (isIdentified()){ - return Messages.get(this, "stats", new DecimalFormat("#.##").format(100f * (Math.pow(1.0905f, soloBuffedBonus()) - 1f))); + return Messages.get(this, "stats", Messages.decimalFormat("#.##", 100f * (Math.pow(1.0905f, soloBuffedBonus()) - 1f))); } else { - return Messages.get(this, "typical_stats", new DecimalFormat("#.##").format(9.05f)); + return Messages.get(this, "typical_stats", Messages.decimalFormat("#.##", 9.05f)); } } diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/rings/RingOfHaste.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/rings/RingOfHaste.java index a35756c5c..353f65181 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/rings/RingOfHaste.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/rings/RingOfHaste.java @@ -25,8 +25,6 @@ import com.shatteredpixel.shatteredpixeldungeon.actors.Char; import com.shatteredpixel.shatteredpixeldungeon.messages.Messages; import com.shatteredpixel.shatteredpixeldungeon.sprites.ItemSpriteSheet; -import java.text.DecimalFormat; - public class RingOfHaste extends Ring { { @@ -35,9 +33,9 @@ public class RingOfHaste extends Ring { public String statsInfo() { if (isIdentified()){ - return Messages.get(this, "stats", new DecimalFormat("#.##").format(100f * (Math.pow(1.2f, soloBuffedBonus()) - 1f))); + return Messages.get(this, "stats", Messages.decimalFormat("#.##", 100f * (Math.pow(1.2f, soloBuffedBonus()) - 1f))); } else { - return Messages.get(this, "typical_stats", new DecimalFormat("#.##").format(20f)); + return Messages.get(this, "typical_stats", Messages.decimalFormat("#.##", 20f)); } } diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/rings/RingOfMight.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/rings/RingOfMight.java index 83e6731a1..c9c61bf14 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/rings/RingOfMight.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/rings/RingOfMight.java @@ -28,8 +28,6 @@ import com.shatteredpixel.shatteredpixeldungeon.items.Item; import com.shatteredpixel.shatteredpixeldungeon.messages.Messages; import com.shatteredpixel.shatteredpixeldungeon.sprites.ItemSpriteSheet; -import java.text.DecimalFormat; - public class RingOfMight extends Ring { { @@ -77,9 +75,9 @@ public class RingOfMight extends Ring { public String statsInfo() { if (isIdentified()){ - return Messages.get(this, "stats", soloBonus(), new DecimalFormat("#.##").format(100f * (Math.pow(1.035, soloBuffedBonus()) - 1f))); + return Messages.get(this, "stats", soloBonus(), Messages.decimalFormat("#.##", 100f * (Math.pow(1.035, soloBuffedBonus()) - 1f))); } else { - return Messages.get(this, "typical_stats", 1, new DecimalFormat("#.##").format(3.5f)); + return Messages.get(this, "typical_stats", 1, Messages.decimalFormat("#.##", 3.5f)); } } diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/rings/RingOfSharpshooting.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/rings/RingOfSharpshooting.java index 367d093ee..a61e20591 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/rings/RingOfSharpshooting.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/rings/RingOfSharpshooting.java @@ -25,8 +25,6 @@ import com.shatteredpixel.shatteredpixeldungeon.actors.Char; import com.shatteredpixel.shatteredpixeldungeon.messages.Messages; import com.shatteredpixel.shatteredpixeldungeon.sprites.ItemSpriteSheet; -import java.text.DecimalFormat; - public class RingOfSharpshooting extends Ring { { @@ -35,9 +33,9 @@ public class RingOfSharpshooting extends Ring { public String statsInfo() { if (isIdentified()){ - return Messages.get(this, "stats", soloBuffedBonus(), new DecimalFormat("#.##").format(100f * (Math.pow(1.2, soloBonus()) - 1f))); + return Messages.get(this, "stats", soloBuffedBonus(), Messages.decimalFormat("#.##", 100f * (Math.pow(1.2, soloBonus()) - 1f))); } else { - return Messages.get(this, "typical_stats", 1, new DecimalFormat("#.##").format(20f)); + return Messages.get(this, "typical_stats", 1, Messages.decimalFormat("#.##", 20f)); } } diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/rings/RingOfTenacity.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/rings/RingOfTenacity.java index d0881611b..c0df677c6 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/rings/RingOfTenacity.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/rings/RingOfTenacity.java @@ -25,8 +25,6 @@ import com.shatteredpixel.shatteredpixeldungeon.actors.Char; import com.shatteredpixel.shatteredpixeldungeon.messages.Messages; import com.shatteredpixel.shatteredpixeldungeon.sprites.ItemSpriteSheet; -import java.text.DecimalFormat; - public class RingOfTenacity extends Ring { { @@ -35,9 +33,9 @@ public class RingOfTenacity extends Ring { public String statsInfo() { if (isIdentified()){ - return Messages.get(this, "stats", new DecimalFormat("#.##").format(100f * (1f - Math.pow(0.85f, soloBuffedBonus())))); + return Messages.get(this, "stats", Messages.decimalFormat("#.##", 100f * (1f - Math.pow(0.85f, soloBuffedBonus())))); } else { - return Messages.get(this, "typical_stats", new DecimalFormat("#.##").format(15f)); + return Messages.get(this, "typical_stats", Messages.decimalFormat("#.##", 15f)); } } diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/rings/RingOfWealth.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/rings/RingOfWealth.java index ae04177d5..24073e4ee 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/rings/RingOfWealth.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/items/rings/RingOfWealth.java @@ -46,7 +46,6 @@ import com.watabou.utils.Bundle; import com.watabou.utils.Random; import com.watabou.utils.Reflection; -import java.text.DecimalFormat; import java.util.ArrayList; import java.util.HashSet; @@ -61,9 +60,9 @@ public class RingOfWealth extends Ring { public String statsInfo() { if (isIdentified()){ - return Messages.get(this, "stats", new DecimalFormat("#.##").format(100f * (Math.pow(1.20f, soloBuffedBonus()) - 1f))); + return Messages.get(this, "stats", Messages.decimalFormat("#.##", 100f * (Math.pow(1.20f, soloBuffedBonus()) - 1f))); } else { - return Messages.get(this, "typical_stats", new DecimalFormat("#.##").format(20f)); + return Messages.get(this, "typical_stats", Messages.decimalFormat("#.##", 20f)); } } diff --git a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/messages/Messages.java b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/messages/Messages.java index e9d6e6fc2..6ecd17727 100644 --- a/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/messages/Messages.java +++ b/core/src/main/java/com/shatteredpixel/shatteredpixeldungeon/messages/Messages.java @@ -27,8 +27,11 @@ import com.shatteredpixel.shatteredpixeldungeon.Assets; import com.shatteredpixel.shatteredpixeldungeon.SPDSettings; import com.shatteredpixel.shatteredpixeldungeon.ShatteredPixelDungeon; +import java.text.DecimalFormat; +import java.text.DecimalFormatSymbols; import java.util.ArrayList; import java.util.Arrays; +import java.util.HashMap; import java.util.HashSet; import java.util.IllegalFormatException; import java.util.Locale; @@ -163,6 +166,15 @@ public class Messages { } } + private static HashMap formatters = new HashMap<>(); + + public static String decimalFormat( String format, double number ){ + if (!formatters.containsKey(format)){ + formatters.put(format, new DecimalFormat(format, DecimalFormatSymbols.getInstance(Locale.ENGLISH))); + } + return formatters.get(format).format(number); + } + public static String capitalize( String str ){ if (str.length() == 0) return str; else return str.substring( 0, 1 ).toUpperCase(locale) + str.substring( 1 );