* Non creature tokens - fixed rollback errors in AI games (example: Food token, see #6331);

Fixed other potentially NPE errors with rarity;
This commit is contained in:
Oleg Agafonov 2020-03-06 03:27:46 +04:00
parent 1b60dfc258
commit cb8d4dc340
12 changed files with 88 additions and 70 deletions

View file

@ -183,7 +183,7 @@ public class Card extends MagePermanent implements MouseMotionListener, MouseLis
if (card.getExpansionSetCode() != null && !card.getExpansionSetCode().isEmpty()) {
sb.append('\n').append(card.getCardNumber()).append(" - ");
sb.append(Sets.getInstance().get(card.getExpansionSetCode()).getName()).append(" - ");
sb.append(card.getRarity().toString());
sb.append(card.getRarity() == null ? "none" : card.getRarity().toString());
}
}
// sb.append("\n").append(card.getId());

View file

@ -15,6 +15,7 @@
import mage.client.util.Event;
import mage.client.util.GUISizeHelper;
import mage.client.util.Listener;
import mage.constants.Rarity;
import mage.utils.CardColorUtil;
import mage.view.CardView;
import mage.view.CardsView;
@ -362,7 +363,14 @@
@Override
public int compare(MageCard o1, MageCard o2) {
int val = o1.getOriginal().getRarity().compareTo(o2.getOriginal().getRarity());
Rarity r1 = o1.getOriginal().getRarity();
Rarity r2 = o2.getOriginal().getRarity();
int val = Integer.compare(
r1 == null ? 0 : r1.getSorting(),
r2 == null ? 0 : r2.getSorting()
);
if (val == 0) {
return o1.getOriginal().getName().compareTo(o2.getOriginal().getName());
} else {

View file

@ -12,6 +12,7 @@ import mage.client.dialog.PreferencesDialog;
import mage.client.plugins.impl.Plugins;
import mage.client.util.*;
import mage.constants.CardType;
import mage.constants.Rarity;
import mage.constants.SubType;
import mage.constants.SuperType;
import mage.util.RandomUtil;
@ -1295,7 +1296,10 @@ public class DragCardGrid extends JPanel implements DragCardSource, DragCardTarg
}
// Rarity
if (!s) {
s |= card.getRarity().toString().toLowerCase(Locale.ENGLISH).contains(searchStr);
Rarity r = card.getRarity();
if (r != null) {
s |= r.toString().toLowerCase(Locale.ENGLISH).contains(searchStr);
}
}
// Type line
if (!s) {

View file

@ -28,7 +28,6 @@ import static mage.client.constants.Constants.DAMAGE_MAX_LEFT;
import static mage.client.constants.Constants.POWBOX_TEXT_MAX_TOP;
/**
*
* @author BetaSteward_at_googlemail.com
*/
public class Permanent extends Card {
@ -40,11 +39,14 @@ public class Permanent extends Card {
protected final BufferedImage tappedImage;
protected BufferedImage flippedImage;
/** Creates new form Permanent
/**
* Creates new form Permanent
*
* @param permanent
* @param bigCard
* @param dimensions
* @param gameId */
* @param gameId
*/
public Permanent(PermanentView permanent, BigCard bigCard, CardDimensions dimensions, UUID gameId) {
super(permanent, bigCard, dimensions, gameId);
this.setSize(this.getPreferredSize());
@ -74,29 +76,28 @@ public class Permanent extends Card {
StringBuilder sb = new StringBuilder();
sb.append(super.getText(cardType));
if (permanent.getOriginal() != null) {
sb.append("\n----- Originally -------\n");
sb.append(permanent.getOriginal().getName());
if (!permanent.getOriginal().getManaCost().isEmpty()) {
sb.append('\n').append(permanent.getOriginal().getManaCost());
}
sb.append('\n').append(getType(permanent.getOriginal()));
if (permanent.getOriginal().getColor().hasColor()) {
sb.append('\n').append(permanent.getOriginal().getColor().toString());
}
if (permanent.getOriginal().isCreature()) {
sb.append('\n').append(permanent.getOriginal().getPower()).append('/').append(permanent.getOriginal().getToughness());
}
else if (permanent.getOriginal().isPlanesWalker()) {
sb.append('\n').append(permanent.getOriginal().getLoyalty());
}
for (String rule: getRules()) {
sb.append('\n').append(rule);
}
if (!permanent.getOriginal().getExpansionSetCode().isEmpty()) {
sb.append('\n').append(permanent.getCardNumber()).append(" - ");
sb.append('\n').append(Sets.getInstance().get(permanent.getOriginal().getExpansionSetCode()).getName()).append(" - ");
sb.append(permanent.getOriginal().getRarity().toString());
}
sb.append("\n----- Originally -------\n");
sb.append(permanent.getOriginal().getName());
if (!permanent.getOriginal().getManaCost().isEmpty()) {
sb.append('\n').append(permanent.getOriginal().getManaCost());
}
sb.append('\n').append(getType(permanent.getOriginal()));
if (permanent.getOriginal().getColor().hasColor()) {
sb.append('\n').append(permanent.getOriginal().getColor().toString());
}
if (permanent.getOriginal().isCreature()) {
sb.append('\n').append(permanent.getOriginal().getPower()).append('/').append(permanent.getOriginal().getToughness());
} else if (permanent.getOriginal().isPlanesWalker()) {
sb.append('\n').append(permanent.getOriginal().getLoyalty());
}
for (String rule : getRules()) {
sb.append('\n').append(rule);
}
if (!permanent.getOriginal().getExpansionSetCode().isEmpty()) {
sb.append('\n').append(permanent.getCardNumber()).append(" - ");
sb.append('\n').append(Sets.getInstance().get(permanent.getOriginal().getExpansionSetCode()).getName()).append(" - ");
sb.append(permanent.getOriginal().getRarity() == null ? "none" : permanent.getOriginal().getRarity().toString());
}
// sb.append("\n").append(card.getId());
}
return sb.toString();
@ -107,12 +108,11 @@ public class Permanent extends Card {
protected List<String> getRules() {
if (permanent.getCounters() != null) {
List<String> rules = new ArrayList<>(permanent.getRules());
for (CounterView counter: permanent.getCounters()) {
for (CounterView counter : permanent.getCounters()) {
rules.add(counter.getCount() + " x " + counter.getName());
}
return rules;
}
else {
} else {
return permanent.getRules();
}
}
@ -139,7 +139,7 @@ public class Permanent extends Card {
}
this.setBounds(r);
this.repaint();
for (MagePermanent perm: links) {
for (MagePermanent perm : links) {
r.x += 20;
r.y += 20;
perm.setBounds(r);
@ -160,22 +160,20 @@ public class Permanent extends Card {
if (permanent.isTapped()) {
this.getText().setVisible(false);
g2.drawImage(tappedImage, 0, 0, this);
}
else {
} else {
this.getText().setVisible(true);
g2.drawImage(small, 0, 0, this);
}
//Add a border, red if card currently has focus
if (isFocusOwner()) {
g2.setColor(Color.RED);
g2.setColor(Color.RED);
} else {
g2.setColor(Color.BLACK);
g2.setColor(Color.BLACK);
}
if (permanent.isTapped()) {
g2.drawRect(0, 0, ClientDefaultSettings.dimensions.getFrameHeight() - 1, ClientDefaultSettings.dimensions.getFrameWidth() - 1);
}
else {
} else {
g2.drawRect(0, 0, ClientDefaultSettings.dimensions.getFrameWidth() - 1, ClientDefaultSettings.dimensions.getFrameHeight() - 1);
}
@ -207,8 +205,7 @@ public class Permanent extends Card {
public Dimension getPreferredSize() {
if (permanent != null && permanent.isTapped()) {
return new Dimension(ClientDefaultSettings.dimensions.getFrameHeight(), ClientDefaultSettings.dimensions.getFrameWidth());
}
else {
} else {
return new Dimension(ClientDefaultSettings.dimensions.getFrameWidth(), ClientDefaultSettings.dimensions.getFrameHeight());
}
}
@ -229,7 +226,7 @@ public class Permanent extends Card {
tooltipPopup.hide();
}
PopupFactory factory = PopupFactory.getSharedInstance();
int x = (int) this.getLocationOnScreen().getX() + (permanent.isTapped()? ClientDefaultSettings.dimensions.getFrameHeight() : ClientDefaultSettings.dimensions.getFrameWidth());
int x = (int) this.getLocationOnScreen().getX() + (permanent.isTapped() ? ClientDefaultSettings.dimensions.getFrameHeight() : ClientDefaultSettings.dimensions.getFrameWidth());
int y = (int) this.getLocationOnScreen().getY() + 40;
tooltipPopup = factory.getPopup(this, tooltipText, x, y);
tooltipPopup.show();
@ -246,7 +243,8 @@ public class Permanent extends Card {
return permanent;
}
/** This method is called from within the constructor to
/**
* This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.

View file

@ -97,8 +97,8 @@ public class MageCardComparator implements Comparator<CardView> {
break;
// Rarity
case 6:
aCom = a.getRarity().getSorting();
bCom = b.getRarity().getSorting();
aCom = a.getRarity() == null ? 0 : a.getRarity().getSorting();
bCom = b.getRarity() == null ? 0 : b.getRarity().getSorting();
break;
// Set name
case 7:

View file

@ -6,8 +6,8 @@ import mage.client.cards.CardEventSource;
import mage.client.cards.ICardGrid;
import mage.client.deckeditor.SortSetting;
import mage.client.plugins.impl.Plugins;
import mage.client.util.ClientEventType;
import mage.client.util.ClientDefaultSettings;
import mage.client.util.ClientEventType;
import mage.client.util.Event;
import mage.client.util.Listener;
import mage.client.util.gui.GuiDisplayUtil;
@ -235,7 +235,7 @@ public class TableModel extends AbstractTableModel implements ICardGrid {
return c.isCreature() ? c.getPower() + '/'
+ c.getToughness() : "-";
case 6:
return c.getRarity().toString();
return c.getRarity() == null ? "" : c.getRarity().toString();
case 7:
return c.getExpansionSetCode();
case 8:

View file

@ -1,19 +1,30 @@
package mage.client.util;
import java.util.Comparator;
import mage.constants.Rarity;
import mage.view.CardView;
import java.util.Comparator;
/**
*
* @author BetaSteward_at_googlemail.com
*/
public class CardViewRarityComparator implements Comparator<CardView> {
@Override
public int compare(CardView o1, CardView o2) {
return o1.getRarity().compareTo(o2.getRarity());
Rarity r1 = o1.getRarity();
Rarity r2 = o2.getRarity();
int val = Integer.compare(
r1 == null ? 0 : r1.getSorting(),
r2 == null ? 0 : r2.getSorting()
);
if (val == 0) {
return o1.getName().compareTo(o2.getName());
} else {
return val;
}
}
}

View file

@ -750,7 +750,7 @@ public abstract class CardPanel extends MagePermanent implements MouseListener,
if (card.getExpansionSetCode() != null && !card.getExpansionSetCode().isEmpty()) {
sb.append('\n').append(card.getCardNumber()).append(" - ");
sb.append(card.getExpansionSetCode()).append(" - ");
sb.append(card.getRarity().toString());
sb.append(card.getRarity() == null ? "none" : card.getRarity().toString());
}
}
return sb.toString();

View file

@ -43,7 +43,7 @@ public final class ArtificialScoringSystem {
if (card.getCardType().contains(CardType.CREATURE)) {
return score + (card.getPower().getValue() + card.getToughness().getValue()) * 10;
} else {
return score + (/*card.getRemoval()*50*/+card.getRarity().getRating() * 30);
return score + (/*card.getRemoval()*50*/+(card.getRarity() == null ? 0 : card.getRarity().getRating() * 30));
}
}

View file

@ -1,7 +1,5 @@
package mage.cards.r;
import java.util.*;
import mage.abilities.Ability;
import mage.abilities.effects.OneShotEffect;
import mage.cards.Card;
@ -19,8 +17,11 @@ import mage.game.Game;
import mage.game.permanent.Permanent;
import mage.players.Player;
import java.util.Objects;
import java.util.Set;
import java.util.UUID;
/**
*
* @author L_J
*/
public final class RareBGone extends CardImpl {
@ -109,7 +110,7 @@ class RarityPredicate implements Predicate<Card> {
@Override
public boolean apply(Card input, Game game) {
return input.getRarity().equals(rarity);
return Objects.equals(input.getRarity(), rarity);
}
@Override

View file

@ -1,10 +1,5 @@
package org.mage.test.sets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import mage.MageObject;
import mage.abilities.Ability;
import mage.abilities.keyword.PartnerWithAbility;
@ -13,17 +8,17 @@ import mage.cards.repository.CardInfo;
import mage.cards.repository.CardScanner;
import mage.constants.CardType;
import mage.constants.Rarity;
import mage.sets.*;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mage.test.serverside.base.MageTestBase;
import java.util.*;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
/**
* @author nigelzor, JayDi85
*/
@ -141,7 +136,7 @@ public class BoosterGenerationTest extends MageTestBase {
allCards.addAll(booster);
}
// check that some dual lands were generated
assertTrue(allCards.stream().anyMatch(card -> card.getCardType().contains(CardType.LAND) && card.getRarity().equals(Rarity.COMMON)));
assertTrue(allCards.stream().anyMatch(card -> card.getCardType().contains(CardType.LAND) && Objects.equals(card.getRarity(), Rarity.COMMON)));
}
@Test

View file

@ -245,7 +245,7 @@ public class VerifyCardDataTest {
Map<String, Integer> cardsList = new HashMap<>();
for (ExpansionSet.SetCardInfo checkCard : set.getSetCardInfo()) {
// only rare cards must have double versions
if (!checkCard.getRarity().equals(Rarity.RARE) && !checkCard.getRarity().equals(Rarity.MYTHIC)) {
if (!Objects.equals(checkCard.getRarity(), Rarity.RARE) && !Objects.equals(checkCard.getRarity(), Rarity.MYTHIC)) {
continue;
}
@ -519,6 +519,7 @@ public class VerifyCardDataTest {
}
// 3. check that getMana works without NPE errors (it uses getNetMana with empty game param for AI score calcs)
// https://github.com/magefree/mage/issues/6300
card.getMana();
}
}