Divided IGenerator into IGeneratorController and IGeneratorDelegate, to clarify generators belonging to another (e.g. GeneratorWeighted has 'sub' generators which it randomly picks from)

This commit is contained in:
Adubbz 2015-04-09 10:58:27 +10:00
parent ab961ba89b
commit 8f7c37384f
17 changed files with 119 additions and 79 deletions

View File

@ -15,7 +15,7 @@ import net.minecraft.world.biome.BiomeGenBase;
import net.minecraftforge.common.BiomeManager.BiomeType; import net.minecraftforge.common.BiomeManager.BiomeType;
import biomesoplenty.api.biome.generation.GenerationManager; import biomesoplenty.api.biome.generation.GenerationManager;
import biomesoplenty.api.biome.generation.GeneratorStage; import biomesoplenty.api.biome.generation.GeneratorStage;
import biomesoplenty.api.biome.generation.IGenerator; import biomesoplenty.api.biome.generation.IGeneratorController;
public class BOPBiome extends BiomeGenBase implements IExtendedBiome public class BOPBiome extends BiomeGenBase implements IExtendedBiome
{ {
@ -41,7 +41,7 @@ public class BOPBiome extends BiomeGenBase implements IExtendedBiome
} }
@Override @Override
public void addGenerator(String name, GeneratorStage stage, IGenerator generator) public void addGenerator(String name, GeneratorStage stage, IGeneratorController generator)
{ {
this.generationManager.addGenerator(name, stage, generator); this.generationManager.addGenerator(name, stage, generator);
} }

View File

