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 biomesoplenty.api.biome.generation.GenerationManager;
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
{
@ -41,7 +41,7 @@ public class BOPBiome extends BiomeGenBase implements IExtendedBiome
}
@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);
}

View file

@ -10,15 +10,15 @@ package biomesoplenty.api.biome;
import java.util.Map;
import net.minecraftforge.common.BiomeManager.BiomeType;
import biomesoplenty.api.biome.generation.GenerationManager;
import biomesoplenty.api.biome.generation.GeneratorStage;
import biomesoplenty.api.biome.generation.IGenerator;
import net.minecraftforge.common.BiomeManager.BiomeType;
import biomesoplenty.api.biome.generation.IGeneratorController;
public interface IExtendedBiome
{
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 Map<BiomeType, Integer> getWeightMap();
public void clearWeights();

View file

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

View file

@ -14,10 +14,9 @@ import net.minecraft.util.BlockPos;
import net.minecraft.util.WeightedRandom;
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 String name;
private GeneratorStage stage;
protected CustomizableWeightedGenerator()
@ -29,7 +28,7 @@ public abstract class CustomizableWeightedGenerator extends WeightedRandom.Item
{
super(weight);
this.identifier = GeneratorRegistry.getIdentifier((Class<? extends IGenerator>)this.getClass());
this.identifier = GeneratorRegistry.getIdentifier((Class<? extends IGeneratorBase>)this.getClass());
this.stage = GeneratorStage.PARENT;
if (this.identifier == null)
@ -38,28 +37,12 @@ public abstract class CustomizableWeightedGenerator extends WeightedRandom.Item
}
}
//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
public void setStage(GeneratorStage stage)
{
this.stage = stage;
}
@Override
public String getName()
{
return this.name;
}
@Override
public GeneratorStage getStage()
{

View file

@ -20,9 +20,9 @@ import com.google.common.collect.Table;
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))
{
@ -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);
Collection<IGenerator> result = columnMap == null ? null : columnMap.values();
Map<String, IGeneratorController> columnMap = this.generatorTable.rowMap().get(stage);
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);
}
@ -57,14 +57,14 @@ public class GenerationManager
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();
IGenerator generator = entry.getValue();
IGeneratorController generator = entry.getValue();
generator.setName(name);
result.put(generator.getStage(), generator.getName(), generator);

View file

@ -13,19 +13,19 @@ import com.google.common.collect.HashBiMap;
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);
}
public static String getIdentifier(Class<? extends IGenerator> generatorClass)
public static String getIdentifier(Class<? extends IGeneratorBase> 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);
}

View file

@ -17,20 +17,12 @@ import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonObject;
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);
/**The identifier for this generator should be consistent across all instances of the same type*/
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 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.generation.GenerationManager;
import biomesoplenty.api.biome.generation.GeneratorStage;
import biomesoplenty.api.biome.generation.IGenerator;
import biomesoplenty.api.biome.generation.IGeneratorController;
public class ExtendedBiomeRegistry
{
@ -65,7 +65,7 @@ public class ExtendedBiomeRegistry
}
@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);
}

View file

