Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

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

Featured Replies

Posted

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.

  • 4 months later...
  • Author

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

 

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.