diff --git a/api/src/main/java/net/momirealms/customcrops/api/mechanic/world/level/CustomCropsWorld.java b/api/src/main/java/net/momirealms/customcrops/api/mechanic/world/level/CustomCropsWorld.java index c70e4e6..ab70de7 100644 --- a/api/src/main/java/net/momirealms/customcrops/api/mechanic/world/level/CustomCropsWorld.java +++ b/api/src/main/java/net/momirealms/customcrops/api/mechanic/world/level/CustomCropsWorld.java @@ -56,6 +56,8 @@ public interface CustomCropsWorld { boolean isChunkLoaded(ChunkPos chunkPos); + Optional getOrCreateLoadedChunkAt(ChunkPos chunkPos); + Optional getLoadedChunkAt(ChunkPos chunkPos); Optional getLoadedRegionAt(RegionPos regionPos); diff --git a/plugin/src/main/java/net/momirealms/customcrops/mechanic/world/CChunk.java b/plugin/src/main/java/net/momirealms/customcrops/mechanic/world/CChunk.java index 60743b0..2933411 100644 --- a/plugin/src/main/java/net/momirealms/customcrops/mechanic/world/CChunk.java +++ b/plugin/src/main/java/net/momirealms/customcrops/mechanic/world/CChunk.java @@ -89,7 +89,8 @@ public class CChunk implements CustomCropsChunk { @Override public void notifyOfflineUpdates() { long current = System.currentTimeMillis(); - int offlineTimeInSeconds = (int) (this.lastLoadedTime - current) / 1000; + int offlineTimeInSeconds = (int) (current - this.lastLoadedTime) / 1000; + CustomCropsPlugin.get().debug("offlineSeconds: " + offlineTimeInSeconds + "s. " + chunkPos.toString()); offlineTimeInSeconds = Math.min(offlineTimeInSeconds, cWorld.getWorldSetting().getMaxOfflineTime()); this.lastLoadedTime = current; var setting = cWorld.getWorldSetting(); diff --git a/plugin/src/main/java/net/momirealms/customcrops/mechanic/world/CWorld.java b/plugin/src/main/java/net/momirealms/customcrops/mechanic/world/CWorld.java index 721724c..da960b9 100644 --- a/plugin/src/main/java/net/momirealms/customcrops/mechanic/world/CWorld.java +++ b/plugin/src/main/java/net/momirealms/customcrops/mechanic/world/CWorld.java @@ -233,10 +233,15 @@ public class CWorld implements CustomCropsWorld { } @Override - public Optional getLoadedChunkAt(ChunkPos chunkPos) { + public Optional getOrCreateLoadedChunkAt(ChunkPos chunkPos) { return Optional.ofNullable(createOrGetChunk(chunkPos)); } + @Override + public Optional getLoadedChunkAt(ChunkPos chunkPos) { + return Optional.ofNullable(loadedChunks.get(chunkPos)); + } + @Override public Optional getLoadedRegionAt(RegionPos regionPos) { return Optional.ofNullable(loadedRegions.get(regionPos)); @@ -350,7 +355,7 @@ public class CWorld implements CustomCropsWorld { @Override public void addWaterToSprinkler(Sprinkler sprinkler, SimpleLocation location, int amount) { - Optional chunk = getLoadedChunkAt(location.getChunkPos()); + Optional chunk = getOrCreateLoadedChunkAt(location.getChunkPos()); if (chunk.isPresent()) { chunk.get().addWaterToSprinkler(sprinkler, location, amount); } else { @@ -360,7 +365,7 @@ public class CWorld implements CustomCropsWorld { @Override public void addFertilizerToPot(Pot pot, Fertilizer fertilizer, SimpleLocation location) { - Optional chunk = getLoadedChunkAt(location.getChunkPos()); + Optional chunk = getOrCreateLoadedChunkAt(location.getChunkPos()); if (chunk.isPresent()) { chunk.get().addFertilizerToPot(pot, fertilizer, location); } else { @@ -370,7 +375,7 @@ public class CWorld implements CustomCropsWorld { @Override public void addWaterToPot(Pot pot, SimpleLocation location, int amount) { - Optional chunk = getLoadedChunkAt(location.getChunkPos()); + Optional chunk = getOrCreateLoadedChunkAt(location.getChunkPos()); if (chunk.isPresent()) { chunk.get().addWaterToPot(pot, location, amount); } else { @@ -380,7 +385,7 @@ public class CWorld implements CustomCropsWorld { @Override public void addPotAt(WorldPot pot, SimpleLocation location) { - Optional chunk = getLoadedChunkAt(location.getChunkPos()); + Optional chunk = getOrCreateLoadedChunkAt(location.getChunkPos()); if (chunk.isPresent()) { chunk.get().addPotAt(pot, location); } else { @@ -390,7 +395,7 @@ public class CWorld implements CustomCropsWorld { @Override public void addSprinklerAt(WorldSprinkler sprinkler, SimpleLocation location) { - Optional chunk = getLoadedChunkAt(location.getChunkPos()); + Optional chunk = getOrCreateLoadedChunkAt(location.getChunkPos()); if (chunk.isPresent()) { chunk.get().addSprinklerAt(sprinkler, location); } else { @@ -400,7 +405,7 @@ public class CWorld implements CustomCropsWorld { @Override public void addCropAt(WorldCrop crop, SimpleLocation location) { - Optional chunk = getLoadedChunkAt(location.getChunkPos()); + Optional chunk = getOrCreateLoadedChunkAt(location.getChunkPos()); if (chunk.isPresent()) { chunk.get().addCropAt(crop, location); } else { @@ -410,7 +415,7 @@ public class CWorld implements CustomCropsWorld { @Override public void addPointToCrop(Crop crop, SimpleLocation location, int points) { - Optional chunk = getLoadedChunkAt(location.getChunkPos()); + Optional chunk = getOrCreateLoadedChunkAt(location.getChunkPos()); if (chunk.isPresent()) { chunk.get().addPointToCrop(crop, location, points); } else { @@ -420,7 +425,7 @@ public class CWorld implements CustomCropsWorld { @Override public void addGlassAt(WorldGlass glass, SimpleLocation location) { - Optional chunk = getLoadedChunkAt(location.getChunkPos()); + Optional chunk = getOrCreateLoadedChunkAt(location.getChunkPos()); if (chunk.isPresent()) { chunk.get().addGlassAt(glass, location); } else { @@ -430,7 +435,7 @@ public class CWorld implements CustomCropsWorld { @Override public void addScarecrowAt(WorldScarecrow scarecrow, SimpleLocation location) { - Optional chunk = getLoadedChunkAt(location.getChunkPos()); + Optional chunk = getOrCreateLoadedChunkAt(location.getChunkPos()); if (chunk.isPresent()) { chunk.get().addScarecrowAt(scarecrow, location); } else { @@ -444,7 +449,7 @@ public class CWorld implements CustomCropsWorld { if (chunk.isPresent()) { chunk.get().removeSprinklerAt(location); } else { - LogUtils.warn("Invalid operation: Removing sprinkler from a not generated chunk"); + LogUtils.warn("Invalid operation: Removing sprinkler from an unloaded chunk"); } } @@ -454,7 +459,7 @@ public class CWorld implements CustomCropsWorld { if (chunk.isPresent()) { chunk.get().removePotAt(location); } else { - LogUtils.warn("Invalid operation: Removing pot from a not generated chunk"); + LogUtils.warn("Invalid operation: Removing pot from an unloaded chunk"); } } @@ -464,7 +469,7 @@ public class CWorld implements CustomCropsWorld { if (chunk.isPresent()) { chunk.get().removeCropAt(location); } else { - LogUtils.warn("Invalid operation: Removing crop from a not generated chunk"); + LogUtils.warn("Invalid operation: Removing crop from an unloaded chunk"); } } @@ -474,7 +479,7 @@ public class CWorld implements CustomCropsWorld { if (chunk.isPresent()) { chunk.get().removeGlassAt(location); } else { - LogUtils.warn("Invalid operation: Removing glass from a not generated chunk"); + LogUtils.warn("Invalid operation: Removing glass from an unloaded chunk"); } } @@ -484,7 +489,7 @@ public class CWorld implements CustomCropsWorld { if (chunk.isPresent()) { chunk.get().removeScarecrowAt(location); } else { - LogUtils.warn("Invalid operation: Removing scarecrow from a not generated chunk"); + LogUtils.warn("Invalid operation: Removing scarecrow from an unloaded chunk"); } } @@ -494,7 +499,7 @@ public class CWorld implements CustomCropsWorld { if (chunk.isPresent()) { return chunk.get().removeBlockAt(location); } else { - LogUtils.warn("Invalid operation: Removing anything from a not generated chunk"); + LogUtils.warn("Invalid operation: Removing anything from an unloaded chunk"); return null; } } @@ -522,7 +527,7 @@ public class CWorld implements CustomCropsWorld { public boolean isPotReachLimit(SimpleLocation location) { Optional chunk = getLoadedChunkAt(location.getChunkPos()); if (chunk.isEmpty()) { - LogUtils.warn("Invalid operation: Querying pot amount from a not generated chunk"); + LogUtils.warn("Invalid operation: Querying pot amount from an unloaded chunk"); return true; } if (setting.getPotPerChunk() < 0) return false; @@ -533,7 +538,7 @@ public class CWorld implements CustomCropsWorld { public boolean isCropReachLimit(SimpleLocation location) { Optional chunk = getLoadedChunkAt(location.getChunkPos()); if (chunk.isEmpty()) { - LogUtils.warn("Invalid operation: Querying crop amount from a not generated chunk"); + LogUtils.warn("Invalid operation: Querying crop amount from an unloaded chunk"); return true; } if (setting.getCropPerChunk() < 0) return false; @@ -544,7 +549,7 @@ public class CWorld implements CustomCropsWorld { public boolean isSprinklerReachLimit(SimpleLocation location) { Optional chunk = getLoadedChunkAt(location.getChunkPos()); if (chunk.isEmpty()) { - LogUtils.warn("Invalid operation: Querying sprinkler amount from a not generated chunk"); + LogUtils.warn("Invalid operation: Querying sprinkler amount from an unloaded chunk"); return true; } if (setting.getSprinklerPerChunk() < 0) return false; diff --git a/plugin/src/main/java/net/momirealms/customcrops/scheduler/FoliaSchedulerImpl.java b/plugin/src/main/java/net/momirealms/customcrops/scheduler/FoliaSchedulerImpl.java index 9b6ce82..61c787e 100644 --- a/plugin/src/main/java/net/momirealms/customcrops/scheduler/FoliaSchedulerImpl.java +++ b/plugin/src/main/java/net/momirealms/customcrops/scheduler/FoliaSchedulerImpl.java @@ -57,10 +57,8 @@ public class FoliaSchedulerImpl implements SyncScheduler { @Override public CancellableTask runTaskSyncLater(Runnable runnable, Location location, long delay) { if (delay == 0) { - if (location == null) { - return new FoliaCancellableTask(Bukkit.getGlobalRegionScheduler().run(plugin, (scheduledTask -> runnable.run()))); - } - return new FoliaCancellableTask(Bukkit.getRegionScheduler().run(plugin, location, (scheduledTask -> runnable.run()))); + runSyncTask(runnable, location); + return new FoliaCancellableTask(null); } if (location == null) { return new FoliaCancellableTask(Bukkit.getGlobalRegionScheduler().runDelayed(plugin, (scheduledTask -> runnable.run()), delay));