prevent NPE when overriding CardView for HumanCostDecision

This commit is contained in:
Anthony Calosa
2022-08-13 08:41:51 +08:00
parent c49521e063
commit 5af8a3939a
2 changed files with 21 additions and 11 deletions

View File

@@ -1170,14 +1170,19 @@ public class HumanCostDecision extends CostDecisionMakerBase {
private boolean confirmAction(CostPart costPart, String message) { private boolean confirmAction(CostPart costPart, String message) {
CardView cardView = ability.getCardView(); CardView cardView = ability.getCardView();
if (GuiBase.getInterface().isLibgdxPort()) { if (GuiBase.getInterface().isLibgdxPort()) {
//for cards like Sword-Point Diplomacy and others that uses imprinted as container for their ability try {
if (cardView != null && cardView.getImprintedCards() != null && cardView.getImprintedCards().size() == 1) //for cards like Sword-Point Diplomacy and others that uses imprinted as container for their ability
cardView = CardView.getCardForUi(ImageUtil.getPaperCardFromImageKey(cardView.getImprintedCards().get(0).getCurrentState().getImageKey())); if (cardView != null && cardView.getImprintedCards() != null && cardView.getImprintedCards().size() == 1)
else if (ability.getTargets() != null && ability.getTargets().isTargetingAnyCard() && ability.getTargets().size() == 1) cardView = CardView.getCardForUi(ImageUtil.getPaperCardFromImageKey(cardView.getImprintedCards().get(0).getCurrentState().getImageKey()));
cardView = CardView.get(ability.getTargetCard()); else if (ability.getTargets() != null && ability.getTargets().isTargetingAnyCard() && ability.getTargets().size() == 1)
else if (cardView.getZone() == null || cardView.getZone().isHidden()) { cardView = CardView.get(ability.getTargetCard());
if (!cardView.hasAlternateState()) //don't override if it has alternatestate since it maybe showing alternate view else if (cardView.getZone() == null || cardView.getZone().isHidden()) {
cardView = CardView.getCardForUi(ImageUtil.getPaperCardFromImageKey(cardView.getCurrentState().getImageKey())); if (!cardView.hasAlternateState()) //don't override if it has alternatestate since it maybe showing alternate view
cardView = CardView.getCardForUi(ImageUtil.getPaperCardFromImageKey(cardView.getCurrentState().getImageKey()));
}
} catch (Exception e) {
//prevent NPE when overriding the cardView, the getPaperCardFromImageKey can return null making the GUI freeze, reset the view if error happens
cardView = ability.getCardView();
} }
return controller.getGui().confirm(cardView, message.replaceAll("\n", " ")); return controller.getGui().confirm(cardView, message.replaceAll("\n", " "));
} else { } else {

View File

@@ -1786,9 +1786,14 @@ public class PlayerControllerHuman extends PlayerController implements IGameCont
public boolean confirmPayment(final CostPart costPart, final String question, SpellAbility sa) { public boolean confirmPayment(final CostPart costPart, final String question, SpellAbility sa) {
if (GuiBase.getInterface().isLibgdxPort()) { if (GuiBase.getInterface().isLibgdxPort()) {
CardView cardView = sa.getView().getHostCard(); CardView cardView = sa.getView().getHostCard();
if (cardView.getZone() == null || cardView.getZone().isHidden()) try {
cardView = CardView.getCardForUi(ImageUtil.getPaperCardFromImageKey(cardView.getCurrentState().getImageKey())); if (cardView.getZone() == null || cardView.getZone().isHidden())
return this.getGui().confirm(sa.getView().getHostCard(), question.replaceAll("\n", " ")); cardView = CardView.getCardForUi(ImageUtil.getPaperCardFromImageKey(cardView.getCurrentState().getImageKey()));
} catch (Exception e) {
//prevent NPE
cardView = sa.getView().getHostCard();
}
return this.getGui().confirm(cardView, question.replaceAll("\n", " "));
} else { } else {
final InputConfirm inp = new InputConfirm(this, question, sa); final InputConfirm inp = new InputConfirm(this, question, sa);
inp.showAndWait(); inp.showAndWait();