if (target == null) {
throw new IllegalArgumentException("Target can't be null");
}
// A copy contains only the attributes of the basic card or basic Token that's the base of the permanent
// else gained abililies would be copied too.
MageObject sourceObj = source;
if (source instanceof PermanentToken) {
sourceObj = ((PermanentToken) source).getToken();
// to show the source image, the original values have to be used
target.setOriginalExpansionSetCode(((Token)sourceObj).getOriginalExpansionSetCode());
target.setOriginalCardNumber(((Token)sourceObj).getOriginalCardNumber());
} else if (source instanceof PermanentCard) {
sourceObj = ((PermanentCard) source).getCard();
target.setOriginalExpansionSetCode(source.getExpansionSetCode());
target.setOriginalCardNumber(source.getCardNumber());
} else {
target.setOriginalExpansionSetCode(source.getExpansionSetCode());
target.setOriginalCardNumber(source.getCardNumber());
}
target.setName(sourceObj.getName());
target.getColor().setColor(sourceObj.getColor());
target.getManaCost().clear();
target.getManaCost().add(sourceObj.getManaCost());
target.getCardType().clear();
for (CardType type : sourceObj.getCardType()) {
target.getCardType().add(type);
}
target.getSubtype().clear();
for (String type : sourceObj.getSubtype()) {
target.getSubtype().add(type);
}
target.getSupertype().clear();
for (String type : sourceObj.getSupertype()) {
target.getSupertype().add(type);
}
target.getAbilities().clear();
for (Ability ability0 : sourceObj.getAbilities()) {
Ability ability = ability0.copy();
ability.newId();
ability.setSourceId(target.getId());
target.addAbility(ability);
}
// Needed to do it this way because only the cardValue does not include the increased value from cards like "Intangible Virtue" will be copied.
target.getPower().initValue(Integer.parseInt(sourceObj.getPower().toString()));
target.getToughness().initValue(Integer.parseInt(sourceObj.getToughness().toString()));
return target;
}