Overhauled the config system for decoration, still a few things to do before it can be put into use

This commit is contained in:
Adubbz 2015-04-06 13:46:10 +10:00
parent b586a2862c
commit cacfe8d374
15 changed files with 380 additions and 152 deletions

View File

@ -11,9 +11,9 @@ package biomesoplenty.api.biome;
import java.util.HashMap;
import java.util.Map;
import biomesoplenty.common.biome.ExtendedBiomeRegistry.GenerationManager;
import net.minecraft.world.biome.BiomeGenBase;
import net.minecraftforge.common.BiomeManager.BiomeType;
import biomesoplenty.api.biome.generation.GenerationManager;
public class BOPBiome extends BiomeGenBase implements IExtendedBiome
{

View File

@ -10,8 +10,8 @@ package biomesoplenty.api.biome;
import java.util.Map;
import biomesoplenty.api.biome.generation.GenerationManager;
import net.minecraftforge.common.BiomeManager.BiomeType;
import biomesoplenty.common.biome.ExtendedBiomeRegistry.GenerationManager;
public interface IExtendedBiome
{

View File

@ -1,25 +0,0 @@
/*******************************************************************************
* Copyright 2014-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;
import java.util.Random;
import net.minecraft.util.BlockPos;
import net.minecraft.world.World;
import com.google.gson.JsonElement;
public interface IGenerator<T>
{
public void generate(World world, Random random, BlockPos pos);
public JsonElement serialize(IGenerator<T> src);
public IGenerator<T> deserialize(JsonElement json);
}

View File

@ -0,0 +1,56 @@
/*******************************************************************************
* 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;
public abstract class CustomizableGenerator<T> implements IGenerator<T>
{
private final String identifier;
private String name;
private GeneratorStage stage;
protected CustomizableGenerator()
{
this.identifier = GeneratorRegistry.getIdentifier((Class<? extends IGenerator<?>>)this.getClass());
if (this.identifier == null)
{
throw new RuntimeException("The identifier for " + this.getClass().getCanonicalName() + " cannot be null!");
}
}
@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()
{
return this.stage;
}
@Override
public final String getIdentifier()
{
return this.identifier;
}
}

View File

@ -0,0 +1,64 @@
/*******************************************************************************
* 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.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import com.google.common.collect.HashBasedTable;
import com.google.common.collect.Table;
public class GenerationManager
{
private Table<GeneratorStage, String, IGenerator<?>> generatorTable = HashBasedTable.create();
public void addGenerator(String name, GeneratorStage stage, IGenerator<?> generator)
{
if (!generatorTable.containsColumn(name))
{
generator.setName(name);
generator.setStage(stage);
this.generatorTable.put(stage, name, generator);
}
else
{
throw new RuntimeException("A generator with name " + name + " already exists!");
}
}
public Map<String, IGenerator<?>> createGeneratorMap()
{
Map<String, IGenerator<?>> result = new HashMap<String, IGenerator<?>>();
for (IGenerator<?> generator : this.generatorTable.values())
{
result.put(generator.getName(), generator);
}
return result;
}
public void createGeneratorTable(Map<String, IGenerator<?>> generators)
{
Table<GeneratorStage, String, IGenerator<?>> result = HashBasedTable.create();
for (Entry<String, IGenerator<?>> entry : generators.entrySet())
{
String name = entry.getKey();
IGenerator<?> generator = entry.getValue();
generator.setName(name);
result.put(generator.getStage(), generator.getName(), generator);
}
this.generatorTable = result;
}
}

View File

@ -0,0 +1,39 @@
/*******************************************************************************
* 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.HashMap;
import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
public class GeneratorRegistry
{
private static BiMap<String, Class<? extends IGenerator<?>>> generatorClasses = HashBiMap.create();
public static void registerGenerator(String identifier, Class<? extends IGenerator<?>> generatorClass)
{
generatorClasses.put(identifier, generatorClass);
}
public static String getIdentifier(Class<? extends IGenerator<?>> generatorClass)
{
return generatorClasses.inverse().get(generatorClass);
}
public static Class<? extends IGenerator<?>> getGeneratorClass(String identifier)
{
return generatorClasses.get(identifier);
}
public static boolean generatorExists(String identifier)
{
return generatorClasses.containsValue(identifier);
}
}

View File

@ -0,0 +1,49 @@
/*******************************************************************************
* 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 com.google.gson.annotations.SerializedName;
public enum GeneratorStage
{
@SerializedName("pre")
PRE,
@SerializedName("big_shroom")
BIG_SHROOM,
@SerializedName("cactus")
CACTUS,
@SerializedName("clay")
CLAY,
@SerializedName("dead_bush")
DEAD_BUSH,
@SerializedName("lilypad")
LILYPAD,
@SerializedName("flowers")
FLOWERS,
@SerializedName("grass")
GRASS,
@SerializedName("lake_water")
LAKE_WATER,
@SerializedName("lake_lava")
LAKE_LAVA,
@SerializedName("pumpkin")
PUMPKIN,
@SerializedName("reed")
REED,
@SerializedName("sand")
SAND,
@SerializedName("sand_pass_2")
SAND_PASS2,
@SerializedName("shroom")
SHROOM,
@SerializedName("tree")
TREE,
@SerializedName("post")
POST;
}

View File

@ -0,0 +1,38 @@
/*******************************************************************************
* Copyright 2014-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;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonSerializationContext;
public interface IGenerator<T>
{
public void 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);
public void readFromJson(JsonObject json, JsonDeserializationContext context);
}

View File

@ -8,16 +8,14 @@
package biomesoplenty.common.biome;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import biomesoplenty.api.biome.BiomeOwner;
import biomesoplenty.api.biome.IExtendedBiome;
import biomesoplenty.api.biome.IGenerator;
import net.minecraft.world.biome.BiomeGenBase;
import net.minecraftforge.common.BiomeManager.BiomeType;
import net.minecraftforge.event.terraingen.DecorateBiomeEvent.Decorate;
import biomesoplenty.api.biome.BiomeOwner;
import biomesoplenty.api.biome.IExtendedBiome;
import biomesoplenty.api.biome.generation.GenerationManager;
public class ExtendedBiomeRegistry
{
@ -88,37 +86,4 @@ public class ExtendedBiomeRegistry
this.weightMap.clear();
}
}
public static class GenerationManager
{
private Map<String, IGenerator<?>> generatorMap = new HashMap();
//TODO: Come up with a better sequencing system
private Map<String, Decorate.EventType> generatorSequenceMap = new HashMap();;
public void addGenerator(String key, IGenerator<?> generator, Decorate.EventType nextFeature)
{
this.generatorMap.put(key, generator);
this.generatorSequenceMap.put(key, nextFeature);
}
public void addGenerator(String key, IGenerator<?> generator)
{
this.addGenerator(key, generator, Decorate.EventType.CUSTOM);
}
public void configureGenerators(Map<String, IGenerator<?>> generatorMap)
{
this.generatorMap.putAll(generatorMap);
}
public Map<String, IGenerator<?>> getGeneratorMap()
{
return Collections.unmodifiableMap(this.generatorMap);
}
public Decorate.EventType getGeneratorStage(String key)
{
return generatorSequenceMap.get(key);
}
}
}

View File

@ -15,18 +15,11 @@ import java.util.Map;
import java.util.Map.Entry;
import net.minecraft.world.biome.BiomeGenBase;
import net.minecraftforge.common.BiomeManager.BiomeEntry;
import net.minecraftforge.common.BiomeManager.BiomeType;
import org.apache.commons.io.FileUtils;
import biomesoplenty.api.biome.BiomeOwner;
import biomesoplenty.api.biome.IExtendedBiome;
import biomesoplenty.common.biome.BOPBiomeManager;
import biomesoplenty.common.biome.ExtendedBiomeRegistry;
import biomesoplenty.common.biome.ExtendedBiomeRegistry.GenerationManager;
import biomesoplenty.common.util.config.JsonBiome;
import biomesoplenty.common.util.config.JsonEntitySpawn;
import biomesoplenty.core.BiomesOPlenty;
import com.google.gson.JsonSyntaxException;
@ -53,11 +46,11 @@ public class BiomeConfigurationHandler
{
JsonBiome jsonBiome = JsonBiome.serializer.fromJson(FileUtils.readFileToString(configFile), JsonBiome.class);
configureBiomeWithJson(biome, jsonBiome);
JsonBiome.configureBiomeWithJson(biome, jsonBiome);
}
catch (JsonSyntaxException e)
{
e.printStackTrace();
BiomesOPlenty.logger.error("An error occurred reading " + configFile.getName(), e);
}
catch (IOException e)
{
@ -97,61 +90,6 @@ public class BiomeConfigurationHandler
}*/
}
private static void configureBiomeWithJson(BiomeGenBase biome, JsonBiome jsonBiome)
{
IExtendedBiome extendedBiome = ExtendedBiomeRegistry.getExtension(biome);
if (extendedBiome != null)
{
GenerationManager generationManager = extendedBiome.getGenerationManager();
if (extendedBiome.getBiomeOwner() == BiomeOwner.BIOMESOPLENTY)
{
if (jsonBiome.biomeId != -1)
{
biome.biomeID = jsonBiome.biomeId;
BiomeGenBase.getBiomeGenArray()[jsonBiome.biomeId] = biome;
}
else
{
biome.biomeID = -1;
}
}
Map<BiomeType, Integer> weightMap = jsonBiome.weights;
//Removes the default weights set by us as they are about to be set from the config file
extendedBiome.clearWeights();
//TODO: Add a system for making Vanilla biome weights configurable. This won't necessarily be in this class, however it's worth noting.
for (Entry<BiomeType, Integer> entry : weightMap.entrySet())
{
if (entry != null)
{
BiomeType biomeType = entry.getKey();
int weight = entry.getValue();
//Updates the biome's weights to be in line with the config file
extendedBiome.addWeight(biomeType, weight);
BOPBiomeManager.addBiome(biomeType, new BiomeEntry(biome, weight));
}
}
biome.biomeName = jsonBiome.biomeName;
biome.topBlock = jsonBiome.topBlock;
biome.fillerBlock = jsonBiome.fillerBlock;
biome.setHeight(new BiomeGenBase.Height(jsonBiome.rootHeight, jsonBiome.rootVariation));
biome.temperature = jsonBiome.temperature;
biome.rainfall = jsonBiome.rainfall;
// TODO: Reflect and modify enableRain and enableSnow
biome.color = jsonBiome.color;
biome.waterColorMultiplier = jsonBiome.waterColorMultiplier;
JsonEntitySpawn.addBiomeEntitySpawns(biome, jsonBiome);
generationManager.configureGenerators(jsonBiome.decoration);
}
}
public static Map<BiomeGenBase, String> getConfigFileMap()
{
return BiomeConfigurationHandler.configFileMap;

View File

@ -16,13 +16,12 @@ import net.minecraft.world.biome.BiomeGenBase;
import net.minecraftforge.event.terraingen.DecorateBiomeEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import biomesoplenty.api.biome.IExtendedBiome;
import biomesoplenty.api.biome.IGenerator;
import biomesoplenty.api.biome.generation.IGenerator;
import biomesoplenty.common.biome.ExtendedBiomeRegistry;
import biomesoplenty.common.biome.ExtendedBiomeRegistry.GenerationManager;
public class DecorateBiomeEventHandler
{
@SubscribeEvent
/*TODO: @SubscribeEvent
public void onBiomeDecorate(DecorateBiomeEvent.Decorate event)
{
World world = event.world;
@ -45,5 +44,5 @@ public class DecorateBiomeEventHandler
}
}
}
}
}*/
}

View File

@ -0,0 +1,17 @@
/*******************************************************************************
* 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.init;
public class ModGenerators
{
public static void init()
{
}
}

View File

@ -10,8 +10,11 @@ package biomesoplenty.common.util.config;
import java.lang.reflect.Type;
import biomesoplenty.api.biome.IGenerator;
import biomesoplenty.api.biome.generation.GeneratorRegistry;
import biomesoplenty.api.biome.generation.GeneratorStage;
import biomesoplenty.api.biome.generation.IGenerator;
import com.google.common.reflect.TypeToken;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
@ -19,15 +22,18 @@ import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import com.google.gson.JsonSyntaxException;
public class GeneratorTypeAdaptor implements JsonSerializer<IGenerator<?>>, JsonDeserializer<IGenerator<?>>
{
@Override
public JsonElement serialize(IGenerator<?> src, Type typeOfSrc, JsonSerializationContext context)
{
JsonObject jsonObject = src.serialize((IGenerator) src).getAsJsonObject();
JsonObject jsonObject = new JsonObject();
src.writeToJson(jsonObject, context);
jsonObject.addProperty("class", src.getClass().getCanonicalName());
jsonObject.addProperty("generator", src.getIdentifier());
jsonObject.add("stage", context.serialize(src.getStage()));
return jsonObject;
}
@ -37,22 +43,46 @@ public class GeneratorTypeAdaptor implements JsonSerializer<IGenerator<?>>, Json
{
JsonObject jsonObject = json.getAsJsonObject();
if (jsonObject.has("class"))
if (jsonObject.has("generator"))
{
try
String generatorIdentifier = jsonObject.get("generator").getAsString();
Class<? extends IGenerator<?>> generatorClass = GeneratorRegistry.getGeneratorClass(generatorIdentifier);
if (generatorClass == null)
{
Class generatorClass = Class.forName(jsonObject.get("class").getAsString());
if (IGenerator.class.isAssignableFrom(generatorClass))
{
IGenerator<?> generator = (IGenerator<?>) generatorClass.newInstance();
return generator.deserialize(json);
}
throw new JsonSyntaxException("Generator " + generatorIdentifier + " doesn't exist");
}
catch (Exception e)
else
{
e.printStackTrace();
IGenerator<?> generator;
try
{
generator = (IGenerator<?>)generatorClass.newInstance();
Type generatorStageType = new TypeToken<GeneratorStage>() {}.getType();
String generatorStageName = jsonObject.get("stage").getAsString();
GeneratorStage generatorStage = (GeneratorStage)context.deserialize(jsonObject.get("stage"), generatorStageType);
if (generatorStage == null)
{
throw new JsonSyntaxException("Generator stage " + generatorStageName + " is invalid");
}
else
{
generator.setStage((GeneratorStage)context.deserialize(jsonObject.get("stage"), generatorStageType));
generator.readFromJson(jsonObject, context);
return generator;
}
}
catch (InstantiationException e)
{
throw new RuntimeException("Generators must have a no-args constructor!", e);
}
catch (IllegalAccessException e)
{
e.printStackTrace();
}
}
}

View File

@ -16,11 +16,12 @@ import net.minecraft.block.state.IBlockState;
import net.minecraft.world.biome.BiomeGenBase;
import net.minecraftforge.common.BiomeManager.BiomeEntry;
import net.minecraftforge.common.BiomeManager.BiomeType;
import biomesoplenty.api.biome.BiomeOwner;
import biomesoplenty.api.biome.IExtendedBiome;
import biomesoplenty.api.biome.IGenerator;
import biomesoplenty.api.biome.generation.GenerationManager;
import biomesoplenty.api.biome.generation.IGenerator;
import biomesoplenty.common.biome.BOPBiomeManager;
import biomesoplenty.common.biome.ExtendedBiomeRegistry;
import biomesoplenty.common.biome.ExtendedBiomeRegistry.GenerationManager;
import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
@ -67,7 +68,7 @@ public class JsonBiome
GenerationManager generationManager = extendedBiome.getGenerationManager();
biome.weights = extendedBiome.getWeightMap();
biome.decoration = generationManager.getGeneratorMap();
biome.decoration = generationManager.createGeneratorMap();
//TODO: Add a system for making Vanilla biome weights configurable. This won't necessarily be in this class, however it's worth noting.
for (Entry<BiomeType, Integer> entry : extendedBiome.getWeightMap().entrySet())
@ -84,4 +85,59 @@ public class JsonBiome
return biome;
}
public static void configureBiomeWithJson(BiomeGenBase biome, JsonBiome jsonBiome)
{
IExtendedBiome extendedBiome = ExtendedBiomeRegistry.getExtension(biome);
if (extendedBiome != null)
{
if (extendedBiome.getBiomeOwner() == BiomeOwner.BIOMESOPLENTY)
{
if (jsonBiome.biomeId != -1)
{
biome.biomeID = jsonBiome.biomeId;
BiomeGenBase.getBiomeGenArray()[jsonBiome.biomeId] = biome;
}
else
{
biome.biomeID = -1;
}
}
Map<BiomeType, Integer> weightMap = jsonBiome.weights;
//Removes the default weights set by us as they are about to be set from the config file
extendedBiome.clearWeights();
//TODO: Add a system for making Vanilla biome weights configurable. This won't necessarily be in this class, however it's worth noting.
for (Entry<BiomeType, Integer> entry : weightMap.entrySet())
{
if (entry != null)
{
BiomeType biomeType = entry.getKey();
int weight = entry.getValue();
//Updates the biome's weights to be in line with the config file
extendedBiome.addWeight(biomeType, weight);
BOPBiomeManager.addBiome(biomeType, new BiomeEntry(biome, weight));
}
}
biome.biomeName = jsonBiome.biomeName;
biome.topBlock = jsonBiome.topBlock;
biome.fillerBlock = jsonBiome.fillerBlock;
biome.setHeight(new BiomeGenBase.Height(jsonBiome.rootHeight, jsonBiome.rootVariation));
biome.temperature = jsonBiome.temperature;
biome.rainfall = jsonBiome.rainfall;
// TODO: Reflect and modify enableRain and enableSnow
biome.color = jsonBiome.color;
biome.waterColorMultiplier = jsonBiome.waterColorMultiplier;
JsonEntitySpawn.addBiomeEntitySpawns(biome, jsonBiome);
GenerationManager generationManager = extendedBiome.getGenerationManager();
generationManager.createGeneratorTable(jsonBiome.decoration);
}
}
}

View File

@ -26,6 +26,7 @@ import biomesoplenty.common.command.BOPCommand;
import biomesoplenty.common.init.ModBiomes;
import biomesoplenty.common.init.ModBlocks;
import biomesoplenty.common.init.ModConfiguration;
import biomesoplenty.common.init.ModGenerators;
import biomesoplenty.common.init.ModHandlers;
import biomesoplenty.common.init.ModItems;
@ -56,6 +57,7 @@ public class BiomesOPlenty
ModBlocks.init();
ModItems.init();
ModGenerators.init();
ModBiomes.init();
ModHandlers.init();
ModConfiguration.initEnd(configDirectory);