Player belongs to game, it cannot have references to any concrete PlayerController

This commit is contained in:
Maxmtg
2013-12-04 07:46:30 +00:00
parent d1ef107b48
commit 2ffa1510ed
2 changed files with 15 additions and 16 deletions

View File

@@ -2943,26 +2943,19 @@ public class Player extends GameEntity implements Comparable<Player> {
}
/**
* Run a procedure using an AI controller
* Run a procedure using a different controller
* @param proc
* @param tempController
*/
public void runAsAi(Runnable proc) {
if (controller instanceof PlayerControllerAi) {
proc.run(); //can just run with current controller if it's an AI controller
return;
}
public void runWithController(Runnable proc, PlayerController tempController) {
PlayerController oldController = controller;
controller = new PlayerControllerAi(this.game, this, this.getOriginalLobbyPlayer());
controller = tempController;
try {
proc.run();
} finally {
controller = oldController;
}
catch (Exception ex) {
//ensure controller restored before throwing exception in case user chooses to continue
controller = oldController;
throw ex;
}
}
/**

View File

@@ -19,6 +19,7 @@ import forge.game.card.CardUtil;
import forge.game.mana.ManaCostBeingPaid;
import forge.game.player.HumanPlay;
import forge.game.player.Player;
import forge.game.player.PlayerControllerAi;
import forge.game.replacement.ReplacementEffect;
import forge.game.spellability.AbilityManaPart;
import forge.game.spellability.SpellAbility;
@@ -49,6 +50,11 @@ public abstract class InputPayMana extends InputSyncronizedBase {
this.saPaidFor = saToPayFor;
}
public void runAsAi(Runnable proc) {
this.player.runWithController(proc, new PlayerControllerAi(this.game, this.player, this.player.getOriginalLobbyPlayer()));
}
@Override
protected void onCardSelected(final Card card, final MouseEvent triggerEvent) {
if (card.getManaAbility().isEmpty()) {
@@ -289,7 +295,7 @@ public abstract class InputPayMana extends InputSyncronizedBase {
ComputerUtilMana.payManaCost(manaCost, saPaidFor, player);
}
};
this.player.runAsAi(proc);
runAsAi(proc);
this.showMessage();
}
}
@@ -311,7 +317,7 @@ public abstract class InputPayMana extends InputSyncronizedBase {
return ComputerUtilMana.canPayManaCost(manaCost, saPaidFor, player);
}
};
this.player.runAsAi(proc);
runAsAi(proc);
canPayManaCost = proc.getResult();
}
if (canPayManaCost) {