Switched to using a wrapper object for weighted entries, allowing for use of generators for multiple purposes

This commit is contained in:
Adubbz 2015-04-10 09:16:36 +10:00
parent b617d62ca4
commit a541caf85b
9 changed files with 113 additions and 92 deletions

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;
package biomesoplenty.api.biome.generation;
import java.util.ArrayList;
import java.util.List;
@ -15,8 +15,6 @@ import java.util.Random;
import net.minecraft.util.BlockPos;
import net.minecraft.util.WeightedRandom;
import net.minecraft.world.World;
import biomesoplenty.api.biome.generation.CustomizableGenerator;
import biomesoplenty.api.biome.generation.CustomizableWeightedGenerator;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonObject;
@ -25,48 +23,40 @@ import com.google.gson.reflect.TypeToken;
public class GeneratorWeighted extends CustomizableGenerator
{
private int amountPerChunk;
private List<CustomizableWeightedGenerator> weightedEntries = new ArrayList<CustomizableWeightedGenerator>();
private List<GeneratorWeightedEntry> weightedEntries = new ArrayList<GeneratorWeightedEntry>();
public GeneratorWeighted() {}
public GeneratorWeighted(int amountPerChunk)
public void add(int weight, IGeneratorController entry)
{
this.amountPerChunk = amountPerChunk;
}
public void add(CustomizableWeightedGenerator entry)
{
this.weightedEntries.add(entry);
this.weightedEntries.add(new GeneratorWeightedEntry(weight, entry));
}
@Override
public void scatter(World world, Random random, BlockPos pos)
{
CustomizableWeightedGenerator generator = (CustomizableWeightedGenerator)WeightedRandom.getRandomItem(random, this.weightedEntries);
GeneratorWeightedEntry generator = (GeneratorWeightedEntry)WeightedRandom.getRandomItem(random, this.weightedEntries);
generator.scatter(world, random, pos, this.amountPerChunk);
generator.scatter(world, random, pos);
}
@Override
public boolean generate(World world, Random random, BlockPos pos)
{
CustomizableWeightedGenerator generator = (CustomizableWeightedGenerator)WeightedRandom.getRandomItem(random, this.weightedEntries);
GeneratorWeightedEntry generator = (GeneratorWeightedEntry)WeightedRandom.getRandomItem(random, this.weightedEntries);
return generator.generate(world, random, pos, this.amountPerChunk);
return generator.generate(world, random, pos);
}
@Override
public void writeToJson(JsonObject json, JsonSerializationContext context)
{
json.addProperty("amount_per_chunk", this.amountPerChunk);
json.add("entries", context.serialize(this.weightedEntries));
}
@Override
public void readFromJson(JsonObject json, JsonDeserializationContext context)
{
this.amountPerChunk = json.get("amount_per_chunk").getAsInt();
this.weightedEntries = context.deserialize(json.get("entries"), new TypeToken<List<CustomizableWeightedGenerator>>() {}.getType());
this.weightedEntries = context.deserialize(json.get("entries"), new TypeToken<List<GeneratorWeightedEntry>>() {}.getType());
}
}

View file

@ -8,28 +8,35 @@
package biomesoplenty.api.biome.generation;
import java.util.Random;
import net.minecraft.util.BlockPos;
import net.minecraft.util.WeightedRandom;
import net.minecraft.world.World;
import biomesoplenty.common.util.biome.GeneratorUtils;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonObject;
import com.google.gson.JsonSerializationContext;
public abstract class CustomizableWeightedGenerator extends WeightedRandom.Item implements IGeneratorDelegate
public final class GeneratorWeightedEntry extends WeightedRandom.Item implements IGeneratorController
{
private final String identifier;
private GeneratorStage stage;
private IGeneratorController wrappedGenerator;
protected CustomizableWeightedGenerator()
public GeneratorWeightedEntry()
{
this(-1);
this(-1, null);
}
protected CustomizableWeightedGenerator(int weight)
public GeneratorWeightedEntry(int weight, IGeneratorController wrappedGenerator)
{
super(weight);
this.identifier = GeneratorRegistry.getIdentifier((Class<? extends IGeneratorBase>)this.getClass());
this.stage = GeneratorStage.PARENT;
this.wrappedGenerator = wrappedGenerator;
if (this.identifier == null)
{
@ -37,16 +44,30 @@ public abstract class CustomizableWeightedGenerator extends WeightedRandom.Item
}
}
@Override
public void scatter(World world, Random random, BlockPos pos)
{
this.wrappedGenerator.scatter(world, random, pos);
}
@Override
public boolean generate(World world, Random random, BlockPos pos)
{
return this.wrappedGenerator.generate(world, random, pos);
}
@Override
public void writeToJson(JsonObject json, JsonSerializationContext context)
{
json.addProperty("weight", this.itemWeight);
json.add("wrapped_generator", context.serialize(this.wrappedGenerator));
}
@Override
public void readFromJson(JsonObject json, JsonDeserializationContext context)
{
this.itemWeight = json.get("weight").getAsInt();
this.wrappedGenerator = GeneratorUtils.deserializeGenerator(json, "wrapped_generator", context);
}
@Override
@ -66,4 +87,14 @@ public abstract class CustomizableWeightedGenerator extends WeightedRandom.Item
{
return this.identifier;
}
//Unnecessary, this shouldn't be used as a standard generator
@Override
public void setName(String name) {}
@Override
public String getName()
{
return null;
}
}

