add nullchecks on watchers

This commit is contained in:
Ingmar Goudt 2019-01-06 00:35:15 +01:00
parent 5cab28182d
commit 95a31759b5
53 changed files with 134 additions and 85 deletions

View file

@ -20,6 +20,6 @@ public enum AttackedThisTurnCondition implements Condition {
@Override
public boolean apply(Game game, Ability source) {
AttackedThisTurnWatcher watcher = (AttackedThisTurnWatcher) game.getState().getWatchers().get(AttackedThisTurnWatcher.class.getSimpleName());
return !watcher.getAttackedThisTurnCreatures().isEmpty();
return watcher != null && !watcher.getAttackedThisTurnCreatures().isEmpty();
}
}

View file

@ -19,7 +19,7 @@ public enum LiveLostLastTurnCondition implements Condition {
public boolean apply(Game game, Ability source) {
PlayerLostLifeWatcher watcher = (PlayerLostLifeWatcher) game.getState().getWatchers().get(PlayerLostLifeWatcher.class.getSimpleName());
if (watcher != null) {
return watcher.getLiveLostLastTurn(source.getControllerId()) > 0;
return watcher.getLifeLostLastTurn(source.getControllerId()) > 0;
} else {
WatcherUtils.logMissingWatcher(game, source, PlayerLostLifeWatcher.class, this.getClass());
}

View file

@ -17,7 +17,7 @@ public enum MorbidCondition implements Condition {
@Override
public boolean apply(Game game, Ability source) {
Watcher watcher = game.getState().getWatchers().get(MorbidWatcher.class.getSimpleName());
return watcher.conditionMet();
return watcher != null && watcher.conditionMet();
}
@Override

View file

@ -24,6 +24,9 @@ public enum NoSpellsWereCastLastTurnCondition implements Condition {
}
CastSpellLastTurnWatcher watcher = (CastSpellLastTurnWatcher) game.getState().getWatchers().get(CastSpellLastTurnWatcher.class.getSimpleName());
if(watcher == null){
return false;
}
// if any player cast spell, return false
for (Integer count : watcher.getAmountOfSpellsCastOnPrevTurn().values()) {
if (count > 0) {

View file

@ -26,7 +26,7 @@ public class OpponentLostLifeCondition extends IntCompareCondition {
PlayerLostLifeWatcher watcher = (PlayerLostLifeWatcher) game.getState().getWatchers().get(PlayerLostLifeWatcher.class.getSimpleName());
if (watcher != null) {
for (UUID opponentId : game.getOpponents(source.getControllerId())) {
int lostLive = watcher.getLiveLost(opponentId);
int lostLive = watcher.getLifeLost(opponentId);
if (lostLive > maxLostLive) {
maxLostLive = lostLive;
}

View file

@ -16,6 +16,9 @@ public enum TwoOrMoreSpellsWereCastLastTurnCondition implements Condition {
@Override
public boolean apply(Game game, Ability source) {
CastSpellLastTurnWatcher watcher = (CastSpellLastTurnWatcher) game.getState().getWatchers().get(CastSpellLastTurnWatcher.class.getSimpleName());
if(watcher == null){
return false;
}
// if any player cast more than two spells, return true
for (Integer count : watcher.getAmountOfSpellsCastOnPrevTurn().values()) {
if (count >= 2) {

View file

@ -14,7 +14,10 @@ public class ZuberasDiedDynamicValue implements DynamicValue {
@Override
public int calculate(Game game, Ability sourceAbility, Effect effect) {
ZuberasDiedWatcher watcher = (ZuberasDiedWatcher) game.getState().getWatchers().get(ZuberasDiedWatcher.class.getSimpleName());
return watcher.zuberasDiedThisTurn;
if(watcher == null){
return 0;
}
return watcher.zuberasDiedThisTurn;
}
@Override

View file

@ -75,19 +75,21 @@ class GravestormEffect extends OneShotEffect {
MageObjectReference spellRef = (MageObjectReference) this.getValue("GravestormSpellRef");
if (spellRef != null) {
GravestormWatcher watcher = (GravestormWatcher) game.getState().getWatchers().get(GravestormWatcher.class.getSimpleName());
int gravestormCount = watcher.getGravestormCount();
if (gravestormCount > 0) {
Spell spell = (Spell) this.getValue("GravestormSpell");
if (spell != null) {
if (!game.isSimulation()) {
game.informPlayers("Gravestorm: " + spell.getName() + " will be copied " + gravestormCount + " time" + (gravestormCount > 1 ? "s" : ""));
}
for (int i = 0; i < gravestormCount; i++) {
spell.createCopyOnStack(game, source, source.getControllerId(), true);
if(watcher != null) {
int gravestormCount = watcher.getGravestormCount();
if (gravestormCount > 0) {
Spell spell = (Spell) this.getValue("GravestormSpell");
if (spell != null) {
if (!game.isSimulation()) {
game.informPlayers("Gravestorm: " + spell.getName() + " will be copied " + gravestormCount + " time" + (gravestormCount > 1 ? "s" : ""));
}
for (int i = 0; i < gravestormCount; i++) {
spell.createCopyOnStack(game, source, source.getControllerId(), true);
}
}
}
return true;
}
return true;
}
return false;
}

View file

@ -49,7 +49,7 @@ public class PlayerLostLifeWatcher extends Watcher {
}
}
public int getLiveLost(UUID playerId) {
public int getLifeLost(UUID playerId) {
return amountOfLifeLostThisTurn.getOrDefault(playerId, 0);
}
@ -64,7 +64,7 @@ public class PlayerLostLifeWatcher extends Watcher {
return amount;
}
public int getLiveLostLastTurn(UUID playerId) {
public int getLifeLostLastTurn(UUID playerId) {
return amountOfLifeLostLastTurn.getOrDefault(playerId, 0);
}