Replacement effects use same reflection-based mechanism as triggers to create and copy instances

This commit is contained in:
Maxmtg
2013-06-04 08:18:09 +00:00
parent a6ed6d7600
commit 73223667ee
15 changed files with 130 additions and 159 deletions

View File

@@ -94,17 +94,6 @@ public class ReplaceDamage extends ReplacementEffect {
return true;
}
/* (non-Javadoc)
* @see forge.card.replacement.ReplacementEffect#getCopy()
*/
@Override
public ReplacementEffect getCopy() {
ReplacementEffect res = new ReplaceDamage(this.getMapParams(), this.getHostCard());
res.setOverridingAbility(this.getOverridingAbility());
res.setActiveZone(validHostZones);
res.setLayer(getLayer());
return res;
}
/* (non-Javadoc)
* @see forge.card.replacement.ReplacementEffect#setReplacingObjects(java.util.HashMap, forge.card.spellability.SpellAbility)

View File

@@ -70,18 +70,6 @@ public class ReplaceDiscard extends ReplacementEffect {
return true;
}
/* (non-Javadoc)
* @see forge.card.replacement.ReplacementEffect#getCopy()
*/
@Override
public ReplacementEffect getCopy() {
ReplacementEffect res = new ReplaceDiscard(this.getMapParams(), this.getHostCard());
res.setOverridingAbility(this.getOverridingAbility());
res.setActiveZone(validHostZones);
res.setLayer(getLayer());
return res;
}
/* (non-Javadoc)
* @see forge.card.replacement.ReplacementEffect#setReplacingObjects(java.util.HashMap, forge.card.spellability.SpellAbility)
*/

View File

@@ -65,17 +65,7 @@ public class ReplaceDraw extends ReplacementEffect {
return true;
}
/* (non-Javadoc)
* @see forge.card.replacement.ReplacementEffect#getCopy()
*/
@Override
public ReplacementEffect getCopy() {
ReplacementEffect res = new ReplaceDraw(this.getMapParams(), this.getHostCard());
res.setOverridingAbility(this.getOverridingAbility());
res.setActiveZone(validHostZones);
res.setLayer(getLayer());
return res;
}
/* (non-Javadoc)
* @see forge.card.replacement.ReplacementEffect#setReplacingObjects(java.util.HashMap, forge.card.spellability.SpellAbility)

View File

@@ -65,18 +65,6 @@ public class ReplaceGainLife extends ReplacementEffect {
return true;
}
/* (non-Javadoc)
* @see forge.card.replacement.ReplacementEffect#getCopy()
*/
@Override
public ReplacementEffect getCopy() {
ReplacementEffect res = new ReplaceGainLife(this.getMapParams(), this.getHostCard());
res.setOverridingAbility(this.getOverridingAbility());
res.setActiveZone(validHostZones);
res.setLayer(getLayer());
return res;
}
/* (non-Javadoc)
* @see forge.card.replacement.ReplacementEffect#setReplacingObjects(java.util.HashMap, forge.card.spellability.SpellAbility)
*/

View File

@@ -37,16 +37,4 @@ public class ReplaceGameLoss extends ReplacementEffect {
return true;
}
/* (non-Javadoc)
* @see forge.card.replacement.ReplacementEffect#getCopy()
*/
@Override
public ReplacementEffect getCopy() {
ReplacementEffect res = new ReplaceGameLoss(this.getMapParams(), this.getHostCard());
res.setOverridingAbility(this.getOverridingAbility());
res.setActiveZone(validHostZones);
res.setLayer(getLayer());
return res;
}
}

View File

@@ -66,18 +66,6 @@ public class ReplaceMoved extends ReplacementEffect {
return true;
}
/* (non-Javadoc)
* @see forge.card.replacement.ReplacementEffect#getCopy()
*/
@Override
public ReplacementEffect getCopy() {
ReplacementEffect res = new ReplaceMoved(this.getMapParams(), this.getHostCard());
res.setOverridingAbility(this.getOverridingAbility());
res.setActiveZone(validHostZones);
res.setLayer(getLayer());
return res;
}
/* (non-Javadoc)
* @see forge.card.replacement.ReplacementEffect#setReplacingObjects(java.util.HashMap, forge.card.spellability.SpellAbility)
*/

View File

@@ -54,16 +54,4 @@ public class ReplaceSetInMotion extends ReplacementEffect {
return true;
}
/* (non-Javadoc)
* @see forge.card.replacement.ReplacementEffect#getCopy()
*/
@Override
public ReplacementEffect getCopy() {
ReplacementEffect res = new ReplaceSetInMotion(this.getMapParams(), this.getHostCard());
res.setOverridingAbility(this.getOverridingAbility());
res.setActiveZone(validHostZones);
res.setLayer(getLayer());
return res;
}
}

