From dd2e082b49b9dbee9e2c1ffc49dca097273b1d60 Mon Sep 17 00:00:00 2001 From: Maxmtg Date: Mon, 8 Oct 2012 12:05:28 +0000 Subject: [PATCH] remove warnings, use FileUtil to read text files --- .../forge/card/spellability/SpellAbility.java | 2 - .../forge/game/player/ComputerUtilAttack.java | 2 +- src/main/java/forge/game/zone/MagicStack.java | 2 +- .../forge/properties/ForgePreferences.java | 28 ++---- .../forge/quest/data/QuestPreferences.java | 33 +++---- .../java/forge/quest/io/ReadPriceList.java | 88 ++++++++----------- src/main/java/forge/util/HttpUtil.java | 1 + 7 files changed, 64 insertions(+), 92 deletions(-) diff --git a/src/main/java/forge/card/spellability/SpellAbility.java b/src/main/java/forge/card/spellability/SpellAbility.java index 142d6c3ba61..3cc926c0d61 100644 --- a/src/main/java/forge/card/spellability/SpellAbility.java +++ b/src/main/java/forge/card/spellability/SpellAbility.java @@ -22,8 +22,6 @@ import java.util.HashMap; import java.util.List; import java.util.Set; -import com.google.common.base.Supplier; - import forge.Card; import forge.Command; diff --git a/src/main/java/forge/game/player/ComputerUtilAttack.java b/src/main/java/forge/game/player/ComputerUtilAttack.java index 2ca2c621281..4e80fa3ad95 100644 --- a/src/main/java/forge/game/player/ComputerUtilAttack.java +++ b/src/main/java/forge/game/player/ComputerUtilAttack.java @@ -805,7 +805,7 @@ public class ComputerUtilAttack { for (int i = 0; i < attackersLeft.size(); i++) { final Card attacker = attackersLeft.get(i); if (this.aiAggression < 5 && !attacker.hasFirstStrike() && !attacker.hasDoubleStrike() - && CombatUtil.getTotalFirstStrikeBlockPower(attacker, AllZone.getHumanPlayer()) + && CombatUtil.getTotalFirstStrikeBlockPower(attacker, ai.getOpponent()) >= attacker.getKillDamage()) { continue; } diff --git a/src/main/java/forge/game/zone/MagicStack.java b/src/main/java/forge/game/zone/MagicStack.java index 482e80dfda0..c72e00cf433 100644 --- a/src/main/java/forge/game/zone/MagicStack.java +++ b/src/main/java/forge/game/zone/MagicStack.java @@ -957,7 +957,7 @@ public class MagicStack extends MyObservable { AllZone.getInputControl().setInput(target); } else { // AI choosing what to haunt - final List oppCreats = CardLists.filterControlledBy(creats, AllZone.getHumanPlayer()); + final List oppCreats = CardLists.filterControlledBy(creats, source.getController().getOpponent()); if (oppCreats.size() != 0) { haunterDiesWork.setTargetCard(CardFactoryUtil.getWorstCreatureAI(oppCreats)); } else { diff --git a/src/main/java/forge/properties/ForgePreferences.java b/src/main/java/forge/properties/ForgePreferences.java index c25d11e919b..b1779ab2406 100644 --- a/src/main/java/forge/properties/ForgePreferences.java +++ b/src/main/java/forge/properties/ForgePreferences.java @@ -17,27 +17,24 @@ */ package forge.properties; -import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; -import java.io.FileInputStream; import java.io.FileNotFoundException; -import java.io.FileOutputStream; -import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; import java.util.HashMap; import java.util.List; import java.util.Map; +import com.google.common.io.Files; + import forge.Constant; import forge.Constant.Preferences; import forge.gui.home.EMenuItem; import forge.gui.match.VMatchUI; import forge.gui.match.nonsingleton.VField; import forge.gui.match.views.VDev; +import forge.util.FileUtil; /** * Holds default preference values in an enum. @@ -165,25 +162,16 @@ public class ForgePreferences { // This code is here temporarily to facilitate this transfer. // After a while, this can be deleted. Doublestrike 21-5-12 final File oldFile = new File("forge.preferences"); + if (oldFile.exists()) { final File newFile = new File(NewConstants.PREFS_GLOBAL_FILE); - final InputStream in = new FileInputStream(oldFile); - final OutputStream out = new FileOutputStream(newFile); - - byte[] buf = new byte[1024]; - int len; - while ((len = in.read(buf)) > 0) { - out.write(buf, 0, len); - } - in.close(); - out.close(); - + Files.copy(oldFile, newFile); oldFile.delete(); } // END TEMPORARY CONSOLIDATION FACILITATION - final BufferedReader input = new BufferedReader(new FileReader(NewConstants.PREFS_GLOBAL_FILE)); - String line = null; - while ((line = input.readLine()) != null) { + List lines = FileUtil.readFile(NewConstants.PREFS_GLOBAL_FILE); + for( String line :lines ) { + if (line.startsWith("#") || (line.length() == 0)) { continue; } diff --git a/src/main/java/forge/quest/data/QuestPreferences.java b/src/main/java/forge/quest/data/QuestPreferences.java index dfbd69c4f83..823b07c9c89 100644 --- a/src/main/java/forge/quest/data/QuestPreferences.java +++ b/src/main/java/forge/quest/data/QuestPreferences.java @@ -17,18 +17,18 @@ */ package forge.quest.data; -import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileNotFoundException; -import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.Serializable; import java.util.HashMap; +import java.util.List; import java.util.Map; import forge.properties.ForgeProps; import forge.properties.NewConstants.Quest; +import forge.util.FileUtil; /** * Holds default preference values in an enum. Loads preferred values when @@ -226,24 +226,19 @@ public class QuestPreferences implements Serializable { /** Instantiates a QuestPreferences object. */ public QuestPreferences() { this.preferenceValues = new HashMap(); - try { - final BufferedReader input = new BufferedReader(new FileReader(ForgeProps.getFile(Quest.PREFS))); - String line = null; - while ((line = input.readLine()) != null) { - if (line.startsWith("#") || (line.length() == 0)) { - continue; - } - - final String[] split = line.split("="); - - if (split.length == 2) { - this.setPreference(split[0], split[1]); - } + + List lines = FileUtil.readFile(ForgeProps.getFile(Quest.PREFS)); + + for (String line : lines ) { + if (line.startsWith("#") || (line.length() == 0)) { + continue; + } + + final String[] split = line.split("="); + + if (split.length == 2) { + this.setPreference(split[0], split[1]); } - } catch (final FileNotFoundException ex) { - // ex.printStackTrace(); - } catch (final IOException ex) { - // ex.printStackTrace(); } } diff --git a/src/main/java/forge/quest/io/ReadPriceList.java b/src/main/java/forge/quest/io/ReadPriceList.java index 3908ff1a923..b810ccb11f3 100644 --- a/src/main/java/forge/quest/io/ReadPriceList.java +++ b/src/main/java/forge/quest/io/ReadPriceList.java @@ -17,18 +17,17 @@ */ package forge.quest.io; -import java.io.BufferedReader; import java.io.File; -import java.io.FileReader; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.Random; import com.esotericsoftware.minlog.Log; -import forge.error.ErrorViewer; import forge.properties.ForgeProps; import forge.properties.NewConstants; +import forge.util.FileUtil; import forge.util.MyRandom; /** @@ -76,61 +75,52 @@ public class ReadPriceList { * @return a {@link java.util.HashMap} object. */ private HashMap readFile(final File file) { - BufferedReader in; final HashMap map = new HashMap(); final Random r = MyRandom.getRandom(); - try { + + List lines = FileUtil.readFile(file); + for (String line : lines) { + if ( line.trim().length() == 0 ) break; + + if (line.startsWith(ReadPriceList.COMMENT)) + continue; + + final String[] s = line.split("="); + final String name = s[0].trim(); + final String price = s[1].trim(); - in = new BufferedReader(new FileReader(file)); - String line = in.readLine(); + // System.out.println("Name: " + name + ", Price: " + + // price); - // stop reading if end of file or blank line is read - while ((line != null) && (line.trim().length() != 0)) { - if (!line.startsWith(ReadPriceList.COMMENT)) { - final String[] s = line.split("="); - final String name = s[0].trim(); - final String price = s[1].trim(); + try { + int val = Integer.parseInt(price.trim()); - // System.out.println("Name: " + name + ", Price: " + - // price); + if (!(name.equals("Plains") || name.equals("Island") || name.equals("Swamp") + || name.equals("Mountain") || name.equals("Forest") + || name.equals("Snow-Covered Plains") || name.equals("Snow-Covered Island") + || name.equals("Snow-Covered Swamp") || name.equals("Snow-Covered Mountain") || name + .equals("Snow-Covered Forest"))) { + float ff = 0; + if (r.nextInt(100) < 90) { + ff = r.nextInt(10) * (float) .01; + } else { + // +/- 50% + ff = r.nextInt(50) * (float) .01; + } - try { - int val = Integer.parseInt(price.trim()); - - if (!(name.equals("Plains") || name.equals("Island") || name.equals("Swamp") - || name.equals("Mountain") || name.equals("Forest") - || name.equals("Snow-Covered Plains") || name.equals("Snow-Covered Island") - || name.equals("Snow-Covered Swamp") || name.equals("Snow-Covered Mountain") || name - .equals("Snow-Covered Forest"))) { - float ff = 0; - if (r.nextInt(100) < 90) { - ff = r.nextInt(10) * (float) .01; - } else { - // +/- 50% - ff = r.nextInt(50) * (float) .01; - } - - if (r.nextInt(100) < 50) { - val = (int) (val * (1 - ff)); - } else { - // +ff% - val = (int) (val * (1 + ff)); - } - } - - map.put(name, val); - } catch (final NumberFormatException nfe) { - Log.warn("NumberFormatException: " + nfe.getMessage()); + if (r.nextInt(100) < 50) { + val = (int) (val * (1 - ff)); + } else { + // +ff% + val = (int) (val * (1 + ff)); } } - line = in.readLine(); - } // if - - } catch (final Exception ex) { - ErrorViewer.showError(ex); - throw new RuntimeException("ReadPriceList : readFile error, " + ex); - } + map.put(name, val); + } catch (final NumberFormatException nfe) { + Log.warn("NumberFormatException: " + nfe.getMessage()); + } + } return map; } // readFile() diff --git a/src/main/java/forge/util/HttpUtil.java b/src/main/java/forge/util/HttpUtil.java index b6779d5b880..cb9e6d183d5 100644 --- a/src/main/java/forge/util/HttpUtil.java +++ b/src/main/java/forge/util/HttpUtil.java @@ -108,6 +108,7 @@ public class HttpUtil { httpOut.write(bufferBytesRead); httpOut.flush(); } + uploadFileReader.close(); } catch (final IOException e) { return; }