if (block.getBlockData() instanceof Ageable ageable) {
// Check if the block is a mature Melon or Pumpkin stem
if ((cropType == Material.MELON_STEM || cropType == Material.PUMPKIN_STEM) && ageable.getAge() == ageable.getMaximumAge()) {
// Find a suitable location adjacent to the stem to place the Melon or Pumpkin block
for (BlockFace face : new BlockFace[]{BlockFace.NORTH, BlockFace.EAST, BlockFace.SOUTH, BlockFace.WEST}) {
Block adjacentBlock = block.getRelative(face);
if (adjacentBlock.getType() == Material.AIR) {
if (cropType == Material.MELON_STEM) {
adjacentBlock.setType(Material.MELON);
block.setType(Material.ATTACHED_MELON_STEM);
} else {
adjacentBlock.setType(Material.PUMPKIN);
block.setType(Material.ATTACHED_PUMPKIN_STEM);
}
// Now, the block type is ATTACHED_MELON_STEM or ATTACHED_PUMPKIN_STEM
BlockData blockData = block.getBlockData();
if (blockData instanceof Directional) {
Directional directional = (Directional) blockData;
directional.setFacingDirection(face);
block.setBlockData((BlockData) directional);
}
break;
}
}
} else {
ageable.setAge(ageable.getMaximumAge());
block.setBlockData(ageable);
}
}