Refactored copying permanents

This commit is contained in:
magenoxx 2012-05-25 10:01:35 +04:00
parent ba375573ff
commit 0335d4d77e
6 changed files with 25 additions and 18 deletions

View file

@ -92,16 +92,17 @@ class PhantasmalImageCopyEffect extends OneShotEffect<PhantasmalImageCopyEffect>
@Override
public boolean apply(Game game, Ability source) {
Player player = game.getPlayer(source.getControllerId());
if ( player != null ) {
Permanent sourcePermanent = game.getPermanent(source.getSourceId());
if (player != null && sourcePermanent != null) {
Target target = new TargetPermanent(new FilterCreaturePermanent());
if (target.canChoose(source.getControllerId(), game)) {
target.setRequired(true);
target.setNotTarget(true);
player.choose(Outcome.Copy, target, source.getSourceId(), game);
UUID targetId = target.getFirstTarget();
Permanent permanent = game.getPermanent(targetId);
if (permanent != null) {
game.copyPermanent(permanent, source, new ApplyToPermanent() {
Permanent copyFromPermanent = game.getPermanent(targetId);
if (copyFromPermanent != null) {
game.copyPermanent(copyFromPermanent, sourcePermanent, source, new ApplyToPermanent() {
@Override
public Boolean apply(Game game, Permanent permanent) {
permanent.getSubtype().add("Illusion");

View file

@ -97,13 +97,14 @@ class PhyrexianMetamorphEffect extends OneShotEffect<PhyrexianMetamorphEffect> {
@Override
public boolean apply(Game game, Ability source) {
Player player = game.getPlayer(source.getControllerId());
if (player != null) {
Permanent sourcePermanent = game.getPermanent(source.getSourceId());
if (player != null && sourcePermanent != null) {
Target target = new TargetPermanent(filter);
if (target.canChoose(source.getControllerId(), game)) {
player.choose(Outcome.Copy, target, source.getSourceId(), game);
Permanent perm = game.getPermanent(target.getFirstTarget());
if (perm != null) {
game.copyPermanent(perm, source, new ApplyToPermanent() {
Permanent copyFromPermanent = game.getPermanent(target.getFirstTarget());
if (copyFromPermanent != null) {
game.copyPermanent(copyFromPermanent, sourcePermanent, source, new ApplyToPermanent() {
@Override
public Boolean apply(Game game, Permanent permanent) {
if (!permanent.getCardType().contains(CardType.ARTIFACT)) {

View file

@ -47,6 +47,9 @@ import java.util.UUID;
*/
public class CopyEffect extends ContinuousEffectImpl<CopyEffect> {
/**
* Object we copy from
*/
private MageObject target;
private UUID sourceId;

View file

@ -66,13 +66,14 @@ public class CopyPermanentEffect extends OneShotEffect<CopyPermanentEffect> {
@Override
public boolean apply(Game game, Ability source) {
Player player = game.getPlayer(source.getControllerId());
if (player != null) {
Permanent sourcePermanent = game.getPermanent(source.getSourceId());
if (player != null && sourcePermanent != null) {
Target target = new TargetPermanent(filter);
if (target.canChoose(source.getControllerId(), game)) {
player.choose(Outcome.Copy, target, source.getSourceId(), game);
Permanent perm = game.getPermanent(target.getFirstTarget());
if (perm != null) {
game.copyPermanent(perm, source, new ApplyToPermanent() {
Permanent copyFromPermanent = game.getPermanent(target.getFirstTarget());
if (copyFromPermanent != null) {
game.copyPermanent(copyFromPermanent, sourcePermanent, source, new ApplyToPermanent() {
@Override
public Boolean apply(Game game, Permanent permanent) {
if (!permanent.getCardType().contains(Constants.CardType.ARTIFACT)) {

View file

@ -164,11 +164,12 @@ public interface Game extends MageItem, Serializable {
/**
* This version supports copying of copies of any depth.
*
* @param targetPermanent
* @param copyFromPermanent
* @param copyToPermanent
* @param source
* @param applier
*/
public void copyPermanent(Permanent targetPermanent, Ability source, ApplyToPermanent applier);
public void copyPermanent(Permanent copyFromPermanent, Permanent copyToPermanent, Ability source, ApplyToPermanent applier);
public void addTriggeredAbility(TriggeredAbility ability);
public void addDelayedTriggeredAbility(DelayedTriggeredAbility delayedAbility);

View file

@ -736,8 +736,8 @@ public abstract class GameImpl<T extends GameImpl<T>> implements Game, Serializa
}
@Override
public void copyPermanent(Permanent targetPermanent, Ability source, ApplyToPermanent applier) {
Permanent permanent = targetPermanent.copy();
public void copyPermanent(Permanent copyFromPermanent, Permanent copyToPermanent, Ability source, ApplyToPermanent applier) {
Permanent permanent = copyFromPermanent.copy();
//getState().addCard(permanent);
permanent.reset(this);
permanent.assignNewId();
@ -745,7 +745,7 @@ public abstract class GameImpl<T extends GameImpl<T>> implements Game, Serializa
Ability newAbility = source.copy();
CopyEffect newEffect = new CopyEffect(permanent, source.getSourceId());
CopyEffect newEffect = new CopyEffect(permanent, copyToPermanent.getId());
newEffect.newId();
newEffect.setTimestamp();
newEffect.init(newAbility, this);
@ -755,7 +755,7 @@ public abstract class GameImpl<T extends GameImpl<T>> implements Game, Serializa
if (effect instanceof CopyEffect) {
CopyEffect copyEffect = (CopyEffect) effect;
// there is another copy effect that our targetPermanent copies stats from
if (copyEffect.getSourceId().equals(targetPermanent.getId())) {
if (copyEffect.getSourceId().equals(copyFromPermanent.getId())) {
MageObject object = ((CopyEffect) effect).getTarget();
if (object instanceof Permanent) {
// so we will use original card instead of target