replaced instances of Optional::of with Optional::ofNullable (fixes #9183)

This commit is contained in:
Evan Kranzler 2022-06-29 18:32:15 -04:00
parent 015444d00f
commit deb29c0659
28 changed files with 36 additions and 71 deletions

View file

@ -78,7 +78,7 @@ class AmuletOfVigorTriggeredAbility extends TriggeredAbilityImpl {
// that triggers depends on stack order, so make each trigger unique with extra info
return "Whenever a permanent enters the battlefield tapped and under your control, untap it."
+ Optional
.of(this.getEffects().get(0).getTargetPointer())
.ofNullable(this.getEffects().get(0).getTargetPointer())
.map(targetPointer -> targetPointer.getData("triggeredName"))
.filter(s -> s != null && !s.isEmpty())
.map(s -> " Triggered permanent: " + s)

View file

@ -24,7 +24,6 @@ import mage.game.Game;
import mage.players.Player;
import mage.target.common.TargetCreaturePermanent;
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;
@ -79,8 +78,7 @@ enum BhaalLordOfMurderCondition implements Condition {
@Override
public boolean apply(Game game, Ability source) {
return Optional.of(game.getPlayer(source.getControllerId()))
.filter(Objects::nonNull)
return Optional.ofNullable(game.getPlayer(source.getControllerId()))
.map(Player::getLife)
.map(x -> 2 * x >= game.getStartingLife())
.orElse(false);

View file

@ -64,8 +64,7 @@ enum BrassKnucklesCondition implements Condition {
@Override
public boolean apply(Game game, Ability source) {
return Optional
.of(source.getSourcePermanentIfItStillExists(game))
.filter(Objects::nonNull)
.ofNullable(source.getSourcePermanentIfItStillExists(game))
.map(Permanent::getAttachedTo)
.map(game::getPermanent)
.filter(Objects::nonNull)

View file

@ -116,8 +116,7 @@ class ConquerorsFlailEffect extends ContinuousRuleModifyingEffectImpl {
return game.isActivePlayer(source.getControllerId())
&& game.getOpponents(source.getControllerId()).contains(event.getPlayerId())
&& Optional
.of(source.getSourcePermanentIfItStillExists(game))
.filter(Objects::nonNull)
.ofNullable(source.getSourcePermanentIfItStillExists(game))
.map(Permanent::getAttachedTo)
.map(game::getPermanent)
.filter(Objects::nonNull)

View file

@ -15,7 +15,6 @@ import mage.constants.SubType;
import mage.game.Game;
import mage.game.permanent.Permanent;
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;
@ -57,8 +56,7 @@ enum CrewCaptainCondition implements Condition {
@Override
public boolean apply(Game game, Ability source) {
return Optional.of(source.getSourcePermanentIfItStillExists(game))
.filter(Objects::nonNull)
return Optional.ofNullable(source.getSourcePermanentIfItStillExists(game))
.map(Permanent::getTurnsOnBattlefield)
.orElseGet(() -> -1) == 0;
}

View file

@ -56,7 +56,7 @@ enum DelayedBlastFireballValue implements DynamicValue {
@Override
public int calculate(Game game, Ability sourceAbility, Effect effect) {
return Optional
.of(sourceAbility.getSourceObjectIfItStillExists(game))
.ofNullable(sourceAbility.getSourceObjectIfItStillExists(game))
.filter(Spell.class::isInstance)
.map(Spell.class::cast)
.map(Spell::getFromZone)

View file

@ -16,7 +16,6 @@ import mage.counters.CounterType;
import mage.game.Game;
import mage.game.permanent.token.TreasureToken;
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;
@ -71,8 +70,7 @@ class DescentIntoAvernusEffect extends OneShotEffect {
@Override
public boolean apply(Game game, Ability source) {
int count = Optional
.of(source.getSourcePermanentOrLKI(game))
.filter(Objects::nonNull)
.ofNullable(source.getSourcePermanentOrLKI(game))
.map(permanent -> permanent.getCounters(game))
.map(counters -> counters.getCount(CounterType.DESCENT))
.orElse(0);

View file

@ -21,7 +21,6 @@ import mage.game.events.GameEvent;
import mage.game.permanent.token.WolfToken;
import mage.game.stack.Spell;
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;
@ -90,8 +89,7 @@ class FaldornDreadWolfHeraldTriggeredAbility extends TriggeredAbilityImpl {
return eEvent.getFromZone() == Zone.EXILED && eEvent.getTarget().isLand(game);
case SPELL_CAST:
return Optional
.of(game.getSpell(event.getTargetId()))
.filter(Objects::nonNull)
.ofNullable(game.getSpell(event.getTargetId()))
.map(Spell::getFromZone)
.equals(Zone.EXILED);
}

View file

@ -17,7 +17,6 @@ import mage.filter.predicate.ObjectSourcePlayerPredicate;
import mage.game.Game;
import mage.game.stack.Spell;
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;
@ -63,8 +62,7 @@ enum FolkHeroPredicate implements ObjectSourcePlayerPredicate<Spell> {
@Override
public boolean apply(ObjectSourcePlayer<Spell> input, Game game) {
return Optional
.of(input.getSource().getSourcePermanentIfItStillExists(game))
.filter(Objects::nonNull)
.ofNullable(input.getSource().getSourcePermanentIfItStillExists(game))
.map(permanent -> input.getObject().shareCreatureTypes(game, permanent))
.orElse(false);
}

View file

@ -22,7 +22,10 @@ import mage.counters.Counter;
import mage.counters.CounterType;
import mage.game.Game;
import java.util.*;
import java.util.Collection;
import java.util.HashMap;
import java.util.Optional;
import java.util.UUID;
import java.util.stream.IntStream;
/**
@ -78,8 +81,7 @@ enum GavelOfTheRighteousCondition implements Condition {
@Override
public boolean apply(Game game, Ability source) {
return Optional.of(source.getSourcePermanentIfItStillExists(game))
.filter(Objects::nonNull)
return Optional.ofNullable(source.getSourcePermanentIfItStillExists(game))
.map(permanent -> permanent.getCounters(game))
.map(HashMap::values)
.map(Collection::stream)

View file

@ -20,7 +20,6 @@ import mage.game.Game;
import mage.game.permanent.Permanent;
import mage.target.TargetPermanent;
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;
@ -67,8 +66,7 @@ enum GreatswordOfTyrPredicate implements ObjectSourcePlayerPredicate<Permanent>
@Override
public boolean apply(ObjectSourcePlayer<Permanent> input, Game game) {
return Optional.of(input.getSource().getSourcePermanentOrLKI(game))
.filter(Objects::nonNull)
return Optional.ofNullable(input.getSource().getSourcePermanentOrLKI(game))
.map(Permanent::getAttachedTo)
.map(uuid -> game.getCombat().getDefendingPlayerId(uuid, game))
.map(input.getObject()::isControlledBy)

View file

@ -14,7 +14,6 @@ import mage.constants.SubType;
import mage.game.Game;
import mage.watchers.common.BlockingOrBlockedWatcher;
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;
@ -58,8 +57,7 @@ enum GuildswornProwlerCondition implements Condition {
@Override
public boolean apply(Game game, Ability source) {
return Optional
.of(source.getSourcePermanentOrLKI(game))
.filter(Objects::nonNull)
.ofNullable(source.getSourcePermanentOrLKI(game))
.map(permanent -> !BlockingOrBlockedWatcher.check(permanent, game))
.orElse(false);
}

View file

@ -18,7 +18,6 @@ import mage.filter.predicate.ObjectSourcePlayerPredicate;
import mage.game.Game;
import mage.game.permanent.Permanent;
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;
@ -74,8 +73,7 @@ enum HauntedOnePredicate implements ObjectSourcePlayerPredicate<Permanent> {
@Override
public boolean apply(ObjectSourcePlayer<Permanent> input, Game game) {
return Optional.of(input.getSource().getSourcePermanentOrLKI(game))
.filter(Objects::nonNull)
return Optional.ofNullable(input.getSource().getSourcePermanentOrLKI(game))
.map(permanent -> permanent.shareCreatureTypes(game, input.getObject()))
.orElse(false);
}

View file

@ -106,7 +106,7 @@ class JourneyToTheLostCityEffect extends RollDieWithResultTableEffect {
int count = cards.count(StaticFilters.FILTER_CARD_CREATURE, game);
if (count > 0) {
for (UUID tokenId : token.getLastAddedTokenIds()) {
Optional.of(game.getPermanent(tokenId))
Optional.ofNullable(game.getPermanent(tokenId))
.ifPresent(permanent -> permanent.addCounters(
CounterType.P1P1.createInstance(count), source, game
));
@ -123,7 +123,7 @@ class JourneyToTheLostCityEffect extends RollDieWithResultTableEffect {
StaticFilters.FILTER_CARD_PERMANENT, game
), Zone.BATTLEFIELD, source, game);
}
Optional.of(source.getSourcePermanentIfItStillExists(game))
Optional.ofNullable(source.getSourcePermanentIfItStillExists(game))
.ifPresent(permanent -> permanent.sacrifice(source, game));
return true;
}

View file

@ -19,7 +19,6 @@ import mage.game.Game;
import mage.game.stack.Spell;
import mage.target.common.TargetCreaturePermanent;
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;
@ -63,8 +62,7 @@ enum LivaanCultistOfTiamatValue implements DynamicValue {
@Override
public int calculate(Game game, Ability sourceAbility, Effect effect) {
return Optional.of(effect.getValue("spellCast"))
.filter(Objects::nonNull)
return Optional.ofNullable(effect.getValue("spellCast"))
.map(Spell.class::cast)
.map(Spell::getManaValue)
.orElse(0);

View file

@ -20,7 +20,6 @@ import mage.game.Game;
import mage.game.stack.Spell;
import mage.target.common.TargetAnyTarget;
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;
@ -76,9 +75,8 @@ enum LozhanDragonsLegacyValue implements DynamicValue {
@Override
public int calculate(Game game, Ability sourceAbility, Effect effect) {
return Optional.of(effect.getValue("spellCast"))
return Optional.ofNullable(effect.getValue("spellCast"))
.map(Spell.class::cast)
.filter(Objects::nonNull)
.map(Spell::getManaValue)
.orElse(0);
}

View file

@ -17,10 +17,10 @@ import mage.filter.common.FilterControlledPlaneswalkerPermanent;
import mage.game.Game;
import mage.game.permanent.Permanent;
import mage.target.TargetPermanent;
import mage.target.common.TargetControlledCreaturePermanent;
import java.util.*;
import java.util.stream.IntStream;
import mage.target.common.TargetControlledCreaturePermanent;
/**
* @author TheElk801
@ -65,8 +65,7 @@ enum LuxiorGiadasGiftValue implements DynamicValue {
@Override
public int calculate(Game game, Ability sourceAbility, Effect effect) {
return Optional.of(sourceAbility.getSourcePermanentIfItStillExists(game))
.filter(Objects::nonNull)
return Optional.ofNullable(sourceAbility.getSourcePermanentIfItStillExists(game))
.map(Permanent::getAttachedTo)
.map(game::getPermanent)
.filter(Objects::nonNull)
@ -112,8 +111,7 @@ class LuxiorGiadasGiftEffect extends ContinuousEffectImpl {
@Override
public boolean apply(Game game, Ability source) {
Optional.of(source.getSourcePermanentIfItStillExists(game))
.filter(Objects::nonNull)
Optional.ofNullable(source.getSourcePermanentIfItStillExists(game))
.map(Permanent::getAttachedTo)
.map(game::getPermanent)
.ifPresent(permanent -> {

View file

@ -51,7 +51,7 @@ enum ManaplasmValue implements DynamicValue {
@Override
public int calculate(Game game, Ability sourceAbility, Effect effect) {
return Optional.of((Spell) effect.getValue("spellCast")).map(Spell::getManaValue).orElse(0);
return Optional.ofNullable((Spell) effect.getValue("spellCast")).map(Spell::getManaValue).orElse(0);
}
@Override

View file

@ -25,7 +25,6 @@ import mage.game.permanent.Permanent;
import mage.util.CardUtil;
import mage.util.functions.CopyApplier;
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;
@ -87,8 +86,7 @@ enum MockingDoppelgangerPredicate implements ObjectSourcePlayerPredicate<Permane
@Override
public boolean apply(ObjectSourcePlayer<Permanent> input, Game game) {
return Optional
.of(input.getSource().getSourcePermanentIfItStillExists(game))
.filter(Objects::nonNull)
.ofNullable(input.getSource().getSourcePermanentIfItStillExists(game))
.map(permanent -> CardUtil.haveSameNames(permanent, input.getObject()))
.orElse(false);
}

View file

@ -22,7 +22,6 @@ import mage.game.Game;
import mage.game.permanent.PermanentCard;
import mage.players.Player;
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;
@ -69,8 +68,7 @@ enum MyrkulLordOfBonesCondition implements Condition {
@Override
public boolean apply(Game game, Ability source) {
return Optional.of(game.getPlayer(source.getControllerId()))
.filter(Objects::nonNull)
return Optional.ofNullable(game.getPlayer(source.getControllerId()))
.map(Player::getLife)
.map(x -> 2 * x >= game.getStartingLife())
.orElse(false);

View file

@ -64,7 +64,7 @@ enum PassionateArchaeologistValue implements DynamicValue {
@Override
public int calculate(Game game, Ability sourceAbility, Effect effect) {
return Optional
.of(effect.getValue("spellCast"))
.ofNullable(effect.getValue("spellCast"))
.filter(Spell.class::isInstance)
.map(Spell.class::cast)
.map(Spell::getManaValue)

View file

@ -80,8 +80,7 @@ class PublicEnemyEffect extends RequirementEffect {
@Override
public UUID mustAttackDefender(Ability source, Game game) {
return Optional.of(source.getSourcePermanentIfItStillExists(game))
.filter(Objects::nonNull)
return Optional.ofNullable(source.getSourcePermanentIfItStillExists(game))
.map(Permanent::getAttachedTo)
.map(game::getControllerId)
.orElse(null);

View file

@ -23,7 +23,6 @@ import mage.filter.predicate.Predicates;
import mage.game.Game;
import mage.players.Player;
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;
@ -81,8 +80,7 @@ enum SailorsBaneValue implements DynamicValue {
@Override
public int calculate(Game game, Ability sourceAbility, Effect effect) {
return Optional
.of(game.getPlayer(sourceAbility.getControllerId()))
.filter(Objects::nonNull)
.ofNullable(game.getPlayer(sourceAbility.getControllerId()))
.map(Player::getGraveyard)
.map(graveyard -> graveyard.count(filter, game))
.orElse(0)

View file

@ -140,7 +140,7 @@ class SchemingFenceDisableEffect extends ContinuousRuleModifyingEffectImpl {
return false;
}
return Optional.of(chosenPermanentMOR)
return Optional.ofNullable(chosenPermanentMOR)
.map(MageObjectReference.class::cast)
.filter(mor -> mor.zoneCounterIsCurrent(game))
.map(MageObjectReference::getSourceId) // TODO: Are both of this line and the next one necessary?
@ -177,7 +177,7 @@ class SchemingFenceGainEffect extends ContinuousEffectImpl {
return false;
}
Permanent chosenPermanent = Optional.of(chosenPermanentMOR)
Permanent chosenPermanent = Optional.ofNullable(chosenPermanentMOR)
.map(MageObjectReference.class::cast)
.map(mor -> mor.getPermanent(game))
.orElse(null);

View file

@ -24,7 +24,6 @@ import mage.game.permanent.Permanent;
import mage.target.TargetPermanent;
import mage.target.common.TargetCreaturePermanent;
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;
@ -93,8 +92,7 @@ class StunningStrikeEffect extends OneShotEffect {
@Override
public boolean apply(Game game, Ability source) {
Optional.of(source.getSourcePermanentOrLKI(game))
.filter(Objects::nonNull)
Optional.ofNullable(source.getSourcePermanentOrLKI(game))
.map(Permanent::getAttachedTo)
.map(game::getPermanent)
.ifPresent(permanent -> {

View file

@ -13,7 +13,6 @@ import mage.constants.Outcome;
import mage.constants.SubType;
import mage.game.Game;
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;
@ -67,8 +66,7 @@ class ViciousBattleragerEffect extends OneShotEffect {
@Override
public boolean apply(Game game, Ability source) {
return Optional.of(game.getPlayer(game.getControllerId(getTargetPointer().getFirst(game, source))))
.filter(Objects::nonNull)
return Optional.ofNullable(game.getPlayer(game.getControllerId(getTargetPointer().getFirst(game, source))))
.map(player -> player.loseLife(5, game, source, false) > 0)
.orElse(false);
}

View file

@ -9,7 +9,6 @@ import mage.game.events.GameEvent;
import mage.game.permanent.Permanent;
import mage.target.targetpointer.FixedTarget;
import java.util.Objects;
import java.util.Optional;
/**
@ -41,8 +40,7 @@ public class BecomesBlockedAttachedTriggeredAbility extends TriggeredAbilityImpl
@Override
public boolean checkTrigger(GameEvent event, Game game) {
Permanent permanent = Optional
.of(getSourcePermanentOrLKI(game))
.filter(Objects::nonNull)
.ofNullable(getSourcePermanentOrLKI(game))
.map(Permanent::getAttachedTo)
.map(game::getPermanent)
.orElse(null);

View file

@ -63,7 +63,7 @@ class CastExiledFromHandCardEffect extends OneShotEffect {
@Override
public boolean apply(Game game, Ability source) {
Optional.of(getValue("exiledHandCardRef"))
Optional.ofNullable(getValue("exiledHandCardRef"))
.filter(Objects::nonNull)
.map(MageObjectReference.class::cast)
.map(mor -> mor.getCard(game))