add hasName method

This commit is contained in:
theelk801 2025-06-23 16:39:45 -04:00
parent fda43021a4
commit 29acaa423b
9 changed files with 49 additions and 33 deletions

View file

@ -63,6 +63,8 @@ public interface MageObject extends MageItem, Serializable, Copyable<MageObject>
void setName(String name); void setName(String name);
boolean hasName(String name);
default List<CardType> getCardType() { default List<CardType> getCardType() {
return getCardType(null); return getCardType(null);
} }

View file

@ -119,6 +119,11 @@ public abstract class MageObjectImpl implements MageObject {
this.name = name; this.name = name;
} }
@Override
public boolean hasName(String name) {
return Objects.equals(name, this.name);
}
@Override @Override
public List<CardType> getCardType(Game game) { public List<CardType> getCardType(Game game) {
if (game != null) { if (game != null) {

View file

@ -235,4 +235,9 @@ public abstract class SplitCard extends CardImpl implements CardWithHalves {
// split card and it's halfes contains own mana costs, so no need to rewrite logic // split card and it's halfes contains own mana costs, so no need to rewrite logic
return super.getManaValue(); return super.getManaValue();
} }
@Override
public boolean hasName(String name) {
return super.hasName(name) || this.getLeftHalfCard().hasName(name) || this.getRightHalfCard().hasName(name);
}
} }

View file

@ -1,13 +1,8 @@
package mage.filter.predicate.mageobject; package mage.filter.predicate.mageobject;
import mage.MageObject; import mage.MageObject;
import mage.cards.CardWithHalves;
import mage.cards.SplitCard;
import mage.constants.SpellAbilityType;
import mage.filter.predicate.Predicate; import mage.filter.predicate.Predicate;
import mage.game.Game; import mage.game.Game;
import mage.game.stack.Spell;
import mage.util.CardUtil;
/** /**
* @author North * @author North
@ -39,9 +34,6 @@ public class NamePredicate implements Predicate<MageObject> {
@Override @Override
public boolean apply(MageObject input, Game game) { public boolean apply(MageObject input, Game game) {
if (name == null) {
return false;
}
// If a player names a card, the player may name either half of a split card, but not both. // If a player names a card, the player may name either half of a split card, but not both.
// A split card has the chosen name if one of its two names matches the chosen name. // A split card has the chosen name if one of its two names matches the chosen name.
// This is NOT the same for double faced cards, where only the front side matches // This is NOT the same for double faced cards, where only the front side matches
@ -50,29 +42,7 @@ public class NamePredicate implements Predicate<MageObject> {
// If the back face of a modal double-faced card is countered, you will not be able to exile any cards, // If the back face of a modal double-faced card is countered, you will not be able to exile any cards,
// including the one that you countered, because those cards have only their front-face characteristics // including the one that you countered, because those cards have only their front-face characteristics
// (including name) in the graveyard, hand, and library. (2021-04-16) // (including name) in the graveyard, hand, and library. (2021-04-16)
return name != null && input.hasName(name);
if (input instanceof SplitCard) {
return CardUtil.haveSameNames(name, ((CardWithHalves) input).getLeftHalfCard().getName(), this.ignoreMtgRuleForEmptyNames) ||
CardUtil.haveSameNames(name, ((CardWithHalves) input).getRightHalfCard().getName(), this.ignoreMtgRuleForEmptyNames) ||
CardUtil.haveSameNames(name, input.getName(), this.ignoreMtgRuleForEmptyNames);
} else if (input instanceof Spell && ((Spell) input).getSpellAbility().getSpellAbilityType() == SpellAbilityType.SPLIT_FUSED) {
SplitCard card = (SplitCard) ((Spell) input).getCard();
return CardUtil.haveSameNames(name, card.getLeftHalfCard().getName(), this.ignoreMtgRuleForEmptyNames) ||
CardUtil.haveSameNames(name, card.getRightHalfCard().getName(), this.ignoreMtgRuleForEmptyNames) ||
CardUtil.haveSameNames(name, card.getName(), this.ignoreMtgRuleForEmptyNames);
} else if (input instanceof Spell && ((Spell) input).isFaceDown(game)) {
// face down spells don't have names, so it's not equal, see https://github.com/magefree/mage/issues/6569
return false;
} else {
if (name.contains(" // ")) {
String leftName = name.substring(0, name.indexOf(" // "));
String rightName = name.substring(name.indexOf(" // ") + 4);
return CardUtil.haveSameNames(leftName, input.getName(), this.ignoreMtgRuleForEmptyNames) ||
CardUtil.haveSameNames(rightName, input.getName(), this.ignoreMtgRuleForEmptyNames);
} else {
return CardUtil.haveSameNames(name, input.getName(), this.ignoreMtgRuleForEmptyNames);
}
}
} }
@Override @Override

View file

@ -1,8 +1,8 @@
package mage.game.command; package mage.game.command;
import mage.game.permanent.token.TokenImpl;
import mage.util.GameLog; import mage.util.GameLog;
import java.util.Objects;
import java.util.UUID; import java.util.UUID;
/** /**
@ -113,4 +113,9 @@ public abstract class CommandObjectImpl implements CommandObject {
public String getLogName() { public String getLogName() {
return GameLog.getColoredObjectIdName(this); return GameLog.getColoredObjectIdName(this);
} }
@Override
public boolean hasName(String name) {
return Objects.equals(name, this.name);
}
} }

View file

@ -163,6 +163,11 @@ public class Commander extends CommandObjectImpl {
return sourceObject.getName() + " [" + sourceObject.getId().toString().substring(0, 3) + ']'; return sourceObject.getName() + " [" + sourceObject.getId().toString().substring(0, 3) + ']';
} }
@Override
public boolean hasName(String name) {
return sourceObject.hasName(name);
}
@Override @Override
public List<CardType> getCardType(Game game) { public List<CardType> getCardType(Game game) {
return sourceObject.getCardType(game); return sourceObject.getCardType(game);

View file

@ -260,6 +260,11 @@ public abstract class PermanentImpl extends CardImpl implements Permanent {
} }
} }
@Override
public boolean hasName(String name) {
return !faceDown && super.hasName(name);
}
@Override @Override
public String getValue(GameState state) { public String getValue(GameState state) {
StringBuilder sb = threadLocalBuilder.get(); StringBuilder sb = threadLocalBuilder.get();

View file

@ -563,6 +563,19 @@ public class Spell extends StackObjectImpl implements Card {
public void setName(String name) { public void setName(String name) {
} }
@Override
public boolean hasName(String name) {
if (this.faceDown) {
return false;
}
if (this.getSpellAbility().getSpellAbilityType() == SpellAbilityType.SPLIT_FUSED
&& (((SplitCard) this.card).getLeftHalfCard().hasName(name)
|| ((SplitCard) this.card).getRightHalfCard().hasName(name))) {
return true;
}
return this.card.hasName(name);
}
@Override @Override
public Rarity getRarity() { public Rarity getRarity() {
return card.getRarity(); return card.getRarity();

View file

@ -198,6 +198,11 @@ public class StackAbility extends StackObjectImpl implements Ability {
this.name = name; this.name = name;
} }
@Override
public boolean hasName(String name) {
return Objects.equals(name, this.name);
}
@Override @Override
public List<CardType> getCardType(Game game) { public List<CardType> getCardType(Game game) {
return emptyCardType; return emptyCardType;
@ -677,6 +682,7 @@ public class StackAbility extends StackObjectImpl implements Ability {
public void initSourceObjectZoneChangeCounter(Game game, boolean force) { public void initSourceObjectZoneChangeCounter(Game game, boolean force) {
ability.initSourceObjectZoneChangeCounter(game, force); ability.initSourceObjectZoneChangeCounter(game, force);
} }
@Override @Override
public int getSourceObjectZoneChangeCounter() { public int getSourceObjectZoneChangeCounter() {
return ability.getSourceObjectZoneChangeCounter(); return ability.getSourceObjectZoneChangeCounter();