Merge pull request #1873 from drmDev/master

Various NPE fixes based on server log
This commit is contained in:
LevelX2 2016-04-14 20:00:40 +02:00
commit 991fee9da8
3 changed files with 74 additions and 45 deletions

View file

@ -40,6 +40,7 @@ import mage.abilities.mana.ManaAbility;
import mage.constants.Zone;
import mage.game.Game;
import mage.util.ThreadLocalStringBuilder;
import org.apache.log4j.Logger;
/**
*
@ -47,6 +48,8 @@ import mage.util.ThreadLocalStringBuilder;
* @param <T>
*/
public class AbilitiesImpl<T extends Ability> extends ArrayList<T> implements Abilities<T> {
private static final Logger logger = Logger.getLogger(AbilitiesImpl.class);
private static final ThreadLocalStringBuilder threadLocalBuilder = new ThreadLocalStringBuilder(200);
@ -102,8 +105,13 @@ public class AbilitiesImpl<T extends Ability> extends ArrayList<T> implements Ab
rules.add(sbRule.toString());
}
String rule = ability.getRule();
if (rule.length() > 0) {
rules.add(Character.toUpperCase(rule.charAt(0)) + rule.substring(1));
if (rule != null) {
if (rule.length() > 0) {
rules.add(Character.toUpperCase(rule.charAt(0)) + rule.substring(1));
}
} else { // logging so we can still can be made aware of rule problems a card has
String cardName = ((SpellAbility) ability).getCardName();
logger.fatal("Error in rule text generation of " + cardName + ": Create a bug report or fix the source code");
}
}
}
@ -332,4 +340,4 @@ public class AbilitiesImpl<T extends Ability> extends ArrayList<T> implements Ab
}
return sb.toString();
}
}
}

View file

