Skip to content

Commit

Permalink
feat: tick chunk in a batch operation
Browse files Browse the repository at this point in the history
  • Loading branch information
smartcmd committed Feb 1, 2025
1 parent c95f7a1 commit 8235b09
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,19 @@ private void checkXYZ(int x, int y, int z) {
Preconditions.checkArgument(z >= 0 && z <= 15);
}

public void tick(long currentTick, Dimension dimension, WorldStorage worldStorage) {
public void tick(long currentTick, Dimension dimension) {
blockEntities.values().forEach(blockEntity -> ((BlockEntityBaseComponentImpl) ((BlockEntityImpl) blockEntity).getBaseComponent()).tick(currentTick));
entities.values().forEach(entity -> ((EntityBaseComponentImpl) ((EntityImpl) entity).getBaseComponent()).tick(currentTick));
tickScheduledUpdates(dimension);
tickRandomUpdates(dimension);
checkAutoSave(worldStorage);
}

public void checkAutoSave(WorldStorage worldStorage) {
autoSaveTimer++;
if (autoSaveTimer >= Server.SETTINGS.storageSettings().chunkAutoSaveCycle()) {
worldStorage.writeChunk(safeChunk);
autoSaveTimer = 0;
}
}

protected void tickScheduledUpdates(Dimension dimension) {
Expand Down Expand Up @@ -232,14 +239,6 @@ public int nextUpdateLCG() {
return (this.updateLCG = (this.updateLCG * 3) ^ LCG_CONSTANT);
}

protected void checkAutoSave(WorldStorage worldStorage) {
autoSaveTimer++;
if (autoSaveTimer >= Server.SETTINGS.storageSettings().chunkAutoSaveCycle()) {
worldStorage.writeChunk(safeChunk);
autoSaveTimer = 0;
}
}

public void beforeSetChunk(Dimension dimension) {
if (blockEntityNbtList != null && !blockEntityNbtList.isEmpty()) {
for (var nbt : blockEntityNbtList) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.allaymc.api.world.Dimension;
import org.allaymc.api.world.chunk.Chunk;
import org.allaymc.api.world.chunk.ChunkLoader;
import org.allaymc.api.world.chunk.OperationType;
import org.allaymc.api.world.generator.WorldGenerator;
import org.allaymc.api.world.service.ChunkService;
import org.allaymc.api.world.storage.WorldStorage;
Expand Down Expand Up @@ -94,7 +95,8 @@ private void tickChunks(long currentTick) {
}

try {
((AllayUnsafeChunk) chunk.toUnsafeChunk()).tick(currentTick, dimension, worldStorage);
chunk.applyOperation(unsafeChunk -> ((AllayUnsafeChunk) unsafeChunk).tick(currentTick, dimension), OperationType.WRITE, OperationType.WRITE);
((AllayUnsafeChunk) chunk.toUnsafeChunk()).checkAutoSave(worldStorage);
} catch (Throwable t) {
log.error("Error while ticking chunk({}, {})!", chunk.getX(), chunk.getZ(), t);
}
Expand Down

0 comments on commit 8235b09

Please sign in to comment.