If anyone is wanting to know, the solution looked like this:
// Helper method to check sunlight conditions (including daylight)
private static boolean isSufficientSunlight(Level world, BlockPos pos) {
int skyLight = world.getBrightness(LightLayer.SKY, pos);
long dayTime = world.getDayTime() % 24000;
// Check if it's daytime and if there is enough sunlight
return dayTime < 12000 && skyLight >= 5;
}
@SubscribeEvent
public static void onCropGrow(BlockEvent.CropGrowEvent.Post event) {
Level world = (Level) event.getLevel();
if (world.isClientSide) return; // Don't run logic on the client side
BlockPos pos = event.getPos();
// Check if the block is a glowberry vine, skip destruction if it is
if (event.getState().getBlock() == Blocks.CAVE_VINES || event.getState().getBlock() == Blocks.CAVE_VINES_PLANT) {
// Skip glowberry vines
return;
}
// Check sunlight and time restrictions
if (world.getBrightness(LightLayer.SKY, pos) < 13) {
// Destroy the crop (turn it into air) if there is not strong sunlight
world.removeBlock(pos, false);
}
}
// Doesn't let animals breed if there is no sunlight
@SubscribeEvent
public static void onAnimalBreed(BabyEntitySpawnEvent event) {
Level world = event.getParentA().getCommandSenderWorld();
if (world.isClientSide) return; // Don't run logic on the client side
if (!(event.getParentA() instanceof Animal) || !(event.getParentB() instanceof Animal)) {
return; // Ensure it is animals breeding
}
Animal parentA = (Animal) event.getParentA();
Animal parentB = (Animal) event.getParentB();
// Doesn't let chicken breed
if (parentA instanceof Chicken || parentB instanceof Chicken){
event.setCanceled(true); return;
}
if (!isSufficientSunlight(world, parentA.blockPosition())) {
// Cancel the breeding event
event.setCanceled(true); return;
}
if (!isSufficientSunlight(world, parentB.blockPosition())) {
// Cancel the breeding event
event.setCanceled(true);
}
}