View File

@@ -37,18 +37,6 @@ public class ReplaceTurnFaceUp extends ReplacementEffect {
return true;
}
/* (non-Javadoc)
* @see forge.card.replacement.ReplacementEffect#getCopy()
*/
@Override
public ReplacementEffect getCopy() {
ReplacementEffect res = new ReplaceTurnFaceUp(this.getMapParams(), this.getHostCard());
res.setOverridingAbility(this.getOverridingAbility());
res.setActiveZone(validHostZones);
res.setLayer(getLayer());
return res;
}
/* (non-Javadoc)
* @see forge.card.replacement.ReplacementEffect#setReplacingObjects(java.util.HashMap, forge.card.spellability.SpellAbility)
*/

View File

@@ -17,7 +17,6 @@
*/
package forge.card.replacement;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -51,6 +50,22 @@ public abstract class ReplacementEffect extends TriggerReplacementBase {
return this.hasRun;
}
/** The map params, denoting what to replace. */
protected final Map<String, String> mapParams;
/**
* Instantiates a new replacement effect.
*
* @param map
* the map
* @param host
* the host
*/
public ReplacementEffect(final Map<String, String> map, final Card host) {
mapParams = map;
this.setHostCard(host);
}
/**
* Checks if is secondary.
*
@@ -117,9 +132,6 @@ public abstract class ReplacementEffect extends TriggerReplacementBase {
this.hasRun = hasRun;
}
/** The map params, denoting what to replace. */
private Map<String, String> mapParams = new HashMap<String, String>();
/**
* <p>
* Getter for the field <code>mapParams</code>.
@@ -131,16 +143,6 @@ public abstract class ReplacementEffect extends TriggerReplacementBase {
return this.mapParams;
}
/**
* Sets the map params.
*
* @param mapParams
* the mapParams to set
*/
public final void setMapParams(final Map<String, String> mapParams) {
this.mapParams = mapParams;
}
/**
* Can replace.
*
@@ -150,20 +152,6 @@ public abstract class ReplacementEffect extends TriggerReplacementBase {
*/
public abstract boolean canReplace(final Map<String, Object> runParams);
/**
* To string.
*
* @return a String
*/
@Override
public String toString() {
if (this.getMapParams().containsKey("Description") && !this.isSuppressed()) {
return this.getMapParams().get("Description");
} else {
return "";
}
}
/**
* <p>
* requirementsCheck.
@@ -210,7 +198,15 @@ public abstract class ReplacementEffect extends TriggerReplacementBase {
*
* @return the copy
*/
public abstract ReplacementEffect getCopy();
public final ReplacementEffect getCopy() {
ReplacementType rt = ReplacementType.getTypeFor(this);
ReplacementEffect res = rt.createReplacement(mapParams, hostCard);
res.setOverridingAbility(this.getOverridingAbility());
res.setActiveZone(validHostZones);
res.setLayer(getLayer());
return res;
}
/**
* Sets the replacing objects.
@@ -224,19 +220,6 @@ public abstract class ReplacementEffect extends TriggerReplacementBase {
// Should be overridden by replacers that need it.
}
/**
* Instantiates a new replacement effect.
*
* @param map
* the map
* @param host
* the host
*/
public ReplacementEffect(final Map<String, String> map, final Card host) {
this.setMapParams(map);
this.setHostCard(host);
}
/**
* @return the layer
*/
@@ -250,4 +233,18 @@ public abstract class ReplacementEffect extends TriggerReplacementBase {
public void setLayer(ReplacementLayer layer0) {
this.layer = layer0;
}
/**
* To string.
*
* @return a String
*/
@Override
public String toString() {
if (this.getMapParams().containsKey("Description") && !this.isSuppressed()) {
return this.getMapParams().get("Description");
} else {
return "";
}
}
}

View File

