Added configurable emerald generation to the alps, removed unnecessary generics for IGenerator

This commit is contained in:
Adubbz 2015-04-07 09:18:35 +10:00
parent ec4bac4004
commit 981ccdc2ca
17 changed files with 276 additions and 39 deletions

View File

@ -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, IGenerator generator)
{
this.generationManager.addGenerator(name, stage, generator);
}

View File

@ -18,7 +18,7 @@ import net.minecraftforge.common.BiomeManager.BiomeType;
public interface IExtendedBiome
{
public BiomeOwner getBiomeOwner();
public void addGenerator(String name, GeneratorStage stage, IGenerator<?> generator);
public void addGenerator(String name, GeneratorStage stage, IGenerator 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<T> implements IGenerator<T>
public abstract class CustomizableGenerator implements IGenerator
{
private final String identifier;
private String name;
@ -16,7 +16,7 @@ public abstract class CustomizableGenerator<T> implements IGenerator<T>
protected CustomizableGenerator()
{
this.identifier = GeneratorRegistry.getIdentifier((Class<? extends IGenerator<?>>)this.getClass());
this.identifier = GeneratorRegistry.getIdentifier((Class<? extends IGenerator>)this.getClass());
if (this.identifier == null)
{

View File

@ -14,13 +14,15 @@ import java.util.Map;
import java.util.Map.Entry;
import com.google.common.collect.HashBasedTable;
import com.google.common.collect.ImmutableCollection;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Table;
public class GenerationManager
{
private Table<GeneratorStage, String, IGenerator<?>> generatorTable = HashBasedTable.create();
private Table<GeneratorStage, String, IGenerator> generatorTable = HashBasedTable.create();
public void addGenerator(String name, GeneratorStage stage, IGenerator<?> generator)
public void addGenerator(String name, GeneratorStage stage, IGenerator generator)
{
if (!this.generatorTable.containsColumn(name))
{
@ -35,16 +37,19 @@ public class GenerationManager
}
}
public Collection<IGenerator<?>> getGeneratorsForStage(GeneratorStage stage)
public ImmutableCollection<IGenerator> getGeneratorsForStage(GeneratorStage stage)
{
return this.generatorTable.rowMap().get(stage).values();
Map<String, IGenerator> columnMap = this.generatorTable.rowMap().get(stage);
Collection<IGenerator> result = columnMap == null ? null : columnMap.values();
return result == null ? ImmutableList.<IGenerator>of() : ImmutableList.<IGenerator>copyOf(result);
}
public Map<String, IGenerator<?>> createGeneratorMap()
public Map<String, IGenerator> createGeneratorMap()
{
Map<String, IGenerator<?>> result = new HashMap<String, IGenerator<?>>();
Map<String, IGenerator> result = new HashMap<String, IGenerator>();
for (IGenerator<?> generator : this.generatorTable.values())
for (IGenerator generator : this.generatorTable.values())
{
result.put(generator.getName(), generator);
}
@ -52,14 +57,14 @@ public class GenerationManager
return result;
}
public void createGeneratorTable(Map<String, IGenerator<?>> generators)
public void createGeneratorTable(Map<String, IGenerator> generators)
{
Table<GeneratorStage, String, IGenerator<?>> result = HashBasedTable.create();
Table<GeneratorStage, String, IGenerator> result = HashBasedTable.create();
for (Entry<String, IGenerator<?>> entry : generators.entrySet())
for (Entry<String, IGenerator> entry : generators.entrySet())
{
String name = entry.getKey();
IGenerator<?> generator = entry.getValue();
IGenerator 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 IGenerator>> generatorClasses = HashBiMap.create();
public static void registerGenerator(String identifier, Class<? extends IGenerator<?>> generatorClass)
public static void registerGenerator(String identifier, Class<? extends IGenerator> generatorClass)
{
generatorClasses.put(identifier, generatorClass);
}
public static String getIdentifier(Class<? extends IGenerator<?>> generatorClass)
public static String getIdentifier(Class<? extends IGenerator> generatorClass)
{
return generatorClasses.inverse().get(generatorClass);
}
public static Class<? extends IGenerator<?>> getGeneratorClass(String identifier)
public static Class<? extends IGenerator> getGeneratorClass(String identifier)
{
return generatorClasses.get(identifier);
}

View File

@ -17,8 +17,9 @@ import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonObject;
import com.google.gson.JsonSerializationContext;
public interface IGenerator<T>
public interface IGenerator
{
public void scatter(World world, Random random, BlockPos pos);
public void generate(World world, Random random, BlockPos pos);
public void setName(String name);

View File

@ -65,7 +65,7 @@ public class ExtendedBiomeRegistry
}
@Override
public void addGenerator(String name, GeneratorStage stage, IGenerator<?> generator)
public void addGenerator(String name, GeneratorStage stage, IGenerator generator)
{
this.generationManager.addGenerator(name, stage, generator);
}

View File

@ -11,6 +11,8 @@ package biomesoplenty.common.biome.overworld;
import net.minecraft.init.Blocks;
import net.minecraftforge.common.BiomeManager.BiomeType;
import biomesoplenty.api.biome.BOPBiome;
import biomesoplenty.api.biome.generation.GeneratorStage;
import biomesoplenty.common.world.feature.GeneratorOreSingle;
public class BiomeGenAlps extends BOPBiome
{
@ -23,9 +25,11 @@ public class BiomeGenAlps extends BOPBiome
this.setEnableSnow();
this.setTemperatureRainfall(0.0F, 0.5F);
this.addWeight(BiomeType.ICY, 5);
this.topBlock = Blocks.snow.getDefaultState();
this.fillerBlock = Blocks.snow.getDefaultState();
this.addWeight(BiomeType.ICY, 5);
this.addGenerator("emeralds", GeneratorStage.SAND, new GeneratorOreSingle(Blocks.emerald_ore.getDefaultState(), 12, 4, 32));
}
}

View File

@ -40,24 +40,23 @@ public class DecorateBiomeEventHandler
}
@SubscribeEvent
public void onPostBiomeDecorate(DecorateBiomeEvent.Pre event)
public void onPostBiomeDecorate(DecorateBiomeEvent.Post event)
{
runGeneratorStage(event.world, event.rand, event.pos, GeneratorStage.POST);
}
private static void runGeneratorStage(World world, Random random, BlockPos pos, GeneratorStage stage)
{
pos.add(16, 0, 16);
BiomeGenBase biome = world.getBiomeGenForCoords(pos);
BiomeGenBase biome = world.getBiomeGenForCoords(pos.add(16, 0, 16));
IExtendedBiome extendedBiome = ExtendedBiomeRegistry.getExtension(biome);
if (extendedBiome != null)
{
GenerationManager generationManager = extendedBiome.getGenerationManager();
for (IGenerator<?> generator : generationManager.getGeneratorsForStage(stage))
for (IGenerator generator : generationManager.getGeneratorsForStage(stage))
{
generator.generate(world, random, pos);
generator.scatter(world, random, pos);
}
}
}

View File

@ -8,10 +8,15 @@
package biomesoplenty.common.init;
import static biomesoplenty.api.biome.generation.GeneratorRegistry.registerGenerator;
import biomesoplenty.common.world.feature.GeneratorOreCluster;
import biomesoplenty.common.world.feature.GeneratorOreSingle;
public class ModGenerators
{
public static void init()
{
registerGenerator("ore_single", GeneratorOreSingle.class);
registerGenerator("ore_cluster", GeneratorOreCluster.class);
}
}

View File

@ -16,7 +16,9 @@ public class ModHandlers
{
public static void init()
{
MinecraftForge.TERRAIN_GEN_BUS.register(new DecorateBiomeEventHandler());
DecorateBiomeEventHandler decorateBiomeHandler = new DecorateBiomeEventHandler();
MinecraftForge.EVENT_BUS.register(decorateBiomeHandler);
MinecraftForge.TERRAIN_GEN_BUS.register(decorateBiomeHandler);
MinecraftForge.EVENT_BUS.register(new GuiEventHandler());
}
}

View File

@ -24,14 +24,14 @@ 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<IGenerator>, JsonDeserializer<IGenerator>
{
@Override
public JsonElement serialize(IGenerator<?> src, Type typeOfSrc, JsonSerializationContext context)
public JsonElement serialize(IGenerator src, Type typeOfSrc, JsonSerializationContext context)
{
JsonObject jsonObject = new JsonObject();
src.writeToJson(jsonObject, context);
jsonObject.addProperty("generator", src.getIdentifier());
jsonObject.add("stage", context.serialize(src.getStage()));
@ -39,14 +39,14 @@ public class GeneratorTypeAdaptor implements JsonSerializer<IGenerator<?>>, Json
}
@Override
public IGenerator<?> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException
public IGenerator 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 IGenerator> generatorClass = GeneratorRegistry.getGeneratorClass(generatorIdentifier);
if (generatorClass == null)
{
@ -54,10 +54,10 @@ public class GeneratorTypeAdaptor implements JsonSerializer<IGenerator<?>>, Json
}
else
{
IGenerator<?> generator;
IGenerator generator;
try
{
generator = (IGenerator<?>)generatorClass.newInstance();
generator = (IGenerator)generatorClass.newInstance();
Type generatorStageType = new TypeToken<GeneratorStage>() {}.getType();
String generatorStageName = jsonObject.get("stage").getAsString();

View File

@ -23,13 +23,14 @@ import biomesoplenty.api.biome.generation.IGenerator;
import biomesoplenty.common.biome.BOPBiomeManager;
import biomesoplenty.common.biome.ExtendedBiomeRegistry;
import com.google.common.reflect.TypeToken;
import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
public class JsonBiome
{
public static final Gson serializer = new GsonBuilder().setPrettyPrinting().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).registerTypeAdapter(IBlockState.class, new JsonBlockState()).registerTypeAdapter(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(IGenerator.class, new GeneratorTypeAdaptor()).create();
public String biomeName;
public int biomeId;
@ -43,7 +44,7 @@ public class JsonBiome
public int color;
public int waterColorMultiplier;
public ArrayList<JsonEntitySpawn> entities;
public Map<String, IGenerator<?>> decoration;
public Map<String, IGenerator> decoration;
public static JsonBiome createFromBiomeGenBase(BiomeGenBase baseBiome)
{

View File

@ -0,0 +1,91 @@
/*******************************************************************************
* 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.common.world.feature;
import java.util.Random;
import net.minecraft.util.BlockPos;
import net.minecraft.world.World;
import biomesoplenty.api.biome.generation.CustomizableGenerator;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonObject;
import com.google.gson.JsonSerializationContext;
public abstract class GeneratorOreBase extends CustomizableGenerator
{
protected int amountPerChunk;
protected int minHeight;
protected int maxHeight;
protected GeneratorOreBase() {}
protected GeneratorOreBase(int amountPerChunk, int minHeight, int maxHeight)
{
this.amountPerChunk = amountPerChunk;
this.minHeight = minHeight;
this.maxHeight = maxHeight;
}
@Override
public void scatter(World world, Random random, BlockPos pos)
{
for (int i = 0; i < amountPerChunk; i++)
{
BlockPos generatedPos = pos.add(random.nextInt(16), random.nextInt(maxHeight - minHeight) + minHeight, random.nextInt(16));
generate(world, random, generatedPos);
}
}
@Override
public void writeToJson(JsonObject json, JsonSerializationContext context)
{
json.addProperty("amount_per_chunk", this.amountPerChunk);
json.addProperty("min_height", this.minHeight);
json.addProperty("max_height", this.maxHeight);
}
@Override
public void readFromJson(JsonObject json, JsonDeserializationContext context)
{
this.amountPerChunk = json.get("amount_per_chunk").getAsInt();
int minHeight = json.get("min_height").getAsInt();
int maxHeight = json.get("max_height").getAsInt();
validateMinMaxHeight(minHeight, maxHeight);
}
protected void validateMinMaxHeight(int minHeight, int maxHeight)
{
if (maxHeight < minHeight)
{
//Swap min and max height so that max is higher than min
int prevMinHeight = minHeight;
minHeight = maxHeight;
maxHeight = prevMinHeight;
}
else if (maxHeight == minHeight)
{
if (minHeight < 255)
{
//Increase max height to be higher than min height
++maxHeight;
}
else
{
//Decrease min height so that max is higher
--minHeight;
}
}
this.minHeight = minHeight;
this.maxHeight = maxHeight;
}
}

View File

@ -0,0 +1,60 @@
/*******************************************************************************
* 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.common.world.feature;
import java.util.Random;
import net.minecraft.block.state.IBlockState;
import net.minecraft.util.BlockPos;
import net.minecraft.world.World;
import net.minecraft.world.gen.feature.WorldGenMinable;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonObject;
import com.google.gson.JsonSerializationContext;
public class GeneratorOreCluster extends GeneratorOreBase
{
private WorldGenMinable generator;
public GeneratorOreCluster() {}
public GeneratorOreCluster(IBlockState state, int amountPerChunk, int clusterSize, int minHeight, int maxHeight)
{
super(amountPerChunk, minHeight, maxHeight);
this.generator = new WorldGenMinable(state, clusterSize);
}
@Override
public void generate(World world, Random random, BlockPos pos)
{
this.generator.generate(world, random, pos);
}
@Override
public void writeToJson(JsonObject json, JsonSerializationContext context)
{
super.writeToJson(json, context);
json.add("state", context.serialize(this.generator.oreBlock));
json.addProperty("cluster_size", this.generator.numberOfBlocks);
}
@Override
public void readFromJson(JsonObject json, JsonDeserializationContext context)
{
super.readFromJson(json, context);
IBlockState state = context.deserialize(json.get("state"), IBlockState.class);
int clusterSize = json.get("cluster_size").getAsInt();
this.generator = new WorldGenMinable(state, clusterSize);
}
}

View File

@ -0,0 +1,66 @@
/*******************************************************************************
* 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.common.world.feature;
import java.util.Random;
import net.minecraft.block.state.IBlockState;
import net.minecraft.block.state.pattern.BlockHelper;
import net.minecraft.init.Blocks;
import net.minecraft.util.BlockPos;
import net.minecraft.world.World;
import net.minecraft.world.gen.feature.WorldGenMinable;
import biomesoplenty.api.biome.generation.CustomizableGenerator;
import com.google.common.base.Predicate;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonObject;
import com.google.gson.JsonSerializationContext;
public class GeneratorOreSingle extends GeneratorOreBase
{
private IBlockState state;
private Predicate replace;
public GeneratorOreSingle() {}
public GeneratorOreSingle(IBlockState state, int amountPerChunk, int minHeight, int maxHeight)
{
super(amountPerChunk, minHeight, maxHeight);
this.state = state;
this.replace = BlockHelper.forBlock(Blocks.stone);
}
@Override
public void generate(World world, Random random, BlockPos pos)
{
if (world.getBlockState(pos).getBlock().isReplaceableOreGen(world, pos, this.replace))
{
world.setBlockState(pos, this.state, 2);
}
}
@Override
public void writeToJson(JsonObject json, JsonSerializationContext context)
{
super.writeToJson(json, context);
json.add("state", context.serialize(this.state));
}
@Override
public void readFromJson(JsonObject json, JsonDeserializationContext context)
{
super.readFromJson(json, context);
this.state = context.deserialize(json.get("state"), IBlockState.class);
this.replace = BlockHelper.forBlock(Blocks.stone);
}
}

View File

@ -4,6 +4,9 @@ public net.minecraft.client.gui.GuiCreateWorld field_146331_K #field_146331_K
public-f net.minecraft.world.biome.BiomeGenBase field_76756_M #biomeID
public net.minecraft.world.WorldType func_151358_j()Lnet/minecraft/world/WorldType; #setNotificationData
protected net.minecraft.world.biome.WorldChunkManager field_76944_d #genBiomes
protected net.minecraft.world.biome.WorldChunkManager field_76945_e #biomeIndexLayer
protected net.minecraft.world.biome.WorldChunkManager field_180301_f #field_180301_f
protected net.minecraft.world.biome.WorldChunkManager field_180301_f #field_180301_f
public-f net.minecraft.world.gen.feature.WorldGenMinable *