9
0
mirror of https://github.com/Xiao-MoMi/Custom-Crops.git synced 2025-12-26 10:29:10 +00:00

make the chunks tick on region threads

This commit is contained in:
XiaoMoMi
2024-03-25 02:28:40 +08:00
parent 9be0d87c3f
commit 64589c3503
8 changed files with 49 additions and 8 deletions

View File

@@ -33,7 +33,7 @@ public class VersionManagerImpl extends VersionManager {
private final CustomCropsPlugin plugin;
private final String pluginVersion;
private final String serverVersion;
private boolean hasRegionScheduler;
private boolean foliaScheduler;
private final boolean isSpigot;
private final boolean isNewerThan1_19_R2;
private final boolean isNewerThan1_19_R3;
@@ -83,10 +83,10 @@ public class VersionManagerImpl extends VersionManager {
}
try {
Class.forName("io.papermc.paper.threadedregions.scheduler.AsyncScheduler");
this.hasRegionScheduler = true;
Class.forName("io.papermc.paper.threadedregions.RegionizedServer");
this.foliaScheduler = true;
} catch (ClassNotFoundException ignored) {
this.hasRegionScheduler = false;
this.foliaScheduler = false;
}
// Check if the server is Mojmap
@@ -105,7 +105,7 @@ public class VersionManagerImpl extends VersionManager {
@Override
public boolean hasRegionScheduler() {
return hasRegionScheduler;
return foliaScheduler;
}
@Override

View File

@@ -21,6 +21,7 @@ import net.momirealms.customcrops.api.CustomCropsPlugin;
import net.momirealms.customcrops.api.common.Pair;
import net.momirealms.customcrops.api.event.SeasonChangeEvent;
import net.momirealms.customcrops.api.manager.ConfigManager;
import net.momirealms.customcrops.api.manager.VersionManager;
import net.momirealms.customcrops.api.manager.WorldManager;
import net.momirealms.customcrops.api.mechanic.item.Crop;
import net.momirealms.customcrops.api.mechanic.item.Fertilizer;
@@ -33,6 +34,7 @@ import net.momirealms.customcrops.api.mechanic.world.SimpleLocation;
import net.momirealms.customcrops.api.mechanic.world.level.*;
import net.momirealms.customcrops.api.mechanic.world.season.Season;
import net.momirealms.customcrops.api.scheduler.CancellableTask;
import net.momirealms.customcrops.api.scheduler.Scheduler;
import net.momirealms.customcrops.api.util.LogUtils;
import net.momirealms.customcrops.utils.EventUtils;
import org.bukkit.World;
@@ -119,8 +121,15 @@ public class CWorld implements CustomCropsWorld {
this.updateSeasonAndDate();
}
if (setting.isSchedulerEnabled()) {
for (CChunk chunk : loadedChunks.values()) {
chunk.secondTimer();
if (VersionManager.folia()) {
Scheduler scheduler = CustomCropsPlugin.get().getScheduler();
for (CChunk chunk : loadedChunks.values()) {
scheduler.runTaskSync(chunk::secondTimer,world.get(), chunk.getChunkPos().x(), chunk.getChunkPos().z());
}
} else {
for (CChunk chunk : loadedChunks.values()) {
chunk.secondTimer();
}
}
}

View File

@@ -21,6 +21,7 @@ import net.momirealms.customcrops.api.CustomCropsPlugin;
import net.momirealms.customcrops.api.scheduler.CancellableTask;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.scheduler.BukkitTask;
public class BukkitSchedulerImpl implements SyncScheduler {
@@ -39,6 +40,11 @@ public class BukkitSchedulerImpl implements SyncScheduler {
Bukkit.getScheduler().runTask(plugin, runnable);
}
@Override
public void runSyncTask(Runnable runnable, World world, int x, int z) {
runSyncTask(runnable, null);
}
@Override
public CancellableTask runTaskSyncTimer(Runnable runnable, Location location, long delay, long period) {
return new BukkitCancellableTask(Bukkit.getScheduler().runTaskTimer(plugin, runnable, delay, period));

View File

@@ -22,6 +22,7 @@ import net.momirealms.customcrops.api.CustomCropsPlugin;
import net.momirealms.customcrops.api.scheduler.CancellableTask;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
public class FoliaSchedulerImpl implements SyncScheduler {
@@ -40,6 +41,11 @@ public class FoliaSchedulerImpl implements SyncScheduler {
}
}
@Override
public void runSyncTask(Runnable runnable, World world, int x, int z) {
Bukkit.getRegionScheduler().execute(plugin, world, x, z, runnable);
}
@Override
public CancellableTask runTaskSyncTimer(Runnable runnable, Location location, long delay, long period) {
if (location == null) {

View File

@@ -23,6 +23,7 @@ import net.momirealms.customcrops.api.scheduler.CancellableTask;
import net.momirealms.customcrops.api.scheduler.Scheduler;
import net.momirealms.customcrops.api.util.LogUtils;
import org.bukkit.Location;
import org.bukkit.World;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.ScheduledThreadPoolExecutor;
@@ -68,6 +69,11 @@ public class SchedulerImpl implements Scheduler {
this.syncScheduler.runSyncTask(runnable, location);
}
@Override
public void runTaskSync(Runnable runnable, World world, int x, int z) {
this.syncScheduler.runSyncTask(runnable, world, x, z);
}
@Override
public void runTaskAsync(Runnable runnable) {
try {

View File

@@ -19,11 +19,14 @@ package net.momirealms.customcrops.scheduler;
import net.momirealms.customcrops.api.scheduler.CancellableTask;
import org.bukkit.Location;
import org.bukkit.World;
public interface SyncScheduler {
void runSyncTask(Runnable runnable, Location location);
void runSyncTask(Runnable runnable, World world, int x, int z);
CancellableTask runTaskSyncTimer(Runnable runnable, Location location, long delayTicks, long periodTicks);
CancellableTask runTaskSyncLater(Runnable runnable, Location location, long delayTicks);