Added Chunk#getLastChangeTime

This commit is contained in:
themode 2020-11-20 08:39:06 +01:00
parent 02eab844a5
commit e453a0f9b5
2 changed files with 36 additions and 9 deletions

View File

@ -191,6 +191,17 @@ public abstract class Chunk implements Viewable, DataContainer {
@NotNull @NotNull
public abstract Set<Integer> getBlockEntities(); public abstract Set<Integer> getBlockEntities();
/**
* Gets the last time that this chunk changed.
* <p>
* "Change" means here data used in {@link ChunkDataPacket}.
* It is necessary to see if the cached version of this chunk can be used
* instead of re writing and compressing everything.
*
* @return the last change time in milliseconds
*/
public abstract long getLastChangeTime();
/** /**
* Serializes the chunk into bytes. * Serializes the chunk into bytes.
* *

View File

@ -57,6 +57,8 @@ public class DynamicChunk extends Chunk {
// Block entities // Block entities
protected final Set<Integer> blockEntities = new CopyOnWriteArraySet<>(); protected final Set<Integer> blockEntities = new CopyOnWriteArraySet<>();
private long lastChangeTime;
public DynamicChunk(@Nullable Biome[] biomes, int chunkX, int chunkZ, public DynamicChunk(@Nullable Biome[] biomes, int chunkX, int chunkZ,
@NotNull PaletteStorage blockPalette, @NotNull PaletteStorage customBlockPalette) { @NotNull PaletteStorage blockPalette, @NotNull PaletteStorage customBlockPalette) {
super(biomes, chunkX, chunkZ, true); super(biomes, chunkX, chunkZ, true);
@ -86,8 +88,8 @@ public class DynamicChunk extends Chunk {
// True if the block is not complete air without any custom block capabilities // True if the block is not complete air without any custom block capabilities
final boolean hasBlock = blockStateId != 0 || customBlockId != 0; final boolean hasBlock = blockStateId != 0 || customBlockId != 0;
this.blockPalette.setBlockAt(x, y, z, blockStateId); setBlockAt(blockPalette, x, y, z, blockStateId);
this.customBlockPalette.setBlockAt(x, y, z, customBlockId); setBlockAt(customBlockPalette, x, y, z, customBlockId);
if (!hasBlock) { if (!hasBlock) {
// Block has been deleted, clear cache and return // Block has been deleted, clear cache and return
@ -155,23 +157,23 @@ public class DynamicChunk extends Chunk {
@Override @Override
public short getBlockStateId(int x, int y, int z) { public short getBlockStateId(int x, int y, int z) {
return this.blockPalette.getBlockAt(x, y, z); return getBlockAt(blockPalette, x, y, z);
} }
@Override @Override
public short getCustomBlockId(int x, int y, int z) { public short getCustomBlockId(int x, int y, int z) {
return customBlockPalette.getBlockAt(x, y, z); return getBlockAt(customBlockPalette, x, y, z);
} }
@Override @Override
protected void refreshBlockValue(int x, int y, int z, short blockStateId, short customBlockId) { protected void refreshBlockValue(int x, int y, int z, short blockStateId, short customBlockId) {
this.blockPalette.setBlockAt(x, y, z, blockStateId); setBlockAt(blockPalette, x, y, z, blockStateId);
this.customBlockPalette.setBlockAt(x, y, z, customBlockId); setBlockAt(customBlockPalette, x, y, z, customBlockId);
} }
@Override @Override
protected void refreshBlockStateId(int x, int y, int z, short blockStateId) { protected void refreshBlockStateId(int x, int y, int z, short blockStateId) {
this.blockPalette.setBlockAt(x, y, z, blockStateId); setBlockAt(blockPalette, x, y, z, blockStateId);
} }
@Override @Override
@ -195,6 +197,11 @@ public class DynamicChunk extends Chunk {
return blockEntities; return blockEntities;
} }
@Override
public long getLastChangeTime() {
return lastChangeTime;
}
/** /**
* Serialize this {@link Chunk} based on {@link #readChunk(BinaryReader, ChunkCallback)} * Serialize this {@link Chunk} based on {@link #readChunk(BinaryReader, ChunkCallback)}
* <p> * <p>
@ -242,8 +249,8 @@ public class DynamicChunk extends Chunk {
for (byte z = 0; z < CHUNK_SIZE_Z; z++) { for (byte z = 0; z < CHUNK_SIZE_Z; z++) {
final int index = getBlockIndex(x, y, z); final int index = getBlockIndex(x, y, z);
final short blockStateId = blockPalette.getBlockAt(x, y, z); final short blockStateId = getBlockAt(blockPalette, x, y, z);
final short customBlockId = customBlockPalette.getBlockAt(x, y, z); final short customBlockId = getBlockAt(customBlockPalette, x, y, z);
// No block at the position // No block at the position
if (blockStateId == 0 && customBlockId == 0) if (blockStateId == 0 && customBlockId == 0)
@ -400,4 +407,13 @@ public class DynamicChunk extends Chunk {
return dynamicChunk; return dynamicChunk;
} }
private short getBlockAt(@NotNull PaletteStorage paletteStorage, int x, int y, int z) {
return paletteStorage.getBlockAt(x, y, z);
}
private void setBlockAt(@NotNull PaletteStorage paletteStorage, int x, int y, int z, short blockId) {
paletteStorage.setBlockAt(x, y, z, blockId);
this.lastChangeTime = System.currentTimeMillis();
}
} }