@@ -254,6 +254,7 @@ public class ReplacementHandler {
* @return A finished instance
*/
public static ReplacementEffect parseReplacement(final String repParse, final Card host) {
final Map<String, String> mapParams = FileSection.parseToMap(repParse, "$", "|");
return ReplacementHandler.parseReplacement(mapParams, host);
}
@@ -269,27 +270,9 @@ public class ReplacementHandler {
* The card that hosts the replacement effect
* @return The finished instance
*/
public static ReplacementEffect parseReplacement(final Map<String, String> mapParams, final Card host) {
ReplacementEffect ret = null;
final String eventToReplace = mapParams.get("Event");
if (eventToReplace.equals("Draw")) {
ret = new ReplaceDraw(mapParams, host);
} else if (eventToReplace.equals("Discard")) {
ret = new ReplaceDiscard(mapParams, host);
} else if (eventToReplace.equals("GainLife")) {
ret = new ReplaceGainLife(mapParams, host);
} else if (eventToReplace.equals("DamageDone")) {
ret = new ReplaceDamage(mapParams, host);
} else if (eventToReplace.equals("GameLoss")) {
ret = new ReplaceGameLoss(mapParams, host);
} else if (eventToReplace.equals("Moved")) {
ret = new ReplaceMoved(mapParams, host);
} else if (eventToReplace.equals("SetInMotion")) {
ret = new ReplaceSetInMotion(mapParams, host);
} else if (eventToReplace.equals("TurnFaceUp")) {
ret = new ReplaceTurnFaceUp(mapParams, host);
}
private static ReplacementEffect parseReplacement(final Map<String, String> mapParams, final Card host) {
final ReplacementType rt = ReplacementType.smartValueOf(mapParams.get("Event"));
ReplacementEffect ret = rt.createReplacement(mapParams, host);
String activeZones = mapParams.get("ActiveZones");
if (null != activeZones) {

View File

@@ -0,0 +1,81 @@
package forge.card.replacement;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;
import forge.Card;
/**
* TODO: Write javadoc for this type.
*
*/
public enum ReplacementType {
DamageDone(ReplaceDamage.class),
Discard(ReplaceDiscard.class),
Draw(ReplaceDraw.class),
GainLife(ReplaceGainLife.class),
GameLoss(ReplaceGameLoss.class),
Moved(ReplaceMoved.class),
SetInMotion(ReplaceSetInMotion.class),
TurnFaceUp(ReplaceTurnFaceUp.class);
Class<? extends ReplacementEffect> clasz;
private ReplacementType(Class<? extends ReplacementEffect> cls) {
clasz = cls;
}
public static ReplacementType getTypeFor(ReplacementEffect e) {
final Class<? extends ReplacementEffect> cls = e.getClass();
for (final ReplacementType v : ReplacementType.values()) {
if (v.clasz.equals(cls)) {
return v;
}
}
return null;
}
public static ReplacementType smartValueOf(String value) {
final String valToCompate = value.trim();
for (final ReplacementType v : ReplacementType.values()) {
if (v.name().compareToIgnoreCase(valToCompate) == 0) {
return v;
}
}
throw new RuntimeException("Element " + value + " not found in TriggerType enum");
}
/**
* TODO: Write javadoc for this method.
* @param mapParams
* @param host
* @param intrinsic
* @return
*/
public ReplacementEffect createReplacement(Map<String, String> mapParams, Card host) {
@SuppressWarnings("unchecked")
Constructor<? extends ReplacementEffect>[] cc = (Constructor<? extends ReplacementEffect>[]) clasz.getDeclaredConstructors();
for (Constructor<? extends ReplacementEffect> c : cc) {
Class<?>[] pp = c.getParameterTypes();
if (pp[0].isAssignableFrom(Map.class)) {
try {
ReplacementEffect res = c.newInstance(mapParams, host);
return res;
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block ignores the exception, but sends it to System.err and probably forge.log.
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block ignores the exception, but sends it to System.err and probably forge.log.
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block ignores the exception, but sends it to System.err and probably forge.log.
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block ignores the exception, but sends it to System.err and probably forge.log.
e.printStackTrace();
}
}
}
throw new RuntimeException("No constructor found that would take Map as 1st parameter in class " + clasz.getName());
}
}

View File

@@ -328,8 +328,7 @@ public enum FControl {
}
public boolean mayShowCard(Card c) {
if ( game == null ) return true;
return !gameHasHumanPlayer || c.canBeShownTo(getCurrentPlayer());
return game == null || !gameHasHumanPlayer || c.canBeShownTo(getCurrentPlayer());
}
/**

View File

@@ -1271,6 +1271,7 @@ public class GameAction {
// Play the Destroy sound
game.fireEvent(new GameEventCardDestroyed());
// Run triggers
final HashMap<String, Object> runParams = new HashMap<String, Object>();
runParams.put("Card", c);

View File

@@ -6,4 +6,6 @@ package forge.gui.framework;
*/
public class InvalidLayoutFileException extends RuntimeException {
private static final long serialVersionUID = 8201703516717298690L;
}