Search in sources :

Example 1 with StaticValue

use of mage.abilities.dynamicvalue.common.StaticValue in project mage by magefree.

the class AddCountersSourceEffect method apply.

@Override
public boolean apply(Game game, Ability source) {
    Player controller = game.getPlayer(source.getControllerId());
    if (controller != null) {
        if (putOnCard) {
            Card card = game.getCard(source.getSourceId());
            if (card != null) {
                if (counter != null) {
                    Counter newCounter = counter.copy();
                    int countersToAdd = amount.calculate(game, source, this);
                    if (countersToAdd > 0 && newCounter.getCount() == 1) {
                        countersToAdd--;
                    }
                    newCounter.add(countersToAdd);
                    List<UUID> appliedEffects = (ArrayList<UUID>) this.getValue("appliedEffects");
                    card.addCounters(newCounter, source.getControllerId(), source, game, appliedEffects);
                    if (informPlayers && !game.isSimulation()) {
                        Player player = game.getPlayer(source.getControllerId());
                        if (player != null) {
                            game.informPlayers(player.getLogName() + " puts " + newCounter.getCount() + ' ' + newCounter.getName().toLowerCase(Locale.ENGLISH) + " counter on " + card.getLogName());
                        }
                    }
                }
                return true;
            }
        } else {
            Permanent permanent = game.getPermanent(source.getSourceId());
            if (permanent == null && source.getAbilityType() == AbilityType.STATIC) {
                permanent = game.getPermanentEntering(source.getSourceId());
            }
            if (permanent != null && (// from static ability
            source.getSourceObjectZoneChangeCounter() == 0 || source.getSourceObjectZoneChangeCounter() == permanent.getZoneChangeCounter(game))) {
                // prevent to add counters to later source objects
                if (counter != null) {
                    Counter newCounter = counter.copy();
                    int countersToAdd = amount.calculate(game, source, this);
                    if (amount instanceof StaticValue || countersToAdd > 0) {
                        if (countersToAdd > 0 && newCounter.getCount() == 1) {
                            countersToAdd--;
                        }
                        newCounter.add(countersToAdd);
                        int before = permanent.getCounters(game).getCount(newCounter.getName());
                        List<UUID> appliedEffects = (ArrayList<UUID>) this.getValue("appliedEffects");
                        // if used from a replacement effect, the basic event determines if an effect was already applied to an event
                        permanent.addCounters(newCounter, source.getControllerId(), source, game, appliedEffects);
                        if (informPlayers && !game.isSimulation()) {
                            int amountAdded = permanent.getCounters(game).getCount(newCounter.getName()) - before;
                            Player player = game.getPlayer(source.getControllerId());
                            if (player != null) {
                                game.informPlayers(player.getLogName() + " puts " + amountAdded + ' ' + newCounter.getName().toLowerCase(Locale.ENGLISH) + " counter on " + permanent.getLogName());
                            }
                        }
                    }
                }
            }
        }
        return true;
    }
    return false;
}
Also used : Player(mage.players.Player) Counter(mage.counters.Counter) Permanent(mage.game.permanent.Permanent) ArrayList(java.util.ArrayList) StaticValue(mage.abilities.dynamicvalue.common.StaticValue) UUID(java.util.UUID) Card(mage.cards.Card)

Aggregations

ArrayList (java.util.ArrayList)1 UUID (java.util.UUID)1 StaticValue (mage.abilities.dynamicvalue.common.StaticValue)1 Card (mage.cards.Card)1 Counter (mage.counters.Counter)1 Permanent (mage.game.permanent.Permanent)1 Player (mage.players.Player)1