11package com .dtteam .dtcobblemon .genfeature ;
22
3- import com .dtteam .dtcobblemon .tree .SaccharineSpecies ;
3+ import com .cobblemon .mod .common .api .pokemon .PokemonProperties ;
4+ import com .cobblemon .mod .common .entity .pokemon .PokemonEntity ;
45import com .dtteam .dynamictrees .api .configuration .ConfigurationProperty ;
56import com .dtteam .dynamictrees .systems .genfeature .BeeNestGenFeature ;
67import com .dtteam .dynamictrees .systems .genfeature .GenFeatureConfiguration ;
78import net .minecraft .core .BlockPos ;
9+ import net .minecraft .core .Direction ;
810import net .minecraft .resources .ResourceLocation ;
11+ import net .minecraft .server .level .WorldGenRegion ;
912import net .minecraft .tags .BlockTags ;
13+ import net .minecraft .util .RandomSource ;
14+ import net .minecraft .world .level .LevelAccessor ;
15+ import net .minecraft .world .level .block .BeehiveBlock ;
16+ import net .minecraft .world .level .block .Block ;
1017import net .minecraft .world .level .block .Blocks ;
18+ import net .minecraft .world .level .block .entity .BlockEntityType ;
19+ import net .minecraft .world .level .block .state .BlockState ;
20+ import org .apache .commons .lang3 .tuple .Pair ;
1121import org .jetbrains .annotations .NotNull ;
1222
23+ import java .util .List ;
24+
1325public class SaccharineBeeNestGenFeature extends BeeNestGenFeature {
1426
15- public static final ConfigurationProperty <Integer > COMBEE_CHANCE = ConfigurationProperty .integer ("combee_chance" );
27+ public static final ConfigurationProperty <Float > COMBEE_PROBABILITY = ConfigurationProperty .floatProperty ("combee_probability" );
28+ public static final ConfigurationProperty <Integer > COMBEE_MIN_LEVEL = ConfigurationProperty .integer ("combee_min_level" );
29+ public static final ConfigurationProperty <Integer > COMBEE_MAX_LEVEL = ConfigurationProperty .integer ("combee_max_level" );
30+ private static final String PokemonArgs = "combee" ;
31+ public static double BeeNestWorldGenChance = 0.5 ;
32+ public static double BeeNestGrowChance = 0.1 ;
1633
1734 public SaccharineBeeNestGenFeature (ResourceLocation registryName ) {
1835 super (registryName );
@@ -21,55 +38,74 @@ public SaccharineBeeNestGenFeature(ResourceLocation registryName) {
2138 @ Override
2239 protected void registerProperties () {
2340 super .registerProperties ();
24- this .register (COMBEE_CHANCE );
41+ this .register (COMBEE_PROBABILITY , COMBEE_MIN_LEVEL , COMBEE_MAX_LEVEL );
2542 }
2643
2744 @ Override @ NotNull
2845 public GenFeatureConfiguration createDefaultConfiguration () {
2946 return super .createDefaultConfiguration ()
3047 .with (NEST_BLOCK , Blocks .BEE_NEST )
3148 .with (MAX_HEIGHT , 32 )
32- .with (COMBEE_CHANCE , 2 )
49+ .with (COMBEE_PROBABILITY , 0.5f )
50+ .with (COMBEE_MIN_LEVEL , 5 )
51+ .with (COMBEE_MAX_LEVEL , 15 )
3352 .with (CAN_GROW_PREDICATE , (world , pos ) -> {
34- if (world .getRandom ().nextFloat () > SaccharineSpecies .BeeNestGrowChance ) {
35- return false ;
36- }
37- // Default flower check predicate, straight from AbstractTreeGrower
53+ if (world .getRandom ().nextFloat () > BeeNestGrowChance ) return false ;
3854 for (BlockPos blockpos : BlockPos .betweenClosed (pos .below ().north (2 ).west (2 ), pos .above ().south (2 ).east (2 ))) {
39- if (world .getBlockState (blockpos ).is (BlockTags .FLOWERS )) {
40- return true ;
41- }
55+ if (world .getBlockState (blockpos ).is (BlockTags .FLOWERS )) return true ;
4256 }
4357 return false ;
4458 })
45- .with (WORLD_GEN_CHANCE_FUNCTION , (world , pos ) -> SaccharineSpecies . BeeNestWorldGenChance )
59+ .with (WORLD_GEN_CHANCE_FUNCTION , (world , pos ) -> BeeNestWorldGenChance )
4660 .with (MAX_COUNT , 1 );
4761 }
4862
49- //TODO: later, it crashes on Cobblemon's side for some reason.
50- // @Override
51- // @SuppressWarnings("deprecation")
52- // protected boolean placeBeeNestWithBees(LevelAccessor world, Block nestBlock, BlockPos pos, Direction faceDir, boolean worldGen, RandomSource random) {
53- // BlockState nestState = nestBlock.defaultBlockState();
54- // if (nestState.hasProperty(BeehiveBlock.FACING)) {
55- // nestState = nestState.setValue(BeehiveBlock.FACING, faceDir);
56- // }
57- // world.setBlock(pos, nestState, 3);
58- // world.getBlockEntity(pos, BlockEntityType.BEEHIVE).ifPresent((blockEntity) -> {
59- // int j = 2 + random.nextInt(2);
60- // int isCombee = random.nextInt(2);
61- // for(int k = 0; k < j; ++k) {
62- // if (isCombee == 0 && world instanceof WorldGenRegion level){
63- // String properties = "${POKEMON_ARGS} lvl=${LEVEL_RANGE.random()}";
64- // PokemonProperties pokemon = PokemonProperties.Companion.parse(properties);
65- // PokemonEntity entity = pokemon.createEntity(level.getLevel());
66- // blockEntity.addOccupant(entity);
67- // } else {
68- // storeBee(random, blockEntity);
69- // }
70- // }
71- //
72- // });
73- // return true;
74- // }
63+ @ Override
64+ protected boolean placeBeeNestInValidPlace (GenFeatureConfiguration configuration , LevelAccessor world , BlockPos rootPos , boolean worldGen , RandomSource random ) {
65+ Block nestBlock = configuration .get (NEST_BLOCK );
66+
67+ int treeHeight = getTreeHeight (world , rootPos , configuration .get (MAX_HEIGHT ));
68+ if (nestAlreadyPresent (world , nestBlock , rootPos , treeHeight )) {
69+ return false ;
70+ }
71+ List <Pair <BlockPos , List <Direction >>> validSpaces = findBranchPits (configuration , world , rootPos , treeHeight );
72+ if (validSpaces == null ) {
73+ return false ;
74+ }
75+ if (!validSpaces .isEmpty ()) {
76+ Pair <BlockPos , List <Direction >> chosenSpace = validSpaces .get (world .getRandom ().nextInt (validSpaces .size ()));
77+ Direction chosenDir = chosenSpace .getValue ().get (world .getRandom ().nextInt (chosenSpace .getValue ().size ()));
78+
79+ return placeBeeNestWithBees (configuration , world , nestBlock , chosenSpace .getKey (), chosenDir , worldGen , random );
80+ }
81+ return false ;
82+ }
83+
84+ @ SuppressWarnings ("deprecation" )
85+ private boolean placeBeeNestWithBees (GenFeatureConfiguration configuration , LevelAccessor world , Block nestBlock , BlockPos pos , Direction faceDir , boolean worldGen , RandomSource random ) {
86+ BlockState nestState = nestBlock .defaultBlockState ();
87+ if (nestState .hasProperty (BeehiveBlock .FACING )) {
88+ nestState = nestState .setValue (BeehiveBlock .FACING , faceDir );
89+ }
90+ float combeeChance = configuration .get (COMBEE_PROBABILITY );
91+ int combeeMinLevel = configuration .get (COMBEE_MIN_LEVEL );
92+ int combeeMaxLevel = configuration .get (COMBEE_MAX_LEVEL );
93+ world .setBlock (pos , nestState , 3 );
94+ world .getBlockEntity (pos , BlockEntityType .BEEHIVE ).ifPresent ((blockEntity ) -> {
95+ int j = 2 + random .nextInt (2 );
96+ float isCombee = random .nextFloat ();
97+ for (int k = 0 ; k < j ; ++k ) {
98+ if (isCombee < combeeChance && world instanceof WorldGenRegion level ){
99+ String properties = PokemonArgs +" lvl=" +(combeeMinLevel + random .nextInt (combeeMaxLevel -combeeMinLevel +1 ));
100+ PokemonProperties pokemon = PokemonProperties .Companion .parse (properties );
101+ PokemonEntity entity = pokemon .createEntity (level .getLevel ());
102+ blockEntity .addOccupant (entity );
103+ } else {
104+ storeBee (random , blockEntity );
105+ }
106+ }
107+
108+ });
109+ return true ;
110+ }
75111}
0 commit comments