Skip to content

Commit 6698ac1

Browse files
committed
Add timestamp field and methods to Chunk
1 parent 86f5b91 commit 6698ac1

1 file changed

Lines changed: 35 additions & 0 deletions

File tree

src/main/java/org/glavo/nbt/chunk/Chunk.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
import org.jetbrains.annotations.Contract;
2525
import org.jetbrains.annotations.Nullable;
2626

27+
import java.time.Instant;
28+
import java.util.Objects;
29+
2730
/// Represents a chunk in a region file.
2831
///
2932
/// A chunk can contain a root tag, which is usually a compound tag containing the chunk data.
@@ -32,16 +35,34 @@ public final class Chunk implements NBTParent<CompoundTag>, NBTElement {
3235
int localIndex;
3336

3437
@Nullable CompoundTag rootTag;
38+
Instant timestamp = Instant.EPOCH;
3539

3640
/// Creates a new empty chunk.
41+
///
42+
/// The root tag is initially `null`, and the timestamp is set to the epoch (1970-01-01T00:00:00Z).
3743
public Chunk() {
3844
}
3945

46+
/// Creates a new empty chunk with the given timestamp.
47+
///
48+
/// The root tag is initially `null`.
49+
public Chunk(Instant timestamp) {
50+
this.timestamp = Objects.requireNonNull(timestamp, "timestamp");
51+
}
52+
4053
/// Creates a new chunk with the given root tag.
54+
///
55+
/// The timestamp is set to the epoch (1970-01-01T00:00:00Z).
4156
public Chunk(@Nullable CompoundTag rootTag) {
4257
setRootTag(rootTag);
4358
}
4459

60+
/// Creates a new chunk with the given timestamp and root tag.
61+
public Chunk(Instant timestamp, @Nullable CompoundTag rootTag) {
62+
this.timestamp = Objects.requireNonNull(timestamp, "timestamp");
63+
setRootTag(rootTag);
64+
}
65+
4566
/// Return the region of this chunk, or `null` if this chunk is not in any region.
4667
@Contract(pure = true)
4768
public @Nullable ChunkRegion getRegion() {
@@ -109,6 +130,20 @@ public void setRootTag(@Nullable CompoundTag rootTag) {
109130
this.rootTag = rootTag;
110131
}
111132

133+
/// Return the timestamp of this chunk.
134+
///
135+
/// The default value is the epoch (1970-01-01T00:00:00Z).
136+
@Contract(pure = true)
137+
public Instant getTimestamp() {
138+
return timestamp;
139+
}
140+
141+
/// Set the timestamp of this chunk.
142+
@Contract(mutates = "this")
143+
public void setTimestamp(Instant timestamp) {
144+
this.timestamp = Objects.requireNonNull(timestamp, "timestamp");
145+
}
146+
112147
@Override
113148
@Contract(mutates = "this,param1")
114149
public void remove(CompoundTag element) throws IllegalArgumentException {

0 commit comments

Comments
 (0)