@ -20,8 +20,8 @@ import biomesoplenty.common.block.BlockBOPLeaves;
import biomesoplenty.common.block.BlockBOPLeaves2;
import biomesoplenty.common.block.BlockBOPGrass.BOPGrassType;
import biomesoplenty.common.config.MiscConfigurationHandler;
import biomesoplenty.common.world.feature.GeneratorWeighted;
import biomesoplenty.common.world.feature.tree.GeneratorBasicTree;
import biomesoplenty.common.world.feature.tree.GeneratorWeighted;
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.generation.GenerationManager;
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;
public class DecorateBiomeEventHandler
@ -58,7 +59,7 @@ public class DecorateBiomeEventHandler
{
GenerationManager generationManager = extendedBiome.getGenerationManager();
for (IGenerator generator : generationManager.getGeneratorsForStage(stage))
for (IGeneratorController generator : generationManager.getGeneratorsForStage(stage))
{
generator.scatter(world, random, pos);
}

View file

@ -11,8 +11,8 @@ package biomesoplenty.common.init;
import static biomesoplenty.api.biome.generation.GeneratorRegistry.registerGenerator;
import biomesoplenty.common.world.feature.GeneratorOreCluster;
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.GeneratorWeighted;
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.GeneratorStage;
import biomesoplenty.api.biome.generation.IGenerator;
import biomesoplenty.api.biome.generation.IGeneratorBase;
import com.google.common.reflect.TypeToken;
import com.google.gson.JsonDeserializationContext;
@ -24,10 +24,10 @@ import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import com.google.gson.JsonSyntaxException;
public class GeneratorTypeAdaptor implements JsonSerializer<IGenerator>, JsonDeserializer<IGenerator>
public class GeneratorTypeAdaptor implements JsonSerializer<IGeneratorBase>, JsonDeserializer<IGeneratorBase>
{
@Override
public JsonElement serialize(IGenerator src, Type typeOfSrc, JsonSerializationContext context)
public JsonElement serialize(IGeneratorBase src, Type typeOfSrc, JsonSerializationContext context)
{
JsonObject jsonObject = new JsonObject();
src.writeToJson(jsonObject, context);
@ -39,14 +39,14 @@ public class GeneratorTypeAdaptor implements JsonSerializer<IGenerator>, JsonDes
}
@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();
if (jsonObject.has("generator"))
{
String generatorIdentifier = jsonObject.get("generator").getAsString();
Class<? extends IGenerator> generatorClass = GeneratorRegistry.getGeneratorClass(generatorIdentifier);
Class<? extends IGeneratorBase> generatorClass = GeneratorRegistry.getGeneratorClass(generatorIdentifier);
if (generatorClass == null)
{
@ -54,10 +54,10 @@ public class GeneratorTypeAdaptor implements JsonSerializer<IGenerator>, JsonDes
}
else
{
IGenerator generator;
IGeneratorBase generator;
try
{
generator = (IGenerator)generatorClass.newInstance();
generator = (IGeneratorBase)generatorClass.newInstance();
Type generatorStageType = new TypeToken<GeneratorStage>() {}.getType();
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.IExtendedBiome;
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.ExtendedBiomeRegistry;
@ -31,7 +32,7 @@ import com.google.gson.GsonBuilder;
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 int biomeId;
@ -45,7 +46,7 @@ public class JsonBiome
public int color;
public int waterColorMultiplier;
public ArrayList<JsonEntitySpawn> entities;
public Map<String, IGenerator> decoration;
public Map<String, IGeneratorController> decoration;
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/.
******************************************************************************/
package biomesoplenty.common.world.feature.tree;
package biomesoplenty.common.world.feature;
import java.util.ArrayList;
import java.util.List;
@ -43,14 +43,9 @@ public class GeneratorWeighted extends CustomizableGenerator
@Override
public void scatter(World world, Random random, BlockPos pos)
{
for (int i = 0; i < this.amountPerChunk; i++)
{
int x = random.nextInt(16) + 8;
int z = random.nextInt(16) + 8;
BlockPos genPos = world.getHeight(pos.add(x, 0, z));
CustomizableWeightedGenerator generator = (CustomizableWeightedGenerator)WeightedRandom.getRandomItem(random, this.weightedEntries);
generate(world, random, genPos);
}
generator.scatter(world, random, pos, this.amountPerChunk);
}
@Override
@ -58,7 +53,7 @@ public class GeneratorWeighted extends CustomizableGenerator
{
CustomizableWeightedGenerator generator = (CustomizableWeightedGenerator)WeightedRandom.getRandomItem(random, this.weightedEntries);
return generator.generate(world, random, pos);
return generator.generate(world, random, pos, this.amountPerChunk);
}
@Override

View file

@ -61,7 +61,20 @@ public class GeneratorBasicTree extends CustomizableWeightedGenerator
}
@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;
boolean hasSpace = true;