mirror of
https://github.com/Card-Forge/forge.git
synced 2025-11-18 03:38:01 +00:00
Prevent AI taking over if exception thrown while AI controller temporarily set
This commit is contained in:
@@ -2943,15 +2943,26 @@ public class Player extends GameEntity implements Comparable<Player> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Run a procedure using a different controller
|
* Run a procedure using an AI controller
|
||||||
* @param proc
|
* @param proc
|
||||||
* @param tempController
|
|
||||||
*/
|
*/
|
||||||
public void runWithController(Runnable proc, PlayerController tempController) {
|
public void runAsAi(Runnable proc) {
|
||||||
|
if (PlayerControllerAi.class.isInstance(controller)) {
|
||||||
|
proc.run(); //can just run with current controller if it's an AI controller
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
PlayerController oldController = controller;
|
PlayerController oldController = controller;
|
||||||
controller = tempController;
|
controller = new PlayerControllerAi(this.game, this, this.getOriginalLobbyPlayer());
|
||||||
proc.run();
|
try {
|
||||||
controller = oldController;
|
proc.run();
|
||||||
|
controller = oldController;
|
||||||
|
}
|
||||||
|
catch (Exception ex) {
|
||||||
|
//ensure controller restored before throwing exception in case user chooses to continue
|
||||||
|
controller = oldController;
|
||||||
|
throw ex;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -19,7 +19,6 @@ import forge.game.card.CardUtil;
|
|||||||
import forge.game.mana.ManaCostBeingPaid;
|
import forge.game.mana.ManaCostBeingPaid;
|
||||||
import forge.game.player.HumanPlay;
|
import forge.game.player.HumanPlay;
|
||||||
import forge.game.player.Player;
|
import forge.game.player.Player;
|
||||||
import forge.game.player.PlayerControllerAi;
|
|
||||||
import forge.game.replacement.ReplacementEffect;
|
import forge.game.replacement.ReplacementEffect;
|
||||||
import forge.game.spellability.AbilityManaPart;
|
import forge.game.spellability.AbilityManaPart;
|
||||||
import forge.game.spellability.SpellAbility;
|
import forge.game.spellability.SpellAbility;
|
||||||
@@ -50,11 +49,6 @@ public abstract class InputPayMana extends InputSyncronizedBase {
|
|||||||
this.saPaidFor = saToPayFor;
|
this.saPaidFor = saToPayFor;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void runAsAi(Runnable proc) {
|
|
||||||
this.player.runWithController(proc, new PlayerControllerAi(this.game, this.player, this.player.getOriginalLobbyPlayer()));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCardSelected(final Card card, final MouseEvent triggerEvent) {
|
protected void onCardSelected(final Card card, final MouseEvent triggerEvent) {
|
||||||
if (card.getManaAbility().isEmpty()) {
|
if (card.getManaAbility().isEmpty()) {
|
||||||
@@ -295,7 +289,7 @@ public abstract class InputPayMana extends InputSyncronizedBase {
|
|||||||
ComputerUtilMana.payManaCost(manaCost, saPaidFor, player);
|
ComputerUtilMana.payManaCost(manaCost, saPaidFor, player);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
runAsAi(proc);
|
this.player.runAsAi(proc);
|
||||||
this.showMessage();
|
this.showMessage();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -317,7 +311,7 @@ public abstract class InputPayMana extends InputSyncronizedBase {
|
|||||||
return ComputerUtilMana.canPayManaCost(manaCost, saPaidFor, player);
|
return ComputerUtilMana.canPayManaCost(manaCost, saPaidFor, player);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
runAsAi(proc);
|
this.player.runAsAi(proc);
|
||||||
canPayManaCost = proc.getResult();
|
canPayManaCost = proc.getResult();
|
||||||
}
|
}
|
||||||
if (canPayManaCost) {
|
if (canPayManaCost) {
|
||||||
|
|||||||
Reference in New Issue
Block a user