Merge branch 'sonarqubefixes02' into 'master'

More SonarQube detected bugs fixed

See merge request core-developers/forge!2021
This commit is contained in:
swordshine
2019-08-11 02:10:30 +00:00
12 changed files with 89 additions and 60 deletions

View File

@@ -150,12 +150,15 @@ public class AiCardMemory {
*/
public boolean isRememberedCardByName(String cardName, MemorySet set) {
Set<Card> memorySet = getMemorySet(set);
Iterator<Card> it = memorySet.iterator();
while (it.hasNext()) {
Card c = it.next();
if (c.getName().equals(cardName)) {
return true;
if (memorySet != null) {
Iterator<Card> it = memorySet.iterator();
while (it.hasNext()) {
Card c = it.next();
if (c.getName().equals(cardName)) {
return true;
}
}
}
@@ -174,12 +177,15 @@ public class AiCardMemory {
*/
public boolean isRememberedCardByName(String cardName, MemorySet set, Player owner) {
Set<Card> memorySet = getMemorySet(set);
Iterator<Card> it = memorySet.iterator();
while (it.hasNext()) {
Card c = it.next();
if (c.getName().equals(cardName) && c.getOwner().equals(owner)) {
return true;
if (memorySet != null) {
Iterator<Card> it = memorySet.iterator();
while (it.hasNext()) {
Card c = it.next();
if (c.getName().equals(cardName) && c.getOwner().equals(owner)) {
return true;
}
}
}
@@ -197,7 +203,12 @@ public class AiCardMemory {
if (c == null)
return false;
getMemorySet(set).add(c);
Set<Card> memorySet = getMemorySet(set);
if (memorySet != null) {
memorySet.add(c);
}
return true;
}
@@ -216,7 +227,12 @@ public class AiCardMemory {
return false;
}
getMemorySet(set).remove(c);
Set<Card> memorySet = getMemorySet(set);
if (memorySet != null) {
memorySet.remove(c);
}
return true;
}
@@ -229,12 +245,15 @@ public class AiCardMemory {
*/
public boolean forgetAnyCardWithName(String cardName, MemorySet set) {
Set<Card> memorySet = getMemorySet(set);
Iterator<Card> it = memorySet.iterator();
while (it.hasNext()) {
Card c = it.next();
if (c.getName().equals(cardName)) {
return forgetCard(c, set);
if (memorySet != null) {
Iterator<Card> it = memorySet.iterator();
while (it.hasNext()) {
Card c = it.next();
if (c.getName().equals(cardName)) {
return forgetCard(c, set);
}
}
}
@@ -251,12 +270,15 @@ public class AiCardMemory {
*/
public boolean forgetAnyCardWithName(String cardName, MemorySet set, Player owner) {
Set<Card> memorySet = getMemorySet(set);
Iterator<Card> it = memorySet.iterator();
while (it.hasNext()) {
Card c = it.next();
if (c.getName().equals(cardName) && c.getOwner().equals(owner)) {
return forgetCard(c, set);
if (memorySet != null) {
Iterator<Card> it = memorySet.iterator();
while (it.hasNext()) {
Card c = it.next();
if (c.getName().equals(cardName) && c.getOwner().equals(owner)) {
return forgetCard(c, set);
}
}
}
@@ -269,14 +291,16 @@ public class AiCardMemory {
* @return true, if the given memory set contains no remembered cards.
*/
public boolean isMemorySetEmpty(MemorySet set) {
return getMemorySet(set).isEmpty();
return set == null ? true : getMemorySet(set).isEmpty();
}
/**
* Clears the given memory set.
*/
public void clearMemorySet(MemorySet set) {
getMemorySet(set).clear();
if (set != null) {
getMemorySet(set).clear();
}
}
/**

View File

@@ -655,10 +655,10 @@ public class AiController {
return false;
}
AiCardMemory.MemorySet memSet;
AiCardMemory.MemorySet memSet = null;
if (phaseType == null && forNextSpell) {
memSet = AiCardMemory.MemorySet.HELD_MANA_SOURCES_FOR_NEXT_SPELL;
} else {
} else if (phaseType != null) {
switch (phaseType) {
case MAIN2:
memSet = AiCardMemory.MemorySet.HELD_MANA_SOURCES_FOR_MAIN2;

View File

@@ -1612,7 +1612,7 @@ public class ComputerUtil {
}
if (saviourApi == ApiType.PutCounter || saviourApi == ApiType.PutCounterAll) {
if (saviour.getParam("CounterType").equals("P1P1")) {
if (saviour != null && saviour.getParam("CounterType").equals("P1P1")) {
toughness = AbilityUtils.calculateAmount(saviour.getHostCard(), saviour.getParam("CounterNum"), saviour);
} else {
return threatened;

View File

@@ -926,7 +926,7 @@ public class ComputerUtilCard {
}
else if (logic.equals("MostProminentInComputerDeckButGreen")) {
List<String> prominence = ComputerUtilCard.getColorByProminence(CardLists.filterControlledBy(game.getCardsInGame(), ai));
if (prominence.get(0) == MagicColor.Constant.GREEN) {
if (prominence.get(0).equals(MagicColor.Constant.GREEN)) {
chosen.add(prominence.get(1));
} else {
chosen.add(prominence.get(0));
@@ -1878,7 +1878,7 @@ public class ComputerUtilCard {
// A special case which checks that this creature will attack if it's the AI's turn
if (needsToPlay.equalsIgnoreCase("WillAttack")) {
if (game.getPhaseHandler().isPlayerTurn(sa.getActivatingPlayer())) {
if (sa != null && game.getPhaseHandler().isPlayerTurn(sa.getActivatingPlayer())) {
return ComputerUtilCard.doesSpecifiedCreatureAttackAI(sa.getActivatingPlayer(), card) ?
AiPlayDecision.WillPlay : AiPlayDecision.BadEtbEffects;
} else {

View File

@@ -631,7 +631,9 @@ public abstract class GameState {
// Note: triggers may fire during combat declarations ("whenever X attacks, ...", etc.)
if (newPhase == PhaseType.COMBAT_DECLARE_ATTACKERS || newPhase == PhaseType.COMBAT_DECLARE_BLOCKERS) {
boolean toDeclareBlockers = newPhase == PhaseType.COMBAT_DECLARE_BLOCKERS;
handleCombat(game, newPlayerTurn, newPlayerTurn.getSingleOpponent(), toDeclareBlockers);
if (newPlayerTurn != null) {
handleCombat(game, newPlayerTurn, newPlayerTurn.getSingleOpponent(), toDeclareBlockers);
}
}
game.getStack().setResolving(false);
@@ -891,7 +893,9 @@ public abstract class GameState {
}
}
sa.setActivatingPlayer(c.getController());
if (sa != null) {
sa.setActivatingPlayer(c.getController());
}
handleScriptedTargetingForSA(game, sa, tgtID);
if (putOnStack) {

View File

@@ -733,6 +733,7 @@ public class PlayerControllerAi extends PlayerController {
return true;
}
}
break;
case "BetterTgtThanRemembered":
if (source.getRememberedCount() > 0) {
Card rem = (Card) source.getFirstRemembered();
@@ -746,6 +747,7 @@ public class PlayerControllerAi extends PlayerController {
}
return false;
}
break;
default:
break;
}

View File

@@ -172,7 +172,7 @@ public class SpecialCardAi {
}
}
return best.getName();
return best != null ? best.getName() : "";
}
}
@@ -1231,7 +1231,7 @@ public class SpecialCardAi {
// no options with smaller CMC, so discard the one that is harder to cast for the one that is
// easier to cast right now, but only if the best card in the library is at least CMC 3
// (probably not worth it to grab low mana cost cards this way)
if (maxCMC != null && maxCMC.getCMC() < bestInLib.getCMC() && bestInLib.getCMC() >= 3) {
if (maxCMC != null && bestInLib != null && maxCMC.getCMC() < bestInLib.getCMC() && bestInLib.getCMC() >= 3) {
return maxCMC;
}
// We appear to be playing Reanimator (or we have a reanimator card in hand already), so it's

View File

@@ -311,6 +311,7 @@ public final class GameActionUtil {
break;
case Flash:
result.getRestrictions().setInstantSpeed(true);
break;
default:
break;
}

View File

@@ -111,7 +111,7 @@ public class SaveOpenDialog extends JPanel {
RetFile = fc.getSelectedFile();
/* Adds extension if it is known and not given */
if (type != null & !(RetFile.getAbsolutePath().endsWith(type.TypeExtension))) {
if (type != null && !(RetFile.getAbsolutePath().endsWith(type.TypeExtension))) {
RetFile = new File(RetFile.getAbsolutePath() + "." + type.TypeExtension);
}

View File

@@ -214,13 +214,13 @@ public class GuiDownloadZipService extends GuiDownloadService {
protected void copyInputStream(final InputStream in, final String outPath) throws IOException {
final byte[] buffer = new byte[1024];
int len;
final BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(outPath));
while((len = in.read(buffer)) >= 0) {
out.write(buffer, 0, len);
try (BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(outPath))) {
while ((len = in.read(buffer)) >= 0) {
out.write(buffer, 0, len);
}
}
in.close();
out.close();
}
}

View File

@@ -218,28 +218,26 @@ public final class FServerManager {
// https://stackoverflow.com/a/34873630
// https://stackoverflow.com/a/901943
private String getRoutableAddress(boolean preferIpv4, boolean preferIPv6) throws SocketException, UnknownHostException {
DatagramSocket s = new DatagramSocket();
s.connect(InetAddress.getByAddress(this.externalAddress), 0);
NetworkInterface n = NetworkInterface.getByInetAddress(s.getLocalAddress());
Enumeration<InetAddress> en = n.getInetAddresses();
while (en.hasMoreElements()) {
InetAddress addr = (InetAddress) en.nextElement();
if (addr instanceof Inet4Address) {
if (preferIPv6) {
continue;
try (DatagramSocket s = new DatagramSocket()) {
s.connect(InetAddress.getByAddress(this.externalAddress), 0);
NetworkInterface n = NetworkInterface.getByInetAddress(s.getLocalAddress());
Enumeration<InetAddress> en = n.getInetAddresses();
while (en.hasMoreElements()) {
InetAddress addr = (InetAddress) en.nextElement();
if (addr instanceof Inet4Address) {
if (preferIPv6) {
continue;
}
return addr.getHostAddress();
}
s.close();
return addr.getHostAddress();
}
if (addr instanceof Inet6Address) {
if (preferIpv4) {
continue;
if (addr instanceof Inet6Address) {
if (preferIpv4) {
continue;
}
return addr.getHostAddress();
}
s.close();
return addr.getHostAddress();
}
}
s.close();
return null;
}

View File

@@ -2040,9 +2040,9 @@ public class PlayerControllerHuman extends PlayerController implements IGameCont
final File f = GuiBase.getInterface().getSaveFile(new File(ForgeConstants.USER_GAMES_DIR, "state.txt"));
if (f != null
&& (!f.exists() || getGui().showConfirmDialog("Overwrite existing file?", "File exists!"))) {
final BufferedWriter bw = new BufferedWriter(new FileWriter(f));
bw.write(state.toString());
bw.close();
try (BufferedWriter bw = new BufferedWriter(new FileWriter(f))) {
bw.write(state.toString());
}
}
} catch (final Exception e) {
String err = e.getClass().getName();