Finalized the system for configurable decoration features. Do note that the current test case (Cactus) has not yet been implemented, only the code for configuring the values which will be used for it. This means that biome decoration can now be implemented properly.

This commit is contained in:
Adubbz 2015-01-21 11:10:19 +11:00
parent ae72d29ec3
commit 19180562fc
9 changed files with 259 additions and 3 deletions

View file

@ -0,0 +1,18 @@
/*******************************************************************************
* 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.Map;
public interface IExtendedDecorator
{
public void addGenerator(String key, IGenerator<?> generator);
public void configureGenerators(Map<String, IGenerator<?>> generatorMap);
public Map<String, IGenerator<?>> getGeneratorMap();
}

View file

@ -0,0 +1,17 @@
/*******************************************************************************
* 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 com.google.gson.JsonElement;
public interface IGenerator<T>
{
public JsonElement serialize(IGenerator<T> src);
public IGenerator<T> deserialize(JsonElement json);
}

View file

@ -14,11 +14,15 @@ import java.util.HashMap;
import java.util.Map.Entry;
import net.minecraft.world.biome.BiomeGenBase;
import net.minecraft.world.gen.feature.WorldGenCactus;
import org.apache.commons.io.FileUtils;
import biomesoplenty.api.biome.BiomeOwner;
import biomesoplenty.api.biome.IExtendedBiome;
import biomesoplenty.api.biome.IExtendedDecorator;
import biomesoplenty.api.biome.IGenerator;
import biomesoplenty.common.decoration.extensions.IExtendedCactusGen;
import biomesoplenty.common.util.config.JsonBiome;
import biomesoplenty.common.util.config.JsonEntitySpawn;
@ -126,9 +130,18 @@ public class BiomeConfigurationHandler
private static void translateVanillaValues(BiomeGenBase biome)
{
IExtendedBiome extendedBiome = (IExtendedBiome)biome;
IExtendedDecorator extendedDecorator = (IExtendedDecorator)biome.theBiomeDecorator;
if (extendedBiome.getBiomeOwner() == BiomeOwner.OTHER)
{
if (biome.theBiomeDecorator.cactiPerChunk > 0)
{
WorldGenCactus cactusGen = new WorldGenCactus();
IExtendedCactusGen extendedCactusGen = (IExtendedCactusGen)cactusGen;
extendedCactusGen.setCactiPerChunk(biome.theBiomeDecorator.cactiPerChunk);
extendedDecorator.addGenerator("cactus", extendedCactusGen);
}
}
}
@ -146,6 +159,8 @@ public class BiomeConfigurationHandler
biome.waterColorMultiplier = jsonBiome.waterColorMultiplier;
JsonEntitySpawn.addBiomeEntitySpawns(biome, jsonBiome);
IExtendedBiome extendedBiome = (IExtendedBiome)biome;
IExtendedDecorator extendedDecorator = (IExtendedDecorator)biome.theBiomeDecorator;
extendedDecorator.configureGenerators(jsonBiome.decoration);
}
}

View file

@ -0,0 +1,18 @@
/*******************************************************************************
* 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.common.decoration.extensions;
import net.minecraft.world.gen.feature.WorldGenCactus;
import biomesoplenty.api.biome.IGenerator;
public interface IExtendedCactusGen extends IGenerator<WorldGenCactus>
{
public void setCactiPerChunk(int amount);
public int getCactiPerChunk();
}

View file

@ -0,0 +1,55 @@
/*******************************************************************************
* 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.common.mixin.biome;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import net.minecraft.world.biome.BiomeDecorator;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import biomesoplenty.api.biome.IExtendedDecorator;
import biomesoplenty.api.biome.IGenerator;
@Mixin(BiomeDecorator.class)
public class MixinBiomeDecorator implements IExtendedDecorator
{
private Map<String, IGenerator<?>> generatorMap;
@Inject(method = "<init>", at = @At("RETURN"))
private void onConstructed(CallbackInfo callbackInfo)
{
this.generatorMap = new HashMap();
}
@Override
public void addGenerator(String key, IGenerator<?> generator)
{
this.generatorMap.put(key, generator);
}
@Override
public void configureGenerators(Map<String, IGenerator<?>> generatorMap)
{
this.generatorMap.putAll(generatorMap);
}
@Override
public Map<String, IGenerator<?>> getGeneratorMap()
{
return Collections.unmodifiableMap(this.generatorMap);
}
}

View file

@ -0,0 +1,60 @@
/*******************************************************************************
* 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.common.mixin.decoration;
import net.minecraft.world.gen.feature.WorldGenCactus;
import net.minecraft.world.gen.feature.WorldGenerator;
import org.spongepowered.asm.mixin.Mixin;
import biomesoplenty.api.biome.IGenerator;
import biomesoplenty.common.decoration.extensions.IExtendedCactusGen;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
@Mixin(WorldGenCactus.class)
public abstract class MixinWorldGenCactus extends WorldGenerator implements IExtendedCactusGen
{
private int cactiPerChunk;
@Override
public void setCactiPerChunk(int amount)
{
this.cactiPerChunk = amount;
}
@Override
public int getCactiPerChunk()
{
return this.cactiPerChunk;
}
@Override
public JsonElement serialize(IGenerator<WorldGenCactus> src)
{
JsonObject jsonCactusGen = new JsonObject();
jsonCactusGen.addProperty("cacti_per_chunk", ((IExtendedCactusGen)src).getCactiPerChunk());
return jsonCactusGen;
}
@Override
public IGenerator<WorldGenCactus> deserialize(JsonElement json)
{
JsonObject jsonCactusGen = json.getAsJsonObject();
WorldGenCactus cactusGen = new WorldGenCactus();
IExtendedCactusGen extendedCactusGen = (IExtendedCactusGen)cactusGen;
extendedCactusGen.setCactiPerChunk(jsonCactusGen.get("cacti_per_chunk").getAsInt());
return (IGenerator<WorldGenCactus>)new WorldGenCactus();
}
}

View file

@ -0,0 +1,61 @@
/*******************************************************************************
* 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.common.util.config;
import java.lang.reflect.Type;
import biomesoplenty.api.biome.IGenerator;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
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.addProperty("class", src.getClass().getCanonicalName());
return jsonObject;
}
@Override
public IGenerator<?> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException
{
JsonObject jsonObject = json.getAsJsonObject();
if (jsonObject.has("class"))
{
try
{
Class generatorClass = Class.forName(jsonObject.get("class").getAsString());
if (IGenerator.class.isAssignableFrom(generatorClass))
{
IGenerator<?> generator = (IGenerator<?>)generatorClass.newInstance();
return generator.deserialize(json);
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
return null;
}
}

View file

@ -9,9 +9,12 @@
package biomesoplenty.common.util.config;
import java.util.ArrayList;
import java.util.Map;
import net.minecraft.block.state.IBlockState;
import net.minecraft.world.biome.BiomeGenBase;
import biomesoplenty.api.biome.IExtendedDecorator;
import biomesoplenty.api.biome.IGenerator;
import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
@ -21,7 +24,9 @@ public class JsonBiome
{
public static final Gson serializer = new GsonBuilder()
.setPrettyPrinting().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
.registerTypeAdapter(IBlockState.class, new JsonBlockState()).create();
.registerTypeAdapter(IBlockState.class, new JsonBlockState())
.registerTypeAdapter(IGenerator.class, new GeneratorTypeAdaptor())
.create();
public String biomeName;
public int biomeId;
@ -34,6 +39,7 @@ public class JsonBiome
public int color;
public int waterColorMultiplier;
public ArrayList<JsonEntitySpawn> entities;
public Map<String, IGenerator<?>> decoration;
public static JsonBiome createFromBiomeGenBase(BiomeGenBase baseBiome)
{
@ -51,6 +57,10 @@ public class JsonBiome
biome.waterColorMultiplier = baseBiome.waterColorMultiplier;
biome.entities = JsonEntitySpawn.getBiomeEntitySpawns(baseBiome);
IExtendedDecorator extendedDecorator = (IExtendedDecorator)baseBiome.theBiomeDecorator;
biome.decoration = extendedDecorator.getGeneratorMap();
return biome;
}
}

View file

@ -1,7 +1,9 @@
{
"package": "biomesoplenty.common.mixin",
"mixins": [
"biome.MixinBiomeDecorator",
"biome.MixinBiomeGenBase",
"decoration.MixinWorldGenCactus",
"renderer.MixinBlockModelShapes"
]
}