strongestbow384

can you make a plugin for papermc 26.3 that makes it so that when crops are unloaded it tracks the time thats passing to simulate as if they were growing the whole time

Kodari AI
KodariOct 8 at 12:20 pm 1
OfflineCropGrowth
OfflineCropGrowth is a PaperMC plugin that simulates crop growth while chunks are unloaded. When a crop is planted, the plugin records its age and timestamp. Upon chunk load, it calculates elapsed time and instantly applies the equivalent growth stages, respecting biome and light conditions. It supports all vanilla crops, configurable growth rates, and a debug command to inspect crop states. The plugin uses persistent data containers to store timestamps and efficiently processes loaded chunks to update crops, ensuring seamless offline progression without requiring chunk loaders.
  1. Create the Paper 26.3 build and plugin metadata.

  2. Persist crop snapshots and timestamps when chunks unload or the server stops.

  3. Simulate missed growth on load using a configurable average time per growth stage.

Created the Paper 26.3 plugin. It saves crop ages and timestamps when chunks unload or the server stops, then simulates missed growth when those chunks load again. It only updates blocks that still match their saved crop type and age.

The default average is 300 seconds per growth stage; adjust growth.seconds-per-stage in config.yml to change it.

strongestbow384

can you simulate the ramdom chance of growing

Kodari AI
KodariOct 8 at 12:23 pm 1

It already simulates random growth: each stage gets a random wait time, averaging growth.seconds-per-stage (currently 300 seconds). So a crop won’t necessarily grow just because that much time passed—for a 300-second average, the chance of at least one stage in 300 seconds is about 63%.

This models stochastic growth, not vanilla’s exact random-tick rules or environmental conditions.

strongestbow384

does tick sprint affect the crops

Kodari AI
KodariOct 8 at 1:07 pm 1

No. Offline growth uses real elapsed time (System.currentTimeMillis()), so tick sprint won’t make the plugin’s simulated growth faster or slower. The configured seconds-per-stage stays based on real seconds.

strongestbow384

can you make it so that animals also grow up

Kodari AI
KodariOct 8 at 3:37 pm 1

Plan:

  1. Snapshot baby animals and their ages alongside crops on chunk unload/server shutdown.

  2. On chunk load, advance each unchanged baby’s age by elapsed real-time ticks, up to adulthood.

  3. Keep the existing snapshot format backward-compatible with files that contain crops only.

Added offline animal aging. Baby animals are saved by UUID and age; when their chunk loads, they advance by elapsed real time at 20 ticks per second, up to adulthood. Existing crop-only snapshots remain compatible.

strongestbow384

can you make it so that crops and animals dont grow/age when theirs no players online

Kodari AI
KodariOct 8 at 6:17 pm 1
  1. Replace wall-clock snapshot timestamps with a persisted clock that advances only while at least one player is online.

  2. Start and stop that clock on player join/quit, and use it for both crop growth and animal aging.

  3. Rebase older snapshots safely so upgrading won’t count offline time.

Updated offline growth to use a persisted active-time clock. Crops and baby animals now accrue simulated growth only while at least one player is online; time spent with an empty server no longer counts. Older saved snapshots are rebased so their offline interval won’t be applied.

strongestbow384

And if the server is stopped will they keep growing

Kodari AI
KodariOct 8 at 6:38 pm 1

No. They only age or grow while the server is running and at least one player is online. Time while the server is stopped is excluded; growth resumes when a player is online again.

strongestbow384

Does it scan for existing crops or do they need to be replanted

Kodari AI
KodariOct 8 at 6:40 pm 1

No replanting needed. The plugin scans crops in each chunk when it unloads, and also scans loaded chunks when the server stops. It simulates growth for the time they’re unloaded after that snapshot is saved. It won’t count time from before the plugin first records a crop.

strongestbow384

when the last player leaves it crashes the server

Kodari AI
KodariOct 8 at 10:54 pm 1

I’ll stop rewriting the full snapshot file for every chunk unload/load. Those events can arrive in a burst when the last player leaves; coalescing saves should prevent repeated synchronous disk I/O from stalling the server.

Fixed the unload burst: chunk snapshots are now saved together instead of synchronously rewriting the YAML file for every chunk. The pending snapshots are still saved on shutdown.

strongestbow384

did you make it so that it doesnt crash

Kodari AI
KodariOct 8 at 10:54 pm 1

I made a change intended to prevent the last-player-leaves crash by batching chunk snapshot saves instead of rewriting the YAML file synchronously for every chunk unload. I can’t guarantee that fixes the crash without seeing the server’s error log; if it still happens, send the crash report or latest log.

Kodari can make mistakes. Review important code before using.
Files
src
build.gradle
Code
4 369
src/main/java/com/kodari/cropoffline/CropOfflineGrowthPlugin.java
package com.kodari.cropoffline;

import org.bukkit.Bukkit;
import org.bukkit.Chunk;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.data.Ageable;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Animals;
import org.bukkit.entity.Entity;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.event.world.ChunkLoadEvent;
import org.bukkit.event.world.ChunkUnloadEvent;
import org.bukkit.plugin.java.JavaPlugin;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;
import java.util.logging.Level;

public final class CropOfflineGrowthPlugin extends JavaPlugin implements Listener {
    private final Map<ChunkKey, ChunkSnapshot> pendingSnapshots = new HashMap<>();
    private File snapshotsFile;
    private double secondsPerStage;
    private long accumulatedGrowthMillis;
    private long growthClockStartedAtMillis = -1L;
    private boolean snapshotSaveScheduled;

