From 0b2e45d2a2077722f959b2d27822fe9ee81687c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emmanuel=20Beno=C3=AEt?= Date: Sat, 2 Jul 2016 15:22:26 +0200 Subject: [PATCH] Helper for custom biomes --- src/java/mmm/world/WBiomeHelper.java | 231 ++++++++++++++++++ src/java/mmm/world/World.java | 37 ++- .../mmm/world/biome/WBLimestonePlateau.java | 40 ++- 3 files changed, 291 insertions(+), 17 deletions(-) create mode 100644 src/java/mmm/world/WBiomeHelper.java diff --git a/src/java/mmm/world/WBiomeHelper.java b/src/java/mmm/world/WBiomeHelper.java new file mode 100644 index 0000000..6fa7e67 --- /dev/null +++ b/src/java/mmm/world/WBiomeHelper.java @@ -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; + } + +} diff --git a/src/java/mmm/world/World.java b/src/java/mmm/world/World.java index 6eb63fe..6a3a49d 100644 --- a/src/java/mmm/world/World.java +++ b/src/java/mmm/world/World.java @@ -49,15 +49,34 @@ public class World BiomeManager.addBiome( BiomeType.WARM , new BiomeManager.BiomeEntry( BIOME_LIMESTONE_HILLS_EDGE , 5 ) ); BiomeDictionary.makeBestGuess( World.BIOME_LIMESTONE_HILLS_EDGE ); - // TEST REMOVE THIS XXX FIXME LOL - WBLimestonePlateau testLol = new WBLimestonePlateau( false , - ( new Biome.BiomeProperties( "LOL TEST" ) ).setBaseHeight( 0.5F ).setHeightVariation( 0.02F ) - .setTemperature( 0.8F ).setRainfall( 0.4F ).setWaterColor( 0xe0ff7f ) ); - testLol.setRegistryName( "mmm:test_lol" ); - GameRegistry.register( testLol ); - BiomeManager.addBiome( BiomeType.COOL , new BiomeManager.BiomeEntry( testLol , 25 ) ); - BiomeManager.addBiome( BiomeType.WARM , new BiomeManager.BiomeEntry( testLol , 25 ) ); - BiomeDictionary.makeBestGuess( testLol ); + WBiomeHelper helper = new WBiomeHelper( WBLimestonePlateau::new ); + helper.setNames( "Limestone Plateau" , "limestone_plateau" ) // + .setElevation( .5f , .02f ) // + .setWeather( .6f , .5f ) // + .setWaterColor( 0xe0ff7f ) // + .setType( BiomeType.COOL , 5 ) // + .setType( BiomeType.WARM , 5 ) // + .register( ); + 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( ); } diff --git a/src/java/mmm/world/biome/WBLimestonePlateau.java b/src/java/mmm/world/biome/WBLimestonePlateau.java index 566196f..a5d5a26 100644 --- a/src/java/mmm/world/biome/WBLimestonePlateau.java +++ b/src/java/mmm/world/biome/WBLimestonePlateau.java @@ -5,6 +5,7 @@ import java.util.Random; import mmm.materials.Materials; import mmm.world.I_WDefaultPopulateHandler; +import mmm.world.WBiomeHelper; import mmm.world.gen.WGLimestoneChaos; import net.minecraft.block.BlockDirt; import net.minecraft.block.material.Material; @@ -13,7 +14,10 @@ import net.minecraft.init.Blocks; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; 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.WorldGenTaiga1; +import net.minecraft.world.gen.feature.WorldGenTaiga2; import net.minecraftforge.event.terraingen.PopulateChunkEvent; @@ -26,26 +30,46 @@ public class WBLimestonePlateau BlockDirt.DirtType.COARSE_DIRT ); 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; - 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 ) { this.theBiomeDecorator.treesPerChunk = 5; - this.theBiomeDecorator.flowersPerChunk = 2; - this.theBiomeDecorator.grassPerChunk = 0; + this.theBiomeDecorator.flowersPerChunk = 4; + this.theBiomeDecorator.grassPerChunk = 1; } else { this.theBiomeDecorator.treesPerChunk = 0; this.theBiomeDecorator.flowersPerChunk = 8; - this.theBiomeDecorator.grassPerChunk = 1; + this.theBiomeDecorator.grassPerChunk = 3; } this.theBiomeDecorator.field_189870_A = 0.1f; this.topBlock = Blocks.GRASS.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 ) { 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 ) { if ( rand.nextInt( 16 ) == 0 ) { - worldIn.setBlockState( mbp , BS_COARSE_DIRT ); + worldIn.setBlockState( mbp , WBLimestonePlateau.BS_COARSE_DIRT ); } }