Add better logs for invalid genome errors #400

This commit is contained in:
mezz committed 2014-12-24 20:02:11 -08:00
1 parent 68f61ab9ab
commit 77295ab74e
3 files changed
+89 -12

No files matched your search

@@ -707,4 +707,9 @@ public class Allele implements IAllele {
name = string;
return this;
}
@Override
public String toString() {
return uid;
}
}
@@ -10,10 +10,14 @@
******************************************************************************/
package forestry.core.genetics;
import com.google.common.base.Objects;
import forestry.api.genetics.AlleleManager;
import forestry.api.genetics.IAllele;
import forestry.api.genetics.IChromosome;
import forestry.api.genetics.ILegacyHandler;
import forestry.core.proxy.Proxies;
import java.util.Random;
import net.minecraft.nbt.NBTTagCompound;
@@ -101,20 +105,59 @@ public class Chromosome implements IChromosome {
return secondary;
}
public void overrideInvalidAlleles(IAllele template, Class<? extends IAllele> chromosomeClass) {
if (primary == null || !chromosomeClass.isInstance(primary))
public boolean overrideInvalidAlleles(IAllele template, Class<? extends IAllele> chromosomeClass) {
boolean foundInvalidAlleles = false;
// use the other chromosome instead of the template if it's valid
if (primary != null && chromosomeClass.isInstance(primary)) {
template = primary;
} else if (secondary != null && chromosomeClass.isInstance(secondary)) {
template = secondary;
}
if (primary == null) {
Proxies.log.warning("Missing primary allele: {0}. Setting to: {1}", this, template);
primary = template;
foundInvalidAlleles = true;
} else if (!chromosomeClass.isInstance(primary)) {
Proxies.log.warning("Wrong primary allele: {0}. Setting to: {1}", this, template);
primary = template;
foundInvalidAlleles = true;
}
if (secondary == null || !chromosomeClass.isInstance(secondary))
if (secondary == null) {
Proxies.log.warning("Missing secondary allele: {0}. Setting to: {1}", this, template);
secondary = template;
foundInvalidAlleles = true;
} else if (!chromosomeClass.isInstance(secondary)) {
Proxies.log.warning("Wrong secondary allele: {0}. Setting to: {1}", this, template);
secondary = template;
foundInvalidAlleles = true;
}
return foundInvalidAlleles;
}
public boolean hasInvalidAlleles(Class<? extends IAllele> chromosomeClass) {
if (primary == null || !chromosomeClass.isInstance(primary))
if (primary == null) {
Proxies.log.severe("Missing primary allele: {0}", this);
return true;
}
if (!chromosomeClass.isInstance(primary)) {
Proxies.log.severe("Wrong primary allele for: {0}. Should be: {1}", this, chromosomeClass.getSimpleName());
return true;
}
if (secondary == null || !chromosomeClass.isInstance(secondary))
if (secondary == null) {
Proxies.log.severe("Missing secondary allele: {0}", this);
return true;
}
if (!chromosomeClass.isInstance(secondary)) {
Proxies.log.severe("Wrong secondary allele for: {0}. Should be: {1}", this, chromosomeClass.getSimpleName());
return true;
}
return false;
}
@@ -146,4 +189,9 @@ public class Chromosome implements IChromosome {
return new Chromosome(choice2, choice1);
}
@Override
public String toString() {
return Objects.toStringHelper(this).add("Primary", primary).add("Secondary", secondary).toString();
}
}
@@ -10,6 +10,12 @@
******************************************************************************/
package forestry.core.genetics;
import com.google.common.base.Objects;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import forestry.api.arboriculture.EnumTreeChromosome;
import forestry.api.genetics.IAllele;
import forestry.api.genetics.IAlleleSpecies;
@@ -17,9 +23,7 @@ import forestry.api.genetics.IChromosome;
import forestry.api.genetics.IChromosomeType;
import forestry.api.genetics.IGenome;
import forestry.core.config.Config;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import forestry.core.proxy.Proxies;
public abstract class Genome implements IGenome {
@@ -81,6 +85,9 @@ public abstract class Genome implements IGenome {
NBTTagList nbttaglist = nbttagcompound.getTagList("Chromosomes", 10);
chromosomes = new Chromosome[chromosomes.length];
Boolean invalidGenome = false;
for (int i = 0; i < nbttaglist.tagCount(); i++) {
NBTTagCompound nbttagcompound1 = nbttaglist.getCompoundTagAt(i);
byte byte0 = nbttagcompound1.getByte(SLOT_TAG);
@@ -88,15 +95,22 @@ public abstract class Genome implements IGenome {
if (byte0 >= 0 && byte0 < chromosomes.length) {
Chromosome chromosome = Chromosome.loadChromosomeFromNBT(nbttagcompound1);
chromosomes[byte0] = chromosome;
if (Config.clearInvalidChromosomes)
chromosome.overrideInvalidAlleles(getDefaultTemplate()[byte0], getSpeciesRoot().getKaryotype()[byte0].getAlleleClass());
if (Config.clearInvalidChromosomes) {
if (chromosome.overrideInvalidAlleles(getDefaultTemplate()[byte0], getSpeciesRoot().getKaryotype()[byte0].getAlleleClass())) {
invalidGenome = true;
}
}
if (chromosome.hasInvalidAlleles(getSpeciesRoot().getKaryotype()[byte0].getAlleleClass()))
throw new RuntimeException("Found Chromosome with invalid Alleles. See config option \"genetics.clear.invalid.chromosomes\".");
throw new RuntimeException("Found Chromosome with invalid Alleles.\nNBTTagCompound: " + nbttaglist + "\nSee config option \"genetics.clear.invalid.chromosomes\".");
}
}
if (invalidGenome) {
Proxies.log.warning("Overrode alleles for genome:\n{0}\nOriginal NBTTagCompound: {1}", this, nbttaglist);
}
// handle old saves that have missing chromosomes
IChromosome speciesChromosome = chromosomes[EnumTreeChromosome.SPECIES.ordinal()];
if (speciesChromosome != null) {
@@ -172,4 +186,14 @@ public abstract class Genome implements IGenome {
return true;
}
@Override
public String toString() {
Objects.ToStringHelper toStringHelper = Objects.toStringHelper(this);
int i = 0;
for (IChromosome chromosome : chromosomes) {
toStringHelper.add(String.valueOf(i++), chromosome);
}
return toStringHelper.toString();
}
}