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) {