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) {
CardView cardView = ability.getCardView();
if (GuiBase.getInterface().isLibgdxPort()) {
//for cards like Sword-Point Diplomacy and others that uses imprinted as container for their ability
if (cardView != null && cardView.getImprintedCards() != null && cardView.getImprintedCards().size() == 1)
cardView = CardView.getCardForUi(ImageUtil.getPaperCardFromImageKey(cardView.getImprintedCards().get(0).getCurrentState().getImageKey()));
else if (ability.getTargets() != null && ability.getTargets().isTargetingAnyCard() && ability.getTargets().size() == 1)
cardView = CardView.get(ability.getTargetCard());
else if (cardView.getZone() == null || cardView.getZone().isHidden()) {
if (!cardView.hasAlternateState()) //don't override if it has alternatestate since it maybe showing alternate view
cardView = CardView.getCardForUi(ImageUtil.getPaperCardFromImageKey(cardView.getCurrentState().getImageKey()));
try {
//for cards like Sword-Point Diplomacy and others that uses imprinted as container for their ability
if (cardView != null && cardView.getImprintedCards() != null && cardView.getImprintedCards().size() == 1)
cardView = CardView.getCardForUi(ImageUtil.getPaperCardFromImageKey(cardView.getImprintedCards().get(0).getCurrentState().getImageKey()));
else if (ability.getTargets() != null && ability.getTargets().isTargetingAnyCard() && ability.getTargets().size() == 1)
cardView = CardView.get(ability.getTargetCard());
else if (cardView.getZone() == null || cardView.getZone().isHidden()) {
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", " "));
} 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) {
if (GuiBase.getInterface().isLibgdxPort()) {
CardView cardView = sa.getView().getHostCard();
if (cardView.getZone() == null || cardView.getZone().isHidden())
cardView = CardView.getCardForUi(ImageUtil.getPaperCardFromImageKey(cardView.getCurrentState().getImageKey()));
return this.getGui().confirm(sa.getView().getHostCard(), question.replaceAll("\n", " "));
try {
if (cardView.getZone() == null || cardView.getZone().isHidden())
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 {
final InputConfirm inp = new InputConfirm(this, question, sa);
inp.showAndWait();