Helper for custom biomes

This commit is contained in:
Emmanuel BENOîT 2016-07-02 15:22:26 +02:00
parent 5779ba020e
commit 0b2e45d2a2
3 changed files with 291 additions and 17 deletions

View file

@ -0,0 +1,231 @@
package mmm.world;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
import net.minecraft.world.biome.Biome;
import net.minecraftforge.common.BiomeDictionary;
import net.minecraftforge.common.BiomeManager;
import net.minecraftforge.common.BiomeManager.BiomeType;
import net.minecraftforge.fml.common.registry.GameRegistry;
public class WBiomeHelper
{
private static final String BIOME_REG_BASE = "mmm:biome/";
private final Function< WBiomeHelper , ? extends Biome > constructor;
private String name;
private String regPath;
private String mutationBase;
private float baseHeight;
private float heightVariation;
private float rainfall;
private float temperature;
private int waterColor = 0xffffff;
private final EnumMap< BiomeType , Integer > biomeTypes = new EnumMap<>( BiomeType.class );
private EnumMap< BiomeDictionary.Type , BiomeDictionary.Type > bdTags;
private final HashMap< String , Object > extra = new HashMap<>( );
private Biome.BiomeProperties cachedProperties;
public WBiomeHelper( final Function< WBiomeHelper , ? extends Biome > constructor )
{
this.constructor = constructor;
}
public WBiomeHelper startMutation( )
{
this.mutationBase = WBiomeHelper.BIOME_REG_BASE + this.regPath;
this.cachedProperties = null;
return this;
}
public WBiomeHelper clearMutation( )
{
this.mutationBase = null;
this.cachedProperties = null;
return this;
}
public WBiomeHelper setNames( final String name , final String regPath )
{
this.name = name;
this.regPath = regPath;
this.cachedProperties = null;
return this;
}
public WBiomeHelper setElevation( final float baseHeight , final float variation )
{
this.baseHeight = baseHeight;
this.heightVariation = variation;
this.cachedProperties = null;
return this;
}
public WBiomeHelper setWeather( final float rainfall , final float temperature )
{
this.rainfall = rainfall;
this.temperature = temperature;
this.cachedProperties = null;
return this;
}
public WBiomeHelper setWaterColor( final int filter )
{
this.waterColor = filter;
this.cachedProperties = null;
return this;
}
public WBiomeHelper setType( final BiomeType biomeType , final int weight )
{
if ( weight > 0 ) {
this.biomeTypes.put( biomeType , weight );
} else {
this.biomeTypes.remove( biomeType );
}
return this;
}
public WBiomeHelper clearTags( )
{
this.bdTags = null;
return this;
}
public WBiomeHelper setTags( final BiomeDictionary.Type... tags )
{
if ( this.bdTags == null ) {
this.bdTags = new EnumMap<>( BiomeDictionary.Type.class );
}
for ( final BiomeDictionary.Type tag : tags ) {
this.bdTags.put( tag , tag );
}
return this;
}
public WBiomeHelper removeTags( final BiomeDictionary.Type... tags )
{
if ( this.bdTags == null ) {
return this;
}
for ( final BiomeDictionary.Type tag : tags ) {
this.bdTags.remove( tag );
}
if ( this.bdTags.isEmpty( ) ) {
this.bdTags = null;
}
return this;
}
public WBiomeHelper setExtraProperty( final String name )
{
this.extra.put( name , name );
return this;
}
public WBiomeHelper setExtraProperty( final String name , final Object value )
{
this.extra.put( name , value );
return this;
}
public WBiomeHelper removeExtraProperty( final String name )
{
this.extra.remove( name );
return this;
}
public boolean hasExtraProperty( final String name )
{
return this.extra.containsKey( name );
}
public < T > T getExtraProperty( final String name , final Class< T > cls )
{
return this.getExtraProperty( name , cls , null );
}
public < T > T getExtraProperty( final String name , final Class< T > cls , final T defaultValue )
{
final Object value = this.extra.get( name );
if ( value == null ) {
return defaultValue;
}
return cls.cast( value );
}
public Biome.BiomeProperties getProperties( )
{
if ( this.cachedProperties != null ) {
return this.cachedProperties;
}
final Biome.BiomeProperties rv = new Biome.BiomeProperties( this.name ) //
.setBaseHeight( this.baseHeight ) //
.setHeightVariation( this.heightVariation ) //
.setRainfall( this.rainfall ) //
.setTemperature( this.temperature ) //
.setWaterColor( this.waterColor ) //
;
if ( this.mutationBase != null ) {
rv.setBaseBiome( this.mutationBase );
}
if ( this.temperature <= .1f ) {
rv.setSnowEnabled( );
}
if ( this.rainfall == 0 ) {
rv.setRainDisabled( );
}
this.cachedProperties = rv;
return rv;
}
public Biome register( )
{
final Biome biome = this.constructor.apply( this );
biome.setRegistryName( WBiomeHelper.BIOME_REG_BASE + this.regPath );
this.cachedProperties = null;
GameRegistry.register( biome );
for ( final Map.Entry< BiomeType , Integer > entry : this.biomeTypes.entrySet( ) ) {
BiomeManager.addBiome( entry.getKey( ) , new BiomeManager.BiomeEntry( biome , entry.getValue( ) ) );
}
if ( this.bdTags == null ) {
BiomeDictionary.makeBestGuess( biome );
} else {
for ( final BiomeDictionary.Type tag : this.bdTags.keySet( ) ) {
BiomeDictionary.registerBiomeType( biome , tag );
}
}
return biome;
}
}

