Fix commander casting which I broke in r32823.

Revert to the original implementation of adding the ability to the Game cache in clone(), but now update this in setHostCard() if the new card comes from a different Game object.
This commit is contained in:
Myrd
2016-12-27 17:24:59 +00:00
parent 2344d02085
commit d64774dba0
4 changed files with 20 additions and 8 deletions

View File

@@ -193,8 +193,11 @@ public class Game {
public SpellAbility getSpellAbility(final SpellAbilityView view) { public SpellAbility getSpellAbility(final SpellAbilityView view) {
return spabCache.get(view); return spabCache.get(view);
} }
public void addSpellAbility(int id, SpellAbility spellAbility) { public void addSpellAbility(SpellAbility spellAbility) {
spabCache.put(Integer.valueOf(id), spellAbility); spabCache.put(spellAbility.getId(), spellAbility);
}
public void removeSpellAbility(SpellAbility spellAbility) {
spabCache.remove(spellAbility.getId());
} }
public Game(List<RegisteredPlayer> players0, GameRules rules0, Match match0) { /* no more zones to map here */ public Game(List<RegisteredPlayer> players0, GameRules rules0, Match match0) { /* no more zones to map here */

View File

@@ -13,6 +13,10 @@ public class GameEntityCache<Entity extends IIdentifiable, View extends Trackabl
entityCache.put(id, entity); entityCache.put(id, entity);
} }
public void remove(Integer id) {
entityCache.remove(id);
}
public Entity get(View entityView) { public Entity get(View entityView) {
if (entityView == null) { return null; } if (entityView == null) { return null; }
return entityCache.get(entityView.getId()); return entityCache.get(entityView.getId());

View File

@@ -2068,9 +2068,6 @@ public class Card extends GameEntity implements Comparable<Card> {
public final void addSpellAbility(final SpellAbility a) { public final void addSpellAbility(final SpellAbility a) {
a.setHostCard(this); a.setHostCard(this);
if (game != null) {
game.addSpellAbility(a.getId(), a);
}
currentState.addSpellAbility(a); currentState.addSpellAbility(a);
currentState.getView().updateAbilityText(this, currentState); currentState.getView().updateAbilityText(this, currentState);
} }

View File

@@ -183,12 +183,12 @@ public abstract class SpellAbility extends CardTraitBase implements ISpellAbilit
} }
view = view0; view = view0;
if (hostCard != null && hostCard.getGame() != null) { if (hostCard != null && hostCard.getGame() != null) {
hostCard.getGame().addSpellAbility(id, this); hostCard.getGame().addSpellAbility(this);
} }
} }
@Override @Override
public int getId() { public final int getId() {
return id; return id;
} }
@Override @Override
@@ -204,6 +204,12 @@ public abstract class SpellAbility extends CardTraitBase implements ISpellAbilit
public void setHostCard(final Card c) { public void setHostCard(final Card c) {
if (hostCard == c) { return; } if (hostCard == c) { return; }
super.setHostCard(c); super.setHostCard(c);
Game oldGame = hostCard != null ? hostCard.getGame() : null;
Game newGame = c != null ? c.getGame() : null;
if (oldGame != newGame) {
if (oldGame != null) { oldGame.removeSpellAbility(this); }
if (newGame != null) { newGame.addSpellAbility(this); }
}
if (subAbility != null) { if (subAbility != null) {
subAbility.setHostCard(c); subAbility.setHostCard(c);
@@ -760,7 +766,9 @@ public abstract class SpellAbility extends CardTraitBase implements ISpellAbilit
clone = (SpellAbility) clone(); clone = (SpellAbility) clone();
clone.id = nextId(); clone.id = nextId();
clone.view = new SpellAbilityView(clone); clone.view = new SpellAbilityView(clone);
if (hostCard != null && hostCard.getGame() != null) {
hostCard.getGame().addSpellAbility(clone);
}
// need to clone the maps too so they can be changed // need to clone the maps too so they can be changed
clone.originalMapParams = Maps.newHashMap(this.originalMapParams); clone.originalMapParams = Maps.newHashMap(this.originalMapParams);
clone.mapParams = Maps.newHashMap(this.mapParams); clone.mapParams = Maps.newHashMap(this.mapParams);