View file

@ -1,24 +0,0 @@
/*******************************************************************************
* 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

@ -13,6 +13,7 @@ import net.minecraft.util.BlockPos;
import net.minecraftforge.common.BiomeManager.BiomeType;
import biomesoplenty.api.biome.BOPBiome;
import biomesoplenty.api.biome.generation.GeneratorStage;
import biomesoplenty.api.biome.generation.GeneratorWeighted;
import biomesoplenty.api.block.BOPBlocks;
import biomesoplenty.api.block.BOPTreeEnums.AllTrees;
import biomesoplenty.common.block.BlockBOPFlower2;
@ -22,9 +23,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.GeneratorFlora;
import biomesoplenty.common.world.feature.tree.GeneratorBasicTree;
import biomesoplenty.common.world.feature.weighted.GeneratorFlora;
public class BiomeGenOriginValley extends BOPBiome
{
@ -40,15 +40,14 @@ public class BiomeGenOriginValley extends BOPBiome
this.topBlock = BOPBlocks.grass.getDefaultState().withProperty(BlockBOPGrass.VARIANT, BOPGrassType.ORIGIN);
GeneratorWeighted treeGenerator = new GeneratorWeighted(4);
treeGenerator.add(new GeneratorBasicTree(1, false, 5, 8, Blocks.log.getDefaultState(),
BOPBlocks.leaves_2.getDefaultState().withProperty(BlockBOPLeaves.getVariantProperty(BlockBOPLeaves2.PAGENUM), AllTrees.ORIGIN)));
GeneratorBasicTree treeGenerator = new GeneratorBasicTree(4, false, 5, 8, Blocks.log.getDefaultState(),
BOPBlocks.leaves_2.getDefaultState().withProperty(BlockBOPLeaves.getVariantProperty(BlockBOPLeaves2.PAGENUM), AllTrees.ORIGIN));
this.addGenerator("trees", GeneratorStage.TREE, treeGenerator);
GeneratorWeighted flowerGenerator = new GeneratorWeighted(4);
flowerGenerator.add(new GeneratorFlora(8, BOPBlocks.flower2.getDefaultState().withProperty(BlockBOPFlower2.VARIANT, FlowerType.ROSE)));
flowerGenerator.add(new GeneratorFlora(10, Blocks.yellow_flower.getDefaultState()));
GeneratorWeighted flowerGenerator = new GeneratorWeighted();
flowerGenerator.add(8, new GeneratorFlora(4, BOPBlocks.flower2.getDefaultState().withProperty(BlockBOPFlower2.VARIANT, FlowerType.ROSE)));
flowerGenerator.add(10, new GeneratorFlora(4, Blocks.yellow_flower.getDefaultState()));
this.addGenerator("flowers", GeneratorStage.FLOWERS, flowerGenerator);
}

View file

@ -9,12 +9,13 @@
package biomesoplenty.common.init;
import static biomesoplenty.api.biome.generation.GeneratorRegistry.registerGenerator;
import biomesoplenty.api.biome.generation.GeneratorWeighted;
import biomesoplenty.api.biome.generation.GeneratorWeightedEntry;
import biomesoplenty.common.world.feature.GeneratorFlora;
import biomesoplenty.common.world.feature.GeneratorGrass;
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.weighted.GeneratorFlora;
import biomesoplenty.common.world.feature.weighted.GeneratorGrass;
public class ModGenerators
{
@ -23,6 +24,7 @@ public class ModGenerators
registerGenerator("ore_single", GeneratorOreSingle.class);
registerGenerator("ore_cluster", GeneratorOreCluster.class);
registerGenerator("weighted", GeneratorWeighted.class);
registerGenerator("weighted_entry", GeneratorWeightedEntry.class);
registerGenerator("basic_tree", GeneratorBasicTree.class);
registerGenerator("flora", GeneratorFlora.class);
registerGenerator("grass", GeneratorGrass.class);

View file

@ -12,6 +12,9 @@ import java.util.Random;
import org.apache.commons.lang3.tuple.Pair;
import biomesoplenty.api.biome.generation.IGeneratorBase;
import biomesoplenty.api.biome.generation.IGeneratorController;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonObject;
import com.google.gson.JsonSyntaxException;
@ -69,6 +72,28 @@ public class GeneratorUtils
return state;
}
public static IGeneratorController deserializeGenerator(JsonObject json, String memberName, JsonDeserializationContext context)
{
return deserializeGeneratorOfType(json, memberName, context, IGeneratorController.class);
}
public static <T extends IGeneratorBase> T deserializeGeneratorOfType(JsonObject json, String memberName, JsonDeserializationContext context, Class<T> type)
{
T generator = context.deserialize(json.get(memberName), type);
if (generator == null)
{
throw new JsonSyntaxException("Property " + memberName + " doesn't exist");
}
if (!(generator.getClass().isAssignableFrom(type)))
{
throw new JsonSyntaxException("Property " + memberName + " is of an invalid type");
}
return generator;
}
public static boolean isBlockTreeReplacable(Block block)
{

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.weighted;
package biomesoplenty.common.world.feature;
import java.util.Random;
@ -19,25 +19,26 @@ import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState;
import net.minecraft.util.BlockPos;
import net.minecraft.world.World;
import biomesoplenty.api.biome.generation.CustomizableWeightedGenerator;
import biomesoplenty.api.biome.generation.CustomizableGenerator;
import biomesoplenty.api.biome.generation.GeneratorWeightedEntry;
import biomesoplenty.common.block.BlockDecoration;
import biomesoplenty.common.util.biome.GeneratorUtils;
public class GeneratorFlora extends CustomizableWeightedGenerator
public class GeneratorFlora extends CustomizableGenerator
{
public IBlockState state;
protected int amountPerChunk;
protected IBlockState state;
public GeneratorFlora() {}
public GeneratorFlora(int weight, IBlockState state)
public GeneratorFlora(int amountPerChunk, IBlockState state)
{
super(weight);
this.amountPerChunk = amountPerChunk;
this.state = state;
}
@Override
public void scatter(World world, Random random, BlockPos pos, int amountPerChunk)
public void scatter(World world, Random random, BlockPos pos)
{
for (int i = 0; i < amountPerChunk; i++)
{
@ -47,12 +48,12 @@ public class GeneratorFlora extends CustomizableWeightedGenerator
int y = GeneratorUtils.safeNextInt(random, world.getHeight(genPos).getY() + 32);
genPos = genPos.add(0, y, 0);
generate(world, random, genPos, amountPerChunk);
generate(world, random, genPos);
}
}
@Override
public boolean generate(World world, Random random, BlockPos pos, int amountPerChunk)
public boolean generate(World world, Random random, BlockPos pos)
{
Block block = this.state.getBlock();
@ -74,16 +75,14 @@ public class GeneratorFlora extends CustomizableWeightedGenerator
@Override
public void writeToJson(JsonObject json, JsonSerializationContext context)
{
super.writeToJson(json, context);
json.addProperty("amount_per_chunk", this.amountPerChunk);
json.add("state", context.serialize(this.state));
}
@Override
public void readFromJson(JsonObject json, JsonDeserializationContext context)
{
super.readFromJson(json, context);
this.amountPerChunk = json.get("amount_per_chunk").getAsInt();
this.state = GeneratorUtils.deserializeStateNonNull(json, "state", context);
}
}

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.weighted;
package biomesoplenty.common.world.feature;
import java.util.Random;
@ -19,9 +19,9 @@ import biomesoplenty.common.util.biome.GeneratorUtils;
public class GeneratorGrass extends GeneratorFlora
{
@Override
public void scatter(World world, Random random, BlockPos pos, int amountPerChunk)
public void scatter(World world, Random random, BlockPos pos)
{
for (int i = 0; i < amountPerChunk; i++)
for (int i = 0; i < this.amountPerChunk; i++)
{
int x = random.nextInt(16) + 8;
int z = random.nextInt(16) + 8;
@ -29,12 +29,12 @@ public class GeneratorGrass extends GeneratorFlora
int y = GeneratorUtils.safeNextInt(random, world.getHeight(genPos).getY() * 2);
genPos = genPos.add(0, y, 0);
generate(world, random, genPos, amountPerChunk);
generate(world, random, genPos);
}
}
@Override
public boolean generate(World world, Random random, BlockPos pos, int amountPerChunk)
public boolean generate(World world, Random random, BlockPos pos)
{
Block block;

View file

@ -22,15 +22,17 @@ import net.minecraft.world.World;
import org.apache.commons.lang3.tuple.Pair;
import biomesoplenty.api.biome.generation.CustomizableWeightedGenerator;
import biomesoplenty.api.biome.generation.CustomizableGenerator;
import biomesoplenty.api.biome.generation.GeneratorWeightedEntry;
import biomesoplenty.common.util.biome.GeneratorUtils;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonObject;
import com.google.gson.JsonSerializationContext;
public class GeneratorBasicTree extends CustomizableWeightedGenerator
public class GeneratorBasicTree extends CustomizableGenerator
{
private int amountPerChunk;
private boolean updateNeighbours;
private int minHeight;
private int maxHeight;
@ -40,10 +42,9 @@ public class GeneratorBasicTree extends CustomizableWeightedGenerator
public GeneratorBasicTree() {}
public GeneratorBasicTree(int weight, boolean updateNeighbours, int minHeight, int maxHeight, IBlockState log, IBlockState leaves, IBlockState vine)
public GeneratorBasicTree(int amountPerChunk, boolean updateNeighbours, int minHeight, int maxHeight, IBlockState log, IBlockState leaves, IBlockState vine)
{
super(weight);
this.amountPerChunk = amountPerChunk;
this.updateNeighbours = updateNeighbours;
Pair<Integer, Integer> heights = GeneratorUtils.validateMinMaxHeight(minHeight, maxHeight);
@ -55,13 +56,13 @@ public class GeneratorBasicTree extends CustomizableWeightedGenerator
this.vine = vine;
}
public GeneratorBasicTree(int weight, boolean updateNeighbours, int minHeight, int maxHeight, IBlockState log, IBlockState leaves)
public GeneratorBasicTree(int amountPerChunk, boolean updateNeighbours, int minHeight, int maxHeight, IBlockState log, IBlockState leaves)
{
this(weight, updateNeighbours, minHeight, maxHeight, log, leaves, null);
this(amountPerChunk, updateNeighbours, minHeight, maxHeight, log, leaves, null);
}
@Override
public void scatter(World world, Random random, BlockPos pos, int amountPerChunk)
public void scatter(World world, Random random, BlockPos pos)
{
for (int i = 0; i < amountPerChunk; i++)
{
@ -69,12 +70,12 @@ public class GeneratorBasicTree extends CustomizableWeightedGenerator
int z = random.nextInt(16) + 8;
BlockPos genPos = world.getHeight(pos.add(x, 0, z));
generate(world, random, genPos, amountPerChunk);
generate(world, random, genPos);
}
}
@Override
public boolean generate(World world, Random random, BlockPos pos, int amountPerChunk)
public boolean generate(World world, Random random, BlockPos pos)
{
int height = random.nextInt(this.maxHeight - this.minHeight) + this.minHeight;
boolean hasSpace = true;
@ -303,8 +304,7 @@ public class GeneratorBasicTree extends CustomizableWeightedGenerator
@Override
public void writeToJson(JsonObject json, JsonSerializationContext context)
{
super.writeToJson(json, context);
json.addProperty("amount_per_chunk", this.amountPerChunk);
json.addProperty("update_neighbours", this.updateNeighbours);
json.addProperty("min_height", this.minHeight);
json.addProperty("max_height", this.maxHeight);
@ -316,8 +316,7 @@ public class GeneratorBasicTree extends CustomizableWeightedGenerator
@Override
public void readFromJson(JsonObject json, JsonDeserializationContext context)
{
super.readFromJson(json, context);
this.amountPerChunk = json.get("amount_per_chunk").getAsInt();
this.updateNeighbours = json.get("update_neighbours").getAsBoolean();
Pair<Integer, Integer> heights = GeneratorUtils.validateMinMaxHeight(minHeight, maxHeight);