Java源码示例:net.minecraft.block.Fertilizable

示例1
private boolean isCorrectBlock(BlockPos pos)
{
	Block block = BlockUtils.getBlock(pos);
	
	if(!(block instanceof Fertilizable) || block instanceof GrassBlock
		|| !((Fertilizable)block).canGrow(MC.world, MC.world.random, pos,
			BlockUtils.getState(pos)))
		return false;
	
	if(block instanceof SaplingBlock)
		return saplings.isChecked();
	else if(block instanceof CropBlock)
		return crops.isChecked();
	else if(block instanceof StemBlock)
		return stems.isChecked();
	else if(block instanceof CocoaBlock)
		return cocoa.isChecked();
	else
		return other.isChecked();
}
 
示例2
@Override
public void grow(ServerWorld world, Random random, BlockPos blockPos, BlockState blockState) {
	BlockPos upPos = blockPos.up();
	BlockState grassBlock = HallowedBlocks.DECEASED_GRASS_BLOCK.getDefaultState();
	
	label48:
	for (int i = 0; i < 128; ++i) {
		BlockPos randomPos = upPos;
		
		for (int j = 0; j < i / 16; ++j) {
			randomPos = randomPos.add(random.nextInt(3) - 1, (random.nextInt(3) - 1) * random.nextInt(3) / 2, random.nextInt(3) - 1);
			if (world.getBlockState(randomPos.down()).getBlock() != this || world.getBlockState(randomPos).isFullCube(world, randomPos)) {
				continue label48;
			}
		}
		
		BlockState randomBlockState = world.getBlockState(randomPos);
		if (randomBlockState.getBlock() == grassBlock.getBlock() && random.nextInt(10) == 0) {
			((Fertilizable) grassBlock.getBlock()).grow(world, random, randomPos, randomBlockState);
		}
		
		if (randomBlockState.isAir()) {
			BlockState stateToPlace;
			if (random.nextInt(8) == 0) {
				List<ConfiguredFeature<?, ?>> list = world.getBiomeAccess().getBiome(randomPos).getFlowerFeatures();
				if (list.isEmpty()) {
					continue;
				}
				stateToPlace = ((FlowerFeature) ((DecoratedFeatureConfig) (list.get(0)).config).feature.feature).getFlowerToPlace(random, randomPos, list.get(0).config);
			} else {
				stateToPlace = grassBlock;
			}
			
			if (stateToPlace.canPlaceAt(world, randomPos)) {
				world.setBlockState(randomPos, stateToPlace, 3);
			}
		}
	}
}