Jump to content

[1.20.4] Advice needed: how to make vanilla plants only survive and grow where there is sunlight.


Recommended Posts

I am a beginner modder, with around 50 hours of experience being guided by tutorials and chat-gpt. My mod is almost done, but I want to make the plants specifically require sunlight to encourage players to establish structure above ground. After looking at the vanilla code for a few hours, I'm still unsere what the best way to do this would be. I would be happy if someone with more experience could point me in the right direction.

Link to comment
Share on other sites

  • 4 months later...

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);
  }
}

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.