Use existing GeneratorRegistry for creation of generators from config files instead of a separate GeneratorFactory class

This commit is contained in:
Cheeserolls 2015-05-26 16:51:45 +01:00
parent 795592d20f
commit e83edcf214
5 changed files with 28 additions and 52 deletions

View File

@ -13,8 +13,6 @@ import java.util.HashMap;
import java.util.Map;
import biomesoplenty.common.util.config.BOPConfig.IConfigObj;
import biomesoplenty.common.world.feature.*;
import biomesoplenty.common.world.feature.tree.*;
import com.google.common.collect.ImmutableCollection;
import com.google.common.collect.ImmutableList;
@ -75,57 +73,12 @@ public class GenerationManager
else
{
// there was previously no generator of this name - attempt to add it
IGenerator generator = GeneratorFactory.create(conf);
IGenerator generator = GeneratorRegistry.createGenerator(conf);
if (generator != null)
{
this.generators.put(name, generator);
}
}
}
// TODO: use GeneratorRegistry instead?
public enum GeneratorFactory
{
FLORA, DOUBLE_FLORA, GRASS, ORE_CLUSTER, ORE_SINGLE, WATERSIDE, BASIC_TREE, BIG_TREE, BUSH;
public static IGenerator create(IConfigObj conf)
{
GeneratorStage stage = conf.getEnum("stage", GeneratorStage.class);
GeneratorFactory factory = conf.getEnum("type", GeneratorFactory.class);
if (stage == null || factory == null) {return null;}
IGenerator generator = factory.create();
generator.setStage(stage);
generator.configure(conf);
return generator;
}
public IGenerator create()
{
switch (this)
{
case FLORA:
return new GeneratorFlora();
case DOUBLE_FLORA:
return new GeneratorDoubleFlora();
case GRASS:
return new GeneratorGrass();
case ORE_CLUSTER:
return new GeneratorOreCluster();
case ORE_SINGLE:
return new GeneratorOreSingle();
case WATERSIDE:
return new GeneratorWaterside();
case BASIC_TREE:
return new GeneratorBasicTree();
case BIG_TREE:
return new GeneratorBigTree();
case BUSH:
return new GeneratorBush();
default:
return null;
}
}
}
}

View File

@ -10,7 +10,6 @@ package biomesoplenty.api.biome.generation;
import biomesoplenty.common.util.config.BOPConfig.IConfigObj;
// TODO - scrap this?
public abstract class GeneratorCustomizable implements IGenerator
{
private final String identifier;

View File

@ -8,6 +8,8 @@
package biomesoplenty.api.biome.generation;
import biomesoplenty.common.util.config.BOPConfig.IConfigObj;
import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
@ -34,4 +36,27 @@ public class GeneratorRegistry
{
return generatorClasses.containsValue(identifier);
}
public static IGenerator createGenerator(IConfigObj conf)
{
GeneratorStage stage = conf.getEnum("stage", GeneratorStage.class);
String identifier = conf.getString("type");
if (stage == null || identifier == null) {return null;}
Class<? extends IGenerator> clazz = getGeneratorClass(identifier);
if (clazz == null)
{
conf.addMessage("No generator is registered with type name " + identifier);
return null;
}
IGenerator generator;
try {
generator = clazz.newInstance();
} catch (Exception e) {
conf.addMessage("Failed to create instance of generator " + identifier + " - " + e.getMessage());
return null;
}
generator.setStage(stage);
generator.configure(conf);
return generator;
}
}

View File

@ -14,7 +14,6 @@ import java.util.Random;
import net.minecraft.util.BlockPos;
import net.minecraft.world.World;
import biomesoplenty.api.biome.generation.GenerationManager.GeneratorFactory;
import biomesoplenty.common.util.config.BOPConfig.IConfigObj;
public class GeneratorWeighted extends GeneratorCustomizable
@ -133,7 +132,7 @@ public class GeneratorWeighted extends GeneratorCustomizable
{
// there was previously no generator of this name - attempt to add it
Integer weight = conf.getInt("weight", null);
IGenerator generator = GeneratorFactory.create(conf);
IGenerator generator = GeneratorRegistry.createGenerator(conf);
if (weight != null && generator != null)
{
this.add(name, weight, generator);

View File

@ -97,7 +97,7 @@ public class ModBiomes
// If there was a valid config file, then use it to configure the biome
if (!conf.isEmpty()) {biome.configure(conf);}
// log any warnings from parsing the config file
for (String msg : conf.flushMessages()) {BiomesOPlenty.logger.warn(idName+" config "+msg);}
for (String msg : conf.flushMessages()) {BiomesOPlenty.logger.warn(msg);}
BiomeGenBase.getBiomeGenArray()[id] = biome;