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 java.util.Map;
import biomesoplenty.common.util.config.BOPConfig.IConfigObj; 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.ImmutableCollection;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
@ -75,57 +73,12 @@ public class GenerationManager
else else
{ {
// there was previously no generator of this name - attempt to add it // 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) if (generator != null)
{ {
this.generators.put(name, generator); 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; import biomesoplenty.common.util.config.BOPConfig.IConfigObj;
// TODO - scrap this?
public abstract class GeneratorCustomizable implements IGenerator public abstract class GeneratorCustomizable implements IGenerator
{ {
private final String identifier; private final String identifier;

View file

@ -8,6 +8,8 @@
package biomesoplenty.api.biome.generation; package biomesoplenty.api.biome.generation;
import biomesoplenty.common.util.config.BOPConfig.IConfigObj;
import com.google.common.collect.BiMap; import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap; import com.google.common.collect.HashBiMap;
@ -34,4 +36,27 @@ public class GeneratorRegistry
{ {
return generatorClasses.containsValue(identifier); 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.util.BlockPos;
import net.minecraft.world.World; import net.minecraft.world.World;
import biomesoplenty.api.biome.generation.GenerationManager.GeneratorFactory;
import biomesoplenty.common.util.config.BOPConfig.IConfigObj; import biomesoplenty.common.util.config.BOPConfig.IConfigObj;
public class GeneratorWeighted extends GeneratorCustomizable 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 // there was previously no generator of this name - attempt to add it
Integer weight = conf.getInt("weight", null); Integer weight = conf.getInt("weight", null);
IGenerator generator = GeneratorFactory.create(conf); IGenerator generator = GeneratorRegistry.createGenerator(conf);
if (weight != null && generator != null) if (weight != null && generator != null)
{ {
this.add(name, weight, generator); 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 there was a valid config file, then use it to configure the biome
if (!conf.isEmpty()) {biome.configure(conf);} if (!conf.isEmpty()) {biome.configure(conf);}
// log any warnings from parsing the config file // 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; BiomeGenBase.getBiomeGenArray()[id] = biome;