net.minecraft.block.PlantBlock Java Examples

The following examples show how to use net.minecraft.block.PlantBlock. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: IForgeBlock.java    From patchwork-api with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Determines if this block can support the passed in plant, allowing it to be planted and grow.
 * Some examples:
 * Reeds check if its a reed, or if its sand/dirt/grass and adjacent to water
 * Cacti checks if its a cacti, or if its sand
 * Nether types check for soul sand
 * Crops check for tilled soil
 * Caves check if it's a solid surface
 * Plains check if its grass or dirt
 * Water check if its still water
 *
 * @param state     The Current state
 * @param world     The current world
 * @param facing    The direction relative to the given position the plant wants to be, typically its UP
 * @param plantable The plant that wants to check
 * @return True to allow the plant to be planted/stay.
 */
default boolean canSustainPlant(BlockState state, BlockView world, BlockPos pos, Direction facing, IPlantable plantable) {
	BlockState plant = plantable.getPlant(world, pos.offset(facing));

	if (plant.getBlock() == Blocks.CACTUS) {
		return this.getBlock() == Blocks.CACTUS || this.getBlock() == Blocks.SAND || this.getBlock() == Blocks.RED_SAND;
	}

	if (plant.getBlock() == Blocks.SUGAR_CANE && this.getBlock() == Blocks.SUGAR_CANE) {
		return true;
	}

	if (plantable instanceof PlantBlock && ((PlantBlockAccessor) plantable).invokeCanPlantOnTop(state, world, pos)) {
		return true;
	}

	switch (plantable.getPlantType(world, pos)) {
	case Desert:
		return this.getBlock() == Blocks.SAND || this.getBlock() == Blocks.TERRACOTTA || this.getBlock() instanceof GlazedTerracottaBlock;
	case Nether:
		return this.getBlock() == Blocks.SOUL_SAND;
	case Crop:
		return this.getBlock() == Blocks.FARMLAND;
	case Cave:
		return Block.isSideSolidFullSquare(state, world, pos, Direction.UP);
	case Plains:
		return this.getBlock() == Blocks.GRASS_BLOCK || Block.isNaturalDirt(this.getBlock()) || this.getBlock() == Blocks.FARMLAND;
	case Water:
		return state.getMaterial() == Material.WATER;
	case Beach:
		boolean isBeach = this.getBlock() == Blocks.GRASS_BLOCK || Block.isNaturalDirt(this.getBlock()) || this.getBlock() == Blocks.SAND;
		boolean hasWater = (world.getBlockState(pos.east()).getMaterial() == Material.WATER
				|| world.getBlockState(pos.west()).getMaterial() == Material.WATER
				|| world.getBlockState(pos.north()).getMaterial() == Material.WATER
				|| world.getBlockState(pos.south()).getMaterial() == Material.WATER);
		return isBeach && hasWater;
	}

	return false;
}