mirror of
https://github.com/GTNewHorizons/ForestryMC.git
synced 2026-11-22 21:09:51 +00:00
Add strict checking for registered genomes
This commit is contained in:
1 parent
c96f260abd
commit
f52059640e
5 files changed
+98
-12
No files matched your search
@@ -41,6 +41,7 @@ import forestry.api.apiculture.IBeekeepingLogic;
|
||||
import forestry.api.apiculture.IBeekeepingMode;
|
||||
import forestry.api.genetics.AlleleManager;
|
||||
import forestry.api.genetics.IAllele;
|
||||
import forestry.api.genetics.IChromosome;
|
||||
import forestry.api.genetics.IChromosomeType;
|
||||
import forestry.api.genetics.IIndividual;
|
||||
import forestry.api.genetics.IMutation;
|
||||
@@ -194,7 +195,8 @@ public class BeeHelper extends SpeciesRoot implements IBeeRoot {
|
||||
/* GENOME CONVERSIONS */
|
||||
@Override
|
||||
public IBeeGenome templateAsGenome(IAllele[] template) {
|
||||
return new BeeGenome(templateAsChromosomes(template));
|
||||
IChromosome[] chromosomes = templateAsChromosomes(template);
|
||||
return new BeeGenome(chromosomes);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -222,7 +224,9 @@ public class BeeHelper extends SpeciesRoot implements IBeeRoot {
|
||||
|
||||
@Override
|
||||
public void registerTemplate(String identifier, IAllele[] template) {
|
||||
beeTemplates.add(new Bee(BeeManager.beeRoot.templateAsGenome(template)));
|
||||
IBeeGenome beeGenome = BeeManager.beeRoot.templateAsGenome(template);
|
||||
IBee bee = new Bee(beeGenome);
|
||||
beeTemplates.add(bee);
|
||||
speciesTemplates.put(identifier, template);
|
||||
}
|
||||
|
||||
|
||||
@@ -10,8 +10,6 @@
|
||||
******************************************************************************/
|
||||
package forestry.core.genetics;
|
||||
|
||||
import com.google.common.base.Objects;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
@@ -189,7 +187,7 @@ public class Chromosome implements IChromosome {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return Objects.toStringHelper(this).add("Primary", primary).add("Secondary", secondary).toString();
|
||||
return "{" + primary + ", " + secondary + "}";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -13,23 +13,32 @@ package forestry.core.genetics;
|
||||
import com.google.common.base.Objects;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.nbt.NBTTagList;
|
||||
|
||||
import forestry.api.arboriculture.EnumTreeChromosome;
|
||||
import forestry.api.apiculture.EnumBeeChromosome;
|
||||
import forestry.api.genetics.IAllele;
|
||||
import forestry.api.genetics.IAlleleSpecies;
|
||||
import forestry.api.genetics.IChromosome;
|
||||
import forestry.api.genetics.IChromosomeType;
|
||||
import forestry.api.genetics.IGenome;
|
||||
import forestry.api.genetics.ISpeciesRoot;
|
||||
import forestry.api.lepidopterology.EnumButterflyChromosome;
|
||||
import forestry.core.config.Config;
|
||||
|
||||
public abstract class Genome implements IGenome {
|
||||
|
||||
private static final String SLOT_TAG = "Slot";
|
||||
private static final Set<IChromosomeType> unusedChromsomes = new HashSet<>();
|
||||
|
||||
static {
|
||||
unusedChromsomes.add(EnumBeeChromosome.HUMIDITY);
|
||||
unusedChromsomes.add(EnumButterflyChromosome.TERRITORY);
|
||||
}
|
||||
|
||||
private IChromosome[] chromosomes;
|
||||
|
||||
@@ -40,12 +49,78 @@ public abstract class Genome implements IGenome {
|
||||
}
|
||||
|
||||
protected Genome(IChromosome[] chromosomes) {
|
||||
if (chromosomes.length != getDefaultTemplate().length) {
|
||||
throw new IllegalArgumentException(String.format("Tried to create a genome for '%s' from an invalid chromosome template.", getSpeciesRoot().getUID()));
|
||||
}
|
||||
checkChromosomes(chromosomes);
|
||||
this.chromosomes = chromosomes;
|
||||
}
|
||||
|
||||
private void checkChromosomes(IChromosome[] chromosomes) {
|
||||
if (chromosomes == null || chromosomes.length != getDefaultTemplate().length) {
|
||||
String message = String.format("Tried to create a genome for '%s' from an invalid chromosome template.\n%s", getSpeciesRoot().getUID(), chromosomesToString(chromosomes));
|
||||
throw new IllegalArgumentException(message);
|
||||
}
|
||||
|
||||
IChromosomeType[] karyotype = getSpeciesRoot().getKaryotype();
|
||||
for (int i = 0; i < karyotype.length; i++) {
|
||||
IChromosomeType chromosomeType = karyotype[i];
|
||||
if (unusedChromsomes.contains(chromosomeType)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
IChromosome chromosome = chromosomes[i];
|
||||
if (chromosome == null) {
|
||||
String message = String.format("Tried to create a genome for '%s' from an invalid chromosome template. " +
|
||||
"Missing chromosome '%s'.\n%s", getSpeciesRoot().getUID(), chromosomeType.getName(), chromosomesToString(chromosomes));
|
||||
throw new IllegalArgumentException(message);
|
||||
}
|
||||
|
||||
IAllele primary = chromosome.getPrimaryAllele();
|
||||
if (primary == null) {
|
||||
String message = String.format("Tried to create a genome for '%s' from an invalid chromosome template. " +
|
||||
"Missing primary allele for '%s'.\n%s", getSpeciesRoot().getUID(), chromosomeType.getName(), chromosomesToString(chromosomes));
|
||||
throw new IllegalArgumentException(message);
|
||||
}
|
||||
|
||||
IAllele secondary = chromosome.getSecondaryAllele();
|
||||
if (secondary == null) {
|
||||
String message = String.format("Tried to create a genome for '%s' from an invalid chromosome template. " +
|
||||
"Missing secondary allele for '%s'.\n%s", getSpeciesRoot().getUID(), chromosomeType.getName(), chromosomesToString(chromosomes));
|
||||
throw new IllegalArgumentException(message);
|
||||
}
|
||||
|
||||
Class<? extends IAllele> chromosomeAlleleClass = chromosomeType.getAlleleClass();
|
||||
if (!chromosomeAlleleClass.isAssignableFrom(primary.getClass())) {
|
||||
String message = String.format("Tried to create a genome for '%s' from an invalid chromosome template. " +
|
||||
"Incorrect type for primary allele '%s'.\n%s.", getSpeciesRoot().getUID(), chromosomeType.getName(), chromosomesToString(chromosomes));
|
||||
throw new IllegalArgumentException(message);
|
||||
}
|
||||
|
||||
if (!chromosomeAlleleClass.isAssignableFrom(secondary.getClass())) {
|
||||
String message = String.format("Tried to create a genome for '%s' from an invalid chromosome template. " +
|
||||
"Incorrect type for secondary allele '%s'.\n%s.", getSpeciesRoot().getUID(), chromosomeType.getName(), chromosomesToString(chromosomes));
|
||||
throw new IllegalArgumentException(message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private String chromosomesToString(IChromosome[] chromosomes) {
|
||||
if (chromosomes == null) {
|
||||
return "null";
|
||||
}
|
||||
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
IChromosomeType[] karyotype = getSpeciesRoot().getKaryotype();
|
||||
for (int i = 0; i < chromosomes.length; i++) {
|
||||
IChromosomeType chromosomeType = karyotype[i];
|
||||
if (unusedChromsomes.contains(chromosomeType)) {
|
||||
continue;
|
||||
}
|
||||
IChromosome chromosome = chromosomes[i];
|
||||
stringBuilder.append(chromosomeType.getName()).append(": ").append(chromosome).append("\n");
|
||||
}
|
||||
|
||||
return stringBuilder.toString();
|
||||
}
|
||||
|
||||
private IAllele[] getDefaultTemplate() {
|
||||
return getSpeciesRoot().getDefaultTemplate();
|
||||
}
|
||||
@@ -130,7 +205,7 @@ public abstract class Genome implements IGenome {
|
||||
}
|
||||
|
||||
// handle old saves that have missing chromosomes
|
||||
IChromosome speciesChromosome = chromosomes[EnumTreeChromosome.SPECIES.ordinal()];
|
||||
IChromosome speciesChromosome = chromosomes[speciesRoot.getKaryotypeKey().ordinal()];
|
||||
if (speciesChromosome != null) {
|
||||
IAlleleSpecies species = (IAlleleSpecies) speciesChromosome.getActiveAllele();
|
||||
IAllele[] template = speciesRoot.getTemplate(species.getUID());
|
||||
|
||||
@@ -54,12 +54,21 @@ public abstract class SpeciesRoot implements ISpeciesRoot {
|
||||
|
||||
@Override
|
||||
public void registerTemplate(IAllele[] template) {
|
||||
if (template == null) {
|
||||
throw new IllegalArgumentException("Tried to register null template");
|
||||
}
|
||||
if (template.length == 0) {
|
||||
throw new IllegalArgumentException("Tried to register empty template");
|
||||
}
|
||||
registerTemplate(template[0].getUID(), template);
|
||||
}
|
||||
|
||||
@Override
|
||||
public IAllele[] getRandomTemplate(Random rand) {
|
||||
return speciesTemplates.values().toArray(new IAllele[0][])[rand.nextInt(speciesTemplates.values().size())];
|
||||
Collection<IAllele[]> templates = speciesTemplates.values();
|
||||
int size = templates.size();
|
||||
IAllele[][] templatesArray = templates.toArray(new IAllele[size][]);
|
||||
return templatesArray[rand.nextInt(size)];
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in new issue
Block a user