@ -62,37 +62,40 @@ public class DiesAttachedTriggeredAbility extends TriggeredAbilityImpl {
if (((ZoneChangeEvent) event).isDiesEvent()) {
ZoneChangeEvent zEvent = (ZoneChangeEvent) event;
boolean triggered = false;
if (zEvent.getTarget().getAttachments() != null && zEvent.getTarget().getAttachments().contains(this.getSourceId())) {
triggered = true;
} else {
// If both (attachment and attached went to graveyard at the same time, the attachemnets can be already removed from the attached object.)
// So check here with the LKI of the enchantment
Permanent attachment = game.getPermanentOrLKIBattlefield(getSourceId());
if (attachment != null
&& zEvent.getTargetId() != null && attachment.getAttachedTo() != null
&& zEvent.getTargetId().equals(attachment.getAttachedTo())) {
Permanent attachedTo = game.getPermanentOrLKIBattlefield(attachment.getAttachedTo());
if (attachedTo != null
&& attachment.getAttachedToZoneChangeCounter() == attachedTo.getZoneChangeCounter(game)) { // zoneChangeCounter is stored in Permanent
triggered = true;
}
}
}
if (triggered) {
for (Effect effect : getEffects()) {
effect.setValue("attachedTo", zEvent.getTarget());
if (setTargetPointer.equals(SetTargetPointer.ATTACHED_TO_CONTROLLER)) {
Permanent attachment = game.getPermanentOrLKIBattlefield(getSourceId());
if (attachment != null && attachment.getAttachedTo() != null) {
Permanent attachedTo = (Permanent) game.getLastKnownInformation(attachment.getAttachedTo(), Zone.BATTLEFIELD, attachment.getAttachedToZoneChangeCounter());
if (attachedTo != null) {
effect.setTargetPointer(new FixedTarget(attachedTo.getControllerId()));
}
if (zEvent != null) {
if (zEvent.getTarget() != null && zEvent.getTarget().getAttachments() != null && zEvent.getTarget().getAttachments().contains(this.getSourceId())) {
triggered = true;
} else {
// If both (attachment and attached went to graveyard at the same time, the attachemnets can be already removed from the attached object.)
// So check here with the LKI of the enchantment
Permanent attachment = game.getPermanentOrLKIBattlefield(getSourceId());
if (attachment != null
&& zEvent.getTargetId() != null && attachment.getAttachedTo() != null
&& zEvent.getTargetId().equals(attachment.getAttachedTo())) {
Permanent attachedTo = game.getPermanentOrLKIBattlefield(attachment.getAttachedTo());
if (attachedTo != null
&& attachment.getAttachedToZoneChangeCounter() == attachedTo.getZoneChangeCounter(game)) { // zoneChangeCounter is stored in Permanent
triggered = true;
}
}
}
return true;
if (triggered) {
for (Effect effect : getEffects()) {
if (zEvent.getTarget() != null) {
effect.setValue("attachedTo", zEvent.getTarget());
if (setTargetPointer.equals(SetTargetPointer.ATTACHED_TO_CONTROLLER)) {
Permanent attachment = game.getPermanentOrLKIBattlefield(getSourceId());
if (attachment != null && attachment.getAttachedTo() != null) {
Permanent attachedTo = (Permanent) game.getLastKnownInformation(attachment.getAttachedTo(), Zone.BATTLEFIELD, attachment.getAttachedToZoneChangeCounter());
if (attachedTo != null) {
effect.setTargetPointer(new FixedTarget(attachedTo.getControllerId()));
}
}
}
}
}
return true;
}
}
}
return false;
@ -109,4 +112,4 @@ public class DiesAttachedTriggeredAbility extends TriggeredAbilityImpl {
sb.append(super.getRule());
return sb.toString();
}
}
}

View file

@ -29,6 +29,7 @@ package mage.abilities.keyword;
import java.util.UUID;
import mage.MageInt;
import mage.MageObject;
import mage.abilities.Ability;
import mage.abilities.SpellAbility;
import mage.abilities.costs.mana.ManaCostsImpl;
@ -51,12 +52,15 @@ import mage.target.Target;
import mage.target.common.TargetControlledPermanent;
import mage.target.targetpointer.FixedTarget;
import mage.util.CardUtil;
import org.apache.log4j.Logger;
/**
*
* @author LevelX2
*/
public class AwakenAbility extends SpellAbility {
private static final Logger logger = Logger.getLogger(AwakenAbility.class);
static private String filterMessage = "a land you control to awake";
@ -120,19 +124,33 @@ public class AwakenAbility extends SpellAbility {
@Override
public boolean apply(Game game, Ability source) {
UUID targetId = null;
for (Target target : source.getTargets()) {
if (target.getFilter().getMessage().equals(filterMessage)) {
targetId = target.getFirstTarget();
if (source != null && source.getTargets() != null) {
for (Target target : source.getTargets()) {
if (target.getFilter() != null && target.getFilter().getMessage().equals(filterMessage)) {
targetId = target.getFirstTarget();
}
}
if (targetId != null) {
FixedTarget fixedTarget = new FixedTarget(targetId);
ContinuousEffect continuousEffect = new BecomesCreatureTargetEffect(new AwakenElementalToken(), false, true, Duration.Custom);
continuousEffect.setTargetPointer(fixedTarget);
game.addEffect(continuousEffect, source);
Effect effect = new AddCountersTargetEffect(CounterType.P1P1.createInstance(awakenValue));
effect.setTargetPointer(fixedTarget);
return effect.apply(game, source);
}
} else { // source should never be null, but we are seeing a lot of NPEs from this section
if (source == null) {
logger.fatal("Source was null in AwakenAbility: Create a bug report or fix the source code");
} else if (source.getTargets() == null) {
MageObject sourceObj = source.getSourceObject(game);
if (sourceObj != null) {
Class<? extends MageObject> sourceClass = sourceObj.getClass();
if (sourceClass != null) {
logger.fatal("getTargets was null in AwakenAbility for " + sourceClass.toString() + " : Create a bug report or fix the source code");
}
}
}
}
if (targetId != null) {
FixedTarget fixedTarget = new FixedTarget(targetId);
ContinuousEffect continuousEffect = new BecomesCreatureTargetEffect(new AwakenElementalToken(), false, true, Duration.Custom);
continuousEffect.setTargetPointer(fixedTarget);
game.addEffect(continuousEffect, source);
Effect effect = new AddCountersTargetEffect(CounterType.P1P1.createInstance(awakenValue));
effect.setTargetPointer(fixedTarget);
return effect.apply(game, source);
}
return true;
}
@ -152,4 +170,4 @@ class AwakenElementalToken extends Token {
this.addAbility(HasteAbility.getInstance());
}
}
}