View file

@ -49,15 +49,34 @@ public class World
BiomeManager.addBiome( BiomeType.WARM , new BiomeManager.BiomeEntry( BIOME_LIMESTONE_HILLS_EDGE , 5 ) ); BiomeManager.addBiome( BiomeType.WARM , new BiomeManager.BiomeEntry( BIOME_LIMESTONE_HILLS_EDGE , 5 ) );
BiomeDictionary.makeBestGuess( World.BIOME_LIMESTONE_HILLS_EDGE ); BiomeDictionary.makeBestGuess( World.BIOME_LIMESTONE_HILLS_EDGE );
// TEST REMOVE THIS XXX FIXME LOL WBiomeHelper helper = new WBiomeHelper( WBLimestonePlateau::new );
WBLimestonePlateau testLol = new WBLimestonePlateau( false , helper.setNames( "Limestone Plateau" , "limestone_plateau" ) //
( new Biome.BiomeProperties( "LOL TEST" ) ).setBaseHeight( 0.5F ).setHeightVariation( 0.02F ) .setElevation( .5f , .02f ) //
.setTemperature( 0.8F ).setRainfall( 0.4F ).setWaterColor( 0xe0ff7f ) ); .setWeather( .6f , .5f ) //
testLol.setRegistryName( "mmm:test_lol" ); .setWaterColor( 0xe0ff7f ) //
GameRegistry.register( testLol ); .setType( BiomeType.COOL , 5 ) //
BiomeManager.addBiome( BiomeType.COOL , new BiomeManager.BiomeEntry( testLol , 25 ) ); .setType( BiomeType.WARM , 5 ) //
BiomeManager.addBiome( BiomeType.WARM , new BiomeManager.BiomeEntry( testLol , 25 ) ); .register( );
BiomeDictionary.makeBestGuess( testLol ); helper.startMutation( );
helper.setNames( "Chaotic Limestone Plateau" , "limestone_plateau/chaos" )//
.setWeather( .8f , .5f ) //
.setElevation( .6f , .07f ) //
.setType( BiomeType.COOL , 1 ) //
.setType( BiomeType.WARM , 1 ) //
.setExtraProperty( "ChaosChance" , 4 ) //
.register( );
helper.setNames( "Chaotic Limestone Forest" , "limestone_plateau/chaos_forest" ) //
.setExtraProperty( "Trees" )//
.setType( BiomeType.COOL , 5 ) //
.setType( BiomeType.WARM , 5 ) //
.register( );
helper.setNames( "Limestone Forest" , "limestone_plateau/forest" ) //
.setElevation( .5f , .02f ) //
.setWeather( .7f , .5f ) //
.setType( BiomeType.COOL , 5 ) //
.setType( BiomeType.WARM , 5 ) //
.removeExtraProperty( "ChaosChance" )//
.register( );
} }

View file