    @Override
    public void onEnable() {
        saveDefaultConfig();
        secondsPerStage = Math.max(0.1, getConfig().getDouble("growth.seconds-per-stage", 300.0));
        snapshotsFile = new File(getDataFolder(), "crop-snapshots.yml");
        loadSnapshots();
        if (!Bukkit.getOnlinePlayers().isEmpty()) {
            growthClockStartedAtMillis = System.currentTimeMillis();
        }
        getServer().getPluginManager().registerEvents(this, this);
        Bukkit.getScheduler().runTask(this, this::applySnapshotsToLoadedChunks);
    }

    @Override
    public void onDisable() {
        for (World world : Bukkit.getWorlds()) {
            for (Chunk chunk : world.getLoadedChunks()) {
                captureChunk(chunk);
            }
        }
        saveSnapshots();
    }

    @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
    public void onChunkUnload(ChunkUnloadEvent event) {
        captureChunk(event.getChunk());
        scheduleSnapshotsSave();
    }

    @EventHandler
    public void onChunkLoad(ChunkLoadEvent event) {
        ChunkKey key = keyFor(event.getChunk());
        ChunkSnapshot snapshot = pendingSnapshots.remove(key);
        if (snapshot == null) {
            return;
        }

        applySnapshot(event.getChunk(), snapshot);
        scheduleSnapshotsSave();
    }

    @EventHandler
    public void onPlayerJoin(PlayerJoinEvent event) {
        if (growthClockStartedAtMillis == -1L) {
            growthClockStartedAtMillis = System.currentTimeMillis();
        }
    }

    @EventHandler
    public void onPlayerQuit(PlayerQuitEvent event) {
        Bukkit.getScheduler().runTaskLater(this, () -> {
            if (Bukkit.getOnlinePlayers().isEmpty() && growthClockStartedAtMillis != -1L) {
                accumulatedGrowthMillis = currentGrowthTimeMillis();
                growthClockStartedAtMillis = -1L;
                scheduleSnapshotsSave();
            }
        }, 1L);
    }

    private void applySnapshotsToLoadedChunks() {
        boolean changed = false;
        for (World world : Bukkit.getWorlds()) {
            for (Chunk chunk : world.getLoadedChunks()) {
                ChunkSnapshot snapshot = pendingSnapshots.remove(keyFor(chunk));
                if (snapshot != null) {
                    applySnapshot(chunk, snapshot);
                    changed = true;
                }
            }
        }
        if (changed) {
            scheduleSnapshotsSave();
        }
    }

    private void captureChunk(Chunk chunk) {
        List<CropSnapshot> crops = new ArrayList<>();
        List<AnimalSnapshot> animals = new ArrayList<>();
        World world = chunk.getWorld();
        for (int x = 0; x < 16; x++) {
            for (int z = 0; z < 16; z++) {
                for (int y = world.getMinHeight(); y < world.getMaxHeight(); y++) {
                    Block block = chunk.getBlock(x, y, z);
                    if (block.getBlockData() instanceof Ageable ageable) {
                        crops.add(new CropSnapshot(x, y, z, block.getType().name(), ageable.getAge()));
                    }
                }
            }
        }

        for (Entity entity : chunk.getEntities()) {
            if (entity instanceof Animals animal && animal.getAge() < 0) {
                animals.add(new AnimalSnapshot(animal.getUniqueId(), animal.getAge()));
            }
        }

        ChunkKey key = keyFor(chunk);
        if (crops.isEmpty() && animals.isEmpty()) {
            pendingSnapshots.remove(key);
        } else {
            pendingSnapshots.put(key, new ChunkSnapshot(currentGrowthTimeMillis(), crops, animals));
        }
    }

    private void applySnapshot(Chunk chunk, ChunkSnapshot snapshot) {
        long elapsedMillis = Math.max(0L, currentGrowthTimeMillis() - snapshot.capturedAtMillis());
        double elapsedSeconds = elapsedMillis / 1000.0;
        long elapsedTicks = elapsedMillis / 50L;

        for (CropSnapshot crop : snapshot.crops()) {
            Block block = chunk.getBlock(crop.x(), crop.y(), crop.z());
            if (!block.getType().name().equals(crop.material())) {
                continue;
            }

            if (!(block.getBlockData() instanceof Ageable ageable) || ageable.getAge() != crop.age()) {
                continue;
            }

            int grownAge = crop.age();
            double remainingSeconds = elapsedSeconds;
            while (grownAge < ageable.getMaximumAge()) {
                double waitSeconds = -Math.log1p(-ThreadLocalRandom.current().nextDouble()) * secondsPerStage;
                if (waitSeconds > remainingSeconds) {
                    break;
                }
                remainingSeconds -= waitSeconds;
                grownAge++;
            }

            if (grownAge > ageable.getAge()) {
                ageable.setAge(grownAge);
                block.setBlockData(ageable, false);
            }
        }

        for (AnimalSnapshot animalSnapshot : snapshot.animals()) {
            for (Entity entity : chunk.getEntities()) {
                if (!entity.getUniqueId().equals(animalSnapshot.entityId())) {
                    continue;
                }
                if (entity instanceof Animals animal && animal.getAge() == animalSnapshot.age()) {
                    int grownAge = (int) Math.min(0L, (long) animalSnapshot.age() + elapsedTicks);
                    animal.setAge(grownAge);
                }
                break;
            }
        }
    }

    private void loadSnapshots() {
        pendingSnapshots.clear();
        if (!snapshotsFile.isFile()) {
            return;
        }

        FileConfiguration data = YamlConfiguration.loadConfiguration(snapshotsFile);
        ConfigurationSection chunks = data.getConfigurationSection("chunks");
        boolean legacyClock = !data.contains("growth-clock-millis");
        accumulatedGrowthMillis = Math.max(0L, data.getLong("growth-clock-millis", 0L));
        if (chunks == null) {