fixes comodification exceptions (like http://www.slightlymagic.net/forum/viewtopic.php?p=115346#p115346 and viewtopic.php?p=114582#p114582)

This commit is contained in:
Maxmtg
2013-04-13 05:20:54 +00:00
parent b9ace52e8b
commit 145a0ce441
3 changed files with 48 additions and 142 deletions

View File

@@ -29,6 +29,8 @@ import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.Set; import java.util.Set;
import java.util.TreeMap; import java.util.TreeMap;
import java.util.concurrent.ConcurrentSkipListMap;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import com.esotericsoftware.minlog.Log; import com.esotericsoftware.minlog.Log;
@@ -110,9 +112,9 @@ public class Card extends GameEntity implements Comparable<Card> {
private GameEntity enchanting = null; private GameEntity enchanting = null;
private ArrayList<String> optionalAdditionalCostsPaid = null; private ArrayList<String> optionalAdditionalCostsPaid = null;
// changes by AF animate and continuous static effects // changes by AF animate and continuous static effects - timestamp is the key of maps
private ArrayList<CardType> changedCardTypes = new ArrayList<CardType>(); private Map<Long, CardType> changedCardTypes = new ConcurrentSkipListMap<Long, CardType>();
private List<CardKeywords> changedCardKeywords = new ArrayList<CardKeywords>(); private Map<Long, CardKeywords> changedCardKeywords = new ConcurrentSkipListMap<Long, CardKeywords>();
private final ArrayList<Object> rememberedObjects = new ArrayList<Object>(); private final ArrayList<Object> rememberedObjects = new ArrayList<Object>();
private final ArrayList<Card> imprintedCards = new ArrayList<Card>(); private final ArrayList<Card> imprintedCards = new ArrayList<Card>();
@@ -1583,16 +1585,9 @@ public class Card extends GameEntity implements Comparable<Card> {
if (this.isImmutable()) { if (this.isImmutable()) {
return new CardColor(this); return new CardColor(this);
} }
CardColor colors = null;
final ArrayList<CardColor> globalChanges;
if (Singletons.getModel().getGame() == null) {
globalChanges = new ArrayList<CardColor>();
}
else {
globalChanges = Singletons.getModel().getGame().getColorChanger().getColorChanges();
}
colors = this.determineColor(globalChanges); final ArrayList<CardColor> globalChanges = getOwner() == null ? new ArrayList<CardColor>() : getOwner().getGame().getColorChanger().getColorChanges();
CardColor colors = this.determineColor(globalChanges);
colors.fixColorless(); colors.fixColorless();
return colors; return colors;
} }
@@ -3469,46 +3464,38 @@ public class Card extends GameEntity implements Comparable<Card> {
public final ArrayList<String> getType() { public final ArrayList<String> getType() {
// see if type changes are in effect // see if type changes are in effect
if (!this.changedCardTypes.isEmpty()) { final ArrayList<String> newType = new ArrayList<String>(this.getCharacteristics().getType());
for (final CardType ct : this.changedCardTypes.values()) {
final ArrayList<String> newType = new ArrayList<String>(this.getCharacteristics().getType()); final ArrayList<String> removeTypes = new ArrayList<String>();
final ArrayList<CardType> types = this.changedCardTypes; if (ct.getRemoveType() != null) {
Collections.sort(types); // sorts types by timeStamp removeTypes.addAll(ct.getRemoveType());
}
for (final CardType ct : types) { // remove old types
final ArrayList<String> removeTypes = new ArrayList<String>(); for (int i = 0; i < newType.size(); i++) {
if (ct.getRemoveType() != null) { final String t = newType.get(i);
removeTypes.addAll(ct.getRemoveType()); if (ct.isRemoveSuperTypes() && forge.card.CardType.isASuperType(t)) {
removeTypes.add(t);
} }
// remove old types if (ct.isRemoveCardTypes() && forge.card.CardType.isACardType(t)) {
for (int i = 0; i < newType.size(); i++) { removeTypes.add(t);
final String t = newType.get(i);
if (ct.isRemoveSuperTypes() && forge.card.CardType.isASuperType(t)) {
removeTypes.add(t);
}
if (ct.isRemoveCardTypes() && forge.card.CardType.isACardType(t)) {
removeTypes.add(t);
}
if (ct.isRemoveSubTypes() && forge.card.CardType.isASubType(t)) {
removeTypes.add(t);
}
if (ct.isRemoveCreatureTypes() && (forge.card.CardType.isACreatureType(t) || t.equals("AllCreatureTypes"))) {
removeTypes.add(t);
}
} }
newType.removeAll(removeTypes); if (ct.isRemoveSubTypes() && forge.card.CardType.isASubType(t)) {
// add new types removeTypes.add(t);
if (ct.getType() != null) {
newType.addAll(ct.getType());
} }
if (ct.isRemoveCreatureTypes() && (forge.card.CardType.isACreatureType(t) || t.equals("AllCreatureTypes"))) {
removeTypes.add(t);
}
}
newType.removeAll(removeTypes);
// add new types
if (ct.getType() != null) {
newType.addAll(ct.getType());
} }
return newType;
} }
// nothing changed return newType;
return new ArrayList<String>(this.getCharacteristics().getType());
} }
/** /**
@@ -3534,8 +3521,7 @@ public class Card extends GameEntity implements Comparable<Card> {
final boolean removeSuperTypes, final boolean removeCardTypes, final boolean removeSubTypes, final boolean removeSuperTypes, final boolean removeCardTypes, final boolean removeSubTypes,
final boolean removeCreatureTypes, final long timestamp) { final boolean removeCreatureTypes, final long timestamp) {
this.changedCardTypes.add(new CardType(types, removeTypes, removeSuperTypes, removeCardTypes, removeSubTypes, this.changedCardTypes.put(timestamp, new CardType(types, removeTypes, removeSuperTypes, removeCardTypes, removeSubTypes, removeCreatureTypes));
removeCreatureTypes, timestamp));
} }
/** /**
@@ -3582,12 +3568,7 @@ public class Card extends GameEntity implements Comparable<Card> {
* long * long
*/ */
public final void removeChangedCardTypes(final long timestamp) { public final void removeChangedCardTypes(final long timestamp) {
for (int i = 0; i < this.changedCardTypes.size(); i++) { this.changedCardTypes.remove(Long.valueOf(timestamp));
final CardType cardT = this.changedCardTypes.get(i);
if (cardT.getTimestamp() == timestamp) {
this.changedCardTypes.remove(cardT);
}
}
} }
// values that are printed on card // values that are printed on card
@@ -4232,7 +4213,7 @@ public class Card extends GameEntity implements Comparable<Card> {
public final void addChangedCardKeywords(final ArrayList<String> keywords, final ArrayList<String> removeKeywords, public final void addChangedCardKeywords(final ArrayList<String> keywords, final ArrayList<String> removeKeywords,
final boolean removeAllKeywords, final long timestamp) { final boolean removeAllKeywords, final long timestamp) {
this.changedCardKeywords.add(new CardKeywords(keywords, removeKeywords, removeAllKeywords, timestamp)); this.changedCardKeywords.put(timestamp, new CardKeywords(keywords, removeKeywords, removeAllKeywords));
} }
/** /**
@@ -4269,12 +4250,7 @@ public class Card extends GameEntity implements Comparable<Card> {
* the timestamp * the timestamp
*/ */
public final void removeChangedCardKeywords(final long timestamp) { public final void removeChangedCardKeywords(final long timestamp) {
for (int i = 0; i < this.changedCardKeywords.size(); i++) { changedCardKeywords.remove(Long.valueOf(timestamp));
final CardKeywords cardK = this.changedCardKeywords.get(i);
if (cardK.getTimestamp() == timestamp) {
this.changedCardKeywords.remove(cardK);
}
}
} }
// Hidden keywords will be left out // Hidden keywords will be left out
@@ -4291,22 +4267,16 @@ public class Card extends GameEntity implements Comparable<Card> {
keywords.addAll(this.getExtrinsicKeyword()); keywords.addAll(this.getExtrinsicKeyword());
// see if keyword changes are in effect // see if keyword changes are in effect
if (!this.changedCardKeywords.isEmpty()) { for (final CardKeywords ck : this.changedCardKeywords.values()) {
final ArrayList<CardKeywords> newKeywords = new ArrayList<CardKeywords>(this.changedCardKeywords); if (ck.isRemoveAllKeywords()) {
Collections.sort(newKeywords); // sorts newKeywords by timeStamp keywords.clear();
} else if (ck.getRemoveKeywords() != null) {
keywords.removeAll(ck.getRemoveKeywords());
}
for (final CardKeywords ck : newKeywords) { if (ck.getKeywords() != null) {
keywords.addAll(ck.getKeywords());
if (ck.isRemoveAllKeywords()) {
keywords.clear();
} else if (ck.getRemoveKeywords() != null) {
keywords.removeAll(ck.getRemoveKeywords());
}
if (ck.getKeywords() != null) {
keywords.addAll(ck.getKeywords());
}
} }
} }

View File

@@ -27,23 +27,11 @@ import java.util.ArrayList;
* @author Forge * @author Forge
* @version $Id$ * @version $Id$
*/ */
public class CardKeywords implements Comparable<CardKeywords> { public class CardKeywords {
// takes care of individual card types // takes care of individual card types
private ArrayList<String> keywords = new ArrayList<String>(); private ArrayList<String> keywords = new ArrayList<String>();
private ArrayList<String> removeKeywords = new ArrayList<String>(); private ArrayList<String> removeKeywords = new ArrayList<String>();
private boolean removeAllKeywords = false; private boolean removeAllKeywords = false;
private long timeStamp = 0;
/**
* <p>
* getTimestamp.
* </p>
*
* @return a long.
*/
public final long getTimestamp() {
return this.timeStamp;
}
/** /**
* *
@@ -58,12 +46,10 @@ public class CardKeywords implements Comparable<CardKeywords> {
* @param stamp * @param stamp
* a long * a long
*/ */
CardKeywords(final ArrayList<String> keywordList, final ArrayList<String> removeKeywordList, CardKeywords(final ArrayList<String> keywordList, final ArrayList<String> removeKeywordList, final boolean removeAll) {
final boolean removeAll, final long stamp) {
this.keywords = keywordList; this.keywords = keywordList;
this.removeKeywords = removeKeywordList; this.removeKeywords = removeKeywordList;
this.removeAllKeywords = removeAll; this.removeAllKeywords = removeAll;
this.timeStamp = stamp;
} }
/** /**
@@ -95,22 +81,4 @@ public class CardKeywords implements Comparable<CardKeywords> {
public final boolean isRemoveAllKeywords() { public final boolean isRemoveAllKeywords() {
return this.removeAllKeywords; return this.removeAllKeywords;
} }
/*
* (non-Javadoc)
*
* @see java.lang.Comparable#compareTo(java.lang.Object)
*/
@Override
public final int compareTo(final CardKeywords anotherCardKeywords) {
int returnValue = 0;
final long anotherTimeStamp = anotherCardKeywords.getTimestamp();
if (this.timeStamp < anotherTimeStamp) {
returnValue = -1;
} else if (this.timeStamp > anotherTimeStamp) {
returnValue = 1;
}
return returnValue;
}
} }

View File

@@ -27,7 +27,7 @@ import java.util.ArrayList;
* @author Forge * @author Forge
* @version $Id$ * @version $Id$
*/ */
public class CardType implements Comparable<CardType> { public class CardType {
// takes care of individual card types // takes care of individual card types
private ArrayList<String> type = new ArrayList<String>(); private ArrayList<String> type = new ArrayList<String>();
private ArrayList<String> removeType = new ArrayList<String>(); private ArrayList<String> removeType = new ArrayList<String>();
@@ -35,18 +35,6 @@ public class CardType implements Comparable<CardType> {
private final boolean removeCardTypes; private final boolean removeCardTypes;
private final boolean removeSubTypes; private final boolean removeSubTypes;
private final boolean removeCreatureTypes; private final boolean removeCreatureTypes;
private long timeStamp = 0;
/**
* <p>
* getTimestamp.
* </p>
*
* @return a long.
*/
public final long getTimestamp() {
return this.timeStamp;
}
/** /**
* Instantiates a new card_ type. * Instantiates a new card_ type.
@@ -67,15 +55,13 @@ public class CardType implements Comparable<CardType> {
* a long * a long
*/ */
CardType(final ArrayList<String> types, final ArrayList<String> removeTypes, final boolean removeSuperType, CardType(final ArrayList<String> types, final ArrayList<String> removeTypes, final boolean removeSuperType,
final boolean removeCardType, final boolean removeSubType, final boolean removeCreatureType, final boolean removeCardType, final boolean removeSubType, final boolean removeCreatureType) {
final long stamp) {
this.type = types; this.type = types;
this.removeType = removeTypes; this.removeType = removeTypes;
this.removeSuperTypes = removeSuperType; this.removeSuperTypes = removeSuperType;
this.removeCardTypes = removeCardType; this.removeCardTypes = removeCardType;
this.removeSubTypes = removeSubType; this.removeSubTypes = removeSubType;
this.removeCreatureTypes = removeCreatureType; this.removeCreatureTypes = removeCreatureType;
this.timeStamp = stamp;
} }
/** /**
@@ -137,22 +123,4 @@ public class CardType implements Comparable<CardType> {
public final boolean isRemoveCreatureTypes() { public final boolean isRemoveCreatureTypes() {
return this.removeCreatureTypes; return this.removeCreatureTypes;
} }
/*
* (non-Javadoc)
*
* @see java.lang.Comparable#compareTo(java.lang.Object)
*/
@Override
public final int compareTo(final CardType anotherCardType) {
int returnValue = 0;
final long anotherTimeStamp = anotherCardType.getTimestamp();
if (this.timeStamp < anotherTimeStamp) {
returnValue = -1;
} else if (this.timeStamp > anotherTimeStamp) {
returnValue = 1;
}
return returnValue;
}
} }