From 4aa51210f99d738236037d72cf4374104c6ff03f Mon Sep 17 00:00:00 2001 From: ingmargoudt Date: Wed, 15 Feb 2017 23:19:40 +0100 Subject: [PATCH] refactor of watchers to use map functions --- ...fDamageAPlayerReceivedThisTurnWatcher.java | 22 ++++--------- .../CardsAmountDrawnThisTurnWatcher.java | 14 ++------ .../CardsDrawnDuringDrawStepWatcher.java | 25 +++++--------- .../common/CardsPutIntoGraveyardWatcher.java | 16 +++------ .../watchers/common/CastFromHandWatcher.java | 3 +- .../common/CastSpellLastTurnWatcher.java | 33 +++++-------------- .../watchers/common/CommanderInfoWatcher.java | 5 +-- .../watchers/common/CreaturesDiedWatcher.java | 5 +-- .../watchers/common/DamageDoneWatcher.java | 20 +++++------ .../watchers/common/DamagedByWatcher.java | 9 +++-- .../common/FirstSpellCastThisTurnWatcher.java | 12 +++---- .../common/ManaSpentToCastWatcher.java | 3 +- .../mage/watchers/common/MiracleWatcher.java | 12 +++---- .../common/PlayerAttackedWatcher.java | 6 ++-- .../common/PlayerGainedLifeWatcher.java | 15 ++------- .../common/PlayerLostLifeWatcher.java | 12 ++----- .../mage/watchers/common/ProwlWatcher.java | 6 ++-- .../common/SourceDidDamageWatcher.java | 9 ++--- 18 files changed, 69 insertions(+), 158 deletions(-) diff --git a/Mage/src/main/java/mage/watchers/common/AmountOfDamageAPlayerReceivedThisTurnWatcher.java b/Mage/src/main/java/mage/watchers/common/AmountOfDamageAPlayerReceivedThisTurnWatcher.java index fe994e1844..23a4884150 100644 --- a/Mage/src/main/java/mage/watchers/common/AmountOfDamageAPlayerReceivedThisTurnWatcher.java +++ b/Mage/src/main/java/mage/watchers/common/AmountOfDamageAPlayerReceivedThisTurnWatcher.java @@ -31,17 +31,16 @@ import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; import java.util.UUID; + import mage.constants.WatcherScope; import mage.game.Game; import mage.game.events.GameEvent; import mage.watchers.Watcher; /** - * * @author jeffwadsworth - * - * Amount of damage received by a player this turn - * + *

+ * Amount of damage received by a player this turn */ public class AmountOfDamageAPlayerReceivedThisTurnWatcher extends Watcher { @@ -63,23 +62,14 @@ public class AmountOfDamageAPlayerReceivedThisTurnWatcher extends Watcher { if (event.getType() == GameEvent.EventType.DAMAGED_PLAYER) { UUID playerId = event.getTargetId(); if (playerId != null) { - Integer amount = amountOfDamageReceivedThisTurn.get(playerId); - if (amount == null) { - amount = event.getAmount(); - } else { - amount = amount + event.getAmount(); - } - amountOfDamageReceivedThisTurn.put(playerId, amount); + amountOfDamageReceivedThisTurn.putIfAbsent(playerId, 0); + amountOfDamageReceivedThisTurn.compute(playerId, (k, v) -> v + event.getAmount()); } } } public int getAmountOfDamageReceivedThisTurn(UUID playerId) { - Integer amount = amountOfDamageReceivedThisTurn.get(playerId); - if (amount != null) { - return amount; - } - return 0; + return amountOfDamageReceivedThisTurn.getOrDefault(playerId, 0); } @Override diff --git a/Mage/src/main/java/mage/watchers/common/CardsAmountDrawnThisTurnWatcher.java b/Mage/src/main/java/mage/watchers/common/CardsAmountDrawnThisTurnWatcher.java index b9f7d74154..ad0f8568d6 100644 --- a/Mage/src/main/java/mage/watchers/common/CardsAmountDrawnThisTurnWatcher.java +++ b/Mage/src/main/java/mage/watchers/common/CardsAmountDrawnThisTurnWatcher.java @@ -38,7 +38,6 @@ import java.util.Map; import java.util.UUID; /** - * * @author Quercitron * @author LevelX2 * @author jeffwadsworth @@ -61,11 +60,8 @@ public class CardsAmountDrawnThisTurnWatcher extends Watcher { @Override public void watch(GameEvent event, Game game) { if (event.getType() == GameEvent.EventType.DREW_CARD) { - if (amountOfCardsDrawnThisTurn.containsKey(event.getPlayerId())) { - amountOfCardsDrawnThisTurn.put(event.getPlayerId(), amountOfCardsDrawnThisTurn.get(event.getPlayerId()) + 1); - } else { - amountOfCardsDrawnThisTurn.put(event.getPlayerId(), 1); - } + amountOfCardsDrawnThisTurn.putIfAbsent(event.getPlayerId(), 0); + amountOfCardsDrawnThisTurn.compute(event.getPlayerId(), (k, amount) -> amount += 1); } } @@ -90,11 +86,7 @@ public class CardsAmountDrawnThisTurnWatcher extends Watcher { } public int getAmountCardsDrawn(UUID playerId) { - Integer amount = amountOfCardsDrawnThisTurn.get(playerId); - if (amount != null) { - return amount; - } - return 0; + return amountOfCardsDrawnThisTurn.getOrDefault(playerId,0); } @Override diff --git a/Mage/src/main/java/mage/watchers/common/CardsDrawnDuringDrawStepWatcher.java b/Mage/src/main/java/mage/watchers/common/CardsDrawnDuringDrawStepWatcher.java index 5a1e21cb5b..8f213a336d 100644 --- a/Mage/src/main/java/mage/watchers/common/CardsDrawnDuringDrawStepWatcher.java +++ b/Mage/src/main/java/mage/watchers/common/CardsDrawnDuringDrawStepWatcher.java @@ -32,6 +32,7 @@ import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; import java.util.UUID; + import mage.constants.PhaseStep; import mage.constants.WatcherScope; import mage.game.Game; @@ -39,11 +40,9 @@ import mage.game.events.GameEvent; import mage.watchers.Watcher; /** - * * @author LevelX2 - * - * Counts cards drawn during draw step - * + *

+ * Counts cards drawn during draw step */ public class CardsDrawnDuringDrawStepWatcher extends Watcher { @@ -65,26 +64,18 @@ public class CardsDrawnDuringDrawStepWatcher extends Watcher { public void watch(GameEvent event, Game game) { if (event.getType() == GameEvent.EventType.DREW_CARD && game.getPhase() != null - && game.getPhase().getStep().getType().equals(PhaseStep.DRAW)) { + && game.getPhase().getStep().getType() == PhaseStep.DRAW) { UUID playerId = event.getPlayerId(); if (playerId != null) { - Integer amount = amountOfCardsDrawnThisTurn.get(playerId); - if (amount == null) { - amount = 1; - } else { - amount++; - } - amountOfCardsDrawnThisTurn.put(playerId, amount); + amountOfCardsDrawnThisTurn.putIfAbsent(playerId, 0); + amountOfCardsDrawnThisTurn.compute(playerId, (k, amount) -> amount + 1); + } } } public int getAmountCardsDrawn(UUID playerId) { - Integer amount = amountOfCardsDrawnThisTurn.get(playerId); - if (amount != null) { - return amount; - } - return 0; + return amountOfCardsDrawnThisTurn.getOrDefault(playerId, 0); } @Override diff --git a/Mage/src/main/java/mage/watchers/common/CardsPutIntoGraveyardWatcher.java b/Mage/src/main/java/mage/watchers/common/CardsPutIntoGraveyardWatcher.java index 4fbf9765f6..24957848cf 100644 --- a/Mage/src/main/java/mage/watchers/common/CardsPutIntoGraveyardWatcher.java +++ b/Mage/src/main/java/mage/watchers/common/CardsPutIntoGraveyardWatcher.java @@ -74,13 +74,9 @@ public class CardsPutIntoGraveyardWatcher extends Watcher { if (event.getType() == GameEvent.EventType.ZONE_CHANGE && ((ZoneChangeEvent) event).getToZone() == Zone.GRAVEYARD) { UUID playerId = event.getPlayerId(); if (playerId != null && game.getCard(event.getTargetId()) != null) { - Integer amount = amountOfCardsThisTurn.get(playerId); - if (amount == null) { - amount = 1; - } else { - ++amount; - } - amountOfCardsThisTurn.put(playerId, amount); + amountOfCardsThisTurn.putIfAbsent(playerId, 0); + amountOfCardsThisTurn.compute(playerId, (k, amount) -> amount += 1); + if (((ZoneChangeEvent) event).getFromZone() == Zone.BATTLEFIELD) { cardsPutToGraveyardFromBattlefield.add(new MageObjectReference(event.getTargetId(), game)); } @@ -89,11 +85,7 @@ public class CardsPutIntoGraveyardWatcher extends Watcher { } public int getAmountCardsPutToGraveyard(UUID playerId) { - Integer amount = amountOfCardsThisTurn.get(playerId); - if (amount != null) { - return amount; - } - return 0; + return amountOfCardsThisTurn.getOrDefault(playerId, 0); } public Set getCardsPutToGraveyardFromBattlefield() { diff --git a/Mage/src/main/java/mage/watchers/common/CastFromHandWatcher.java b/Mage/src/main/java/mage/watchers/common/CastFromHandWatcher.java index bf2d2f77b6..b52afa6ce8 100644 --- a/Mage/src/main/java/mage/watchers/common/CastFromHandWatcher.java +++ b/Mage/src/main/java/mage/watchers/common/CastFromHandWatcher.java @@ -4,6 +4,7 @@ import java.util.HashSet; import java.util.Objects; import java.util.Set; import java.util.UUID; + import mage.constants.WatcherScope; import mage.constants.Zone; import mage.game.Game; @@ -37,7 +38,7 @@ public class CastFromHandWatcher extends Watcher { spellsCastFromHand.clear(); step = null; } - if (event.getType() == GameEvent.EventType.SPELL_CAST && event.getZone().equals(Zone.HAND)) { + if (event.getType() == GameEvent.EventType.SPELL_CAST && event.getZone() == Zone.HAND) { if (step == null) { step = game.getTurn().getStep(); } diff --git a/Mage/src/main/java/mage/watchers/common/CastSpellLastTurnWatcher.java b/Mage/src/main/java/mage/watchers/common/CastSpellLastTurnWatcher.java index 7e96ee21ea..17e204b339 100644 --- a/Mage/src/main/java/mage/watchers/common/CastSpellLastTurnWatcher.java +++ b/Mage/src/main/java/mage/watchers/common/CastSpellLastTurnWatcher.java @@ -27,20 +27,16 @@ */ package mage.watchers.common; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; -import java.util.UUID; import mage.MageObjectReference; import mage.constants.WatcherScope; import mage.game.Game; import mage.game.events.GameEvent; import mage.watchers.Watcher; +import java.util.*; +import java.util.Map.Entry; + /** - * * @author nantuko, BetaSteward_at_googlemail.com */ public class CastSpellLastTurnWatcher extends Watcher { @@ -70,13 +66,9 @@ public class CastSpellLastTurnWatcher extends Watcher { spellsCastThisTurnInOrder.add(new MageObjectReference(event.getTargetId(), game)); UUID playerId = event.getPlayerId(); if (playerId != null) { - Integer amount = amountOfSpellsCastOnCurrentTurn.get(playerId); - if (amount == null) { - amount = 1; - } else { - amount = amount + 1; - } - amountOfSpellsCastOnCurrentTurn.put(playerId, amount); + amountOfSpellsCastOnCurrentTurn.putIfAbsent(playerId, 0); + amountOfSpellsCastOnCurrentTurn.compute(playerId, (k, a) -> a + 1); + } } } @@ -98,20 +90,11 @@ public class CastSpellLastTurnWatcher extends Watcher { } public int getAmountOfSpellsAllPlayersCastOnCurrentTurn() { - int totalAmount = 0; - for (Integer amount : amountOfSpellsCastOnCurrentTurn.values()) { - totalAmount += amount; - } - return totalAmount; + return amountOfSpellsCastOnCurrentTurn.values().stream().mapToInt(Integer::intValue).sum(); } public int getAmountOfSpellsPlayerCastOnCurrentTurn(UUID playerId) { - Integer value = amountOfSpellsCastOnCurrentTurn.get(playerId); - if (value != null) { - return value; - } else { - return 0; - } + return amountOfSpellsCastOnCurrentTurn.getOrDefault(playerId, 0); } public int getSpellOrder(MageObjectReference spell, Game game) { diff --git a/Mage/src/main/java/mage/watchers/common/CommanderInfoWatcher.java b/Mage/src/main/java/mage/watchers/common/CommanderInfoWatcher.java index 3f4b4f90fd..769484331b 100644 --- a/Mage/src/main/java/mage/watchers/common/CommanderInfoWatcher.java +++ b/Mage/src/main/java/mage/watchers/common/CommanderInfoWatcher.java @@ -77,10 +77,7 @@ public class CommanderInfoWatcher extends Watcher { DamagedPlayerEvent damageEvent = (DamagedPlayerEvent) event; if (damageEvent.isCombatDamage()) { UUID playerUUID = event.getTargetId(); - Integer damage = damageToPlayer.get(playerUUID); - if (damage == null) { - damage = 0; - } + Integer damage = damageToPlayer.getOrDefault(playerUUID,0); damage += damageEvent.getAmount(); damageToPlayer.put(playerUUID, damage); Player player = game.getPlayer(playerUUID); diff --git a/Mage/src/main/java/mage/watchers/common/CreaturesDiedWatcher.java b/Mage/src/main/java/mage/watchers/common/CreaturesDiedWatcher.java index 12daad0fc0..669d37026d 100644 --- a/Mage/src/main/java/mage/watchers/common/CreaturesDiedWatcher.java +++ b/Mage/src/main/java/mage/watchers/common/CreaturesDiedWatcher.java @@ -81,10 +81,7 @@ public class CreaturesDiedWatcher extends Watcher { } public int getAmountOfCreaturesDiesThisTurn(UUID playerId) { - if (amountOfCreaturesThatDiedByController.containsKey(playerId)) { - return amountOfCreaturesThatDiedByController.get(playerId); - } - return 0; + return amountOfCreaturesThatDiedByController.getOrDefault(playerId, 0); } @Override diff --git a/Mage/src/main/java/mage/watchers/common/DamageDoneWatcher.java b/Mage/src/main/java/mage/watchers/common/DamageDoneWatcher.java index d30882f742..d9df669f88 100644 --- a/Mage/src/main/java/mage/watchers/common/DamageDoneWatcher.java +++ b/Mage/src/main/java/mage/watchers/common/DamageDoneWatcher.java @@ -5,17 +5,17 @@ */ package mage.watchers.common; -import java.util.HashMap; -import java.util.Map; -import java.util.UUID; import mage.MageObjectReference; import mage.constants.WatcherScope; import mage.game.Game; import mage.game.events.GameEvent; import mage.watchers.Watcher; +import java.util.HashMap; +import java.util.Map; +import java.util.UUID; + /** - * * @author LevelX2 */ public class DamageDoneWatcher extends Watcher { @@ -50,12 +50,12 @@ public class DamageDoneWatcher extends Watcher { case DAMAGED_PLANESWALKER: case DAMAGED_PLAYER: { MageObjectReference damageSourceRef = new MageObjectReference(event.getSourceId(), game); - int damageDone = damagingObjects.containsKey(damageSourceRef) ? damagingObjects.get(damageSourceRef) : 0; - damagingObjects.put(damageSourceRef, damageDone + event.getAmount()); + damagingObjects.putIfAbsent(damageSourceRef, 0); + damagingObjects.compute(damageSourceRef, (k, damage) -> damage + event.getAmount()); MageObjectReference damageTargetRef = new MageObjectReference(event.getTargetId(), game); - int damageReceived = damagedObjects.containsKey(damageTargetRef) ? damagedObjects.get(damageTargetRef) : 0; - damagedObjects.put(damageTargetRef, damageReceived + event.getAmount()); + damagedObjects.putIfAbsent(damageTargetRef, 0); + damagedObjects.compute(damageTargetRef, (k, damage) -> damage + event.getAmount()); } } } @@ -69,12 +69,12 @@ public class DamageDoneWatcher extends Watcher { public int damageDoneBy(UUID objectId, int zoneChangeCounter, Game game) { MageObjectReference mor = new MageObjectReference(objectId, zoneChangeCounter, game); - return damagingObjects.containsKey(mor) ? damagingObjects.get(mor) : 0; + return damagingObjects.getOrDefault(mor, 0); } public int damageDoneTo(UUID objectId, int zoneChangeCounter, Game game) { MageObjectReference mor = new MageObjectReference(objectId, zoneChangeCounter, game); - return damagedObjects.containsKey(mor) ? damagedObjects.get(mor) : 0; + return damagedObjects.getOrDefault(mor, 0); } public boolean isDamaged(UUID objectId, int zoneChangeCounter, Game game) { diff --git a/Mage/src/main/java/mage/watchers/common/DamagedByWatcher.java b/Mage/src/main/java/mage/watchers/common/DamagedByWatcher.java index 0043bc03de..00816564e9 100644 --- a/Mage/src/main/java/mage/watchers/common/DamagedByWatcher.java +++ b/Mage/src/main/java/mage/watchers/common/DamagedByWatcher.java @@ -30,6 +30,7 @@ package mage.watchers.common; import java.util.HashSet; import java.util.Set; import java.util.UUID; + import mage.MageObject; import mage.MageObjectReference; import mage.constants.WatcherScope; @@ -40,7 +41,6 @@ import mage.game.permanent.Permanent; import mage.watchers.Watcher; /** - * * @author BetaSteward_at_googlemail.com */ public class DamagedByWatcher extends Watcher { @@ -72,12 +72,11 @@ public class DamagedByWatcher extends Watcher { @Override public void watch(GameEvent event, Game game) { boolean eventHasAppropriateType = (event.getType() == EventType.DAMAGED_CREATURE) || - (watchPlaneswalkers && event.getType() == EventType.DAMAGED_PLANESWALKER); + (watchPlaneswalkers && event.getType() == EventType.DAMAGED_PLANESWALKER); if (eventHasAppropriateType && sourceId.equals(event.getSourceId())) { MageObjectReference mor = new MageObjectReference(event.getTargetId(), game); - if (!damagedBySource.contains(mor)) { - damagedBySource.add(mor); - } + damagedBySource.add(mor); + } } diff --git a/Mage/src/main/java/mage/watchers/common/FirstSpellCastThisTurnWatcher.java b/Mage/src/main/java/mage/watchers/common/FirstSpellCastThisTurnWatcher.java index 145bd9abf0..0c4a6f0ae3 100644 --- a/Mage/src/main/java/mage/watchers/common/FirstSpellCastThisTurnWatcher.java +++ b/Mage/src/main/java/mage/watchers/common/FirstSpellCastThisTurnWatcher.java @@ -3,6 +3,7 @@ package mage.watchers.common; import java.util.HashMap; import java.util.Map; import java.util.UUID; + import mage.constants.WatcherScope; import mage.game.Game; import mage.game.events.GameEvent; @@ -12,7 +13,6 @@ import mage.watchers.Watcher; /** * @author jeffwadsworth -* */ public class FirstSpellCastThisTurnWatcher extends Watcher { @@ -36,9 +36,9 @@ public class FirstSpellCastThisTurnWatcher extends Watcher { case CAST_SPELL: Spell spell = (Spell) game.getObject(event.getTargetId()); if (spell != null && !playerFirstSpellCast.containsKey(spell.getControllerId())) { - if (event.getType().equals(EventType.SPELL_CAST)) { + if (event.getType() == EventType.SPELL_CAST) { playerFirstSpellCast.put(spell.getControllerId(), spell.getId()); - } else if (event.getType().equals(EventType.CAST_SPELL)) { + } else if (event.getType() == EventType.CAST_SPELL) { playerFirstCastSpell.put(spell.getControllerId(), spell.getId()); } } @@ -58,10 +58,6 @@ public class FirstSpellCastThisTurnWatcher extends Watcher { } public UUID getIdOfFirstCastSpell(UUID playerId) { - if (playerFirstSpellCast.get(playerId) == null) { - return playerFirstCastSpell.get(playerId); - } else { - return playerFirstSpellCast.get(playerId); - } + return playerFirstSpellCast.getOrDefault(playerId, playerFirstSpellCast.get(playerId)); } } diff --git a/Mage/src/main/java/mage/watchers/common/ManaSpentToCastWatcher.java b/Mage/src/main/java/mage/watchers/common/ManaSpentToCastWatcher.java index 2577498b4e..d00e66597c 100644 --- a/Mage/src/main/java/mage/watchers/common/ManaSpentToCastWatcher.java +++ b/Mage/src/main/java/mage/watchers/common/ManaSpentToCastWatcher.java @@ -39,7 +39,6 @@ import mage.watchers.Watcher; /** * Watcher saves the mana that was spent to cast a spell * - * * @author LevelX2 */ public class ManaSpentToCastWatcher extends Watcher { @@ -64,7 +63,7 @@ public class ManaSpentToCastWatcher extends Watcher { } } if (event.getType() == GameEvent.EventType.ZONE_CHANGE && this.getSourceId().equals(event.getSourceId())) { - if (((ZoneChangeEvent) event).getFromZone().equals(Zone.BATTLEFIELD)) { + if (((ZoneChangeEvent) event).getFromZone() == Zone.BATTLEFIELD) { payment = null; } } diff --git a/Mage/src/main/java/mage/watchers/common/MiracleWatcher.java b/Mage/src/main/java/mage/watchers/common/MiracleWatcher.java index d981691877..f5ff489e4b 100644 --- a/Mage/src/main/java/mage/watchers/common/MiracleWatcher.java +++ b/Mage/src/main/java/mage/watchers/common/MiracleWatcher.java @@ -31,6 +31,7 @@ import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; import java.util.UUID; + import mage.abilities.Ability; import mage.abilities.keyword.MiracleAbility; import mage.cards.Card; @@ -74,14 +75,9 @@ public class MiracleWatcher extends Watcher { if (game.getPhase() != null && event.getType() == GameEvent.EventType.DREW_CARD) { UUID playerId = event.getPlayerId(); if (playerId != null) { - Integer amount = amountOfCardsDrawnThisTurn.get(playerId); - if (amount == null) { - amount = 1; - } else { - amount++; - } - amountOfCardsDrawnThisTurn.put(playerId, amount); - if (amount == 1) { + amountOfCardsDrawnThisTurn.putIfAbsent(playerId, 0); + amountOfCardsDrawnThisTurn.compute(playerId, (p, amount) -> amount + 1); + if (amountOfCardsDrawnThisTurn.get(playerId) == 1) { checkMiracleAbility(event, game); } } diff --git a/Mage/src/main/java/mage/watchers/common/PlayerAttackedWatcher.java b/Mage/src/main/java/mage/watchers/common/PlayerAttackedWatcher.java index 0b3d231a10..96e1f89209 100644 --- a/Mage/src/main/java/mage/watchers/common/PlayerAttackedWatcher.java +++ b/Mage/src/main/java/mage/watchers/common/PlayerAttackedWatcher.java @@ -66,8 +66,8 @@ public class PlayerAttackedWatcher extends Watcher { @Override public void watch(GameEvent event, Game game) { if (event.getType() == GameEvent.EventType.ATTACKER_DECLARED) { - int numberAttackers = playerAttacked.containsKey(event.getPlayerId()) ? playerAttacked.get(event.getPlayerId()) : 0; - playerAttacked.put(event.getPlayerId(), numberAttackers + 1); + playerAttacked.putIfAbsent(event.getPlayerId(), 0); + playerAttacked.compute(event.getPlayerId(), (p,amount)->amount + 1); } } @@ -78,6 +78,6 @@ public class PlayerAttackedWatcher extends Watcher { } public int getNumberOfAttackersCurrentTurn(UUID playerId) { - return playerAttacked.containsKey(playerId) ? playerAttacked.get(playerId) : 0; + return playerAttacked.getOrDefault(playerId, 0); } } diff --git a/Mage/src/main/java/mage/watchers/common/PlayerGainedLifeWatcher.java b/Mage/src/main/java/mage/watchers/common/PlayerGainedLifeWatcher.java index cc26a60ab8..f3e9039c39 100644 --- a/Mage/src/main/java/mage/watchers/common/PlayerGainedLifeWatcher.java +++ b/Mage/src/main/java/mage/watchers/common/PlayerGainedLifeWatcher.java @@ -65,23 +65,14 @@ public class PlayerGainedLifeWatcher extends Watcher { if (event.getType() == GameEvent.EventType.GAINED_LIFE) { UUID playerId = event.getPlayerId(); if (playerId != null) { - Integer amount = amountOfLifeGainedThisTurn.get(playerId); - if (amount == null) { - amount = event.getAmount(); - } else { - amount = amount + event.getAmount(); - } - amountOfLifeGainedThisTurn.put(playerId, amount); + amountOfLifeGainedThisTurn.putIfAbsent(playerId, 0); + amountOfLifeGainedThisTurn.compute(playerId, (p, amount) -> amount +event.getAmount()); } } } public int getLiveGained(UUID playerId) { - Integer amount = amountOfLifeGainedThisTurn.get(playerId); - if (amount != null) { - return amount; - } - return 0; + return amountOfLifeGainedThisTurn.getOrDefault(playerId, 0); } @Override diff --git a/Mage/src/main/java/mage/watchers/common/PlayerLostLifeWatcher.java b/Mage/src/main/java/mage/watchers/common/PlayerLostLifeWatcher.java index b07e66b75d..44db03d8bb 100644 --- a/Mage/src/main/java/mage/watchers/common/PlayerLostLifeWatcher.java +++ b/Mage/src/main/java/mage/watchers/common/PlayerLostLifeWatcher.java @@ -75,19 +75,11 @@ public class PlayerLostLifeWatcher extends Watcher { } public int getLiveLost(UUID playerId) { - Integer amount = amountOfLifeLostThisTurn.get(playerId); - if (amount != null) { - return amount; - } - return 0; + return amountOfLifeLostThisTurn.getOrDefault(playerId, 0); } public int getLiveLostLastTurn(UUID playerId) { - Integer amount = amountOfLifeLostLastTurn.get(playerId); - if (amount != null) { - return amount; - } - return 0; + return amountOfLifeLostLastTurn.getOrDefault(playerId, 0); } @Override diff --git a/Mage/src/main/java/mage/watchers/common/ProwlWatcher.java b/Mage/src/main/java/mage/watchers/common/ProwlWatcher.java index ab3a4c0e66..7f6d5f86f8 100644 --- a/Mage/src/main/java/mage/watchers/common/ProwlWatcher.java +++ b/Mage/src/main/java/mage/watchers/common/ProwlWatcher.java @@ -80,10 +80,8 @@ public class ProwlWatcher extends Watcher { if (creature.getAbilities().containsKey(ChangelingAbility.getInstance().getId()) || creature.getSubtype(game).contains(ChangelingAbility.ALL_CREATURE_TYPE)) { allSubtypes.add(creature.getControllerId()); } else { - Set subtypes = damagingSubtypes.get(creature.getControllerId()); - if (subtypes == null) { - subtypes = new LinkedHashSet<>(); - } + Set subtypes = damagingSubtypes.getOrDefault(creature.getControllerId(), new LinkedHashSet<>()); + subtypes.addAll(creature.getSubtype(game)); damagingSubtypes.put(creature.getControllerId(), subtypes); } diff --git a/Mage/src/main/java/mage/watchers/common/SourceDidDamageWatcher.java b/Mage/src/main/java/mage/watchers/common/SourceDidDamageWatcher.java index 7e01b65dd1..4c5ee497b5 100644 --- a/Mage/src/main/java/mage/watchers/common/SourceDidDamageWatcher.java +++ b/Mage/src/main/java/mage/watchers/common/SourceDidDamageWatcher.java @@ -33,9 +33,7 @@ import mage.game.events.GameEvent; import mage.game.events.GameEvent.EventType; import mage.watchers.Watcher; -import java.util.ArrayList; -import java.util.List; -import java.util.UUID; +import java.util.*; /** * Watcher stores which sources did damage to anything. @@ -44,7 +42,7 @@ import java.util.UUID; */ public class SourceDidDamageWatcher extends Watcher { - public final List damageSources = new ArrayList<>(); + public final Set damageSources = new HashSet<>(); public SourceDidDamageWatcher() { super("SourceDidDamageWatcher", WatcherScope.GAME); @@ -65,9 +63,8 @@ public class SourceDidDamageWatcher extends Watcher { if (event.getType() == EventType.DAMAGED_CREATURE || event.getType() == EventType.DAMAGED_PLANESWALKER || event.getType() == EventType.DAMAGED_PLAYER) { - if (!damageSources.contains(event.getSourceId())) { damageSources.add(event.getSourceId()); - } + } }