remove warnings, use FileUtil to read text files

This commit is contained in:
Maxmtg
2012-10-08 12:05:28 +00:00
parent c59d1307d7
commit dd2e082b49
7 changed files with 64 additions and 92 deletions

View File

@@ -22,8 +22,6 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import com.google.common.base.Supplier;
import forge.Card; import forge.Card;
import forge.Command; import forge.Command;

View File

@@ -805,7 +805,7 @@ public class ComputerUtilAttack {
for (int i = 0; i < attackersLeft.size(); i++) { for (int i = 0; i < attackersLeft.size(); i++) {
final Card attacker = attackersLeft.get(i); final Card attacker = attackersLeft.get(i);
if (this.aiAggression < 5 && !attacker.hasFirstStrike() && !attacker.hasDoubleStrike() if (this.aiAggression < 5 && !attacker.hasFirstStrike() && !attacker.hasDoubleStrike()
&& CombatUtil.getTotalFirstStrikeBlockPower(attacker, AllZone.getHumanPlayer()) && CombatUtil.getTotalFirstStrikeBlockPower(attacker, ai.getOpponent())
>= attacker.getKillDamage()) { >= attacker.getKillDamage()) {
continue; continue;
} }

View File

@@ -957,7 +957,7 @@ public class MagicStack extends MyObservable {
AllZone.getInputControl().setInput(target); AllZone.getInputControl().setInput(target);
} else { } else {
// AI choosing what to haunt // AI choosing what to haunt
final List<Card> oppCreats = CardLists.filterControlledBy(creats, AllZone.getHumanPlayer()); final List<Card> oppCreats = CardLists.filterControlledBy(creats, source.getController().getOpponent());
if (oppCreats.size() != 0) { if (oppCreats.size() != 0) {
haunterDiesWork.setTargetCard(CardFactoryUtil.getWorstCreatureAI(oppCreats)); haunterDiesWork.setTargetCard(CardFactoryUtil.getWorstCreatureAI(oppCreats));
} else { } else {

View File

@@ -17,27 +17,24 @@
*/ */
package forge.properties; package forge.properties;
import java.io.BufferedReader;
import java.io.BufferedWriter; import java.io.BufferedWriter;
import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import com.google.common.io.Files;
import forge.Constant; import forge.Constant;
import forge.Constant.Preferences; import forge.Constant.Preferences;
import forge.gui.home.EMenuItem; import forge.gui.home.EMenuItem;
import forge.gui.match.VMatchUI; import forge.gui.match.VMatchUI;
import forge.gui.match.nonsingleton.VField; import forge.gui.match.nonsingleton.VField;
import forge.gui.match.views.VDev; import forge.gui.match.views.VDev;
import forge.util.FileUtil;
/** /**
* Holds default preference values in an enum. * Holds default preference values in an enum.
@@ -165,25 +162,16 @@ public class ForgePreferences {
// This code is here temporarily to facilitate this transfer. // This code is here temporarily to facilitate this transfer.
// After a while, this can be deleted. Doublestrike 21-5-12 // After a while, this can be deleted. Doublestrike 21-5-12
final File oldFile = new File("forge.preferences"); final File oldFile = new File("forge.preferences");
if (oldFile.exists()) { if (oldFile.exists()) {
final File newFile = new File(NewConstants.PREFS_GLOBAL_FILE); final File newFile = new File(NewConstants.PREFS_GLOBAL_FILE);
final InputStream in = new FileInputStream(oldFile); Files.copy(oldFile, newFile);
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();
oldFile.delete(); oldFile.delete();
} // END TEMPORARY CONSOLIDATION FACILITATION } // END TEMPORARY CONSOLIDATION FACILITATION
final BufferedReader input = new BufferedReader(new FileReader(NewConstants.PREFS_GLOBAL_FILE)); List<String> lines = FileUtil.readFile(NewConstants.PREFS_GLOBAL_FILE);
String line = null; for( String line :lines ) {
while ((line = input.readLine()) != null) {
if (line.startsWith("#") || (line.length() == 0)) { if (line.startsWith("#") || (line.length() == 0)) {
continue; continue;
} }

View File

@@ -17,18 +17,18 @@
*/ */
package forge.quest.data; package forge.quest.data;
import java.io.BufferedReader;
import java.io.BufferedWriter; import java.io.BufferedWriter;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.io.Serializable; import java.io.Serializable;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
import forge.properties.ForgeProps; import forge.properties.ForgeProps;
import forge.properties.NewConstants.Quest; import forge.properties.NewConstants.Quest;
import forge.util.FileUtil;
/** /**
* Holds default preference values in an enum. Loads preferred values when * Holds default preference values in an enum. Loads preferred values when
@@ -226,24 +226,19 @@ public class QuestPreferences implements Serializable {
/** Instantiates a QuestPreferences object. */ /** Instantiates a QuestPreferences object. */
public QuestPreferences() { public QuestPreferences() {
this.preferenceValues = new HashMap<QPref, String>(); this.preferenceValues = new HashMap<QPref, String>();
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("="); List<String> lines = FileUtil.readFile(ForgeProps.getFile(Quest.PREFS));
if (split.length == 2) { for (String line : lines ) {
this.setPreference(split[0], split[1]); 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();
} }
} }

View File

@@ -17,18 +17,17 @@
*/ */
package forge.quest.io; package forge.quest.io;
import java.io.BufferedReader;
import java.io.File; import java.io.File;
import java.io.FileReader;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Random; import java.util.Random;
import com.esotericsoftware.minlog.Log; import com.esotericsoftware.minlog.Log;
import forge.error.ErrorViewer;
import forge.properties.ForgeProps; import forge.properties.ForgeProps;
import forge.properties.NewConstants; import forge.properties.NewConstants;
import forge.util.FileUtil;
import forge.util.MyRandom; import forge.util.MyRandom;
/** /**
@@ -76,61 +75,52 @@ public class ReadPriceList {
* @return a {@link java.util.HashMap} object. * @return a {@link java.util.HashMap} object.
*/ */
private HashMap<String, Integer> readFile(final File file) { private HashMap<String, Integer> readFile(final File file) {
BufferedReader in;
final HashMap<String, Integer> map = new HashMap<String, Integer>(); final HashMap<String, Integer> map = new HashMap<String, Integer>();
final Random r = MyRandom.getRandom(); final Random r = MyRandom.getRandom();
try {
in = new BufferedReader(new FileReader(file)); List<String> lines = FileUtil.readFile(file);
String line = in.readLine(); for (String line : lines) {
if ( line.trim().length() == 0 ) break;
// stop reading if end of file or blank line is read if (line.startsWith(ReadPriceList.COMMENT))
while ((line != null) && (line.trim().length() != 0)) { continue;
if (!line.startsWith(ReadPriceList.COMMENT)) {
final String[] s = line.split("=");
final String name = s[0].trim();
final String price = s[1].trim();
// System.out.println("Name: " + name + ", Price: " + final String[] s = line.split("=");
// price); final String name = s[0].trim();
final String price = s[1].trim();
try { // System.out.println("Name: " + name + ", Price: " +
int val = Integer.parseInt(price.trim()); // price);
if (!(name.equals("Plains") || name.equals("Island") || name.equals("Swamp") try {
|| name.equals("Mountain") || name.equals("Forest") int val = Integer.parseInt(price.trim());
|| 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) { if (!(name.equals("Plains") || name.equals("Island") || name.equals("Swamp")
val = (int) (val * (1 - ff)); || name.equals("Mountain") || name.equals("Forest")
} else { || name.equals("Snow-Covered Plains") || name.equals("Snow-Covered Island")
// +ff% || name.equals("Snow-Covered Swamp") || name.equals("Snow-Covered Mountain") || name
val = (int) (val * (1 + ff)); .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;
}
map.put(name, val); if (r.nextInt(100) < 50) {
} catch (final NumberFormatException nfe) { val = (int) (val * (1 - ff));
Log.warn("NumberFormatException: " + nfe.getMessage()); } else {
// +ff%
val = (int) (val * (1 + ff));
} }
} }
line = in.readLine();
} // if
} catch (final Exception ex) { map.put(name, val);
ErrorViewer.showError(ex); } catch (final NumberFormatException nfe) {
throw new RuntimeException("ReadPriceList : readFile error, " + ex); Log.warn("NumberFormatException: " + nfe.getMessage());
}
} }
return map; return map;
} // readFile() } // readFile()

View File

@@ -108,6 +108,7 @@ public class HttpUtil {
httpOut.write(bufferBytesRead); httpOut.write(bufferBytesRead);
httpOut.flush(); httpOut.flush();
} }
uploadFileReader.close();
} catch (final IOException e) { } catch (final IOException e) {
return; return;
} }