@ -10,15 +10,15 @@ package biomesoplenty.api.biome;
import java.util.Map; import java.util.Map;
import net.minecraftforge.common.BiomeManager.BiomeType;
import biomesoplenty.api.biome.generation.GenerationManager; import biomesoplenty.api.biome.generation.GenerationManager;
import biomesoplenty.api.biome.generation.GeneratorStage; import biomesoplenty.api.biome.generation.GeneratorStage;
import biomesoplenty.api.biome.generation.IGenerator; import biomesoplenty.api.biome.generation.IGeneratorController;
import net.minecraftforge.common.BiomeManager.BiomeType;
public interface IExtendedBiome public interface IExtendedBiome
{ {
public BiomeOwner getBiomeOwner(); public BiomeOwner getBiomeOwner();
public void addGenerator(String name, GeneratorStage stage, IGenerator generator); public void addGenerator(String name, GeneratorStage stage, IGeneratorController generator);
public GenerationManager getGenerationManager(); public GenerationManager getGenerationManager();
public Map<BiomeType, Integer> getWeightMap(); public Map<BiomeType, Integer> getWeightMap();
public void clearWeights(); public void clearWeights();

View File

@ -8,7 +8,7 @@
package biomesoplenty.api.biome.generation; package biomesoplenty.api.biome.generation;
public abstract class CustomizableGenerator implements IGenerator public abstract class CustomizableGenerator implements IGeneratorController
{ {
private final String identifier; private final String identifier;
private String name; private String name;
@ -16,7 +16,7 @@ public abstract class CustomizableGenerator implements IGenerator
protected CustomizableGenerator() protected CustomizableGenerator()
{ {
this.identifier = GeneratorRegistry.getIdentifier((Class<? extends IGenerator>)this.getClass()); this.identifier = GeneratorRegistry.getIdentifier((Class<? extends IGeneratorBase>)this.getClass());
if (this.identifier == null) if (this.identifier == null)
{ {

View File

@ -14,10 +14,9 @@ import net.minecraft.util.BlockPos;
import net.minecraft.util.WeightedRandom; import net.minecraft.util.WeightedRandom;
import net.minecraft.world.World; import net.minecraft.world.World;
public abstract class CustomizableWeightedGenerator extends WeightedRandom.Item implements IGenerator public abstract class CustomizableWeightedGenerator extends WeightedRandom.Item implements IGeneratorDelegate
{ {
private final String identifier; private final String identifier;
private String name;
private GeneratorStage stage; private GeneratorStage stage;
protected CustomizableWeightedGenerator() protected CustomizableWeightedGenerator()
@ -29,7 +28,7 @@ public abstract class CustomizableWeightedGenerator extends WeightedRandom.Item
{ {
super(weight); super(weight);
this.identifier = GeneratorRegistry.getIdentifier((Class<? extends IGenerator>)this.getClass()); this.identifier = GeneratorRegistry.getIdentifier((Class<? extends IGeneratorBase>)this.getClass());
this.stage = GeneratorStage.PARENT; this.stage = GeneratorStage.PARENT;
if (this.identifier == null) if (this.identifier == null)
@ -37,16 +36,6 @@ public abstract class CustomizableWeightedGenerator extends WeightedRandom.Item
throw new RuntimeException("The identifier for " + this.getClass().getCanonicalName() + " cannot be null!"); throw new RuntimeException("The identifier for " + this.getClass().getCanonicalName() + " cannot be null!");
} }
} }
//Scattering should be handled in GeneratorWeighted
@Override
public final void scatter(World world, Random random, BlockPos pos) {}
@Override
public void setName(String name)
{
this.name = name;
}
@Override @Override
public void setStage(GeneratorStage stage) public void setStage(GeneratorStage stage)
@ -54,12 +43,6 @@ public abstract class CustomizableWeightedGenerator extends WeightedRandom.Item
this.stage = stage; this.stage = stage;
} }
@Override
public String getName()
{
return this.name;
}
@Override @Override
public GeneratorStage getStage() public GeneratorStage getStage()
{ {

View File

@ -20,9 +20,9 @@ import com.google.common.collect.Table;
public class GenerationManager public class GenerationManager
{ {
private Table<GeneratorStage, String, IGenerator> generatorTable = HashBasedTable.create(); private Table<GeneratorStage, String, IGeneratorController> generatorTable = HashBasedTable.create();
public void addGenerator(String name, GeneratorStage stage, IGenerator generator) public void addGenerator(String name, GeneratorStage stage, IGeneratorController generator)
{ {
if (!this.generatorTable.containsColumn(name)) if (!this.generatorTable.containsColumn(name))
{ {
@ -37,19 +37,19 @@ public class GenerationManager
} }
} }
public ImmutableCollection<IGenerator> getGeneratorsForStage(GeneratorStage stage) public ImmutableCollection<IGeneratorController> getGeneratorsForStage(GeneratorStage stage)
{ {
Map<String, IGenerator> columnMap = this.generatorTable.rowMap().get(stage); Map<String, IGeneratorController> columnMap = this.generatorTable.rowMap().get(stage);
Collection<IGenerator> result = columnMap == null ? null : columnMap.values(); Collection<IGeneratorController> result = columnMap == null ? null : columnMap.values();
return result == null ? ImmutableList.<IGenerator>of() : ImmutableList.<IGenerator>copyOf(result); return result == null ? ImmutableList.<IGeneratorController>of() : ImmutableList.<IGeneratorController>copyOf(result);
} }
public Map<String, IGenerator> createGeneratorMap() public Map<String, IGeneratorController> createGeneratorMap()
{ {
Map<String, IGenerator> result = new HashMap<String, IGenerator>(); Map<String, IGeneratorController> result = new HashMap<String, IGeneratorController>();
for (IGenerator generator : this.generatorTable.values()) for (IGeneratorController generator : this.generatorTable.values())
{ {
result.put(generator.getName(), generator); result.put(generator.getName(), generator);
} }
@ -57,14 +57,14 @@ public class GenerationManager
return result; return result;
} }
public void createGeneratorTable(Map<String, IGenerator> generators) public void createGeneratorTable(Map<String, IGeneratorController> generators)
{ {
Table<GeneratorStage, String, IGenerator> result = HashBasedTable.create(); Table<GeneratorStage, String, IGeneratorController> result = HashBasedTable.create();
for (Entry<String, IGenerator> entry : generators.entrySet()) for (Entry<String, IGeneratorController> entry : generators.entrySet())
{ {
String name = entry.getKey(); String name = entry.getKey();
IGenerator generator = entry.getValue(); IGeneratorController generator = entry.getValue();
generator.setName(name); generator.setName(name);
result.put(generator.getStage(), generator.getName(), generator); result.put(generator.getStage(), generator.getName(), generator);

View File

@ -13,19 +13,19 @@ import com.google.common.collect.HashBiMap;
public class GeneratorRegistry public class GeneratorRegistry
{ {
private static BiMap<String, Class<? extends IGenerator>> generatorClasses = HashBiMap.create(); private static BiMap<String, Class<? extends IGeneratorBase>> generatorClasses = HashBiMap.create();
public static void registerGenerator(String identifier, Class<? extends IGenerator> generatorClass) public static void registerGenerator(String identifier, Class<? extends IGeneratorBase> generatorClass)
{ {
generatorClasses.put(identifier, generatorClass); generatorClasses.put(identifier, generatorClass);
} }
public static String getIdentifier(Class<? extends IGenerator> generatorClass) public static String getIdentifier(Class<? extends IGeneratorBase> generatorClass)
{ {
return generatorClasses.inverse().get(generatorClass); return generatorClasses.inverse().get(generatorClass);
} }
public static Class<? extends IGenerator> getGeneratorClass(String identifier) public static Class<? extends IGeneratorBase> getGeneratorClass(String identifier)
{ {
return generatorClasses.get(identifier); return generatorClasses.get(identifier);
} }

View File

@ -17,20 +17,12 @@ import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.google.gson.JsonSerializationContext; import com.google.gson.JsonSerializationContext;
public interface IGenerator public interface IGeneratorBase
{ {
public void scatter(World world, Random random, BlockPos pos);
public boolean generate(World world, Random random, BlockPos pos);
public void setName(String name);
public void setStage(GeneratorStage stage); public void setStage(GeneratorStage stage);
/**The identifier for this generator should be consistent across all instances of the same type*/ /**The identifier for this generator should be consistent across all instances of the same type*/
public String getIdentifier(); public String getIdentifier();
/**A unique name use to classify the purpose of a generator. For example, emeralds and ruby use the
* same generator (and thus, have the same identifier) but have differing names.
*/
public String getName();
public GeneratorStage getStage(); public GeneratorStage getStage();
public void writeToJson(JsonObject json, JsonSerializationContext context); public void writeToJson(JsonObject json, JsonSerializationContext context);

View File

@ -0,0 +1,31 @@
/*******************************************************************************
* Copyright 2015, the Biomes O' Plenty Team
*
* This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International Public License.
*
* To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-nd/4.0/.
******************************************************************************/
package biomesoplenty.api.biome.generation;
import java.util.Random;
import net.minecraft.util.BlockPos;
import net.minecraft.world.World;
/**
* Responsible for handling its own associated generation, or referring it
* to delegate generators.
*/
public interface IGeneratorController extends IGeneratorBase
{
public void scatter(World world, Random random, BlockPos pos);
public boolean generate(World world, Random random, BlockPos pos);
public void setName(String name);
/**A unique name use to classify the purpose of a generator. For example, emeralds and ruby use the
* same generator (and thus, have the same identifier) but have differing names.
*/
public String getName();
}

View File

@ -0,0 +1,24 @@
/*******************************************************************************
* Copyright 2015, the Biomes O' Plenty Team
*
* This work is licensed under a Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International Public License.
*
* To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-nd/4.0/.
******************************************************************************/
package biomesoplenty.api.biome.generation;
import java.util.Random;
import net.minecraft.util.BlockPos;
import net.minecraft.world.World;
/**
* Handles its own generation when referred to by a
* controller.
*/
public interface IGeneratorDelegate extends IGeneratorBase
{
public void scatter(World world, Random random, BlockPos pos, int amountPerChunk);
public boolean generate(World world, Random random, BlockPos pos, int amountPerChunk);
}

View File

@ -17,7 +17,7 @@ import biomesoplenty.api.biome.BiomeOwner;
import biomesoplenty.api.biome.IExtendedBiome; import biomesoplenty.api.biome.IExtendedBiome;
import biomesoplenty.api.biome.generation.GenerationManager; import biomesoplenty.api.biome.generation.GenerationManager;
import biomesoplenty.api.biome.generation.GeneratorStage; import biomesoplenty.api.biome.generation.GeneratorStage;
import biomesoplenty.api.biome.generation.IGenerator; import biomesoplenty.api.biome.generation.IGeneratorController;
public class ExtendedBiomeRegistry public class ExtendedBiomeRegistry
{ {
@ -65,7 +65,7 @@ public class ExtendedBiomeRegistry
} }
@Override @Override
public void addGenerator(String name, GeneratorStage stage, IGenerator generator) public void addGenerator(String name, GeneratorStage stage, IGeneratorController generator)
{ {
this.generationManager.addGenerator(name, stage, generator); this.generationManager.addGenerator(name, stage, generator);
} }

View File

@ -20,8 +20,8 @@ import biomesoplenty.common.block.BlockBOPLeaves;
import biomesoplenty.common.block.BlockBOPLeaves2; import biomesoplenty.common.block.BlockBOPLeaves2;
import biomesoplenty.common.block.BlockBOPGrass.BOPGrassType; import biomesoplenty.common.block.BlockBOPGrass.BOPGrassType;
import biomesoplenty.common.config.MiscConfigurationHandler; import biomesoplenty.common.config.MiscConfigurationHandler;
import biomesoplenty.common.world.feature.GeneratorWeighted;
import biomesoplenty.common.world.feature.tree.GeneratorBasicTree; import biomesoplenty.common.world.feature.tree.GeneratorBasicTree;
import biomesoplenty.common.world.feature.tree.GeneratorWeighted;
public class BiomeGenOriginValley extends BOPBiome public class BiomeGenOriginValley extends BOPBiome
{ {

View File

@ -21,7 +21,8 @@ import biomesoplenty.api.biome.BiomeOwner;
import biomesoplenty.api.biome.IExtendedBiome; import biomesoplenty.api.biome.IExtendedBiome;
import biomesoplenty.api.biome.generation.GenerationManager; import biomesoplenty.api.biome.generation.GenerationManager;
import biomesoplenty.api.biome.generation.GeneratorStage; import biomesoplenty.api.biome.generation.GeneratorStage;
import biomesoplenty.api.biome.generation.IGenerator; import biomesoplenty.api.biome.generation.IGeneratorBase;
import biomesoplenty.api.biome.generation.IGeneratorController;
import biomesoplenty.common.biome.ExtendedBiomeRegistry; import biomesoplenty.common.biome.ExtendedBiomeRegistry;
public class DecorateBiomeEventHandler public class DecorateBiomeEventHandler
@ -58,7 +59,7 @@ public class DecorateBiomeEventHandler
{ {
GenerationManager generationManager = extendedBiome.getGenerationManager(); GenerationManager generationManager = extendedBiome.getGenerationManager();
for (IGenerator generator : generationManager.getGeneratorsForStage(stage)) for (IGeneratorController generator : generationManager.getGeneratorsForStage(stage))
{ {
generator.scatter(world, random, pos); generator.scatter(world, random, pos);
} }

View File

@ -11,8 +11,8 @@ package biomesoplenty.common.init;
import static biomesoplenty.api.biome.generation.GeneratorRegistry.registerGenerator; import static biomesoplenty.api.biome.generation.GeneratorRegistry.registerGenerator;
import biomesoplenty.common.world.feature.GeneratorOreCluster; import biomesoplenty.common.world.feature.GeneratorOreCluster;
import biomesoplenty.common.world.feature.GeneratorOreSingle; import biomesoplenty.common.world.feature.GeneratorOreSingle;
import biomesoplenty.common.world.feature.GeneratorWeighted;
import biomesoplenty.common.world.feature.tree.GeneratorBasicTree; import biomesoplenty.common.world.feature.tree.GeneratorBasicTree;
import biomesoplenty.common.world.feature.tree.GeneratorWeighted;
public class ModGenerators public class ModGenerators
{ {

View File

@ -12,7 +12,7 @@ import java.lang.reflect.Type;
import biomesoplenty.api.biome.generation.GeneratorRegistry; import biomesoplenty.api.biome.generation.GeneratorRegistry;
import biomesoplenty.api.biome.generation.GeneratorStage; import biomesoplenty.api.biome.generation.GeneratorStage;
import biomesoplenty.api.biome.generation.IGenerator; import biomesoplenty.api.biome.generation.IGeneratorBase;
import com.google.common.reflect.TypeToken; import com.google.common.reflect.TypeToken;
import com.google.gson.JsonDeserializationContext; import com.google.gson.JsonDeserializationContext;
@ -24,10 +24,10 @@ import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer; import com.google.gson.JsonSerializer;
import com.google.gson.JsonSyntaxException; import com.google.gson.JsonSyntaxException;
public class GeneratorTypeAdaptor implements JsonSerializer<IGenerator>, JsonDeserializer<IGenerator> public class GeneratorTypeAdaptor implements JsonSerializer<IGeneratorBase>, JsonDeserializer<IGeneratorBase>
{ {
@Override @Override
public JsonElement serialize(IGenerator src, Type typeOfSrc, JsonSerializationContext context) public JsonElement serialize(IGeneratorBase src, Type typeOfSrc, JsonSerializationContext context)
{ {
JsonObject jsonObject = new JsonObject(); JsonObject jsonObject = new JsonObject();
src.writeToJson(jsonObject, context); src.writeToJson(jsonObject, context);
@ -39,14 +39,14 @@ public class GeneratorTypeAdaptor implements JsonSerializer<IGenerator>, JsonDes
} }
@Override @Override
public IGenerator deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException public IGeneratorBase deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException
{ {
JsonObject jsonObject = json.getAsJsonObject(); JsonObject jsonObject = json.getAsJsonObject();
if (jsonObject.has("generator")) if (jsonObject.has("generator"))
{ {
String generatorIdentifier = jsonObject.get("generator").getAsString(); String generatorIdentifier = jsonObject.get("generator").getAsString();
Class<? extends IGenerator> generatorClass = GeneratorRegistry.getGeneratorClass(generatorIdentifier); Class<? extends IGeneratorBase> generatorClass = GeneratorRegistry.getGeneratorClass(generatorIdentifier);
if (generatorClass == null) if (generatorClass == null)
{ {
@ -54,10 +54,10 @@ public class GeneratorTypeAdaptor implements JsonSerializer<IGenerator>, JsonDes
} }
else else
{ {
IGenerator generator; IGeneratorBase generator;
try try
{ {
generator = (IGenerator)generatorClass.newInstance(); generator = (IGeneratorBase)generatorClass.newInstance();
Type generatorStageType = new TypeToken<GeneratorStage>() {}.getType(); Type generatorStageType = new TypeToken<GeneratorStage>() {}.getType();
GeneratorStage generatorStage = (GeneratorStage)context.deserialize(jsonObject.get("stage"), generatorStageType); GeneratorStage generatorStage = (GeneratorStage)context.deserialize(jsonObject.get("stage"), generatorStageType);

View File

@ -19,7 +19,8 @@ import net.minecraftforge.common.BiomeManager.BiomeType;
import biomesoplenty.api.biome.BiomeOwner; import biomesoplenty.api.biome.BiomeOwner;
import biomesoplenty.api.biome.IExtendedBiome; import biomesoplenty.api.biome.IExtendedBiome;
import biomesoplenty.api.biome.generation.GenerationManager; import biomesoplenty.api.biome.generation.GenerationManager;
import biomesoplenty.api.biome.generation.IGenerator; import biomesoplenty.api.biome.generation.IGeneratorBase;
import biomesoplenty.api.biome.generation.IGeneratorController;
import biomesoplenty.common.biome.BOPBiomeManager; import biomesoplenty.common.biome.BOPBiomeManager;
import biomesoplenty.common.biome.ExtendedBiomeRegistry; import biomesoplenty.common.biome.ExtendedBiomeRegistry;
@ -31,7 +32,7 @@ import com.google.gson.GsonBuilder;
public class JsonBiome public class JsonBiome
{ {
public static final Gson serializer = new GsonBuilder().setPrettyPrinting().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).registerTypeHierarchyAdapter(IBlockState.class, new JsonBlockState()).registerTypeHierarchyAdapter(IGenerator.class, new GeneratorTypeAdaptor()).create(); public static final Gson serializer = new GsonBuilder().setPrettyPrinting().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).registerTypeHierarchyAdapter(IBlockState.class, new JsonBlockState()).registerTypeHierarchyAdapter(IGeneratorBase.class, new GeneratorTypeAdaptor()).create();
public String biomeName; public String biomeName;
public int biomeId; public int biomeId;
@ -45,7 +46,7 @@ public class JsonBiome
public int color; public int color;
public int waterColorMultiplier; public int waterColorMultiplier;
public ArrayList<JsonEntitySpawn> entities; public ArrayList<JsonEntitySpawn> entities;
public Map<String, IGenerator> decoration; public Map<String, IGeneratorController> decoration;
public static JsonBiome createFromBiomeGenBase(BiomeGenBase baseBiome) public static JsonBiome createFromBiomeGenBase(BiomeGenBase baseBiome)
{ {

View File

@ -6,7 +6,7 @@
* To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-nd/4.0/. * To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-nd/4.0/.
******************************************************************************/ ******************************************************************************/
package biomesoplenty.common.world.feature.tree; package biomesoplenty.common.world.feature;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -43,14 +43,9 @@ public class GeneratorWeighted extends CustomizableGenerator
@Override @Override
public void scatter(World world, Random random, BlockPos pos) public void scatter(World world, Random random, BlockPos pos)
{ {
for (int i = 0; i < this.amountPerChunk; i++) CustomizableWeightedGenerator generator = (CustomizableWeightedGenerator)WeightedRandom.getRandomItem(random, this.weightedEntries);
{
int x = random.nextInt(16) + 8; generator.scatter(world, random, pos, this.amountPerChunk);
int z = random.nextInt(16) + 8;
BlockPos genPos = world.getHeight(pos.add(x, 0, z));
generate(world, random, genPos);
}
} }
@Override @Override
@ -58,7 +53,7 @@ public class GeneratorWeighted extends CustomizableGenerator
{ {
CustomizableWeightedGenerator generator = (CustomizableWeightedGenerator)WeightedRandom.getRandomItem(random, this.weightedEntries); CustomizableWeightedGenerator generator = (CustomizableWeightedGenerator)WeightedRandom.getRandomItem(random, this.weightedEntries);
return generator.generate(world, random, pos); return generator.generate(world, random, pos, this.amountPerChunk);
} }
@Override @Override

View File

@ -61,7 +61,20 @@ public class GeneratorBasicTree extends CustomizableWeightedGenerator
} }
@Override @Override
public boolean generate(World world, Random random, BlockPos pos) public void scatter(World world, Random random, BlockPos pos, int amountPerChunk)
{
for (int i = 0; i < amountPerChunk; i++)
{
int x = random.nextInt(16) + 8;
int z = random.nextInt(16) + 8;
BlockPos genPos = world.getHeight(pos.add(x, 0, z));
generate(world, random, genPos, amountPerChunk);
}
}
@Override
public boolean generate(World world, Random random, BlockPos pos, int amountPerChunk)
{ {
int height = random.nextInt(this.maxHeight - this.minHeight) + this.minHeight; int height = random.nextInt(this.maxHeight - this.minHeight) + this.minHeight;
boolean hasSpace = true; boolean hasSpace = true;