Skip to content
This repository was archived by the owner on Apr 28, 2020. It is now read-only.

Commit da620d1

Browse files
KittenRunemoro
authored andcommitted
Add NBT (de)serialization utility interfaces (#31)
* Fix NBTSerializer being dumb * Rewrite NBTSerializer
1 parent 7fa089c commit da620d1

2 files changed

Lines changed: 63 additions & 0 deletions

File tree

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package org.dimdev.rift.util;
2+
3+
import net.minecraft.nbt.NBTTagCompound;
4+
5+
import javax.annotation.Nonnull;
6+
7+
/**
8+
* Base interface for (de)serializable objects to serialize
9+
* themselves to and from {@link NBTTagCompound} tag compounds
10+
*/
11+
public interface NBTSerializable {
12+
/**
13+
* Writes this object's data to the given compound
14+
* @param compound The tag compound to be written to
15+
* @return The written tag compound
16+
*/
17+
@Nonnull
18+
NBTTagCompound serialize(@Nonnull NBTTagCompound compound);
19+
20+
/**
21+
* Reads this object's data from the given compound
22+
* @param compound The tag compound to be read from
23+
* @return The given tag compound
24+
*/
25+
@Nonnull
26+
NBTTagCompound deserialize(@Nonnull NBTTagCompound compound);
27+
28+
/**
29+
* Writes this object's data to a new tag compound
30+
* @return The written tag compound
31+
*/
32+
@Nonnull
33+
default NBTTagCompound writeToNBT() {
34+
return this.serialize(new NBTTagCompound());
35+
}
36+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package org.dimdev.rift.util;
2+
3+
import net.minecraft.nbt.NBTTagCompound;
4+
5+
import javax.annotation.Nonnull;
6+
7+
/**
8+
* Base interface for (de)serializing objects of type {@code T}
9+
* to and from {@link NBTTagCompound} tag compounds
10+
*/
11+
public interface NBTSerializer<T> {
12+
/**
13+
* Writes an object {@code T} to the given compound
14+
* @param instance The instance to be serialized
15+
* @return The written tag compound
16+
*/
17+
@Nonnull
18+
NBTTagCompound serialize(@Nonnull T instance);
19+
20+
/**
21+
* Reads an object {@code T} from the given compound
22+
* @param compound The tag compound to be read from
23+
* @return An instance of {@code T}
24+
*/
25+
@Nonnull
26+
T deserialize(@Nonnull NBTTagCompound compound);
27+
}

0 commit comments

Comments
 (0)