-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for serializing and deserializing Duration
Added a new DurationAdapter to handle Java's Duration type. This includes registering the adapter in the Serializer and implementing serialization to and deserialization from LongTags. This ensures seamless handling of Duration objects in the NBT system.
- Loading branch information
Showing
2 changed files
with
30 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
nbt/src/main/java/core/nbt/serialization/adapter/DurationAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
|
||
package core.nbt.serialization.adapter; | ||
|
||
import core.nbt.serialization.ParserException; | ||
import core.nbt.serialization.TagAdapter; | ||
import core.nbt.serialization.TagDeserializationContext; | ||
import core.nbt.serialization.TagSerializationContext; | ||
import core.nbt.tag.LongTag; | ||
import core.nbt.tag.Tag; | ||
import org.jspecify.annotations.NullMarked; | ||
|
||
import java.time.Duration; | ||
|
||
@NullMarked | ||
public class DurationAdapter implements TagAdapter<Duration> { | ||
public static final DurationAdapter INSTANCE = new DurationAdapter(); | ||
|
||
@Override | ||
public Duration deserialize(Tag tag, TagDeserializationContext context) throws ParserException { | ||
return Duration.ofMillis(tag.getAsLong()); | ||
} | ||
|
||
@Override | ||
public Tag serialize(Duration duration, TagSerializationContext context) throws ParserException { | ||
return new LongTag(duration.toMillis()); | ||
} | ||
} |