@ -5,6 +5,7 @@ import java.util.Random;
import mmm.materials.Materials; import mmm.materials.Materials;
import mmm.world.I_WDefaultPopulateHandler; import mmm.world.I_WDefaultPopulateHandler;
import mmm.world.WBiomeHelper;
import mmm.world.gen.WGLimestoneChaos; import mmm.world.gen.WGLimestoneChaos;
import net.minecraft.block.BlockDirt; import net.minecraft.block.BlockDirt;
import net.minecraft.block.material.Material; import net.minecraft.block.material.Material;
@ -13,7 +14,10 @@ import net.minecraft.init.Blocks;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World; import net.minecraft.world.World;
import net.minecraft.world.biome.Biome; import net.minecraft.world.biome.Biome;
import net.minecraft.world.gen.feature.WorldGenAbstractTree;
import net.minecraft.world.gen.feature.WorldGenLakes; import net.minecraft.world.gen.feature.WorldGenLakes;
import net.minecraft.world.gen.feature.WorldGenTaiga1;
import net.minecraft.world.gen.feature.WorldGenTaiga2;
import net.minecraftforge.event.terraingen.PopulateChunkEvent; import net.minecraftforge.event.terraingen.PopulateChunkEvent;
@ -26,26 +30,46 @@ public class WBLimestonePlateau
BlockDirt.DirtType.COARSE_DIRT ); BlockDirt.DirtType.COARSE_DIRT );
private static final IBlockState BS_LIMESTONE = Materials.ROCK_LIMESTONE.getDefaultState( ); private static final IBlockState BS_LIMESTONE = Materials.ROCK_LIMESTONE.getDefaultState( );
private static final WorldGenTaiga1 PINE_GENERATOR = new WorldGenTaiga1( );
private static final WorldGenTaiga2 SPRUCE_GENERATOR = new WorldGenTaiga2( false );
private final int chaosChance; private final int chaosChance;
public WBLimestonePlateau( final boolean trees , final BiomeProperties properties ) public WBLimestonePlateau( final WBiomeHelper helper )
{ {
super( properties ); super( helper.getProperties( ) );
final boolean trees = helper.hasExtraProperty( "Trees" );
if ( trees ) { if ( trees ) {
this.theBiomeDecorator.treesPerChunk = 5; this.theBiomeDecorator.treesPerChunk = 5;
this.theBiomeDecorator.flowersPerChunk = 2; this.theBiomeDecorator.flowersPerChunk = 4;
this.theBiomeDecorator.grassPerChunk = 0; this.theBiomeDecorator.grassPerChunk = 1;
} else { } else {
this.theBiomeDecorator.treesPerChunk = 0; this.theBiomeDecorator.treesPerChunk = 0;
this.theBiomeDecorator.flowersPerChunk = 8; this.theBiomeDecorator.flowersPerChunk = 8;
this.theBiomeDecorator.grassPerChunk = 1; this.theBiomeDecorator.grassPerChunk = 3;
} }
this.theBiomeDecorator.field_189870_A = 0.1f; this.theBiomeDecorator.field_189870_A = 0.1f;
this.topBlock = Blocks.GRASS.getDefaultState( ); this.topBlock = Blocks.GRASS.getDefaultState( );
this.fillerBlock = Blocks.DIRT.getDefaultState( ); this.fillerBlock = Blocks.DIRT.getDefaultState( );
this.chaosChance = 4; // XXX this.chaosChance = helper.getExtraProperty( "ChaosChance" , Integer.class , 32 );
}
@Override
public WorldGenAbstractTree genBigTreeChance( final Random rand )
{
final int rnd = rand.nextInt( 20 );
if ( rnd < 1 ) {
return Biome.BIG_TREE_FEATURE;
} else if ( rnd < 5 ) {
return Biome.TREE_FEATURE;
} else if ( rnd < 7 ) {
return WBLimestonePlateau.PINE_GENERATOR;
} else {
return WBLimestonePlateau.SPRUCE_GENERATOR;
}
} }
@ -111,7 +135,7 @@ public class WBLimestonePlateau
private void replaceWithLimestone( final World worldIn , final Random rand , final BlockPos.MutableBlockPos mbp ) private void replaceWithLimestone( final World worldIn , final Random rand , final BlockPos.MutableBlockPos mbp )
{ {
if ( rand.nextInt( 8 ) != 0 ) { if ( rand.nextInt( 8 ) != 0 ) {
worldIn.setBlockState( mbp , BS_LIMESTONE ); worldIn.setBlockState( mbp , WBLimestonePlateau.BS_LIMESTONE );
} }
} }
@ -119,7 +143,7 @@ public class WBLimestonePlateau
private void replaceWithCoarseDirt( final World worldIn , final Random rand , final BlockPos.MutableBlockPos mbp ) private void replaceWithCoarseDirt( final World worldIn , final Random rand , final BlockPos.MutableBlockPos mbp )
{ {
if ( rand.nextInt( 16 ) == 0 ) { if ( rand.nextInt( 16 ) == 0 ) {
worldIn.setBlockState( mbp , BS_COARSE_DIRT ); worldIn.setBlockState( mbp , WBLimestonePlateau.BS_COARSE_DIRT );
} }
} }