9
0
mirror of https://github.com/Xiao-MoMi/Custom-Crops.git synced 2025-12-25 18:09:28 +00:00

fix some chunk bugs

This commit is contained in:
XiaoMoMi
2024-04-03 20:52:00 +08:00
parent 8d8e311bfd
commit 6303649782
4 changed files with 30 additions and 24 deletions

View File

@@ -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();

View File

@@ -233,10 +233,15 @@ public class CWorld implements CustomCropsWorld {
}
@Override
public Optional<CustomCropsChunk> getLoadedChunkAt(ChunkPos chunkPos) {
public Optional<CustomCropsChunk> getOrCreateLoadedChunkAt(ChunkPos chunkPos) {
return Optional.ofNullable(createOrGetChunk(chunkPos));
}
@Override
public Optional<CustomCropsChunk> getLoadedChunkAt(ChunkPos chunkPos) {
return Optional.ofNullable(loadedChunks.get(chunkPos));
}
@Override
public Optional<CustomCropsRegion> 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<CustomCropsChunk> chunk = getLoadedChunkAt(location.getChunkPos());
Optional<CustomCropsChunk> 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<CustomCropsChunk> chunk = getLoadedChunkAt(location.getChunkPos());
Optional<CustomCropsChunk> 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<CustomCropsChunk> chunk = getLoadedChunkAt(location.getChunkPos());
Optional<CustomCropsChunk> 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<CustomCropsChunk> chunk = getLoadedChunkAt(location.getChunkPos());
Optional<CustomCropsChunk> 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<CustomCropsChunk> chunk = getLoadedChunkAt(location.getChunkPos());
Optional<CustomCropsChunk> 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<CustomCropsChunk> chunk = getLoadedChunkAt(location.getChunkPos());
Optional<CustomCropsChunk> 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<CustomCropsChunk> chunk = getLoadedChunkAt(location.getChunkPos());
Optional<CustomCropsChunk> 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<CustomCropsChunk> chunk = getLoadedChunkAt(location.getChunkPos());
Optional<CustomCropsChunk> 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<CustomCropsChunk> chunk = getLoadedChunkAt(location.getChunkPos());
Optional<CustomCropsChunk> 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<CustomCropsChunk> 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<CustomCropsChunk> 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<CustomCropsChunk> 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;

View File

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